@wordpress/block-editor
Version:
153 lines (152 loc) • 3.55 kB
JavaScript
// packages/block-editor/src/components/link-control/search-item.js
import { __ } from "@wordpress/i18n";
import { MenuItem, TextHighlight } from "@wordpress/components";
import {
Icon,
globe,
page,
tag,
postList,
category,
file,
home,
verse
} from "@wordpress/icons";
import { __unstableStripHTML as stripHTML } from "@wordpress/dom";
import { safeDecodeURI, filterURLForDisplay, getPath } from "@wordpress/url";
import { pipe } from "@wordpress/compose";
import deprecated from "@wordpress/deprecated";
import { jsx } from "react/jsx-runtime";
var TYPES = {
post: {
icon: postList,
label: __("Post")
},
page: {
icon: page,
label: __("Page")
},
post_tag: {
icon: tag,
label: __("Tag")
},
category: {
icon: category,
label: __("Category")
},
attachment: {
icon: file,
label: __("Attachment")
}
};
function SearchItemIcon({ isURL, suggestion }) {
let icon = null;
if (isURL) {
icon = globe;
} else if (suggestion.type in TYPES) {
icon = TYPES[suggestion.type].icon;
if (suggestion.type === "page") {
if (suggestion.isFrontPage) {
icon = home;
}
if (suggestion.isBlogHome) {
icon = verse;
}
}
}
if (icon) {
return /* @__PURE__ */ jsx(
Icon,
{
className: "block-editor-link-control__search-item-icon",
icon
}
);
}
return null;
}
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(/\/$/, "");
}
var partialRight = (fn, ...partialArgs) => (...args) => fn(...args, ...partialArgs);
var defaultTo = (d) => (v) => {
return v === null || v === void 0 || v !== v ? d : v;
};
function getURLForDisplay(url) {
if (!url) {
return url;
}
return pipe(
safeDecodeURI,
getPath,
defaultTo(""),
partialRight(filterURLForDisplay, 24),
removeTrailingSlash,
addLeadingSlash
)(url);
}
var LinkControlSearchItem = ({
itemProps,
suggestion,
searchTerm,
onClick,
isURL = false,
shouldShowType = false
}) => {
const info = isURL ? __("Press ENTER to add this link") : getURLForDisplay(suggestion.url);
return /* @__PURE__ */ jsx(
MenuItem,
{
...itemProps,
info,
iconPosition: "left",
icon: /* @__PURE__ */ jsx(SearchItemIcon, { suggestion, isURL }),
onClick,
shortcut: shouldShowType && getVisualTypeName(suggestion),
className: "block-editor-link-control__search-item",
children: /* @__PURE__ */ jsx(
TextHighlight,
{
text: stripHTML(suggestion.title),
highlight: searchTerm
}
)
}
);
};
function getVisualTypeName(suggestion) {
if (suggestion.isFrontPage) {
return __("Front page");
}
if (suggestion.isBlogHome) {
return __("Blog home");
}
if (suggestion.type in TYPES) {
return TYPES[suggestion.type].label;
}
return suggestion.type;
}
var search_item_default = LinkControlSearchItem;
var __experimentalLinkControlSearchItem = (props) => {
deprecated("wp.blockEditor.__experimentalLinkControlSearchItem", {
since: "6.8"
});
return /* @__PURE__ */ jsx(LinkControlSearchItem, { ...props });
};
export {
LinkControlSearchItem,
__experimentalLinkControlSearchItem,
search_item_default as default
};
//# sourceMappingURL=search-item.js.map