@nextcloud/vue
Version:
Nextcloud vue components
44 lines (43 loc) • 2.15 kB
JavaScript
import { n as i } from "./_plugin-vue2_normalizer-5b4c43a4.mjs";
const l = (e, t) => {
const h = [];
let s = 0, n = e.toLowerCase().indexOf(t.toLowerCase(), s), r = 0;
for (; n > -1 && r < e.length; )
s = n + t.length, h.push({ start: n, end: s }), n = e.toLowerCase().indexOf(t.toLowerCase(), s), r++;
return h;
}, a = { name: "NcHighlight", props: { text: { type: String, default: "" }, search: { type: String, default: "" }, highlight: { type: Array, default: () => [] } }, computed: { ranges() {
let e = [];
return !this.search && this.highlight.length === 0 || (this.highlight.length > 0 ? e = this.highlight : e = l(this.text, this.search), e.forEach((t, h) => {
t.end < t.start && (e[h] = { start: t.end, end: t.start });
}), e = e.reduce((t, h) => (h.start < this.text.length && h.end > 0 && t.push({ start: h.start < 0 ? 0 : h.start, end: h.end > this.text.length ? this.text.length : h.end }), t), []), e.sort((t, h) => t.start - h.start), e = e.reduce((t, h) => {
if (!t.length)
t.push(h);
else {
const s = t.length - 1;
t[s].end >= h.start ? t[s] = { start: t[s].start, end: Math.max(t[s].end, h.end) } : t.push(h);
}
return t;
}, [])), e;
}, chunks() {
if (this.ranges.length === 0)
return [{ start: 0, end: this.text.length, highlight: !1, text: this.text }];
const e = [];
let t = 0, h = 0;
for (; t < this.text.length; ) {
const s = this.ranges[h];
if (s.start === t) {
e.push({ ...s, highlight: !0, text: this.text.slice(s.start, s.end) }), h++, t = s.end, h >= this.ranges.length && t < this.text.length && (e.push({ start: t, end: this.text.length, highlight: !1, text: this.text.slice(t) }), t = this.text.length);
continue;
}
e.push({ start: t, end: s.start, highlight: !1, text: this.text.slice(t, s.start) }), t = s.start;
}
return e;
} }, render(e) {
return this.ranges.length ? e("span", {}, this.chunks.map((t) => t.highlight ? e("strong", {}, t.text) : t.text)) : e("span", {}, this.text);
} }, g = null, u = null;
var o = i(a, g, u, !1, null, null, null, null);
const d = o.exports;
export {
l as F,
d as N
};