UNPKG

@wordpress/block-library

Version:
131 lines (129 loc) 4.32 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-library/src/paragraph/use-enter.js var use_enter_exports = {}; __export(use_enter_exports, { useOnEnter: () => useOnEnter }); module.exports = __toCommonJS(use_enter_exports); var import_element = require("@wordpress/element"); var import_compose = require("@wordpress/compose"); var import_keycodes = require("@wordpress/keycodes"); var import_data = require("@wordpress/data"); var import_block_editor = require("@wordpress/block-editor"); var import_blocks = require("@wordpress/blocks"); function useOnEnter(props) { const { batch } = (0, import_data.useRegistry)(); const { moveBlocksToPosition, replaceInnerBlocks, duplicateBlocks, insertBlock } = (0, import_data.useDispatch)(import_block_editor.store); const { getBlockRootClientId, getBlockIndex, getBlockOrder, getBlockName, getBlock, getNextBlockClientId, canInsertBlockType } = (0, import_data.useSelect)(import_block_editor.store); const propsRef = (0, import_element.useRef)(props); propsRef.current = props; return (0, import_compose.useRefEffect)((element) => { function onKeyDown(event) { if (event.defaultPrevented) { return; } if (event.keyCode !== import_keycodes.ENTER) { return; } const { content, clientId } = propsRef.current; if (content.length) { return; } const wrapperClientId = getBlockRootClientId(clientId); if (!(0, import_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, import_blocks.getDefaultBlockName)(); if (!canInsertBlockType( defaultBlockName, getBlockRootClientId(wrapperClientId) )) { return; } event.preventDefault(); const wrapperBlock = getBlock(wrapperClientId); batch(() => { duplicateBlocks([wrapperClientId]); const blockIndex = getBlockIndex(wrapperClientId); replaceInnerBlocks( wrapperClientId, wrapperBlock.innerBlocks.slice(0, position) ); replaceInnerBlocks( getNextBlockClientId(wrapperClientId), wrapperBlock.innerBlocks.slice(position + 1) ); insertBlock( (0, import_blocks.createBlock)(defaultBlockName), blockIndex + 1, getBlockRootClientId(wrapperClientId), true ); }); } element.addEventListener("keydown", onKeyDown); return () => { element.removeEventListener("keydown", onKeyDown); }; }, []); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useOnEnter }); //# sourceMappingURL=use-enter.js.map