UNPKG

@wordpress/block-library

Version:
68 lines (67 loc) • 3.21 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_rich_text = require("@wordpress/rich-text"); let _wordpress_data = require("@wordpress/data"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_blocks = require("@wordpress/blocks"); // packages/block-library/src/paragraph/use-enter.js const { subscribeOwnedListener } = (0, require("../lock-unlock.cjs").unlock)(_wordpress_rich_text.privateApis); function useOnEnter(props) { const { batch } = (0, _wordpress_data.useRegistry)(); const { moveBlocksToPosition, replaceBlocks, selectionChange } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const { getBlockRootClientId, getBlockIndex, getBlockOrder, getBlockName, getBlock, canInsertBlockType } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store); const propsRef = (0, _wordpress_element.useRef)(props); propsRef.current = props; return (0, _wordpress_compose.useRefEffect)((element) => { function onBeforeInput(event) { if (event.defaultPrevented) return; if (event.inputType !== "insertParagraph") return; const { content, clientId } = propsRef.current; if (content.length) return; const wrapperClientId = getBlockRootClientId(clientId); if (!(0, _wordpress_blocks.hasBlockSupport)(getBlockName(wrapperClientId), "__experimentalOnEnter", false)) return; const order = getBlockOrder(wrapperClientId); const position = order.indexOf(clientId); if (position === order.length - 1) { let newWrapperClientId = wrapperClientId; while (!canInsertBlockType(getBlockName(clientId), getBlockRootClientId(newWrapperClientId))) newWrapperClientId = getBlockRootClientId(newWrapperClientId); if (typeof newWrapperClientId === "string") { event.preventDefault(); moveBlocksToPosition([clientId], wrapperClientId, getBlockRootClientId(newWrapperClientId), getBlockIndex(newWrapperClientId) + 1); } return; } const defaultBlockName = (0, _wordpress_blocks.getDefaultBlockName)(); const wrapperBlockName = getBlockName(wrapperClientId); const grandparentClientId = getBlockRootClientId(wrapperClientId); if (!canInsertBlockType(defaultBlockName, grandparentClientId) || !canInsertBlockType(wrapperBlockName, grandparentClientId)) return; event.preventDefault(); const wrapperBlock = getBlock(wrapperClientId); const head = (0, _wordpress_blocks.cloneBlock)({ ...wrapperBlock, innerBlocks: wrapperBlock.innerBlocks.slice(0, position) }); const middle = (0, _wordpress_blocks.createBlock)(defaultBlockName); const tail = (0, _wordpress_blocks.cloneBlock)({ ...wrapperBlock, innerBlocks: wrapperBlock.innerBlocks.slice(position + 1) }); batch(() => { replaceBlocks(wrapperClientId, [ head, middle, tail ]); selectionChange(middle.clientId); }); } return subscribeOwnedListener(element, "beforeinput", onBeforeInput, true); }, []); } exports.useOnEnter = useOnEnter; //# sourceMappingURL=use-enter.cjs.map