TextArea: Position cursor after user selects suggested correction

This commit is contained in:
Simon Rozman 2024-04-02 14:27:59 +02:00
parent e6324c55b3
commit 3e868b2517

View File

@ -576,8 +576,12 @@ class BesService {
this.abortController.abort() this.abortController.abort()
match.range.deleteContents() match.range.deleteContents()
match.range.insertNode(document.createTextNode(replacement)) match.range.insertNode(document.createTextNode(replacement))
if (this.textAreaService) if (this.textAreaService) {
this.textAreaService.handleReplacement(this.hostElement) let text = this.textAreaService.textAreaEl.value
this.textAreaService.textAreaEl.value =
text.substring(0, match.match.offset) + replacement + text.substring(match.match.offset + match.match.length)
this.textAreaService.textAreaEl.selectionStart = this.textAreaService.textAreaEl.selectionEnd = match.match.offset
}
this.clearMistakeMarkup(el) this.clearMistakeMarkup(el)
// In my opinion, this approach provides the most straightforward solution for repositioning mistakes after a change. // In my opinion, this approach provides the most straightforward solution for repositioning mistakes after a change.
// It maintains reasonable performance as it only checks the block element that has been modified, // It maintains reasonable performance as it only checks the block element that has been modified,
@ -785,16 +789,6 @@ class BesTAService {
this.cloneDiv.dispatchEvent(clickEvent) this.cloneDiv.dispatchEvent(clickEvent)
} }
/**
* This function handles the replacement of the text in the textarea element
*
* @param {HTMLElement} el Element whose outerText will be used as a replacement
*/
handleReplacement(el) {
// TODO: think of a way to reposition the cursor after the replacement
this.textAreaEl.value = el.outerText
}
/** /**
* Registers grammar checking service * Registers grammar checking service
* *