UNPKG

@wordpress/block-library

Version:
100 lines (98 loc) 3.6 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/list-item/hooks/use-enter.js var use_enter_exports = {}; __export(use_enter_exports, { default: () => useEnter }); module.exports = __toCommonJS(use_enter_exports); var import_blocks = require("@wordpress/blocks"); var import_compose = require("@wordpress/compose"); var import_rich_text = require("@wordpress/rich-text"); var import_data = require("@wordpress/data"); var import_block_editor = require("@wordpress/block-editor"); var import_utils = require("../utils.cjs"); var import_lock_unlock = require("../../lock-unlock.cjs"); var { subscribeOwnedListener } = (0, import_lock_unlock.unlock)(import_rich_text.privateApis); function useEnter(clientId) { const registry = (0, import_data.useRegistry)(); return (0, import_compose.useRefEffect)( (element) => { function onBeforeInput(event) { if (event.defaultPrevented || event.inputType !== "insertParagraph") { return; } const select = registry.select(import_block_editor.store); const { getBlock, getBlockAttributes, getBlockRootClientId, getBlockIndex } = select; const { replaceBlocks, selectionChange } = registry.dispatch(import_block_editor.store); const { content } = getBlockAttributes(clientId) ?? {}; if (content?.length) { return; } event.preventDefault(); const canOutdent = !!(0, import_utils.getOutdentTarget)(select, clientId); if (canOutdent) { (0, import_utils.outdentListItems)(registry); return; } const topParentListBlock = getBlock( getBlockRootClientId(clientId) ); const blockIndex = getBlockIndex(clientId); const head = (0, import_blocks.cloneBlock)({ ...topParentListBlock, innerBlocks: topParentListBlock.innerBlocks.slice( 0, blockIndex ) }); const middle = (0, import_blocks.createBlock)((0, import_blocks.getDefaultBlockName)()); const after = [ ...topParentListBlock.innerBlocks[blockIndex].innerBlocks[0]?.innerBlocks || [], ...topParentListBlock.innerBlocks.slice(blockIndex + 1) ]; const tail = after.length ? [ (0, import_blocks.cloneBlock)({ ...topParentListBlock, innerBlocks: after }) ] : []; replaceBlocks( topParentListBlock.clientId, [head, middle, ...tail], 1 ); selectionChange(middle.clientId); } return subscribeOwnedListener( element, "beforeinput", onBeforeInput, true ); }, [clientId, registry] ); } //# sourceMappingURL=use-enter.cjs.map