@@ -112,10 +112,18 @@ const MenuBar = () => {
112
112
const state = view . state
113
113
114
114
const currentCursorPosition = state . selection . from
115
+
115
116
const schema = ctx . get ( schemaCtx )
116
117
const nextNode = schema . node ( excalidrawSchema . type ( ctx ) , { } )
117
-
118
- view . dispatch ( state . tr . insert ( currentCursorPosition , nextNode ) )
118
+ const tr = state . tr
119
+ tr . replaceSelectionWith ( nextNode )
120
+ // 判断是否插入的 node 位于文档的末尾
121
+ const isNewNodeIsEof =
122
+ currentCursorPosition === state . doc . content . size ||
123
+ currentCursorPosition + 1 === state . doc . content . size
124
+ if ( isNewNodeIsEof ) tr . insert ( tr . doc . content . size , schema . text ( '\n' ) )
125
+
126
+ view . dispatch ( tr )
119
127
} ,
120
128
} ,
121
129
{
@@ -127,11 +135,20 @@ const MenuBar = () => {
127
135
const state = view . state
128
136
129
137
const currentCursorPosition = state . selection . from
130
- const nextNode = ctx . get ( schemaCtx ) . node ( 'diagram' , {
138
+ const schema = ctx . get ( schemaCtx )
139
+ const nextNode = schema . node ( 'diagram' , {
131
140
value : '<auto_open>' ,
132
141
} )
133
142
134
- view . dispatch ( state . tr . insert ( currentCursorPosition , nextNode ) )
143
+ const tr = state . tr
144
+ tr . replaceSelectionWith ( nextNode )
145
+ // 判断是否插入的 node 位于文档的末尾
146
+ const isNewNodeIsEof =
147
+ currentCursorPosition === state . doc . content . size ||
148
+ currentCursorPosition + 1 === state . doc . content . size
149
+ if ( isNewNodeIsEof ) tr . insert ( tr . doc . content . size , schema . text ( '\n' ) )
150
+
151
+ view . dispatch ( tr )
135
152
} ,
136
153
} ,
137
154
]
0 commit comments