2020-11-16 06:25:25 +08:00
|
|
|
import { Command, Extension } from '@tiptap/core'
|
2020-10-23 04:40:40 +08:00
|
|
|
import { history, undo, redo } from 'prosemirror-history'
|
2020-03-06 18:02:35 +08:00
|
|
|
|
2020-09-10 00:55:19 +08:00
|
|
|
export interface HistoryOptions {
|
2020-09-28 03:29:51 +08:00
|
|
|
depth: number,
|
|
|
|
newGroupDelay: number,
|
2020-04-14 04:32:39 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands {
|
2021-02-11 01:05:02 +08:00
|
|
|
/**
|
|
|
|
* Undo recent changes
|
|
|
|
*/
|
2021-02-10 16:59:35 +08:00
|
|
|
undo: () => Command,
|
2021-02-11 01:05:02 +08:00
|
|
|
/**
|
|
|
|
* Reapply reverted changes
|
|
|
|
*/
|
2021-02-10 16:59:35 +08:00
|
|
|
redo: () => Command,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
export const History = Extension.create<HistoryOptions>({
|
2020-12-01 21:55:11 +08:00
|
|
|
name: 'history',
|
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
defaultOptions: {
|
2020-09-27 16:52:09 +08:00
|
|
|
depth: 100,
|
|
|
|
newGroupDelay: 500,
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-02-10 16:59:35 +08:00
|
|
|
undo: () => ({ state, dispatch }) => {
|
2020-10-22 18:34:49 +08:00
|
|
|
return undo(state, dispatch)
|
|
|
|
},
|
2021-02-10 16:59:35 +08:00
|
|
|
redo: () => ({ state, dispatch }) => {
|
2020-10-22 18:34:49 +08:00
|
|
|
return redo(state, dispatch)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addProseMirrorPlugins() {
|
|
|
|
return [
|
|
|
|
history(this.options),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
2020-11-13 18:42:04 +08:00
|
|
|
'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
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|