@wordpress/block-editor
Version:
8 lines (7 loc) • 10.4 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../../src/components/inserter/hooks/use-insertion-point.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useDispatch, useRegistry, useSelect } from '@wordpress/data';\nimport { isUnmodifiedDefaultBlock } from '@wordpress/blocks';\nimport { _n, sprintf } from '@wordpress/i18n';\nimport { speak } from '@wordpress/a11y';\nimport { useCallback } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../../store';\nimport { unlock } from '../../../lock-unlock';\n\nfunction getIndex( {\n\tdestinationRootClientId,\n\tdestinationIndex,\n\trootClientId,\n\tregistry,\n} ) {\n\tif ( rootClientId === destinationRootClientId ) {\n\t\treturn destinationIndex;\n\t}\n\tconst parents = [\n\t\t'',\n\t\t...registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.getBlockParents( destinationRootClientId ),\n\t\tdestinationRootClientId,\n\t];\n\tconst parentIndex = parents.indexOf( rootClientId );\n\tif ( parentIndex !== -1 ) {\n\t\treturn (\n\t\t\tregistry\n\t\t\t\t.select( blockEditorStore )\n\t\t\t\t.getBlockIndex( parents[ parentIndex + 1 ] ) + 1\n\t\t);\n\t}\n\treturn registry.select( blockEditorStore ).getBlockOrder( rootClientId )\n\t\t.length;\n}\n\n/**\n * @typedef WPInserterConfig\n *\n * @property {string=} rootClientId If set, insertion will be into the\n * block with this ID.\n * @property {number=} insertionIndex If set, insertion will be into this\n * explicit position.\n * @property {string=} clientId If set, insertion will be after the\n * block with this ID.\n * @property {boolean=} isAppender Whether the inserter is an appender\n * or not.\n * @property {Function=} onSelect Called after insertion.\n */\n\n/**\n * Returns the insertion point state given the inserter config.\n *\n * @param {WPInserterConfig} config Inserter Config.\n * @return {Array} Insertion Point State (rootClientID, onInsertBlocks and onToggle).\n */\nfunction useInsertionPoint( {\n\trootClientId = '',\n\tinsertionIndex,\n\tclientId,\n\tisAppender,\n\tonSelect,\n\tshouldFocusBlock = true,\n\tselectBlockOnInsert = true,\n} ) {\n\tconst registry = useRegistry();\n\tconst {\n\t\tgetSelectedBlock,\n\t\tgetClosestAllowedInsertionPoint,\n\t\tisBlockInsertionPointVisible,\n\t} = unlock( useSelect( blockEditorStore ) );\n\tconst { destinationRootClientId, destinationIndex } = useSelect(\n\t\t( select ) => {\n\t\t\tconst {\n\t\t\t\tgetSelectedBlockClientId,\n\t\t\t\tgetBlockRootClientId,\n\t\t\t\tgetBlockIndex,\n\t\t\t\tgetBlockOrder,\n\t\t\t\tgetInsertionPoint,\n\t\t\t} = unlock( select( blockEditorStore ) );\n\t\t\tconst selectedBlockClientId = getSelectedBlockClientId();\n\t\t\tlet _destinationRootClientId = rootClientId;\n\t\t\tlet _destinationIndex;\n\t\t\tconst insertionPoint = getInsertionPoint();\n\n\t\t\tif ( insertionIndex !== undefined ) {\n\t\t\t\t// Insert into a specific index.\n\t\t\t\t_destinationIndex = insertionIndex;\n\t\t\t} else if (\n\t\t\t\tinsertionPoint &&\n\t\t\t\tinsertionPoint.hasOwnProperty( 'index' )\n\t\t\t) {\n\t\t\t\t_destinationRootClientId = insertionPoint?.rootClientId\n\t\t\t\t\t? insertionPoint.rootClientId\n\t\t\t\t\t: rootClientId;\n\t\t\t\t_destinationIndex = insertionPoint.index;\n\t\t\t} else if ( clientId ) {\n\t\t\t\t// Insert after a specific client ID.\n\t\t\t\t_destinationIndex = getBlockIndex( clientId );\n\t\t\t} else if ( ! isAppender && selectedBlockClientId ) {\n\t\t\t\t_destinationRootClientId = getBlockRootClientId(\n\t\t\t\t\tselectedBlockClientId\n\t\t\t\t);\n\t\t\t\t_destinationIndex = getBlockIndex( selectedBlockClientId ) + 1;\n\t\t\t} else {\n\t\t\t\t// Insert at the end of the list.\n\t\t\t\t_destinationIndex = getBlockOrder(\n\t\t\t\t\t_destinationRootClientId\n\t\t\t\t).length;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tdestinationRootClientId: _destinationRootClientId,\n\t\t\t\tdestinationIndex: _destinationIndex,\n\t\t\t};\n\t\t},\n\t\t[ rootClientId, insertionIndex, clientId, isAppender ]\n\t);\n\n\tconst {\n\t\treplaceBlocks,\n\t\tinsertBlocks,\n\t\tshowInsertionPoint,\n\t\thideInsertionPoint,\n\t\tsetLastFocus,\n\t} = unlock( useDispatch( blockEditorStore ) );\n\n\tconst onInsertBlocks = useCallback(\n\t\t( blocks, meta, shouldForceFocusBlock = false, _rootClientId ) => {\n\t\t\t// When we are trying to move focus or select a new block on insert, we also\n\t\t\t// need to clear the last focus to avoid the focus being set to the wrong block\n\t\t\t// when tabbing back into the canvas if the block was added from outside the\n\t\t\t// editor canvas.\n\t\t\tif (\n\t\t\t\tshouldForceFocusBlock ||\n\t\t\t\tshouldFocusBlock ||\n\t\t\t\tselectBlockOnInsert\n\t\t\t) {\n\t\t\t\tsetLastFocus( null );\n\t\t\t}\n\n\t\t\tconst selectedBlock = getSelectedBlock();\n\n\t\t\tif (\n\t\t\t\t! isAppender &&\n\t\t\t\tselectedBlock &&\n\t\t\t\tisUnmodifiedDefaultBlock( selectedBlock, 'content' )\n\t\t\t) {\n\t\t\t\treplaceBlocks(\n\t\t\t\t\tselectedBlock.clientId,\n\t\t\t\t\tblocks,\n\t\t\t\t\tnull,\n\t\t\t\t\tshouldFocusBlock || shouldForceFocusBlock ? 0 : null,\n\t\t\t\t\tmeta\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tinsertBlocks(\n\t\t\t\t\tblocks,\n\t\t\t\t\tisAppender || _rootClientId === undefined\n\t\t\t\t\t\t? destinationIndex\n\t\t\t\t\t\t: getIndex( {\n\t\t\t\t\t\t\t\tdestinationRootClientId,\n\t\t\t\t\t\t\t\tdestinationIndex,\n\t\t\t\t\t\t\t\trootClientId: _rootClientId,\n\t\t\t\t\t\t\t\tregistry,\n\t\t\t\t\t\t } ),\n\t\t\t\t\tisAppender || _rootClientId === undefined\n\t\t\t\t\t\t? destinationRootClientId\n\t\t\t\t\t\t: _rootClientId,\n\t\t\t\t\tselectBlockOnInsert,\n\t\t\t\t\tshouldFocusBlock || shouldForceFocusBlock ? 0 : null,\n\t\t\t\t\tmeta\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst blockLength = Array.isArray( blocks ) ? blocks.length : 1;\n\t\t\tconst message = sprintf(\n\t\t\t\t// translators: %d: the name of the block that has been added\n\t\t\t\t_n( '%d block added.', '%d blocks added.', blockLength ),\n\t\t\t\tblockLength\n\t\t\t);\n\t\t\tspeak( message );\n\n\t\t\tif ( onSelect ) {\n\t\t\t\tonSelect( blocks );\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tisAppender,\n\t\t\tgetSelectedBlock,\n\t\t\treplaceBlocks,\n\t\t\tinsertBlocks,\n\t\t\tdestinationRootClientId,\n\t\t\tdestinationIndex,\n\t\t\tonSelect,\n\t\t\tshouldFocusBlock,\n\t\t\tselectBlockOnInsert,\n\t\t\tsetLastFocus,\n\t\t\tregistry,\n\t\t]\n\t);\n\n\tconst onToggleInsertionPoint = useCallback(\n\t\t( item ) => {\n\t\t\tif ( item && ! isBlockInsertionPointVisible() ) {\n\t\t\t\tconst allowedDestinationRootClientId =\n\t\t\t\t\tgetClosestAllowedInsertionPoint(\n\t\t\t\t\t\titem.name,\n\t\t\t\t\t\tdestinationRootClientId\n\t\t\t\t\t);\n\t\t\t\tif ( allowedDestinationRootClientId !== null ) {\n\t\t\t\t\tshowInsertionPoint(\n\t\t\t\t\t\tallowedDestinationRootClientId,\n\t\t\t\t\t\tgetIndex( {\n\t\t\t\t\t\t\tdestinationRootClientId,\n\t\t\t\t\t\t\tdestinationIndex,\n\t\t\t\t\t\t\trootClientId: allowedDestinationRootClientId,\n\t\t\t\t\t\t\tregistry,\n\t\t\t\t\t\t} )\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\thideInsertionPoint();\n\t\t\t}\n\t\t},\n\t\t[\n\t\t\tgetClosestAllowedInsertionPoint,\n\t\t\tisBlockInsertionPointVisible,\n\t\t\tshowInsertionPoint,\n\t\t\thideInsertionPoint,\n\t\t\tdestinationRootClientId,\n\t\t\tdestinationIndex,\n\t\t\tregistry,\n\t\t]\n\t);\n\n\treturn [ destinationRootClientId, onInsertBlocks, onToggleInsertionPoint ];\n}\n\nexport default useInsertionPoint;\n"],
"mappings": ";AAGA,SAAS,aAAa,aAAa,iBAAiB;AACpD,SAAS,gCAAgC;AACzC,SAAS,IAAI,eAAe;AAC5B,SAAS,aAAa;AACtB,SAAS,mBAAmB;AAK5B,SAAS,SAAS,wBAAwB;AAC1C,SAAS,cAAc;AAEvB,SAAS,SAAU;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAI;AACH,MAAK,iBAAiB,yBAA0B;AAC/C,WAAO;AAAA,EACR;AACA,QAAM,UAAU;AAAA,IACf;AAAA,IACA,GAAG,SACD,OAAQ,gBAAiB,EACzB,gBAAiB,uBAAwB;AAAA,IAC3C;AAAA,EACD;AACA,QAAM,cAAc,QAAQ,QAAS,YAAa;AAClD,MAAK,gBAAgB,IAAK;AACzB,WACC,SACE,OAAQ,gBAAiB,EACzB,cAAe,QAAS,cAAc,CAAE,CAAE,IAAI;AAAA,EAElD;AACA,SAAO,SAAS,OAAQ,gBAAiB,EAAE,cAAe,YAAa,EACrE;AACH;AAsBA,SAAS,kBAAmB;AAAA,EAC3B,eAAe;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,mBAAmB;AAAA,EACnB,sBAAsB;AACvB,GAAI;AACH,QAAM,WAAW,YAAY;AAC7B,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACD,IAAI,OAAQ,UAAW,gBAAiB,CAAE;AAC1C,QAAM,EAAE,yBAAyB,iBAAiB,IAAI;AAAA,IACrD,CAAE,WAAY;AACb,YAAM;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD,IAAI,OAAQ,OAAQ,g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