This commit is contained in:
j3d1 2024-05-22 23:49:13 +02:00
parent 73de3982b1
commit 75c13e2d8b

View file

@ -1,5 +1,5 @@
<template> <template>
<div contenteditable @input="onchange"> <div contenteditable @input="onchange" ref="text">
<span v-html="rawhtml(value)"></span> <span v-html="rawhtml(value)"></span>
</div> </div>
</template> </template>
@ -17,6 +17,11 @@ export default {
default: null default: null
} }
}, },
data() {
return {
selection: null,
};
},
emits: ['input'], emits: ['input'],
methods: { methods: {
rawhtml(value) { rawhtml(value) {
@ -27,8 +32,21 @@ export default {
} }
}, },
onchange(event) { onchange(event) {
this.selection = window.getSelection();
this.$emit('input', event.target.innerText); this.$emit('input', event.target.innerText);
} }
},
watch: {
value() {
if (this.selection) {
const div = this.$refs.text;
const range = document.createRange();
range.setStart(div.childNodes[0], this.selection.anchorOffset);
range.setEnd(div.childNodes[0], this.selection.focusOffset);
this.selection.removeAllRanges();
this.selection.addRange(range);
}
}
} }
}; };
</script> </script>