@universal-material/web
Version:
Material web components
69 lines • 2.1 kB
JavaScript
import { __decorate } from "tslib";
import { html, LitElement } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { normalizeText } from '../shared/normalize-text.js';
import { styles } from './highlight.styles.js';
const regExpEscape = (text) => text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
let UmHighlight = class UmHighlight extends LitElement {
static { this.styles = styles; }
#result;
#term;
/**
* The result text to display. If the term is found inside this text, it's highlighted
*/
get result() {
return this.#result;
}
set result(value) {
this.#result = value;
this.setParts();
}
/**
* The searched term
*/
get term() {
return this.#term;
}
set term(value) {
this.#term = value;
this.setParts();
}
render() {
return this.parts
? this.parts.map((part, index) => index % 2
? html `<strong>${part}</strong>`
: html `<span>${part}</span>`)
: html `<span></span>`;
}
setParts() {
const resultStr = this.result || '';
const resultNormalized = normalizeText(resultStr).toLowerCase();
const termLC = normalizeText(this.term || '').toLowerCase();
let currentIdx = 0;
if (termLC.length <= 0) {
this.parts = [resultStr];
return;
}
this.parts = resultNormalized
.split(new RegExp(`(${regExpEscape(termLC)})`))
.map(part => {
const originalPart = resultStr.substring(currentIdx, currentIdx + part.length);
currentIdx += part.length;
return originalPart;
});
}
};
__decorate([
state()
], UmHighlight.prototype, "parts", void 0);
__decorate([
property()
], UmHighlight.prototype, "result", null);
__decorate([
property()
], UmHighlight.prototype, "term", null);
UmHighlight = __decorate([
customElement('u-highlight')
], UmHighlight);
export { UmHighlight };
//# sourceMappingURL=highlight.js.map