I have a contenteditable div, when I am trying to paste some text in that div it is always getting pasted in the end. I am using view child to access the reference of the contenteditable div and using inner text to get the value.
Question How can i be able to paste the copied text at the current cursor position.
Please find my code below.
Component.html
<div class="text-block" contenteditable #textBlockElement (input)="textOnChange($event.target.innerText)" (paste)="pasteOnContenteditable($event)"></div>
Component.ts
@ViewChild('textBlockElement ', { static: false }) textBlockElement : ElementRef;
pasteOnContenteditable(e : any) {
e.preventDefault();
let clipboardData = e.clipboardData;
let pastedText = clipboardData.getData('text');
let textEl : HTMLElement = this.textBlockElement.nativeElement;
textEl.innerText = textEl.innerText + pastedText;
}
textOnChange(textVal : string){
console.log(textVal);
}