tiptap/packages/extension-history/index.ts

53 lines
967 B
TypeScript
Raw Normal View History

2020-10-22 18:34:49 +08:00
import { Command, createExtension } from '@tiptap/core'
2020-03-06 18:02:35 +08:00
import {
history,
undo,
redo,
} from 'prosemirror-history'
2020-10-22 18:34:49 +08:00
// declare module '@tiptap/core/src/Editor' {
// interface Commands {
// undo: () => Command,
// redo: () => Command,
// }
// }
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
}
2020-10-22 18:34:49 +08:00
export default createExtension({
name: 'history',
defaultOptions: <HistoryOptions>{
2020-09-27 16:52:09 +08:00
depth: 100,
newGroupDelay: 500,
2020-10-22 18:34:49 +08:00
},
addCommands() {
return {
undo: () => ({ state, dispatch }) => {
return undo(state, dispatch)
},
redo: () => ({ state, dispatch }) => {
return redo(state, dispatch)
},
}
},
addProseMirrorPlugins() {
return [
history(this.options),
]
},
addKeyboardShortcuts() {
return {
'Mod-z': () => this.editor.undo(),
'Mod-y': () => this.editor.redo(),
'Shift-Mod-z': () => this.editor.redo(),
}
},
})