-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcreateEditorView.ts
52 lines (47 loc) · 1.58 KB
/
createEditorView.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
48
49
50
51
52
import type { Plugin } from 'prosemirror-state'
import type { NodeViewConstructor } from 'prosemirror-view'
import { exampleSetup } from 'prosemirror-example-setup'
import { keymap } from 'prosemirror-keymap'
import { DOMParser } from 'prosemirror-model'
import { schema } from 'prosemirror-schema-basic'
import { EditorState } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import 'prosemirror-view/style/prosemirror.css'
import 'prosemirror-example-setup/style/style.css'
import 'prosemirror-menu/style/menu.css'
export function createEditorView(element: HTMLElement, nodeViews: Record<string, NodeViewConstructor>, plugins: Plugin[]) {
const content = document.querySelector('#content')
if (!content)
throw new Error('Content element not found')
return new EditorView(element, {
state: EditorState.create({
doc: DOMParser.fromSchema(schema).parse(content),
schema,
plugins: [
...exampleSetup({ schema }),
keymap({
'Mod-[': (state, dispatch) => {
const { selection } = state
const node = selection.$from.node()
if (node.type.name !== 'heading')
return false
let level = node.attrs.level
if (level >= 6)
level = 1
else
level += 1
dispatch?.(
state.tr.setNodeMarkup(selection.$from.before(), null, {
...node.attrs,
level,
}),
)
return true
},
}),
...plugins,
],
}),
nodeViews,
})
}