From b033a6d877b0f5132a1f7880dbd38944f8fecbbe Mon Sep 17 00:00:00 2001 From: jedi Date: Thu, 6 Jun 2024 21:06:06 +0200 Subject: [PATCH] stash --- web/src/components/inputs/FormatedText.vue | 30 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/web/src/components/inputs/FormatedText.vue b/web/src/components/inputs/FormatedText.vue index 02c131a..60ff865 100644 --- a/web/src/components/inputs/FormatedText.vue +++ b/web/src/components/inputs/FormatedText.vue @@ -60,7 +60,31 @@ export default { }; walk(container); return position + offset; - } + }, + findNode(container, offset) { + let position = 0; + let found = false; + let node = null; + const walk = (elem) => { + if (position + elem.length >= offset) { + found = true; + node = elem; + return; + } + if (elem.nodeType === 3) { + position += elem.length; + } else { + for (let i = 0; i < elem.childNodes.length; i++) { + walk(elem.childNodes[i]); + if (found) { + return; + } + } + } + }; + walk(container); + return [node, offset - position] + }, }, watch: { value() { @@ -68,8 +92,8 @@ export default { const div = this.$refs.text; const range = document.createRange(); console.log(this.selection); - range.setStart(div, Math.min(this.selection.anchorOffset, this.value.length)); - range.setEnd(div, Math.min(this.selection.focusOffset, this.value.length)); + range.setStart(...this.findNode(div, this.selection.start)); + range.setEnd(...this.findNode(div, this.selection.end)); this.selection.removeAllRanges(); this.selection.addRange(range); }