|
|
import { BaseEditor, Editor } from 'slate' |
|
|
import { History } from './history' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const HISTORY = new WeakMap<Editor, History>() |
|
|
export const SAVING = new WeakMap<Editor, boolean | undefined>() |
|
|
export const MERGING = new WeakMap<Editor, boolean | undefined>() |
|
|
export const SPLITTING_ONCE = new WeakMap<Editor, boolean | undefined>() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface HistoryEditor extends BaseEditor { |
|
|
history: History |
|
|
undo: () => void |
|
|
redo: () => void |
|
|
writeHistory: (stack: 'undos' | 'redos', batch: any) => void |
|
|
} |
|
|
|
|
|
|
|
|
export const HistoryEditor = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isHistoryEditor(value: any): value is HistoryEditor { |
|
|
return History.isHistory(value.history) && Editor.isEditor(value) |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isMerging(editor: HistoryEditor): boolean | undefined { |
|
|
return MERGING.get(editor) |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isSplittingOnce(editor: HistoryEditor): boolean | undefined { |
|
|
return SPLITTING_ONCE.get(editor) |
|
|
}, |
|
|
|
|
|
setSplittingOnce(editor: HistoryEditor, value: boolean | undefined): void { |
|
|
SPLITTING_ONCE.set(editor, value) |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isSaving(editor: HistoryEditor): boolean | undefined { |
|
|
return SAVING.get(editor) |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
redo(editor: HistoryEditor): void { |
|
|
editor.redo() |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
undo(editor: HistoryEditor): void { |
|
|
editor.undo() |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
withMerging(editor: HistoryEditor, fn: () => void): void { |
|
|
const prev = HistoryEditor.isMerging(editor) |
|
|
MERGING.set(editor, true) |
|
|
fn() |
|
|
MERGING.set(editor, prev) |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
withNewBatch(editor: HistoryEditor, fn: () => void): void { |
|
|
const prev = HistoryEditor.isMerging(editor) |
|
|
MERGING.set(editor, true) |
|
|
SPLITTING_ONCE.set(editor, true) |
|
|
fn() |
|
|
MERGING.set(editor, prev) |
|
|
SPLITTING_ONCE.delete(editor) |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
withoutMerging(editor: HistoryEditor, fn: () => void): void { |
|
|
const prev = HistoryEditor.isMerging(editor) |
|
|
MERGING.set(editor, false) |
|
|
fn() |
|
|
MERGING.set(editor, prev) |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
withoutSaving(editor: HistoryEditor, fn: () => void): void { |
|
|
const prev = HistoryEditor.isSaving(editor) |
|
|
SAVING.set(editor, false) |
|
|
try { |
|
|
fn() |
|
|
} finally { |
|
|
SAVING.set(editor, prev) |
|
|
} |
|
|
}, |
|
|
} |
|
|
|