@wordpress/block-editor
Version:
103 lines (102 loc) • 3.11 kB
JavaScript
// packages/block-editor/src/components/link-control/use-search-handler.js
import { getProtocol, prependHTTP } from "@wordpress/url";
import { useCallback } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import isURLLike from "./is-url-like";
import {
CREATE_TYPE,
TEL_TYPE,
MAILTO_TYPE,
INTERNAL_TYPE,
URL_TYPE
} from "./constants";
import { store as blockEditorStore } from "../../store";
var handleNoop = () => Promise.resolve([]);
var handleDirectEntry = (val) => {
let type = URL_TYPE;
const protocol = getProtocol(val) || "";
if (protocol.includes("mailto")) {
type = MAILTO_TYPE;
}
if (protocol.includes("tel")) {
type = TEL_TYPE;
}
if (val?.startsWith("#")) {
type = INTERNAL_TYPE;
}
return Promise.resolve([
{
id: val,
title: val,
url: type === "URL" ? prependHTTP(val) : val,
type
}
]);
};
var handleEntitySearch = async (val, suggestionsQuery, fetchSearchSuggestions, withCreateSuggestion, pageOnFront, pageForPosts) => {
const { isInitialSuggestions } = suggestionsQuery;
const results = await fetchSearchSuggestions(val, suggestionsQuery);
results.map((result) => {
if (Number(result.id) === pageOnFront) {
result.isFrontPage = true;
return result;
} else if (Number(result.id) === pageForPosts) {
result.isBlogHome = true;
return result;
}
return result;
});
if (isInitialSuggestions) {
return results;
}
return isURLLike(val) || !withCreateSuggestion ? results : results.concat({
// the `id` prop is intentionally omitted here because it
// is never exposed as part of the component's public API.
// see: https://github.com/WordPress/gutenberg/pull/19775#discussion_r378931316.
title: val,
// Must match the existing `<input>`s text value.
url: val,
// Must match the existing `<input>`s text value.
type: CREATE_TYPE
});
};
function useSearchHandler(suggestionsQuery, allowDirectEntry, withCreateSuggestion) {
const { fetchSearchSuggestions, pageOnFront, pageForPosts } = useSelect(
(select) => {
const { getSettings } = select(blockEditorStore);
return {
pageOnFront: getSettings().pageOnFront,
pageForPosts: getSettings().pageForPosts,
fetchSearchSuggestions: getSettings().__experimentalFetchLinkSuggestions
};
},
[]
);
const directEntryHandler = allowDirectEntry ? handleDirectEntry : handleNoop;
return useCallback(
(val, { isInitialSuggestions }) => {
return isURLLike(val) ? directEntryHandler(val, { isInitialSuggestions }) : handleEntitySearch(
val,
{ ...suggestionsQuery, isInitialSuggestions },
fetchSearchSuggestions,
withCreateSuggestion,
pageOnFront,
pageForPosts
);
},
[
directEntryHandler,
fetchSearchSuggestions,
pageOnFront,
pageForPosts,
suggestionsQuery,
withCreateSuggestion
]
);
}
export {
useSearchHandler as default,
handleDirectEntry,
handleNoop
};
//# sourceMappingURL=use-search-handler.js.map