UNPKG

@gechiui/block-editor

Version:
78 lines (61 loc) 2.03 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _element = require("@gechiui/element"); var _reactAutosizeTextarea = _interopRequireDefault(require("react-autosize-textarea")); var _data = require("@gechiui/data"); var _blocks = require("@gechiui/blocks"); var _store = require("../../store"); /** * External dependencies */ /** * GeChiUI dependencies */ /** * Internal dependencies */ function BlockHTML(_ref) { let { clientId } = _ref; const [html, setHtml] = (0, _element.useState)(''); const block = (0, _data.useSelect)(select => select(_store.store).getBlock(clientId), [clientId]); const { updateBlock } = (0, _data.useDispatch)(_store.store); const onChange = () => { const blockType = (0, _blocks.getBlockType)(block.name); if (!blockType) { return; } const attributes = (0, _blocks.getBlockAttributes)(blockType, html, block.attributes); // If html is empty we reset the block to the default HTML and mark it as valid to avoid triggering an error const content = html ? html : (0, _blocks.getSaveContent)(blockType, attributes); const isValid = html ? (0, _blocks.isValidBlockContent)(blockType, attributes, content) : true; updateBlock(clientId, { attributes, originalContent: content, isValid }); // Ensure the state is updated if we reset so it displays the default content if (!html) { setHtml({ content }); } }; (0, _element.useEffect)(() => { setHtml((0, _blocks.getBlockContent)(block)); }, [block]); return (0, _element.createElement)(_reactAutosizeTextarea.default, { className: "block-editor-block-list__block-html-textarea", value: html, onBlur: onChange, onChange: event => setHtml(event.target.value) }); } var _default = BlockHTML; exports.default = _default; //# sourceMappingURL=block-html.js.map