Browse Source

fix: 修正在文档中部插入文本光标位置错误,但是目前将文本加粗或者斜体时会出错

ChenYangfan 6 years ago
parent
commit
23d7c59f33
1 changed files with 4 additions and 13 deletions
  1. 4 13
      src/components/editor/EditableItem.vue

+ 4 - 13
src/components/editor/EditableItem.vue

@@ -30,9 +30,7 @@ export default {
     ...mapState({})
   },
   mounted() {},
-  updated() {
-    console.log('updated', this.range)
-  },
+  updated() {},
   methods: {
     handleCompositionStart() {
       this.isCompositionStarted = true
@@ -42,11 +40,6 @@ export default {
       this.handleInputValue(e)
     },
     handleKeyDown(e) {
-      if (!this.range) {
-        this.range = window.getSelection().getRangeAt(0)
-      }
-      console.log('keydown', this.range)
-
       switch (e.key) {
         case 'Enter':
           e.preventDefault()
@@ -84,13 +77,11 @@ export default {
         itemId: this.item.id,
         diff
       })
+      this.range = window.getSelection().getRangeAt(0)
+      const offset = this.range.startOffset
 
       this.$nextTick(() => {
-        console.log(this.range.startOffset)
-        this.range.setStart(
-          this.range.startContainer,
-          this.range.startOffset + 1
-        )
+        this.range.setStart(this.range.startContainer.firstChild, offset)
 
         this.range.collapse()
         window.getSelection().removeAllRanges()