Skip to content

Commit

Permalink
Fix clearing hoghligh functionality when a user turn the feature off (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
sha4be authored Jul 18, 2024
1 parent c7a85d8 commit de4083a
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ export class <%= classify(name) %>Directive implements OnChanges, OnInit {

ngOnChanges(changes: HighlightSimpleChanges) {
if ((changes.highlight && !changes.highlight.firstChange) || (changes.caseSensitive && !changes.caseSensitive.firstChange)) {
if (this._selected) {
this.transformText();
}
this.handleHighlightText();
}
}

ngOnInit(): void {
this.handleHighlightText();
}

private handleHighlightText(): void {
if (this._selected) {
this.transformText();
this.transformText();
} else if (this.isStringHighlighted) {
this.clearHighlights();
}
}

Expand Down Expand Up @@ -149,4 +153,13 @@ export class <%= classify(name) %>Directive implements OnChanges, OnInit {
result += this.highlightSource?.substring(lastIndex);
return result;
}

private get isStringHighlighted(): boolean {
return !!this.el.nativeElement.querySelector('mark');
}

private clearHighlights(): void {
const content = this.sanitizer.sanitize(SecurityContext.STYLE, this.el.nativeElement.innerText);
(this.el.nativeElement as HTMLElement).innerHTML = content;
}
}

0 comments on commit de4083a

Please sign in to comment.