{"version":3,"file":"use-enter.mjs","names":["richTextPrivateApis","blockEditorStore"],"sources":["../../src/paragraph/use-enter.js"],"sourcesContent":["import { useRef } from '@wordpress/element';\nimport { useRefEffect } from '@wordpress/compose';\nimport { privateApis as richTextPrivateApis } from '@wordpress/rich-text';\nimport { useSelect, useDispatch, useRegistry } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport {\n\thasBlockSupport,\n\tcreateBlock,\n\tcloneBlock,\n\tgetDefaultBlockName,\n} from '@wordpress/blocks';\nimport { unlock } from '../lock-unlock';\n\nconst { subscribeOwnedListener } = unlock( richTextPrivateApis );\n\nexport function useOnEnter( props ) {\n\tconst { batch } = useRegistry();\n\tconst { moveBlocksToPosition, replaceBlocks, selectionChange } =\n\t\tuseDispatch( blockEditorStore );\n\tconst {\n\t\tgetBlockRootClientId,\n\t\tgetBlockIndex,\n\t\tgetBlockOrder,\n\t\tgetBlockName,\n\t\tgetBlock,\n\t\tcanInsertBlockType,\n\t} = useSelect( blockEditorStore );\n\tconst propsRef = useRef( props );\n\tpropsRef.current = props;\n\treturn useRefEffect( ( element ) => {\n\t\tfunction onBeforeInput( event ) {\n\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( event.inputType !== 'insertParagraph' ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst { content, clientId } = propsRef.current;\n\n\t\t\t// The paragraph should be empty.\n\t\t\tif ( content.length ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst wrapperClientId = getBlockRootClientId( clientId );\n\n\t\t\tif (\n\t\t\t\t! hasBlockSupport(\n\t\t\t\t\tgetBlockName( wrapperClientId ),\n\t\t\t\t\t'__experimentalOnEnter',\n\t\t\t\t\tfalse\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst order = getBlockOrder( wrapperClientId );\n\t\t\tconst position = order.indexOf( clientId );\n\n\t\t\t// If it is the last block, exit.\n\t\t\tif ( position === order.length - 1 ) {\n\t\t\t\tlet newWrapperClientId = wrapperClientId;\n\n\t\t\t\twhile (\n\t\t\t\t\t! canInsertBlockType(\n\t\t\t\t\t\tgetBlockName( clientId ),\n\t\t\t\t\t\tgetBlockRootClientId( newWrapperClientId )\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\tnewWrapperClientId =\n\t\t\t\t\t\tgetBlockRootClientId( newWrapperClientId );\n\t\t\t\t}\n\n\t\t\t\tif ( typeof newWrapperClientId === 'string' ) {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\tmoveBlocksToPosition(\n\t\t\t\t\t\t[ clientId ],\n\t\t\t\t\t\twrapperClientId,\n\t\t\t\t\t\tgetBlockRootClientId( newWrapperClientId ),\n\t\t\t\t\t\tgetBlockIndex( newWrapperClientId ) + 1\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst defaultBlockName = getDefaultBlockName();\n\t\t\tconst wrapperBlockName = getBlockName( wrapperClientId );\n\t\t\tconst grandparentClientId = getBlockRootClientId( wrapperClientId );\n\n\t\t\tif (\n\t\t\t\t! canInsertBlockType( defaultBlockName, grandparentClientId ) ||\n\t\t\t\t! canInsertBlockType( wrapperBlockName, grandparentClientId )\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tevent.preventDefault();\n\n\t\t\t// If it is in the middle, split the block in two.\n\t\t\tconst wrapperBlock = getBlock( wrapperClientId );\n\t\t\tconst head = cloneBlock( {\n\t\t\t\t...wrapperBlock,\n\t\t\t\tinnerBlocks: wrapperBlock.innerBlocks.slice( 0, position ),\n\t\t\t} );\n\t\t\tconst middle = createBlock( defaultBlockName );\n\t\t\tconst tail = cloneBlock( {\n\t\t\t\t...wrapperBlock,\n\t\t\t\tinnerBlocks: wrapperBlock.innerBlocks.slice( position + 1 ),\n\t\t\t} );\n\n\t\t\tbatch( () => {\n\t\t\t\treplaceBlocks( wrapperClientId, [ head, middle, tail ] );\n\t\t\t\t// The selected paragraph is a descendant of the replaced\n\t\t\t\t// wrapper, so `replaceBlocks` leaves the selection stale.\n\t\t\t\t// Move it to the new default block explicitly.\n\t\t\t\tselectionChange( middle.clientId );\n\t\t\t} );\n\t\t}\n\n\t\t// Enter is handled on beforeinput: moving focus while the keydown\n\t\t// is still being handled leaves the iOS keyboard's\n\t\t// auto-capitalization stale. Capture phase so we run before\n\t\t// writing-flow's ancestor-bubble handler that gates on\n\t\t// `event.defaultPrevented`.\n\t\treturn subscribeOwnedListener(\n\t\t\telement,\n\t\t\t'beforeinput',\n\t\t\tonBeforeInput,\n\t\t\ttrue\n\t\t);\n\t}, [] );\n}\n"],"mappings":";;;;;;;;AAaA,MAAM,EAAE,2BAA2B,OAAQA,WAAoB;AAE/D,SAAgB,WAAY,OAAQ;CACnC,MAAM,EAAE,UAAU,YAAY;CAC9B,MAAM,EAAE,sBAAsB,eAAe,oBAC5C,YAAaC,KAAiB;CAC/B,MAAM,EACL,sBACA,eACA,eACA,cACA,UACA,uBACG,UAAWA,KAAiB;CAChC,MAAM,WAAW,OAAQ,KAAM;CAC/B,SAAS,UAAU;CACnB,OAAO,cAAgB,YAAa;EACnC,SAAS,cAAe,OAAQ;GAC/B,IAAK,MAAM,kBACV;GAGD,IAAK,MAAM,cAAc,mBACxB;GAGD,MAAM,EAAE,SAAS,aAAa,SAAS;GAGvC,IAAK,QAAQ,QACZ;GAGD,MAAM,kBAAkB,qBAAsB,QAAS;GAEvD,IACC,CAAE,gBACD,aAAc,eAAgB,GAC9B,yBACA,KACD,GAEA;GAGD,MAAM,QAAQ,cAAe,eAAgB;GAC7C,MAAM,WAAW,MAAM,QAAS,QAAS;GAGzC,IAAK,aAAa,MAAM,SAAS,GAAI;IACpC,IAAI,qBAAqB;IAEzB,OACC,CAAE,mBACD,aAAc,QAAS,GACvB,qBAAsB,kBAAmB,CAC1C,GAEA,qBACC,qBAAsB,kBAAmB;IAG3C,I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