@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 9.28 kB
Source Map (JSON)
{"version":3,"file":"use-block-suggestions.cjs","names":["unlock","blockEditorPrivateApis","createElement","BlockIcon","__","useSelect","blockEditorStore","useDispatch","useCallback","createBlock","createBlocksFromInnerBlocksTemplate","sprintf"],"sources":["../../../src/navigation-link/shared/use-block-suggestions.js"],"sourcesContent":["import { __, sprintf } from '@wordpress/i18n';\nimport { speak } from '@wordpress/a11y';\nimport { createElement, useCallback } from '@wordpress/element';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport {\n\tcreateBlock,\n\tcreateBlocksFromInnerBlocksTemplate,\n} from '@wordpress/blocks';\nimport {\n\tBlockIcon,\n\tstore as blockEditorStore,\n\tprivateApis as blockEditorPrivateApis,\n} from '@wordpress/block-editor';\nimport { unlock } from '../../lock-unlock';\n\nconst { searchItems, normalizeString } = unlock( blockEditorPrivateApis );\n\n/**\n * The `type` of a suggestion that inserts a block rather than setting a link.\n */\nexport const BLOCK_SUGGESTION_TYPE = 'block';\n\n/**\n * Whether a suggestion inserts a block rather than setting a link.\n *\n * The `type` alone is not enough: a site can register a post type called\n * \"block\", whose results arrive with that type. Only a block suggestion\n * carries the inserter item it was made from.\n *\n * @param {Object} suggestion A link suggestion.\n * @return {boolean} Whether it is a block suggestion.\n */\nexport function isBlockSuggestion( suggestion ) {\n\treturn (\n\t\tsuggestion?.type === BLOCK_SUGGESTION_TYPE &&\n\t\ttypeof suggestion.blockItemId === 'string'\n\t);\n}\n\n/**\n * The link variations are left out: searching already finds the pages, posts\n * and terms they would link to.\n */\nconst EXCLUDED_BLOCK_NAMES = [ 'core/navigation-link' ];\n\n// Match on what a block is called, not on its description or category, which\n// would list blocks for words that only happen to describe them.\nconst SEARCH_FIELDS = [\n\t{ get: ( item ) => item.title },\n\t{ get: ( item ) => item.keywords },\n];\n\n/**\n * Adds the blocks that match a search to the link suggestions.\n *\n * A block whose title starts with the search is what was asked for by name,\n * so it goes before the links. Any other matching block goes after them.\n *\n * @param {Array} suggestions Link suggestions.\n * @param {Array} blockItems Inserter items the Navigation allows.\n * @param {string} searchTerm What the user typed.\n * @return {Array} The suggestions, with any matching blocks added.\n */\nexport function addBlockSuggestions( suggestions, blockItems, searchTerm ) {\n\tif ( ! searchTerm?.trim() ) {\n\t\treturn suggestions;\n\t}\n\n\tconst matches = searchItems( blockItems, searchTerm, {\n\t\tfields: SEARCH_FIELDS,\n\t\tfilter: ( item ) =>\n\t\t\t! item.isDisabled && ! EXCLUDED_BLOCK_NAMES.includes( item.name ),\n\t} );\n\n\tif ( ! matches.length ) {\n\t\treturn suggestions;\n\t}\n\n\tconst search = normalizeString( searchTerm.trim() );\n\tconst before = [];\n\tconst after = [];\n\n\tfor ( const item of matches ) {\n\t\tconst suggestion = {\n\t\t\tid: item.id,\n\t\t\tblockItemId: item.id,\n\t\t\ttype: BLOCK_SUGGESTION_TYPE,\n\t\t\ttitle: item.title,\n\t\t\ticon: createElement( BlockIcon, { icon: item.icon } ),\n\t\t\ttypeLabel: __( 'Block' ),\n\t\t};\n\n\t\tif ( normalizeString( item.title ).startsWith( search ) ) {\n\t\t\tbefore.push( suggestion );\n\t\t} else {\n\t\t\tafter.push( suggestion );\n\t\t}\n\t}\n\n\treturn [ ...before, ...suggestions, ...after ];\n}\n\n/**\n * Lists the blocks a Navigation allows in a link's search results, and\n * inserts the one that is chosen.\n *\n * The allowed blocks are read when a search runs, not subscribed to, so an\n * open link UI does no work as the editor changes.\n *\n * @param {Object} options Hook options.\n * @param {string} options.clientId Client ID of the link or submenu.\n * @param {boolean} options.isEnabled Whether to list blocks at all.\n * @param {string[]=} options.allowedBlockNames Names of the blocks to list.\n * Every allowed block is listed\n * when left out, and none when\n * empty.\n * @param {Function=} options.onBlockInsert Called with the inserted block by a\n * caller that tracks the new link,\n * such as the list view.\n * @param {Function=} options.onClose Closes the link UI.\n * @return {{ transformSuggestions: Function|undefined, insertBlockFromSuggestion: Function }}\n * The `transformSuggestions` to give LinkControl, and the handler for a chosen\n * block suggestion.\n */\nexport function useBlockSuggestions( {\n\tclientId,\n\tisEnabled,\n\tallowedBlockNames,\n\tonBlockInsert,\n\tonClose,\n} ) {\n\tconst {\n\t\tgetBlockIndex,\n\t\tgetBlockOrder,\n\t\tgetBlockRootClientId,\n\t\tgetInserterItems,\n\t} = useSelect( blockEditorStore );\n\tconst { insertBlock, replaceBlock } = useDispatch( blockEditorStore );\n\n\tconst transformSuggestions = useCallback(\n\t\t( suggestions, { isInitialSuggestions, searchTerm } ) => {\n\t\t\t// An empty list asks for no blocks.\n\t\t\tif ( isInitialSuggestions || allowedBlockNames?.length === 0 ) {\n\t\t\t\treturn suggestions;\n\t\t\t}\n\n\t\t\tconst items = getInserterItems( getBlockRootClientId( clientId ) );\n\t\t\tconst blockItems = allowedBlockNames\n\t\t\t\t? items.filter( ( { name } ) =>\n\t\t\t\t\t\tallowedBlockNames.includes( name )\n\t\t\t\t\t)\n\t\t\t\t: items;\n\n\t\t\treturn addBlockSuggestions( suggestions, blockItems, searchTerm );\n\t\t},\n\t\t[ clientId, getBlockRootClientId, getInserterItems, allowedBlockNames ]\n\t);\n\n\tconst insertBlockFromSuggestion = ( itemId ) => {\n\t\tconst rootClientId = getBlockRootClientId( clientId );\n\t\tconst item = getInserterItems( rootClientId ).find(\n\t\t\t( { id } ) => id === itemId\n\t\t);\n\n\t\tif ( ! item ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst block = createBlock(\n\t\t\titem.name,\n\t\t\titem.initialAttributes,\n\t\t\tcreateBlocksFromInnerBlocksTemplate( item.innerBlocks )\n\t\t);\n\t\tconst index = getBlockIndex( clientId );\n\n\t\tif ( onBlockInsert ) {\n\t\t\t// A caller that tracks the new link, such as the list view, removes\n\t\t\t// it itself once told about the block, as with the \"Add block\" pane.\n\t\t\tinsertBlock( block, index, rootClientId, false );\n\t\t\tonBlockInsert( block );\n\t\t} else if ( getBlockOrder( clientId ).length ) {\n\t\t\t// A submenu keeps its items, so the block goes before it, where the\n\t\t\t// \"Add block\" pane puts it.\n\t\t\tinsertBlock( block, index, rootClientId );\n\t\t\tonClose?.();\n\t\t} else {\n\t\t\t// An empty link is replaced by the block.\n\t\t\treplaceBlock( clientId, block );\n\t\t}\n\n\t\tspeak(\n\t\t\tsprintf(\n\t\t\t\t/* translators: %s: The title of the block that was added. */\n\t\t\t\t__( '%s block added.' ),\n\t\t\t\titem.title\n\t\t\t)\n\t\t);\n\t};\n\n\treturn {\n\t\ttransformSuggestions: isEnabled ? transformSuggestions : undefined,\n\t\tinsertBlockFromSuggestion,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;AAeA,MAAM,EAAE,aAAa,qBAAA,G,+BAAoBA,CAAAA,CAAAA,OAAAA,CAAQC,wBAAAA,WAAuB;;;;AAKxE,MAAa,wBAAwB;;;;;;;;;;;AAYrC,SAAgB,kBAAmB,YAAa;CAC/C,OACC,YAAY,SAAA,WACZ,OAAO,WAAW,gBAAgB;AAEpC;;;;;AAMA,MAAM,uBAAuB,CAAE,sBAAuB;AAItD,MAAM,gBAAgB,CACrB,EAAE,MAAO,SAAU,KAAK,MAAM,GAC9B,EAAE,MAAO,SAAU,KAAK,SAAS,CAClC;;;;;;;;;;;;AAaA,SAAgB,oBAAqB,aAAa,YAAY,YAAa;CAC1E,IAAK,CAAE,YAAY,KAAK,GACvB,OAAO;CAGR,MAAM,UAAU,YAAa,YAAY,YAAY;EACpD,QAAQ;EACR,SAAU,SACT,CAAE,KAAK,cAAc,CAAE,qBAAqB,SAAU,KAAK,IAAK;CAClE,CAAE;CAEF,IAAK,CAAE,QAAQ,QACd,OAAO;CAGR,MAAM,SAAS,gBAAiB,WAAW,KAAK,CAAE;CAClD,MAAM,SAAS,CAAC;CAChB,MAAM,QAAQ,CAAC;CAEf,KAAM,MAAM,QAAQ,SAAU;EAC7B,MAAM,aAAa;GAClB,IAAI,KAAK;GACT,aAAa,KAAK;GAClB,MAAM;GACN,OAAO,KAAK;GACZ,OAAA,GAAMC,mBAAAA,c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