@wordpress/block-editor
Version:
143 lines (138 loc) • 4.25 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.__experimentalLinkControlSearchItem = exports.LinkControlSearchItem = void 0;
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _icons = require("@wordpress/icons");
var _dom = require("@wordpress/dom");
var _url = require("@wordpress/url");
var _compose = require("@wordpress/compose");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
const ICONS_MAP = {
post: _icons.postList,
page: _icons.page,
post_tag: _icons.tag,
category: _icons.category,
attachment: _icons.file
};
function SearchItemIcon({
isURL,
suggestion
}) {
let icon = null;
if (isURL) {
icon = _icons.globe;
} else if (suggestion.type in ICONS_MAP) {
icon = ICONS_MAP[suggestion.type];
if (suggestion.type === 'page') {
if (suggestion.isFrontPage) {
icon = _icons.home;
}
if (suggestion.isBlogHome) {
icon = _icons.verse;
}
}
}
if (icon) {
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
className: "block-editor-link-control__search-item-icon",
icon: icon
});
}
return null;
}
/**
* Adds a leading slash to a url if it doesn't already have one.
* @param {string} url the url to add a leading slash to.
* @return {string} the url with a leading slash.
*/
function addLeadingSlash(url) {
const trimmedURL = url?.trim();
if (!trimmedURL?.length) {
return url;
}
return url?.replace(/^\/?/, '/');
}
function removeTrailingSlash(url) {
const trimmedURL = url?.trim();
if (!trimmedURL?.length) {
return url;
}
return url?.replace(/\/$/, '');
}
const partialRight = (fn, ...partialArgs) => (...args) => fn(...args, ...partialArgs);
const defaultTo = d => v => {
return v === null || v === undefined || v !== v ? d : v;
};
/**
* Prepares a URL for display in the UI.
* - decodes the URL.
* - filters it (removes protocol, www, etc.).
* - truncates it if necessary.
* - adds a leading slash.
* @param {string} url the url.
* @return {string} the processed url to display.
*/
function getURLForDisplay(url) {
if (!url) {
return url;
}
return (0, _compose.pipe)(_url.safeDecodeURI, _url.getPath, defaultTo(''), partialRight(_url.filterURLForDisplay, 24), removeTrailingSlash, addLeadingSlash)(url);
}
const LinkControlSearchItem = ({
itemProps,
suggestion,
searchTerm,
onClick,
isURL = false,
shouldShowType = false
}) => {
const info = isURL ? (0, _i18n.__)('Press ENTER to add this link') : getURLForDisplay(suggestion.url);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
...itemProps,
info: info,
iconPosition: "left",
icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(SearchItemIcon, {
suggestion: suggestion,
isURL: isURL
}),
onClick: onClick,
shortcut: shouldShowType && getVisualTypeName(suggestion),
className: "block-editor-link-control__search-item",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextHighlight
// The component expects a plain text string.
, {
text: (0, _dom.__unstableStripHTML)(suggestion.title),
highlight: searchTerm
})
});
};
exports.LinkControlSearchItem = LinkControlSearchItem;
function getVisualTypeName(suggestion) {
if (suggestion.isFrontPage) {
return 'front page';
}
if (suggestion.isBlogHome) {
return 'blog home';
}
// Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label.
return suggestion.type === 'post_tag' ? 'tag' : suggestion.type;
}
var _default = exports.default = LinkControlSearchItem;
const __experimentalLinkControlSearchItem = props => {
(0, _deprecated.default)('wp.blockEditor.__experimentalLinkControlSearchItem', {
since: '6.8'
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(LinkControlSearchItem, {
...props
});
};
exports.__experimentalLinkControlSearchItem = __experimentalLinkControlSearchItem;
//# sourceMappingURL=search-item.js.map