tiptap/packages/extension-history/index.ts

43 lines
850 B
TypeScript
Raw Normal View History

2020-09-22 05:17:30 +08:00
import { Command, Extension } from '@tiptap/core'
2020-03-06 18:02:35 +08:00
import {
history,
undo,
redo,
} from 'prosemirror-history'
declare module '@tiptap/core/src/Editor' {
2020-09-22 16:49:38 +08:00
interface Commands {
2020-09-22 05:17:30 +08:00
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-09-09 05:44:45 +08:00
export default new Extension<HistoryOptions>()
.name('history')
.defaults({
2020-09-27 16:52:09 +08:00
depth: 100,
newGroupDelay: 500,
2020-09-09 05:44:45 +08:00
})
.commands(() => ({
2020-09-22 05:17:30 +08:00
undo: () => ({ state, dispatch }) => {
return undo(state, dispatch)
2020-09-09 05:44:45 +08:00
},
2020-09-22 05:17:30 +08:00
redo: () => ({ state, dispatch }) => {
return redo(state, dispatch)
2020-09-09 05:44:45 +08:00
},
}))
.keys(({ editor }) => ({
'Mod-z': () => editor.undo(),
'Mod-y': () => editor.redo(),
'Shift-Mod-z': () => editor.redo(),
}))
.plugins(({ options }) => [
2020-09-28 03:29:51 +08:00
history(options),
2020-09-09 05:44:45 +08:00
])
.create()