-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
prosemirror.js
58 lines (53 loc) · 1.93 KB
/
prosemirror.js
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
/* eslint-env browser */
import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'
import { ySyncPlugin, yCursorPlugin, yUndoPlugin, undo, redo, initProseMirrorDoc } from 'y-prosemirror'
import { EditorState } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import { schema } from './schema.js'
import { exampleSetup } from 'prosemirror-example-setup'
import { keymap } from 'prosemirror-keymap'
window.addEventListener('load', () => {
const ydoc = new Y.Doc()
const provider = new WebsocketProvider(
'wss://demos.yjs.dev/ws', // use the public ws server
// `ws${location.protocol.slice(4)}//${location.host}/ws`, // alternatively: use the local ws server (run `npm start` in root directory)
'prosemirror-demo-2024/06',
ydoc
)
const yXmlFragment = ydoc.getXmlFragment('prosemirror')
const editor = document.createElement('div')
editor.setAttribute('id', 'editor')
const editorContainer = document.createElement('div')
editorContainer.insertBefore(editor, null)
const { doc, mapping } = initProseMirrorDoc(yXmlFragment, schema)
const prosemirrorView = new EditorView(editor, {
state: EditorState.create({
doc,
schema,
plugins: [
ySyncPlugin(yXmlFragment, { mapping }),
yCursorPlugin(provider.awareness),
yUndoPlugin(),
keymap({
'Mod-z': undo,
'Mod-y': redo,
'Mod-Shift-z': redo
})
].concat(exampleSetup({ schema }))
})
})
document.body.insertBefore(editorContainer, null)
const connectBtn = /** @type {HTMLElement} */ (document.getElementById('y-connect-btn'))
connectBtn.addEventListener('click', () => {
if (provider.shouldConnect) {
provider.disconnect()
connectBtn.textContent = 'Connect'
} else {
provider.connect()
connectBtn.textContent = 'Disconnect'
}
})
// @ts-ignore
window.example = { provider, ydoc, yXmlFragment, prosemirrorView }
})