2020-12-02 16:28:55 +08:00
|
|
|
import { Extension, Command } from '@tiptap/core'
|
2020-09-26 05:22:21 +08:00
|
|
|
import {
|
2020-11-25 16:50:54 +08:00
|
|
|
redo,
|
|
|
|
undo,
|
|
|
|
ySyncPlugin,
|
|
|
|
yUndoPlugin,
|
2020-09-26 05:22:21 +08:00
|
|
|
} from 'y-prosemirror'
|
2020-09-26 05:55:34 +08:00
|
|
|
|
2020-09-26 16:43:08 +08:00
|
|
|
export interface CollaborationOptions {
|
2020-11-30 20:35:49 +08:00
|
|
|
provider: any,
|
2020-09-26 05:55:34 +08:00
|
|
|
}
|
2020-09-26 05:22:21 +08:00
|
|
|
|
2020-11-16 06:25:25 +08:00
|
|
|
const Collaboration = Extension.create({
|
2020-12-02 16:44:46 +08:00
|
|
|
name: 'collaboration',
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
defaultOptions: <CollaborationOptions>{
|
2020-11-30 20:35:49 +08:00
|
|
|
provider: null,
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
2020-12-02 16:28:55 +08:00
|
|
|
addCommands() {
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Undo recent changes
|
|
|
|
*/
|
|
|
|
undo: (): Command => ({ state }) => {
|
|
|
|
return undo(state)
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Reapply reverted changes
|
|
|
|
*/
|
|
|
|
redo: (): Command => ({ state }) => {
|
|
|
|
return redo(state)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
|
|
|
'Mod-z': () => this.editor.commands.undo(),
|
|
|
|
'Mod-y': () => this.editor.commands.redo(),
|
|
|
|
'Shift-Mod-z': () => this.editor.commands.redo(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
addProseMirrorPlugins() {
|
|
|
|
return [
|
2020-11-30 20:35:49 +08:00
|
|
|
ySyncPlugin(
|
|
|
|
this.options.provider.doc.getXmlFragment('prosemirror'),
|
|
|
|
),
|
2020-10-22 18:34:49 +08:00
|
|
|
yUndoPlugin(),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2020-11-30 20:50:06 +08:00
|
|
|
onDestroy() {
|
|
|
|
this.options.provider?.destroy()
|
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
})
|
2020-10-23 04:40:40 +08:00
|
|
|
|
|
|
|
export default Collaboration
|
|
|
|
|
2020-11-11 04:18:22 +08:00
|
|
|
declare module '@tiptap/core' {
|
2020-10-23 04:40:40 +08:00
|
|
|
interface AllExtensions {
|
|
|
|
Collaboration: typeof Collaboration,
|
|
|
|
}
|
|
|
|
}
|