Skip to content

Commit

Permalink
fix: get pos and insert node
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Feb 3, 2024
1 parent e7dfbd0 commit bcb3e5b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
25 changes: 21 additions & 4 deletions src/components/modules/dashboard/writing/Writing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,18 @@ const MenuBar = () => {
const state = view.state

const currentCursorPosition = state.selection.from

const schema = ctx.get(schemaCtx)
const nextNode = schema.node(excalidrawSchema.type(ctx), {})

view.dispatch(state.tr.insert(currentCursorPosition, nextNode))
const tr = state.tr
tr.replaceSelectionWith(nextNode)
// 判断是否插入的 node 位于文档的末尾
const isNewNodeIsEof =
currentCursorPosition === state.doc.content.size ||
currentCursorPosition + 1 === state.doc.content.size
if (isNewNodeIsEof) tr.insert(tr.doc.content.size, schema.text('\n'))

view.dispatch(tr)
},
},
{
Expand All @@ -127,11 +135,20 @@ const MenuBar = () => {
const state = view.state

const currentCursorPosition = state.selection.from
const nextNode = ctx.get(schemaCtx).node('diagram', {
const schema = ctx.get(schemaCtx)
const nextNode = schema.node('diagram', {
value: '<auto_open>',
})

view.dispatch(state.tr.insert(currentCursorPosition, nextNode))
const tr = state.tr
tr.replaceSelectionWith(nextNode)
// 判断是否插入的 node 位于文档的末尾
const isNewNodeIsEof =
currentCursorPosition === state.doc.content.size ||
currentCursorPosition + 1 === state.doc.content.size
if (isNewNodeIsEof) tr.insert(tr.doc.content.size, schema.text('\n'))

view.dispatch(tr)
},
},
]
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/editor/Milkdown/plugins/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const NormalCodeBlock: FC<{

const pos = nodeCtx.getPos()
const tr = view.state.tr
if (!pos) return
if (typeof pos === 'undefined') return
if (!code) {
// remove node

Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/editor/Milkdown/plugins/Mermaid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const MermaidRender = () => {
const Content: FC<ModalContentPropsInternal> = ({ dismiss }) => {
const deleteNode = () => {
const pos = getPos()
if (!pos) return
if (typeof pos === 'undefined') return
view.dispatch(view.state.tr.delete(pos, pos + node.nodeSize))
dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const SharedModalAction: Component<{

const deleteNode = () => {
const pos = getPos()
if (!pos) return

if (typeof pos === 'undefined') return
view.dispatch(view.state.tr.delete(pos, pos + node.nodeSize))
dismiss()
}
Expand All @@ -45,7 +46,7 @@ export const SharedModalAction: Component<{
}
// set first firstChild text
const pos = getPos()
if (!pos) return
if (typeof pos === 'undefined') return
const tr = view.state.tr

const nextValue = getValue()!
Expand Down
2 changes: 1 addition & 1 deletion src/providers/root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function DashboardAppProviders({ children }: PropsWithChildren) {
<ModalStackProvider key="modalStackProvider" />
<EventProvider key="viewportProvider" />

<DebugProvider key="debugProvider" />
{/* <DebugProvider key="debugProvider" /> */}
</ProviderComposer>
)
}

0 comments on commit bcb3e5b

Please sign in to comment.