stash
This commit is contained in:
parent
bf65ce3469
commit
7eb6073c3c
1 changed files with 27 additions and 3 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue