diff --git a/src/components/icons/mermaid.tsx b/src/components/icons/mermaid.tsx new file mode 100644 index 0000000000..9b1eeb18ac --- /dev/null +++ b/src/components/icons/mermaid.tsx @@ -0,0 +1,18 @@ +import type { SVGProps } from 'react' + +export function SimpleIconsMermaid(props: SVGProps) { + return ( + + + + ) +} diff --git a/src/components/modules/dashboard/writing/Writing.tsx b/src/components/modules/dashboard/writing/Writing.tsx index 8d4431e41f..cf5e73261f 100644 --- a/src/components/modules/dashboard/writing/Writing.tsx +++ b/src/components/modules/dashboard/writing/Writing.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef } from 'react' +import React, { isValidElement, useEffect, useRef } from 'react' import { produce } from 'immer' import { atom, useAtomValue, useSetAtom, useStore } from 'jotai' import type { FC } from 'react' @@ -15,6 +15,7 @@ import { } from '@milkdown/preset-commonmark' import { callCommand } from '@milkdown/utils' +import { SimpleIconsMermaid } from '~/components/icons/mermaid' import { useEventCallback } from '~/hooks/common/use-event-callback' import { clsxm } from '~/lib/helper' import { jotaiStore } from '~/lib/store' @@ -112,6 +113,23 @@ const MenuBar = () => { language: 'excalidraw', }) + view.dispatch(state.tr.insert(currentCursorPosition, nextNode)) + }, + }, + { + icon: , + action: () => { + const ctx = editorRef?.editor.ctx + if (!ctx) return + const view = ctx.get(editorViewCtx) + if (!view) return + const state = view.state + + const currentCursorPosition = state.selection.from + const nextNode = ctx.get(schemaCtx).node('diagram', { + value: '', + }) + view.dispatch(state.tr.insert(currentCursorPosition, nextNode)) }, }, @@ -131,7 +149,11 @@ const MenuBar = () => { }) }} > - + {isValidElement(menu.icon) ? ( + menu.icon + ) : typeof menu.icon === 'string' ? ( + + ) : null} ))} diff --git a/src/components/ui/editor/Milkdown/plugins/Mermaid.tsx b/src/components/ui/editor/Milkdown/plugins/Mermaid.tsx index e291ead9d3..356af93966 100644 --- a/src/components/ui/editor/Milkdown/plugins/Mermaid.tsx +++ b/src/components/ui/editor/Milkdown/plugins/Mermaid.tsx @@ -1,4 +1,5 @@ import { useNodeViewContext } from '@prosemirror-adapter/react' +import { useEffect, useRef } from 'react' import type { MilkdownPlugin } from '@milkdown/ctx' import type { ModalContentPropsInternal } from '~/components/ui/modal' import type { FC } from 'react' @@ -13,10 +14,13 @@ import { TextArea } from '~/components/ui/input' import { useModalStack } from '~/components/ui/modal' import { useUncontrolledInput } from '~/hooks/common/use-uncontrolled-input' +const autoOpenValue = '' + const MermaidRender = () => { const { contentRef, node, setAttrs, view, getPos } = useNodeViewContext() const value = node.attrs.value + const autoOpen = value === autoOpenValue const modalStack = useModalStack() @@ -28,10 +32,16 @@ const MermaidRender = () => { view.dispatch(view.state.tr.delete(pos, pos + node.nodeSize)) dismiss() } - const [, getValue, ref] = useUncontrolledInput(value) + const defaultValue = value === autoOpenValue ? '' : value + const [, getValue, ref] = + useUncontrolledInput(defaultValue) return (
-