@wordpress/components
Version:
UI components for WordPress.
125 lines (123 loc) • 4.26 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/components/src/text/utils.ts
var utils_exports = {};
__export(utils_exports, {
createHighlighterText: () => createHighlighterText
});
module.exports = __toCommonJS(utils_exports);
var import_memize = __toESM(require("memize"));
var import_highlight_words_core = require("highlight-words-core");
var import_element = require("@wordpress/element");
var lowercaseProps = (object) => {
const mapped = {};
for (const key in object) {
mapped[key.toLowerCase()] = object[key];
}
return mapped;
};
var memoizedLowercaseProps = (0, import_memize.default)(lowercaseProps);
function createHighlighterText({
activeClassName = "",
activeIndex = -1,
activeStyle,
autoEscape,
caseSensitive = false,
children,
findChunks,
highlightClassName = "",
highlightStyle = {},
highlightTag = "mark",
sanitize,
searchWords = [],
unhighlightClassName = "",
unhighlightStyle
}) {
if (!children) {
return null;
}
if (typeof children !== "string") {
return children;
}
const textToHighlight = children;
const chunks = (0, import_highlight_words_core.findAll)({
autoEscape,
caseSensitive,
findChunks,
sanitize,
searchWords,
textToHighlight
});
const HighlightTag = highlightTag;
let highlightIndex = -1;
let highlightClassNames = "";
let highlightStyles;
const textContent = chunks.map((chunk, index) => {
const text = textToHighlight.substr(chunk.start, chunk.end - chunk.start);
if (chunk.highlight) {
highlightIndex++;
let highlightClass;
if (typeof highlightClassName === "object") {
if (!caseSensitive) {
highlightClassName = memoizedLowercaseProps(highlightClassName);
highlightClass = highlightClassName[text.toLowerCase()];
} else {
highlightClass = highlightClassName[text];
}
} else {
highlightClass = highlightClassName;
}
const isActive = highlightIndex === +activeIndex;
highlightClassNames = `${highlightClass} ${isActive ? activeClassName : ""}`;
highlightStyles = isActive === true && activeStyle !== null ? Object.assign({}, highlightStyle, activeStyle) : highlightStyle;
const props = {
children: text,
className: highlightClassNames,
key: index,
style: highlightStyles
};
if (typeof HighlightTag !== "string") {
props.highlightIndex = highlightIndex;
}
return (0, import_element.createElement)(HighlightTag, props);
}
return (0, import_element.createElement)("span", {
children: text,
className: unhighlightClassName,
key: index,
style: unhighlightStyle
});
});
return textContent;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createHighlighterText
});
//# sourceMappingURL=utils.js.map