-
-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathutils.ts
47 lines (42 loc) · 1.48 KB
/
utils.ts
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
import type { Editor } from '@milkdown/core'
import { editorViewCtx, parserCtx, serializerCtx } from '@milkdown/core'
import { Slice } from '@milkdown/prose/model'
export async function setup(createEditor: () => Promise<Editor>) {
globalThis.commands = {}
const editor = await createEditor()
globalThis.__milkdown__ = editor
globalThis.__view__ = editor.action((ctx) => ctx.get(editorViewCtx))
globalThis.__setMarkdown__ = (markdown: string) =>
editor.action((ctx) => {
const view = ctx.get(editorViewCtx)
const parser = ctx.get(parserCtx)
const doc = parser(markdown)
if (!doc) return
const state = view.state
view.dispatch(
state.tr.replace(
0,
state.doc.content.size,
new Slice(doc.content, 0, 0)
)
)
})
globalThis.__getMarkdown__ = () =>
editor.action((ctx) => {
const view = ctx.get(editorViewCtx)
const serializer = ctx.get(serializerCtx)
return serializer(view.state.doc)
})
globalThis.__inspect__ = () => editor.inspect()
const logButton = document.querySelector<HTMLDivElement>('#log')
if (logButton) {
// oxlint-disable-next-line no-console
logButton.onclick = () => console.log(globalThis.__getMarkdown__())
}
const inspectButton = document.querySelector<HTMLDivElement>('#inspect')
if (inspectButton) {
// oxlint-disable-next-line no-console
inspectButton.onclick = () => console.log(globalThis.__inspect__())
}
return editor
}