Browse Source

improvement: 对光标位置状态追踪的尝试

目前的状态:不支持输入法,输入过快会鬼畜(都是rAF的问题),跟随输入移动光标只能在最后一个字符处生效(只用Selection不够,需要再看看Range)
ChenYangfan 6 years ago
parent
commit
fa6d93ab93

+ 18 - 1
src/components/editor/EditableItem.vue

@@ -4,7 +4,7 @@
     :is="item.type"
     :is="item.type"
     :id="item.id"
     :id="item.id"
     @input="handleInput"
     @input="handleInput"
-    @keydown.stop="handleKeyDown"
+    @keydown="handleKeyDown"
     contenteditable
     contenteditable
     v-html="item.content"
     v-html="item.content"
   >
   >
@@ -14,12 +14,18 @@
 <script>
 <script>
 import blockTypeRegs from '../../utils/block-type-regs'
 import blockTypeRegs from '../../utils/block-type-regs'
 import { MODIFY_ITEM, ADD_ITEM_AFTER } from '../../store/types/editor-types'
 import { MODIFY_ITEM, ADD_ITEM_AFTER } from '../../store/types/editor-types'
+import { mapState } from 'vuex'
 export default {
 export default {
   name: 'editable-item',
   name: 'editable-item',
   props: ['item', 'pageId'],
   props: ['item', 'pageId'],
   data: function() {
   data: function() {
     return {}
     return {}
   },
   },
+  computed: {
+    ...mapState({
+      range: state => state.editor.range
+    })
+  },
   mounted() {},
   mounted() {},
   updated() {},
   updated() {},
   methods: {
   methods: {
@@ -31,6 +37,11 @@ export default {
             pageId: this.pageId,
             pageId: this.pageId,
             itemId: this.item.id
             itemId: this.item.id
           })
           })
+          requestAnimationFrame(() => {
+            const focusNode = document.getElementById(this.item.id).nextSibling
+            focusNode.focus()
+          })
+
           break
           break
 
 
         default:
         default:
@@ -53,6 +64,12 @@ export default {
         itemId: this.item.id,
         itemId: this.item.id,
         diff
         diff
       })
       })
+      requestAnimationFrame(() => {
+        const selection = window.getSelection()
+        selection.collapseToEnd()
+        selection.extend(selection.anchorNode, e.target.textContent.length)
+        selection.collapseToEnd()
+      })
     }
     }
   }
   }
 }
 }

+ 2 - 4
src/components/editor/Editor.vue

@@ -14,12 +14,10 @@ export default {
   components: {
   components: {
     EditingPageVue
     EditingPageVue
   },
   },
-  created() {
-  },
+  created() {},
   computed: {
   computed: {
     ...mapState({
     ...mapState({
-      pages: state => state.editor.pages,
-      selection: state => state.editor.selection
+      pages: state => state.editor.pages
     })
     })
   }
   }
 }
 }

+ 5 - 14
src/store/modules/editor.js

@@ -1,7 +1,7 @@
 import {
 import {
   INIT_BY_STATE,
   INIT_BY_STATE,
   INIT_BY_DEFAULT,
   INIT_BY_DEFAULT,
-  SET_SELECTION,
+  SET_RANGE,
   ADD_PAGE_AFTER,
   ADD_PAGE_AFTER,
   DELETE_PAGE,
   DELETE_PAGE,
   ADD_ITEM_AFTER,
   ADD_ITEM_AFTER,
@@ -14,7 +14,7 @@ import uuid from 'uuid'
 export default {
 export default {
   state: {
   state: {
     pages: [],
     pages: [],
-    selection: {}
+    range: {}
   },
   },
 
 
   mutations: {
   mutations: {
@@ -35,9 +35,9 @@ export default {
         }]
         }]
       }]
       }]
     },
     },
-    [SET_SELECTION](state, payload) {
-      state.selection = {
-        ...payload.selection
+    [SET_RANGE](state, payload) {
+      state.range = {
+        ...payload.range
       }
       }
     },
     },
     [ADD_PAGE_AFTER](state, payload) {
     [ADD_PAGE_AFTER](state, payload) {
@@ -111,12 +111,3 @@ export default {
   actions: {},
   actions: {},
   getters: {}
   getters: {}
 }
 }
-
-// [SET_TAG_NAME](state, payload) {
-//   state.editableItems = [...state.editableItems.map(item =>
-//     item.id === payload.id ? {
-//       ...item,
-//       tagName: payload.tagName
-//     } : item
-//   )]
-// },

+ 1 - 1
src/store/types/editor-types.js

@@ -1,6 +1,6 @@
 export const INIT_BY_STATE = 'initByState'
 export const INIT_BY_STATE = 'initByState'
 export const INIT_BY_DEFAULT = 'initByDefault'
 export const INIT_BY_DEFAULT = 'initByDefault'
-export const SET_SELECTION = 'setSelection'
+export const SET_RANGE = 'setRange'
 export const ADD_PAGE_AFTER = 'addPageAfter'
 export const ADD_PAGE_AFTER = 'addPageAfter'
 export const DELETE_PAGE = 'initByState'
 export const DELETE_PAGE = 'initByState'
 // export const SPLITE_PAGE = 'splitePage'
 // export const SPLITE_PAGE = 'splitePage'