tiptap/packages/extension-history/index.ts

45 lines
870 B
TypeScript
Raw Normal View History

2020-09-09 05:44:45 +08:00
import { Extension } from '@tiptap/core'
2020-03-06 18:02:35 +08:00
import {
history,
undo,
redo,
undoDepth,
redoDepth,
} from 'prosemirror-history'
declare module '@tiptap/core/src/Editor' {
interface Editor {
undo(): Editor,
2020-04-02 19:36:18 +08:00
redo(): Editor,
2020-03-06 18:02:35 +08:00
}
}
2020-04-14 04:32:39 +08:00
interface HistoryOptions {
2020-09-09 05:44:45 +08:00
historyPluginOptions: Object,
2020-04-14 04:32:39 +08:00
}
2020-09-09 05:44:45 +08:00
export default new Extension<HistoryOptions>()
.name('history')
.defaults({
historyPluginOptions: {},
})
.commands(() => ({
undo: (next, { view }) => () => {
undo(view.state, view.dispatch)
next()
},
redo: (next, { view }) => () => {
redo(view.state, view.dispatch)
next()
},
}))
.keys(({ editor }) => ({
'Mod-z': () => editor.undo(),
'Mod-y': () => editor.redo(),
'Shift-Mod-z': () => editor.redo(),
}))
.plugins(({ options }) => [
history(options.historyPluginOptions)
])
.create()