@wordpress/components
Version:
UI components for WordPress.
89 lines (88 loc) • 2.46 kB
JavaScript
import memoize from "memize";
import { findAll } from "highlight-words-core";
import { createElement } from "@wordpress/element";
const lowercaseProps = (object) => {
const mapped = {};
for (const key in object) {
mapped[key.toLowerCase()] = object[key];
}
return mapped;
};
const memoizedLowercaseProps = memoize(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 = 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 createElement(HighlightTag, props);
}
return createElement("span", {
children: text,
className: unhighlightClassName,
key: index,
style: unhighlightStyle
});
});
return textContent;
}
export {
createHighlighterText
};
//# sourceMappingURL=utils.js.map