@wordpress/block-editor
Version:
133 lines (131 loc) • 4.95 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/components/link-control/use-search-handler.js
var use_search_handler_exports = {};
__export(use_search_handler_exports, {
default: () => useSearchHandler,
handleDirectEntry: () => handleDirectEntry,
handleNoop: () => handleNoop
});
module.exports = __toCommonJS(use_search_handler_exports);
var import_url = require("@wordpress/url");
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_is_url_like = __toESM(require("./is-url-like"));
var import_constants = require("./constants");
var import_store = require("../../store");
var handleNoop = () => Promise.resolve([]);
var handleDirectEntry = (val) => {
let type = import_constants.URL_TYPE;
const protocol = (0, import_url.getProtocol)(val) || "";
if (protocol.includes("mailto")) {
type = import_constants.MAILTO_TYPE;
}
if (protocol.includes("tel")) {
type = import_constants.TEL_TYPE;
}
if (val?.startsWith("#")) {
type = import_constants.INTERNAL_TYPE;
}
return Promise.resolve([
{
id: val,
title: val,
url: type === "URL" ? (0, import_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 (0, import_is_url_like.default)(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: import_constants.CREATE_TYPE
});
};
function useSearchHandler(suggestionsQuery, allowDirectEntry, withCreateSuggestion) {
const { fetchSearchSuggestions, pageOnFront, pageForPosts } = (0, import_data.useSelect)(
(select) => {
const { getSettings } = select(import_store.store);
return {
pageOnFront: getSettings().pageOnFront,
pageForPosts: getSettings().pageForPosts,
fetchSearchSuggestions: getSettings().__experimentalFetchLinkSuggestions
};
},
[]
);
const directEntryHandler = allowDirectEntry ? handleDirectEntry : handleNoop;
return (0, import_element.useCallback)(
(val, { isInitialSuggestions }) => {
return (0, import_is_url_like.default)(val) ? directEntryHandler(val, { isInitialSuggestions }) : handleEntitySearch(
val,
{ ...suggestionsQuery, isInitialSuggestions },
fetchSearchSuggestions,
withCreateSuggestion,
pageOnFront,
pageForPosts
);
},
[
directEntryHandler,
fetchSearchSuggestions,
pageOnFront,
pageForPosts,
suggestionsQuery,
withCreateSuggestion
]
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
handleDirectEntry,
handleNoop
});
//# sourceMappingURL=use-search-handler.js.map