2020-09-26 05:22:21 +08:00
|
|
|
import { Extension } from '@tiptap/core'
|
2020-09-26 05:55:34 +08:00
|
|
|
import * as Y from 'yjs'
|
2020-09-26 05:22:21 +08:00
|
|
|
import {
|
|
|
|
redo, undo, yCursorPlugin, ySyncPlugin, yUndoPlugin,
|
|
|
|
} from 'y-prosemirror'
|
|
|
|
import { WebrtcProvider } from 'y-webrtc'
|
2020-09-26 05:55:34 +08:00
|
|
|
import { keymap } from 'prosemirror-keymap'
|
|
|
|
|
|
|
|
export interface YjsOptions {
|
|
|
|
name: string,
|
|
|
|
color: string,
|
|
|
|
}
|
2020-09-26 05:22:21 +08:00
|
|
|
|
|
|
|
const ydoc = new Y.Doc()
|
2020-09-26 05:55:34 +08:00
|
|
|
const provider = new WebrtcProvider('example', ydoc)
|
2020-09-26 05:22:21 +08:00
|
|
|
const type = ydoc.getXmlFragment('prosemirror')
|
|
|
|
|
2020-09-26 05:55:34 +08:00
|
|
|
export default new Extension<YjsOptions>()
|
2020-09-26 05:22:21 +08:00
|
|
|
.name('yjs')
|
2020-09-26 05:55:34 +08:00
|
|
|
.defaults({
|
|
|
|
name: 'Someone',
|
|
|
|
color: '#cccccc',
|
2020-09-26 05:22:21 +08:00
|
|
|
})
|
2020-09-26 05:55:34 +08:00
|
|
|
.plugins(({ options }) => [
|
|
|
|
ySyncPlugin(type),
|
|
|
|
yCursorPlugin((() => {
|
|
|
|
provider.awareness.setLocalStateField('user', { name: options.name, color: options.color })
|
|
|
|
|
|
|
|
return provider.awareness
|
|
|
|
})()),
|
|
|
|
yUndoPlugin(),
|
|
|
|
keymap({
|
|
|
|
'Mod-z': undo,
|
|
|
|
'Mod-y': redo,
|
|
|
|
'Mod-Shift-z': redo,
|
|
|
|
}),
|
|
|
|
])
|
2020-09-26 05:22:21 +08:00
|
|
|
.create()
|