UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 6.26 kB
{ "version": 3, "sources": ["../../../src/components/link-control/use-search-handler.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { getProtocol, prependHTTP } from '@wordpress/url';\nimport { useCallback } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport isURLLike from './is-url-like';\nimport {\n\tCREATE_TYPE,\n\tTEL_TYPE,\n\tMAILTO_TYPE,\n\tINTERNAL_TYPE,\n\tURL_TYPE,\n} from './constants';\nimport { store as blockEditorStore } from '../../store';\n\nexport const handleNoop = () => Promise.resolve( [] );\n\nexport const handleDirectEntry = ( val ) => {\n\tlet type = URL_TYPE;\n\n\tconst protocol = getProtocol( val ) || '';\n\n\tif ( protocol.includes( 'mailto' ) ) {\n\t\ttype = MAILTO_TYPE;\n\t}\n\n\tif ( protocol.includes( 'tel' ) ) {\n\t\ttype = TEL_TYPE;\n\t}\n\n\tif ( val?.startsWith( '#' ) ) {\n\t\ttype = INTERNAL_TYPE;\n\t}\n\n\treturn Promise.resolve( [\n\t\t{\n\t\t\tid: val,\n\t\t\ttitle: val,\n\t\t\turl: type === 'URL' ? prependHTTP( val ) : val,\n\t\t\ttype,\n\t\t},\n\t] );\n};\n\nconst handleEntitySearch = async (\n\tval,\n\tsuggestionsQuery,\n\tfetchSearchSuggestions,\n\twithCreateSuggestion,\n\tpageOnFront,\n\tpageForPosts\n) => {\n\tconst { isInitialSuggestions } = suggestionsQuery;\n\n\tconst results = await fetchSearchSuggestions( val, suggestionsQuery );\n\n\t// Identify front page and update type to match.\n\tresults.map( ( result ) => {\n\t\tif ( Number( result.id ) === pageOnFront ) {\n\t\t\tresult.isFrontPage = true;\n\t\t\treturn result;\n\t\t} else if ( Number( result.id ) === pageForPosts ) {\n\t\t\tresult.isBlogHome = true;\n\t\t\treturn result;\n\t\t}\n\n\t\treturn result;\n\t} );\n\n\t// If displaying initial suggestions just return plain results.\n\tif ( isInitialSuggestions ) {\n\t\treturn results;\n\t}\n\n\t// Here we append a faux suggestion to represent a \"CREATE\" option. This\n\t// is detected in the rendering of the search results and handled as a\n\t// special case. This is currently necessary because the suggestions\n\t// dropdown will only appear if there are valid suggestions and\n\t// therefore unless the create option is a suggestion it will not\n\t// display in scenarios where there are no results returned from the\n\t// API. In addition promoting CREATE to a first class suggestion affords\n\t// the a11y benefits afforded by `URLInput` to all suggestions (eg:\n\t// keyboard handling, ARIA roles...etc).\n\t//\n\t// Note also that the value of the `title` and `url` properties must correspond\n\t// to the text value of the `<input>`. This is because `title` is used\n\t// when creating the suggestion. Similarly `url` is used when using keyboard to select\n\t// the suggestion (the <form> `onSubmit` handler falls-back to `url`).\n\treturn isURLLike( val ) || ! withCreateSuggestion\n\t\t? results\n\t\t: results.concat( {\n\t\t\t\t// the `id` prop is intentionally omitted here because it\n\t\t\t\t// is never exposed as part of the component's public API.\n\t\t\t\t// see: https://github.com/WordPress/gutenberg/pull/19775#discussion_r378931316.\n\t\t\t\ttitle: val, // Must match the existing `<input>`s text value.\n\t\t\t\turl: val, // Must match the existing `<input>`s text value.\n\t\t\t\ttype: CREATE_TYPE,\n\t\t } );\n};\n\nexport default function useSearchHandler(\n\tsuggestionsQuery,\n\tallowDirectEntry,\n\twithCreateSuggestion\n) {\n\tconst { fetchSearchSuggestions, pageOnFront, pageForPosts } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getSettings } = select( blockEditorStore );\n\n\t\t\treturn {\n\t\t\t\tpageOnFront: getSettings().pageOnFront,\n\t\t\t\tpageForPosts: getSettings().pageForPosts,\n\t\t\t\tfetchSearchSuggestions:\n\t\t\t\t\tgetSettings().__experimentalFetchLinkSuggestions,\n\t\t\t};\n\t\t},\n\t\t[]\n\t);\n\n\tconst directEntryHandler = allowDirectEntry\n\t\t? handleDirectEntry\n\t\t: handleNoop;\n\n\treturn useCallback(\n\t\t( val, { isInitialSuggestions } ) => {\n\t\t\treturn isURLLike( val )\n\t\t\t\t? directEntryHandler( val, { isInitialSuggestions } )\n\t\t\t\t: handleEntitySearch(\n\t\t\t\t\t\tval,\n\t\t\t\t\t\t{ ...suggestionsQuery, isInitialSuggestions },\n\t\t\t\t\t\tfetchSearchSuggestions,\n\t\t\t\t\t\twithCreateSuggestion,\n\t\t\t\t\t\tpageOnFront,\n\t\t\t\t\t\tpageForPosts\n\t\t\t\t );\n\t\t},\n\t\t[\n\t\t\tdirectEntryHandler,\n\t\t\tfetchSearchSuggestions,\n\t\t\tpageOnFront,\n\t\t\tpageForPosts,\n\t\t\tsuggestionsQuery,\n\t\t\twithCreateSuggestion,\n\t\t]\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAyC;AACzC,qBAA4B;AAC5B,kBAA0B;AAK1B,yBAAsB;AACtB,uBAMO;AACP,mBAA0C;AAEnC,IAAM,aAAa,MAAM,QAAQ,QAAS,CAAC,CAAE;AAE7C,IAAM,oBAAoB,CAAE,QAAS;AAC3C,MAAI,OAAO;AAEX,QAAM,eAAW,wBAAa,GAAI,KAAK;AAEvC,MAAK,SAAS,SAAU,QAAS,GAAI;AACpC,WAAO;AAAA,EACR;AAEA,MAAK,SAAS,SAAU,KAAM,GAAI;AACjC,WAAO;AAAA,EACR;AAEA,MAAK,KAAK,WAAY,GAAI,GAAI;AAC7B,WAAO;AAAA,EACR;AAEA,SAAO,QAAQ,QAAS;AAAA,IACvB;AAAA,MACC,IAAI;AAAA,MACJ,OAAO;AAAA,MACP,KAAK,SAAS,YAAQ,wBAAa,GAAI,IAAI;AAAA,MAC3C;AAAA,IACD;AAAA,EACD,CAAE;AACH;AAEA,IAAM,qBAAqB,OAC1B,KACA,kBACA,wBACA,sBACA,aACA,iBACI;AACJ,QAAM,EAAE,qBAAqB,IAAI;AAEjC,QAAM,UAAU,MAAM,uBAAwB,KAAK,gBAAiB;AAGpE,UAAQ,IAAK,CAAE,WAAY;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