@wordpress/block-editor
Version:
167 lines (165 loc) • 6.1 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/inserter/search-items.js
var search_items_exports = {};
__export(search_items_exports, {
extractWords: () => extractWords,
getItemSearchRank: () => getItemSearchRank,
getNormalizedSearchTerms: () => getNormalizedSearchTerms,
normalizeString: () => normalizeString,
searchBlockItems: () => searchBlockItems,
searchItems: () => searchItems
});
module.exports = __toCommonJS(search_items_exports);
var import_remove_accents = __toESM(require("remove-accents"));
var import_change_case = require("change-case");
var defaultGetName = (item) => item.name || "";
var defaultGetTitle = (item) => item.title;
var defaultGetDescription = (item) => item.description || "";
var defaultGetKeywords = (item) => item.keywords || [];
var defaultGetCategory = (item) => item.category;
var defaultGetCollection = () => null;
var splitRegexp = [
/([\p{Ll}\p{Lo}\p{N}])([\p{Lu}\p{Lt}])/gu,
// One lowercase or digit, followed by one uppercase.
/([\p{Lu}\p{Lt}])([\p{Lu}\p{Lt}][\p{Ll}\p{Lo}])/gu
// One uppercase followed by one uppercase and one lowercase.
];
var stripRegexp = new RegExp("(\\p{C}|\\p{P}|\\p{S})+", "giu");
var extractedWords = /* @__PURE__ */ new Map();
var normalizedStrings = /* @__PURE__ */ new Map();
function extractWords(input = "") {
if (extractedWords.has(input)) {
return extractedWords.get(input);
}
const result = (0, import_change_case.noCase)(input, {
splitRegexp,
stripRegexp
}).split(" ").filter(Boolean);
extractedWords.set(input, result);
return result;
}
function normalizeString(input = "") {
if (normalizedStrings.has(input)) {
return normalizedStrings.get(input);
}
let result = (0, import_remove_accents.default)(input);
result = result.replace(/^\//, "");
result = result.toLowerCase();
normalizedStrings.set(input, result);
return result;
}
var getNormalizedSearchTerms = (input = "") => {
return extractWords(normalizeString(input));
};
var removeMatchingTerms = (unmatchedTerms, unprocessedTerms) => {
return unmatchedTerms.filter(
(term) => !getNormalizedSearchTerms(unprocessedTerms).some(
(unprocessedTerm) => unprocessedTerm.includes(term)
)
);
};
var searchBlockItems = (items, categories, collections, searchInput) => {
const normalizedSearchTerms = getNormalizedSearchTerms(searchInput);
if (normalizedSearchTerms.length === 0) {
return items;
}
const config = {
getCategory: (item) => categories.find(({ slug }) => slug === item.category)?.title,
getCollection: (item) => collections[item.name.split("/")[0]]?.title
};
return searchItems(items, searchInput, config);
};
var searchItems = (items = [], searchInput = "", config = {}) => {
const normalizedSearchTerms = getNormalizedSearchTerms(searchInput);
if (normalizedSearchTerms.length === 0) {
return items;
}
const rankedItems = items.map((item) => {
return [item, getItemSearchRank(item, searchInput, config)];
}).filter(([, rank]) => rank > 0);
rankedItems.sort(([, rank1], [, rank2]) => rank2 - rank1);
return rankedItems.map(([item]) => item);
};
function getItemSearchRank(item, searchTerm, config = {}) {
const {
getName = defaultGetName,
getTitle = defaultGetTitle,
getDescription = defaultGetDescription,
getKeywords = defaultGetKeywords,
getCategory = defaultGetCategory,
getCollection = defaultGetCollection
} = config;
const name = getName(item);
const title = getTitle(item);
const description = getDescription(item);
const keywords = getKeywords(item);
const category = getCategory(item);
const collection = getCollection(item);
const normalizedSearchInput = normalizeString(searchTerm);
const normalizedTitle = normalizeString(title);
let rank = 0;
if (normalizedSearchInput === normalizedTitle) {
rank += 30;
} else if (normalizedTitle.startsWith(normalizedSearchInput)) {
rank += 20;
} else {
const terms = [
name,
title,
description,
...keywords,
category,
collection
].join(" ");
const normalizedSearchTerms = extractWords(normalizedSearchInput);
const unmatchedTerms = removeMatchingTerms(
normalizedSearchTerms,
terms
);
if (unmatchedTerms.length === 0) {
rank += 10;
}
}
if (rank !== 0 && name.startsWith("core/")) {
const isCoreBlockVariation = name !== item.id;
rank += isCoreBlockVariation ? 1 : 2;
}
return rank;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
extractWords,
getItemSearchRank,
getNormalizedSearchTerms,
normalizeString,
searchBlockItems,
searchItems
});
//# sourceMappingURL=search-items.js.map