UNPKG

@wordpress/block-editor

Version:
92 lines (81 loc) 2.57 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { createElement } from "@wordpress/element"; /** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { safeDecodeURI, filterURLForDisplay } from '@wordpress/url'; import { __ } from '@wordpress/i18n'; import { Button, TextHighlight } from '@wordpress/components'; import { Icon, globe, page, tag, postList, category, file } from '@wordpress/icons'; import { __unstableStripHTML as stripHTML } from '@wordpress/dom'; const ICONS_MAP = { post: postList, page, post_tag: tag, category, attachment: file }; function SearchItemIcon({ isURL, suggestion }) { let icon = null; if (isURL) { icon = globe; } else if (suggestion.type in ICONS_MAP) { icon = ICONS_MAP[suggestion.type]; } if (icon) { return createElement(Icon, { className: "block-editor-link-control__search-item-icon", icon: icon }); } return null; } export const LinkControlSearchItem = ({ itemProps, suggestion, isSelected = false, onClick, isURL = false, searchTerm = '', shouldShowType = false }) => { return createElement(Button, _extends({}, itemProps, { onClick: onClick, className: classnames('block-editor-link-control__search-item', { 'is-selected': isSelected, 'is-url': isURL, 'is-entity': !isURL }) }), createElement(SearchItemIcon, { suggestion: suggestion, isURL: isURL }), createElement("span", { className: "block-editor-link-control__search-item-header" }, createElement("span", { className: "block-editor-link-control__search-item-title" }, createElement(TextHighlight // The component expects a plain text string. , { text: stripHTML(suggestion.title), highlight: searchTerm })), createElement("span", { "aria-hidden": !isURL, className: "block-editor-link-control__search-item-info" }, !isURL && (filterURLForDisplay(safeDecodeURI(suggestion.url)) || ''), isURL && __('Press ENTER to add this link'))), shouldShowType && suggestion.type && createElement("span", { className: "block-editor-link-control__search-item-type" }, getVisualTypeName(suggestion))); }; function getVisualTypeName(suggestion) { if (suggestion.isFrontPage) { return 'front page'; } // Rename 'post_tag' to 'tag'. Ideally, the API would return the localised CPT or taxonomy label. return suggestion.type === 'post_tag' ? 'tag' : suggestion.type; } export default LinkControlSearchItem; //# sourceMappingURL=search-item.js.map