You can achieve it by getting the selected word from text and replacing it with HTML tag to highlight it.
Here is the working example -
export class AppComponent {
text = "Start editing to see some magic happen";
selectedWord: string;
highlight(event) {
this.selectedWord = window.getSelection().toString();
this.text = this.text.replace(
this.selectedWord,
"<mark>" + this.selectedWord + "</mark>"
);
}
}
In the html file, you need to use innerHTML for text and call highlight() method on doubleclick event like this -
<p [innerHTML]="text" (dblclick)="highlight($event)"></p>
Here is also a stackbiltz working example.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…