@wordpress/block-editor
Version:
120 lines (114 loc) • 4.37 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useSearchHandler;
exports.handleNoop = exports.handleDirectEntry = void 0;
var _url = require("@wordpress/url");
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _isUrlLike = _interopRequireDefault(require("./is-url-like"));
var _constants = require("./constants");
var _store = require("../../store");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const handleNoop = () => Promise.resolve([]);
exports.handleNoop = handleNoop;
const handleDirectEntry = val => {
let type = _constants.URL_TYPE;
const protocol = (0, _url.getProtocol)(val) || '';
if (protocol.includes('mailto')) {
type = _constants.MAILTO_TYPE;
}
if (protocol.includes('tel')) {
type = _constants.TEL_TYPE;
}
if (val?.startsWith('#')) {
type = _constants.INTERNAL_TYPE;
}
return Promise.resolve([{
id: val,
title: val,
url: type === 'URL' ? (0, _url.prependHTTP)(val) : val,
type
}]);
};
exports.handleDirectEntry = handleDirectEntry;
const handleEntitySearch = async (val, suggestionsQuery, fetchSearchSuggestions, withCreateSuggestion, pageOnFront, pageForPosts) => {
const {
isInitialSuggestions
} = suggestionsQuery;
const results = await fetchSearchSuggestions(val, suggestionsQuery);
// Identify front page and update type to match.
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 displaying initial suggestions just return plain results.
if (isInitialSuggestions) {
return results;
}
// Here we append a faux suggestion to represent a "CREATE" option. This
// is detected in the rendering of the search results and handled as a
// special case. This is currently necessary because the suggestions
// dropdown will only appear if there are valid suggestions and
// therefore unless the create option is a suggestion it will not
// display in scenarios where there are no results returned from the
// API. In addition promoting CREATE to a first class suggestion affords
// the a11y benefits afforded by `URLInput` to all suggestions (eg:
// keyboard handling, ARIA roles...etc).
//
// Note also that the value of the `title` and `url` properties must correspond
// to the text value of the `<input>`. This is because `title` is used
// when creating the suggestion. Similarly `url` is used when using keyboard to select
// the suggestion (the <form> `onSubmit` handler falls-back to `url`).
return (0, _isUrlLike.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: _constants.CREATE_TYPE
});
};
function useSearchHandler(suggestionsQuery, allowDirectEntry, withCreateSuggestion) {
const {
fetchSearchSuggestions,
pageOnFront,
pageForPosts
} = (0, _data.useSelect)(select => {
const {
getSettings
} = select(_store.store);
return {
pageOnFront: getSettings().pageOnFront,
pageForPosts: getSettings().pageForPosts,
fetchSearchSuggestions: getSettings().__experimentalFetchLinkSuggestions
};
}, []);
const directEntryHandler = allowDirectEntry ? handleDirectEntry : handleNoop;
return (0, _element.useCallback)((val, {
isInitialSuggestions
}) => {
return (0, _isUrlLike.default)(val) ? directEntryHandler(val, {
isInitialSuggestions
}) : handleEntitySearch(val, {
...suggestionsQuery,
isInitialSuggestions
}, fetchSearchSuggestions, withCreateSuggestion, pageOnFront, pageForPosts);
}, [directEntryHandler, fetchSearchSuggestions, pageOnFront, pageForPosts, suggestionsQuery, withCreateSuggestion]);
}
//# sourceMappingURL=use-search-handler.js.map