@@ -24,6 +24,17 @@ import { EDITOR_TO_SELECTION, NODE_TO_KEY } from '../../utils/weak-maps'
24
24
import { DomEditor } from '../dom-editor'
25
25
import { ElementWithId } from '../interface'
26
26
27
+ const getMatches = ( e : IDomEditor , path : Path ) => {
28
+ const matches : [ Path , Key ] [ ] = [ ]
29
+
30
+ for ( const [ n , p ] of Editor . levels ( e , { at : path } ) ) {
31
+ const key = DomEditor . findKey ( e , n )
32
+
33
+ matches . push ( [ p , key ] )
34
+ }
35
+ return matches
36
+ }
37
+
27
38
/**
28
39
* 把 elem 插入到编辑器
29
40
* @param editor editor
@@ -56,62 +67,56 @@ export const withContent = <T extends Editor>(editor: T) => {
56
67
insertText ( text )
57
68
}
58
69
59
- // 重写 apply 方法
60
- // apply 方法非常重要,它最终执行 operation https://docs.slatejs.org/concepts/05-operations
61
- // operation 的接口定义参考 slate src/interfaces/operation.ts
70
+ // 重写 apply 方法,参考 slate 最新版本的实现
62
71
e . apply = ( op : Operation ) => {
63
72
const matches : [ Path , Key ] [ ] = [ ]
64
73
65
74
switch ( op . type ) {
66
75
case 'insert_text' :
67
76
case 'remove_text' :
68
- case 'set_node' : {
69
- for ( const [ node , path ] of Editor . levels ( e , { at : op . path } ) ) {
70
- // 在当前节点寻找
71
- const key = DomEditor . findKey ( e , node )
72
-
73
- matches . push ( [ path , key ] )
74
- }
77
+ case 'set_node' :
78
+ case 'split_node' : {
79
+ matches . push ( ...getMatches ( e , op . path ) )
75
80
break
76
81
}
77
82
78
83
case 'insert_node' :
79
- case 'remove_node' :
80
- case 'merge_node' :
81
- case 'split_node' : {
82
- for ( const [ node , path ] of Editor . levels ( e , { at : Path . parent ( op . path ) } ) ) {
83
- // 在父节点寻找
84
- const key = DomEditor . findKey ( e , node )
84
+ case 'remove_node' : {
85
+ matches . push ( ...getMatches ( e , Path . parent ( op . path ) ) )
86
+ break
87
+ }
85
88
86
- matches . push ( [ path , key ] )
87
- }
89
+ case 'merge_node' : {
90
+ const prevPath = Path . previous ( op . path )
91
+
92
+ matches . push ( ...getMatches ( e , prevPath ) )
88
93
break
89
94
}
90
95
91
96
case 'move_node' : {
92
- for ( const [ node , path ] of Editor . levels ( e , {
93
- at : Path . common ( Path . parent ( op . path ) , Path . parent ( op . newPath ) ) ,
94
- } ) ) {
95
- const key = DomEditor . findKey ( e , node )
97
+ const commonPath = Path . common (
98
+ Path . parent ( op . path ) ,
99
+ Path . parent ( op . newPath ) ,
100
+ )
96
101
97
- matches . push ( [ path , key ] )
98
- }
102
+ matches . push ( ...getMatches ( e , commonPath ) )
99
103
break
100
104
}
101
105
default :
102
106
}
103
107
104
- // 执行原本的 apply - 重要!!!
108
+ // 执行原本的 apply
105
109
apply ( op )
106
110
107
- // 绑定 node 和 key
111
+ // 更新 node 和 key 的映射
108
112
for ( const [ path , key ] of matches ) {
109
113
const [ node ] = Editor . node ( e , path )
110
114
111
115
NODE_TO_KEY . set ( node , key )
112
116
}
113
117
}
114
118
119
+ // 重写 deleteBackward,参考 slate 最新版本的实现
115
120
e . deleteBackward = unit => {
116
121
if ( unit !== 'line' ) {
117
122
return deleteBackward ( unit )
@@ -125,7 +130,11 @@ export const withContent = <T extends Editor>(editor: T) => {
125
130
126
131
if ( parentBlockEntry ) {
127
132
const [ , parentBlockPath ] = parentBlockEntry
128
- const parentElementRange = Editor . range ( editor , parentBlockPath , editor . selection . anchor )
133
+ const parentElementRange = Editor . range (
134
+ editor ,
135
+ parentBlockPath ,
136
+ editor . selection . anchor ,
137
+ )
129
138
130
139
const currentLineRange = findCurrentLineRange ( e , parentElementRange )
131
140
0 commit comments