Skip to content

Commit bcb3e5b

Browse files
committed
fix: get pos and insert node
Signed-off-by: Innei <[email protected]>
1 parent e7dfbd0 commit bcb3e5b

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

src/components/modules/dashboard/writing/Writing.tsx

+21-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,18 @@ const MenuBar = () => {
112112
const state = view.state
113113

114114
const currentCursorPosition = state.selection.from
115+
115116
const schema = ctx.get(schemaCtx)
116117
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)
119127
},
120128
},
121129
{
@@ -127,11 +135,20 @@ const MenuBar = () => {
127135
const state = view.state
128136

129137
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', {
131140
value: '<auto_open>',
132141
})
133142

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)
135152
},
136153
},
137154
]

src/components/ui/editor/Milkdown/plugins/CodeBlock.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const NormalCodeBlock: FC<{
5151

5252
const pos = nodeCtx.getPos()
5353
const tr = view.state.tr
54-
if (!pos) return
54+
if (typeof pos === 'undefined') return
5555
if (!code) {
5656
// remove node
5757

src/components/ui/editor/Milkdown/plugins/Mermaid.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const MermaidRender = () => {
2828
const Content: FC<ModalContentPropsInternal> = ({ dismiss }) => {
2929
const deleteNode = () => {
3030
const pos = getPos()
31-
if (!pos) return
31+
if (typeof pos === 'undefined') return
3232
view.dispatch(view.state.tr.delete(pos, pos + node.nodeSize))
3333
dismiss()
3434
}

src/components/ui/editor/Milkdown/plugins/__internal/SharedModalAction.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export const SharedModalAction: Component<{
2020

2121
const deleteNode = () => {
2222
const pos = getPos()
23-
if (!pos) return
23+
24+
if (typeof pos === 'undefined') return
2425
view.dispatch(view.state.tr.delete(pos, pos + node.nodeSize))
2526
dismiss()
2627
}
@@ -45,7 +46,7 @@ export const SharedModalAction: Component<{
4546
}
4647
// set first firstChild text
4748
const pos = getPos()
48-
if (!pos) return
49+
if (typeof pos === 'undefined') return
4950
const tr = view.state.tr
5051

5152
const nextValue = getValue()!

src/providers/root/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function DashboardAppProviders({ children }: PropsWithChildren) {
6767
<ModalStackProvider key="modalStackProvider" />
6868
<EventProvider key="viewportProvider" />
6969

70-
<DebugProvider key="debugProvider" />
70+
{/* <DebugProvider key="debugProvider" /> */}
7171
</ProviderComposer>
7272
)
7373
}

0 commit comments

Comments
 (0)