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'
|
|
|
|
|
2020-09-26 16:43:08 +08:00
|
|
|
export interface CollaborationOptions {
|
2020-09-26 05:55:34 +08:00
|
|
|
name: string,
|
|
|
|
color: string,
|
2020-09-26 16:43:08 +08:00
|
|
|
provider?: any,
|
|
|
|
type?: any,
|
2020-09-26 05:55:34 +08:00
|
|
|
}
|
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 16:43:08 +08:00
|
|
|
export default new Extension<CollaborationOptions>()
|
|
|
|
.name('collaboration')
|
2020-09-26 05:55:34 +08:00
|
|
|
.defaults({
|
|
|
|
name: 'Someone',
|
|
|
|
color: '#cccccc',
|
2020-09-26 16:43:08 +08:00
|
|
|
provider: null,
|
|
|
|
type: null,
|
2020-09-26 05:22:21 +08:00
|
|
|
})
|
2020-09-26 05:55:34 +08:00
|
|
|
.plugins(({ options }) => [
|
2020-09-26 16:43:08 +08:00
|
|
|
// Collaboration
|
2020-09-26 05:55:34 +08:00
|
|
|
ySyncPlugin(type),
|
|
|
|
yUndoPlugin(),
|
|
|
|
keymap({
|
|
|
|
'Mod-z': undo,
|
|
|
|
'Mod-y': redo,
|
|
|
|
'Mod-Shift-z': redo,
|
|
|
|
}),
|
2020-09-26 16:43:08 +08:00
|
|
|
|
|
|
|
// CollaborationCursor
|
|
|
|
yCursorPlugin((() => {
|
|
|
|
provider.awareness.setLocalStateField('user', { name: options.name, color: options.color })
|
|
|
|
|
|
|
|
return provider.awareness
|
|
|
|
})()),
|
2020-09-26 05:55:34 +08:00
|
|
|
])
|
2020-09-26 05:22:21 +08:00
|
|
|
.create()
|