-
-
Notifications
You must be signed in to change notification settings - Fork 593
/
Excalidraw.tsx
364 lines (320 loc) · 10.4 KB
/
Excalidraw.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { useNodeViewContext } from '@prosemirror-adapter/react'
import { lazy, Suspense, useEffect, useMemo, useRef, useState } from 'react'
import { useForceUpdate } from 'framer-motion'
import { useAtom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'
import { diff } from 'jsondiffpatch'
import { visit } from 'unist-util-visit'
import type { MilkdownPlugin } from '@milkdown/ctx'
import type { Node } from '@milkdown/transformer'
import type { ExcalidrawRefObject } from '~/components/ui/excalidraw'
import type { ModalContentPropsInternal } from '~/components/ui/modal'
import type { FC } from 'react'
import type { PluginCtx } from './types'
import { setBlockType } from '@milkdown/prose/commands'
import { InputRule } from '@milkdown/prose/inputrules'
import {
$command,
$inputRule,
$nodeSchema,
$remark,
$view,
} from '@milkdown/utils'
import { BlockLoading } from '~/components/modules/shared/BlockLoading'
import { StyledButton } from '~/components/ui/button'
import { CheckBoxLabel } from '~/components/ui/checkbox'
import { useModalStack } from '~/components/ui/modal'
import { safeJsonParse } from '~/lib/helper'
import { buildNSKey } from '~/lib/ns'
import { toast } from '~/lib/toast'
import { FileTypeEnum, uploadFileToServer } from '~/lib/upload'
import { SharedModalAction } from './__internal/SharedModalAction'
function createExcalidrawDiv(contents: string) {
return {
type: 'excalidraw',
value: contents,
}
}
function visitCodeBlock(ast: Node) {
return visit(
ast,
'code',
(node, index, parent: Node & { children: Node[] }) => {
const { lang, value } = node
// If this codeblock is not excalidraw, bail.
if (lang !== 'excalidraw') return node
const newNode = createExcalidrawDiv(value)
if (parent && index != null) parent.children.splice(index, 1, newNode)
return node
},
)
}
function remarkExcalidraw() {
function transformer(tree: Node) {
visitCodeBlock(tree)
}
return transformer
}
const id = 'excalidraw'
/// Schema for diagram node.
export const excalidrawSchema = $nodeSchema(id, () => {
return {
content: 'text*',
group: 'block',
marks: '',
defining: true,
atom: true,
isolating: true,
attrs: {
value: {
default: '',
},
},
parseDOM: [
{
tag: `div[data-type="${id}"]`,
preserveWhitespace: 'full',
getAttrs: (dom) => {
return {
value: (dom as any)?.dataset?.value || '',
}
},
},
],
toDOM: (node) => {
const code = node.attrs.value as string
const dom = document.createElement('div')
dom.dataset.type = id
dom.dataset.value = code
dom.textContent = code
return dom
},
parseMarkdown: {
match: ({ type }) => type === id,
runner: (state, node, type) => {
const value = node.value as string
state.addNode(type, { value })
},
},
toMarkdown: {
match: (node) => node.type.name === id,
runner: (state, node) => {
state.addNode('code', undefined, node.attrs.value || '', {
lang: 'excalidraw',
})
},
},
}
})
/// A input rule that will insert a diagram node when you type ` ```excalidraw `.
const insertExcalidrawInputRules = $inputRule(
(ctx) =>
new InputRule(/^```excalidraw$/, (state, _match, start, end) => {
const nodeType = excalidrawSchema.type(ctx)
const $start = state.doc.resolve(start)
if (
!$start
.node(-1)
.canReplaceWith($start.index(-1), $start.indexAfter(-1), nodeType)
)
return null
return state.tr
.delete(start, end)
.setBlockType(start, start, nodeType, {})
}),
)
/// A remark plugin that will parse mermaid code block.
const remarkExcalidrawPlugin = $remark(
'remarkExcalidraw',
() => remarkExcalidraw,
)
/// A command that will insert a excalidraw node.
const insertExcalidrawCommand = $command(
'InsertExcalidrawCommand',
(ctx) => () => setBlockType(excalidrawSchema.type(ctx)),
)
const excalidrawOptionAtom = atomWithStorage(buildNSKey('excalidraw'), {
embed: false,
delta: true,
})
export const ExcalidrawPlugins: (pluginCtx: PluginCtx) => MilkdownPlugin[] = ({
nodeViewFactory,
}) => [
insertExcalidrawInputRules,
insertExcalidrawCommand,
excalidrawSchema as any as MilkdownPlugin,
remarkExcalidrawPlugin as any as MilkdownPlugin,
$view(excalidrawSchema.node, () =>
nodeViewFactory({
component: ExcalidrawBoard,
}),
),
]
const ExcalidrawBoard: FC = () => {
const modalStack = useModalStack()
const nodeCtx = useNodeViewContext()
const content = nodeCtx.node.attrs.value
const [initialContent, resetInitialContent] = useState(content)
const [forceUpdate, key] = useForceUpdate()
useEffect(() => {
forceUpdate()
}, [content])
const Excalidraw = useMemo(
() =>
lazy(() =>
import('~/components/ui/excalidraw/Excalidraw').then((m) => ({
default: m.Excalidraw,
})),
),
[],
)
const handleEdit = () => {
const Content: FC<ModalContentPropsInternal> = () => {
const valueRef = useRef<string | undefined>(content)
const valueGetterRef = useRef(() => valueRef.current)
const [editorOption, setEditorOption] = useAtom(excalidrawOptionAtom)
const excalidrawRef = useRef<ExcalidrawRefObject>(null)
const alreadyUploadValueFileMap = useRef(
{} as Record<string, string>,
).current
const getFinalSaveValue = async (): Promise<string | undefined> => {
if (editorOption.delta) {
const currentData = valueRef.current
if (!currentData) {
toast.error('无法获取当前数据,更新失败')
return
}
// 如果是增量存储
if (!initialContent) {
// 没有初始数据的话,直接刷新文件 Link
return fullFileUpdateAsLink()
}
// 初始数据是嵌入式数据
const isEmbeddedData = safeJsonParse(initialContent)
if (isEmbeddedData) {
// 如果是嵌入式数据,直接更新为 link
return fullFileUpdateAsLink()
}
// 初始数据是文件链接
const dataRefData = excalidrawRef.current?.getRefData()
if (!dataRefData) {
toast.error('无法获取原始数据增量更新失败')
return
}
const delta = diff(dataRefData, JSON.parse(currentData))
const firstLine = initialContent.split('\n')[0]
return [firstLine, JSON.stringify(delta, null, 0)].join('\n')
} else if (editorOption.embed) {
return valueRef.current
}
if (!editorOption.delta && !editorOption.embed) {
return fullFileUpdateAsLink()
}
async function fullFileUpdateAsLink() {
// 更新为链接类型
const currentData = valueRef.current
if (!currentData) return
const hasUploaded = alreadyUploadValueFileMap[currentData]
if (hasUploaded) {
return hasUploaded
}
const file = new File([currentData], 'file.excalidraw', {})
toast.info('正在上传文件')
const result = await uploadFileToServer(FileTypeEnum.File, file)
toast.success('上传成功')
const refName = `ref:file/${result.name}`
alreadyUploadValueFileMap[currentData] = refName
return refName
}
}
return (
<div className="flex h-full w-full flex-col">
<Suspense>
<Excalidraw
ref={excalidrawRef}
className="h-full w-full flex-grow"
data={content}
viewModeEnabled={false}
zenModeEnabled={false}
onChange={async (elements, appState, files) => {
const serializeAsJSON = await import(
'@excalidraw/excalidraw'
).then((m) => m.serializeAsJSON)
valueRef.current = JSON.stringify(
JSON.parse(
serializeAsJSON(elements, appState, files, 'database'),
),
null,
0,
)
}}
/>
<div className="relative">
<div className="absolute bottom-1 left-1 space-x-2">
<CheckBoxLabel
label="使用内嵌数据"
onCheckChange={(checked) => {
setEditorOption((prev) => ({ ...prev, embed: checked }))
}}
checked={editorOption.embed}
/>
<CheckBoxLabel
label="使用增量存储"
checked={editorOption.embed ? false : editorOption.delta}
disabled={editorOption.embed}
onCheckChange={(checked) => {
setEditorOption((prev) => ({ ...prev, delta: checked }))
}}
/>
</div>
<SharedModalAction
getValue={valueGetterRef.current}
nodeCtx={nodeCtx}
save={async () => {
const value = await getFinalSaveValue()
if (!value) return
nodeCtx.setAttrs({ value })
resetInitialContent(value)
}}
>
<StyledButton
variant="secondary"
onClick={async () => {
const value = await getFinalSaveValue()
if (!value) {
toast.error('无法获取当前数据')
return
}
await navigator.clipboard.writeText(
`\`\`\`excalidraw\n${value}\n\`\`\``,
)
toast.success('已复制')
}}
>
复制
</StyledButton>
</SharedModalAction>
</div>
</Suspense>
</div>
)
}
modalStack.present({
title: 'Excalidraw',
max: true,
content: Content,
})
}
return (
<div onClick={handleEdit} className="cursor-pointer">
<Suspense fallback={<BlockLoading />}>
<Excalidraw
className="pointer-events-none"
showExtendButton={false}
key={key}
data={content}
/>
</Suspense>
</div>
)
}