|
|
|
@ -13,13 +13,14 @@ export class OpSearchHighlightDirective implements AfterViewChecked { |
|
|
|
|
|
|
|
|
|
constructor(readonly elementRef:ElementRef) { } |
|
|
|
|
|
|
|
|
|
ngAfterViewChecked() { |
|
|
|
|
ngAfterViewChecked():void { |
|
|
|
|
if (!this.query) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const el = this.elementRef.nativeElement as HTMLElement; |
|
|
|
|
|
|
|
|
|
const textNode = Array.from(this.elementRef.nativeElement.childNodes).find((n:Node) => n.nodeType === n.TEXT_NODE) as Node|undefined; |
|
|
|
|
const textNode = Array.from(el.childNodes).find((n:Node) => n.nodeType === n.TEXT_NODE) as Node; |
|
|
|
|
const content = textNode?.textContent || ''; |
|
|
|
|
if (!content) { |
|
|
|
|
return; |
|
|
|
@ -32,11 +33,11 @@ export class OpSearchHighlightDirective implements AfterViewChecked { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const start = content.slice(0, startIndex); |
|
|
|
|
const result = content.slice(startIndex, startIndex + query.length);
|
|
|
|
|
const result = content.slice(startIndex, startIndex + query.length); |
|
|
|
|
const end = content.slice(startIndex + query.length); |
|
|
|
|
|
|
|
|
|
const newNode = document.createElement('span'); |
|
|
|
|
newNode.innerHTML = `${start}<span class="op-search-highlight">${result}</span>${end}`; |
|
|
|
|
this.elementRef.nativeElement.replaceChild(newNode, textNode); |
|
|
|
|
el.replaceChild(newNode, textNode); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|