2021-06-05 03:56:29 +08:00
|
|
|
import { Extension } from '@tiptap/core'
|
2023-02-03 00:37:33 +08:00
|
|
|
import { history, redo, undo } from '@tiptap/pm/history'
|
2020-03-06 18:02:35 +08:00
|
|
|
|
2020-09-10 00:55:19 +08:00
|
|
|
export interface HistoryOptions {
|
2024-05-11 20:30:44 +08:00
|
|
|
/**
|
|
|
|
* The amount of history events that are collected before the oldest events are discarded.
|
|
|
|
* @default 100
|
|
|
|
* @example 50
|
|
|
|
*/
|
2020-09-28 03:29:51 +08:00
|
|
|
depth: number,
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The delay (in milliseconds) between changes after which a new group should be started.
|
|
|
|
* @default 500
|
|
|
|
* @example 1000
|
|
|
|
*/
|
2020-09-28 03:29:51 +08:00
|
|
|
newGroupDelay: number,
|
2020-04-14 04:32:39 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-06-05 03:56:29 +08:00
|
|
|
interface Commands<ReturnType> {
|
2021-02-16 18:27:58 +08:00
|
|
|
history: {
|
|
|
|
/**
|
|
|
|
* Undo recent changes
|
2024-05-11 20:30:44 +08:00
|
|
|
* @example editor.commands.undo()
|
2021-02-16 18:27:58 +08:00
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
undo: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
/**
|
|
|
|
* Reapply reverted changes
|
2024-05-11 20:30:44 +08:00
|
|
|
* @example editor.commands.redo()
|
2021-02-16 18:27:58 +08:00
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
redo: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-11 20:30:44 +08:00
|
|
|
/**
|
|
|
|
* This extension allows you to undo and redo recent changes.
|
|
|
|
* @see https://www.tiptap.dev/api/extensions/history
|
|
|
|
*
|
|
|
|
* **Important**: If the `@tiptap/extension-collaboration` package is used, make sure to remove
|
|
|
|
* the `history` extension, as it is not compatible with the `collaboration` extension.
|
|
|
|
*
|
|
|
|
* `@tiptap/extension-collaboration` uses its own history implementation.
|
|
|
|
*/
|
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-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
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(),
|
|
|
|
'Shift-Mod-z': () => this.editor.commands.redo(),
|
2023-09-10 16:02:38 +08:00
|
|
|
'Mod-y': () => this.editor.commands.redo(),
|
2021-07-15 05:51:53 +08:00
|
|
|
|
|
|
|
// Russian keyboard layouts
|
|
|
|
'Mod-я': () => this.editor.commands.undo(),
|
2021-07-14 23:29:28 +08:00
|
|
|
'Shift-Mod-я': () => this.editor.commands.redo(),
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|