@ckeditor/ckeditor5-undo 0.0.0-internal-20241017.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/LICENSE.md +17 -0
- package/README.md +22 -0
- package/ckeditor5-metadata.json +23 -0
- package/dist/augmentation.d.ts +20 -0
- package/dist/basecommand.d.ts +76 -0
- package/dist/index-content.css +4 -0
- package/dist/index-editor.css +4 -0
- package/dist/index.css +4 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +547 -0
- package/dist/index.js.map +1 -0
- package/dist/redocommand.d.ts +31 -0
- package/dist/translations/ar.d.ts +8 -0
- package/dist/translations/ar.js +5 -0
- package/dist/translations/ar.umd.js +11 -0
- package/dist/translations/ast.d.ts +8 -0
- package/dist/translations/ast.js +5 -0
- package/dist/translations/ast.umd.js +11 -0
- package/dist/translations/az.d.ts +8 -0
- package/dist/translations/az.js +5 -0
- package/dist/translations/az.umd.js +11 -0
- package/dist/translations/bg.d.ts +8 -0
- package/dist/translations/bg.js +5 -0
- package/dist/translations/bg.umd.js +11 -0
- package/dist/translations/bn.d.ts +8 -0
- package/dist/translations/bn.js +5 -0
- package/dist/translations/bn.umd.js +11 -0
- package/dist/translations/ca.d.ts +8 -0
- package/dist/translations/ca.js +5 -0
- package/dist/translations/ca.umd.js +11 -0
- package/dist/translations/cs.d.ts +8 -0
- package/dist/translations/cs.js +5 -0
- package/dist/translations/cs.umd.js +11 -0
- package/dist/translations/da.d.ts +8 -0
- package/dist/translations/da.js +5 -0
- package/dist/translations/da.umd.js +11 -0
- package/dist/translations/de-ch.d.ts +8 -0
- package/dist/translations/de-ch.js +5 -0
- package/dist/translations/de-ch.umd.js +11 -0
- package/dist/translations/de.d.ts +8 -0
- package/dist/translations/de.js +5 -0
- package/dist/translations/de.umd.js +11 -0
- package/dist/translations/el.d.ts +8 -0
- package/dist/translations/el.js +5 -0
- package/dist/translations/el.umd.js +11 -0
- package/dist/translations/en-au.d.ts +8 -0
- package/dist/translations/en-au.js +5 -0
- package/dist/translations/en-au.umd.js +11 -0
- package/dist/translations/en-gb.d.ts +8 -0
- package/dist/translations/en-gb.js +5 -0
- package/dist/translations/en-gb.umd.js +11 -0
- package/dist/translations/en.d.ts +8 -0
- package/dist/translations/en.js +5 -0
- package/dist/translations/en.umd.js +11 -0
- package/dist/translations/eo.d.ts +8 -0
- package/dist/translations/eo.js +5 -0
- package/dist/translations/eo.umd.js +11 -0
- package/dist/translations/es.d.ts +8 -0
- package/dist/translations/es.js +5 -0
- package/dist/translations/es.umd.js +11 -0
- package/dist/translations/et.d.ts +8 -0
- package/dist/translations/et.js +5 -0
- package/dist/translations/et.umd.js +11 -0
- package/dist/translations/eu.d.ts +8 -0
- package/dist/translations/eu.js +5 -0
- package/dist/translations/eu.umd.js +11 -0
- package/dist/translations/fa.d.ts +8 -0
- package/dist/translations/fa.js +5 -0
- package/dist/translations/fa.umd.js +11 -0
- package/dist/translations/fi.d.ts +8 -0
- package/dist/translations/fi.js +5 -0
- package/dist/translations/fi.umd.js +11 -0
- package/dist/translations/fr.d.ts +8 -0
- package/dist/translations/fr.js +5 -0
- package/dist/translations/fr.umd.js +11 -0
- package/dist/translations/gl.d.ts +8 -0
- package/dist/translations/gl.js +5 -0
- package/dist/translations/gl.umd.js +11 -0
- package/dist/translations/he.d.ts +8 -0
- package/dist/translations/he.js +5 -0
- package/dist/translations/he.umd.js +11 -0
- package/dist/translations/hi.d.ts +8 -0
- package/dist/translations/hi.js +5 -0
- package/dist/translations/hi.umd.js +11 -0
- package/dist/translations/hr.d.ts +8 -0
- package/dist/translations/hr.js +5 -0
- package/dist/translations/hr.umd.js +11 -0
- package/dist/translations/hu.d.ts +8 -0
- package/dist/translations/hu.js +5 -0
- package/dist/translations/hu.umd.js +11 -0
- package/dist/translations/id.d.ts +8 -0
- package/dist/translations/id.js +5 -0
- package/dist/translations/id.umd.js +11 -0
- package/dist/translations/it.d.ts +8 -0
- package/dist/translations/it.js +5 -0
- package/dist/translations/it.umd.js +11 -0
- package/dist/translations/ja.d.ts +8 -0
- package/dist/translations/ja.js +5 -0
- package/dist/translations/ja.umd.js +11 -0
- package/dist/translations/km.d.ts +8 -0
- package/dist/translations/km.js +5 -0
- package/dist/translations/km.umd.js +11 -0
- package/dist/translations/kn.d.ts +8 -0
- package/dist/translations/kn.js +5 -0
- package/dist/translations/kn.umd.js +11 -0
- package/dist/translations/ko.d.ts +8 -0
- package/dist/translations/ko.js +5 -0
- package/dist/translations/ko.umd.js +11 -0
- package/dist/translations/ku.d.ts +8 -0
- package/dist/translations/ku.js +5 -0
- package/dist/translations/ku.umd.js +11 -0
- package/dist/translations/lt.d.ts +8 -0
- package/dist/translations/lt.js +5 -0
- package/dist/translations/lt.umd.js +11 -0
- package/dist/translations/lv.d.ts +8 -0
- package/dist/translations/lv.js +5 -0
- package/dist/translations/lv.umd.js +11 -0
- package/dist/translations/ms.d.ts +8 -0
- package/dist/translations/ms.js +5 -0
- package/dist/translations/ms.umd.js +11 -0
- package/dist/translations/nb.d.ts +8 -0
- package/dist/translations/nb.js +5 -0
- package/dist/translations/nb.umd.js +11 -0
- package/dist/translations/ne.d.ts +8 -0
- package/dist/translations/ne.js +5 -0
- package/dist/translations/ne.umd.js +11 -0
- package/dist/translations/nl.d.ts +8 -0
- package/dist/translations/nl.js +5 -0
- package/dist/translations/nl.umd.js +11 -0
- package/dist/translations/no.d.ts +8 -0
- package/dist/translations/no.js +5 -0
- package/dist/translations/no.umd.js +11 -0
- package/dist/translations/pl.d.ts +8 -0
- package/dist/translations/pl.js +5 -0
- package/dist/translations/pl.umd.js +11 -0
- package/dist/translations/pt-br.d.ts +8 -0
- package/dist/translations/pt-br.js +5 -0
- package/dist/translations/pt-br.umd.js +11 -0
- package/dist/translations/pt.d.ts +8 -0
- package/dist/translations/pt.js +5 -0
- package/dist/translations/pt.umd.js +11 -0
- package/dist/translations/ro.d.ts +8 -0
- package/dist/translations/ro.js +5 -0
- package/dist/translations/ro.umd.js +11 -0
- package/dist/translations/ru.d.ts +8 -0
- package/dist/translations/ru.js +5 -0
- package/dist/translations/ru.umd.js +11 -0
- package/dist/translations/si.d.ts +8 -0
- package/dist/translations/si.js +5 -0
- package/dist/translations/si.umd.js +11 -0
- package/dist/translations/sk.d.ts +8 -0
- package/dist/translations/sk.js +5 -0
- package/dist/translations/sk.umd.js +11 -0
- package/dist/translations/sq.d.ts +8 -0
- package/dist/translations/sq.js +5 -0
- package/dist/translations/sq.umd.js +11 -0
- package/dist/translations/sr-latn.d.ts +8 -0
- package/dist/translations/sr-latn.js +5 -0
- package/dist/translations/sr-latn.umd.js +11 -0
- package/dist/translations/sr.d.ts +8 -0
- package/dist/translations/sr.js +5 -0
- package/dist/translations/sr.umd.js +11 -0
- package/dist/translations/sv.d.ts +8 -0
- package/dist/translations/sv.js +5 -0
- package/dist/translations/sv.umd.js +11 -0
- package/dist/translations/th.d.ts +8 -0
- package/dist/translations/th.js +5 -0
- package/dist/translations/th.umd.js +11 -0
- package/dist/translations/ti.d.ts +8 -0
- package/dist/translations/ti.js +5 -0
- package/dist/translations/ti.umd.js +11 -0
- package/dist/translations/tk.d.ts +8 -0
- package/dist/translations/tk.js +5 -0
- package/dist/translations/tk.umd.js +11 -0
- package/dist/translations/tr.d.ts +8 -0
- package/dist/translations/tr.js +5 -0
- package/dist/translations/tr.umd.js +11 -0
- package/dist/translations/tt.d.ts +8 -0
- package/dist/translations/tt.js +5 -0
- package/dist/translations/tt.umd.js +11 -0
- package/dist/translations/ug.d.ts +8 -0
- package/dist/translations/ug.js +5 -0
- package/dist/translations/ug.umd.js +11 -0
- package/dist/translations/uk.d.ts +8 -0
- package/dist/translations/uk.js +5 -0
- package/dist/translations/uk.umd.js +11 -0
- package/dist/translations/ur.d.ts +8 -0
- package/dist/translations/ur.js +5 -0
- package/dist/translations/ur.umd.js +11 -0
- package/dist/translations/uz.d.ts +8 -0
- package/dist/translations/uz.js +5 -0
- package/dist/translations/uz.umd.js +11 -0
- package/dist/translations/vi.d.ts +8 -0
- package/dist/translations/vi.js +5 -0
- package/dist/translations/vi.umd.js +11 -0
- package/dist/translations/zh-cn.d.ts +8 -0
- package/dist/translations/zh-cn.js +5 -0
- package/dist/translations/zh-cn.umd.js +11 -0
- package/dist/translations/zh.d.ts +8 -0
- package/dist/translations/zh.js +5 -0
- package/dist/translations/zh.umd.js +11 -0
- package/dist/undo.d.ts +125 -0
- package/dist/undocommand.d.ts +41 -0
- package/dist/undoediting.d.ts +45 -0
- package/dist/undoui.d.ts +42 -0
- package/lang/contexts.json +4 -0
- package/lang/translations/ar.po +26 -0
- package/lang/translations/ast.po +26 -0
- package/lang/translations/az.po +26 -0
- package/lang/translations/bg.po +26 -0
- package/lang/translations/bn.po +26 -0
- package/lang/translations/ca.po +26 -0
- package/lang/translations/cs.po +26 -0
- package/lang/translations/da.po +26 -0
- package/lang/translations/de-ch.po +26 -0
- package/lang/translations/de.po +26 -0
- package/lang/translations/el.po +26 -0
- package/lang/translations/en-au.po +26 -0
- package/lang/translations/en-gb.po +26 -0
- package/lang/translations/en.po +26 -0
- package/lang/translations/eo.po +26 -0
- package/lang/translations/es.po +26 -0
- package/lang/translations/et.po +26 -0
- package/lang/translations/eu.po +26 -0
- package/lang/translations/fa.po +26 -0
- package/lang/translations/fi.po +26 -0
- package/lang/translations/fr.po +26 -0
- package/lang/translations/gl.po +26 -0
- package/lang/translations/he.po +26 -0
- package/lang/translations/hi.po +26 -0
- package/lang/translations/hr.po +26 -0
- package/lang/translations/hu.po +26 -0
- package/lang/translations/id.po +26 -0
- package/lang/translations/it.po +26 -0
- package/lang/translations/ja.po +26 -0
- package/lang/translations/km.po +26 -0
- package/lang/translations/kn.po +26 -0
- package/lang/translations/ko.po +26 -0
- package/lang/translations/ku.po +26 -0
- package/lang/translations/lt.po +26 -0
- package/lang/translations/lv.po +26 -0
- package/lang/translations/ms.po +26 -0
- package/lang/translations/nb.po +26 -0
- package/lang/translations/ne.po +26 -0
- package/lang/translations/nl.po +26 -0
- package/lang/translations/no.po +26 -0
- package/lang/translations/pl.po +26 -0
- package/lang/translations/pt-br.po +26 -0
- package/lang/translations/pt.po +26 -0
- package/lang/translations/ro.po +26 -0
- package/lang/translations/ru.po +26 -0
- package/lang/translations/si.po +26 -0
- package/lang/translations/sk.po +26 -0
- package/lang/translations/sq.po +26 -0
- package/lang/translations/sr-latn.po +26 -0
- package/lang/translations/sr.po +26 -0
- package/lang/translations/sv.po +26 -0
- package/lang/translations/th.po +26 -0
- package/lang/translations/ti.po +26 -0
- package/lang/translations/tk.po +26 -0
- package/lang/translations/tr.po +26 -0
- package/lang/translations/tt.po +26 -0
- package/lang/translations/ug.po +26 -0
- package/lang/translations/uk.po +26 -0
- package/lang/translations/ur.po +26 -0
- package/lang/translations/uz.po +26 -0
- package/lang/translations/vi.po +26 -0
- package/lang/translations/zh-cn.po +26 -0
- package/lang/translations/zh.po +26 -0
- package/package.json +39 -0
- package/src/augmentation.d.ts +16 -0
- package/src/augmentation.js +5 -0
- package/src/basecommand.d.ts +72 -0
- package/src/basecommand.js +192 -0
- package/src/index.d.ts +13 -0
- package/src/index.js +11 -0
- package/src/redocommand.d.ts +27 -0
- package/src/redocommand.js +40 -0
- package/src/undo.d.ts +121 -0
- package/src/undo.js +127 -0
- package/src/undocommand.d.ts +37 -0
- package/src/undocommand.js +44 -0
- package/src/undoediting.d.ts +41 -0
- package/src/undoediting.js +102 -0
- package/src/undoui.d.ts +38 -0
- package/src/undoui.js +79 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/basecommand
|
|
7
|
+
*/
|
|
8
|
+
import { Command } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
import { transformSets, NoOperation } from '@ckeditor/ckeditor5-engine';
|
|
10
|
+
/**
|
|
11
|
+
* Base class for the undo feature commands: {@link module:undo/undocommand~UndoCommand} and {@link module:undo/redocommand~RedoCommand}.
|
|
12
|
+
*/
|
|
13
|
+
export default class BaseCommand extends Command {
|
|
14
|
+
/**
|
|
15
|
+
* @inheritDoc
|
|
16
|
+
*/
|
|
17
|
+
constructor(editor) {
|
|
18
|
+
super(editor);
|
|
19
|
+
/**
|
|
20
|
+
* Stack of items stored by the command. These are pairs of:
|
|
21
|
+
*
|
|
22
|
+
* * {@link module:engine/model/batch~Batch batch} saved by the command,
|
|
23
|
+
* * {@link module:engine/model/selection~Selection selection} state at the moment of saving the batch.
|
|
24
|
+
*/
|
|
25
|
+
this._stack = [];
|
|
26
|
+
/**
|
|
27
|
+
* Stores all batches that were created by this command.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
this._createdBatches = new WeakSet();
|
|
32
|
+
// Refresh state, so the command is inactive right after initialization.
|
|
33
|
+
this.refresh();
|
|
34
|
+
// This command should not depend on selection change.
|
|
35
|
+
this._isEnabledBasedOnSelection = false;
|
|
36
|
+
// Set the transparent batch for the `editor.data.set()` call if the
|
|
37
|
+
// batch type is not set already.
|
|
38
|
+
this.listenTo(editor.data, 'set', (evt, data) => {
|
|
39
|
+
// Create a shallow copy of the options to not change the original args.
|
|
40
|
+
// And make sure that an object is assigned to data[ 1 ].
|
|
41
|
+
data[1] = { ...data[1] };
|
|
42
|
+
const options = data[1];
|
|
43
|
+
// If batch type is not set, default to non-undoable batch.
|
|
44
|
+
if (!options.batchType) {
|
|
45
|
+
options.batchType = { isUndoable: false };
|
|
46
|
+
}
|
|
47
|
+
}, { priority: 'high' });
|
|
48
|
+
// Clear the stack for the `transparent` batches.
|
|
49
|
+
this.listenTo(editor.data, 'set', (evt, data) => {
|
|
50
|
+
// We can assume that the object exists and it has a `batchType` property.
|
|
51
|
+
// It was ensured with a higher priority listener before.
|
|
52
|
+
const options = data[1];
|
|
53
|
+
if (!options.batchType.isUndoable) {
|
|
54
|
+
this.clearStack();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @inheritDoc
|
|
60
|
+
*/
|
|
61
|
+
refresh() {
|
|
62
|
+
this.isEnabled = this._stack.length > 0;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns all batches created by this command.
|
|
66
|
+
*/
|
|
67
|
+
get createdBatches() {
|
|
68
|
+
return this._createdBatches;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Stores a batch in the command, together with the selection state of the {@link module:engine/model/document~Document document}
|
|
72
|
+
* created by the editor which this command is registered to.
|
|
73
|
+
*
|
|
74
|
+
* @param batch The batch to add.
|
|
75
|
+
*/
|
|
76
|
+
addBatch(batch) {
|
|
77
|
+
const docSelection = this.editor.model.document.selection;
|
|
78
|
+
const selection = {
|
|
79
|
+
ranges: docSelection.hasOwnRange ? Array.from(docSelection.getRanges()) : [],
|
|
80
|
+
isBackward: docSelection.isBackward
|
|
81
|
+
};
|
|
82
|
+
this._stack.push({ batch, selection });
|
|
83
|
+
this.refresh();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Removes all items from the stack.
|
|
87
|
+
*/
|
|
88
|
+
clearStack() {
|
|
89
|
+
this._stack = [];
|
|
90
|
+
this.refresh();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Restores the {@link module:engine/model/document~Document#selection document selection} state after a batch was undone.
|
|
94
|
+
*
|
|
95
|
+
* @param ranges Ranges to be restored.
|
|
96
|
+
* @param isBackward A flag describing whether the restored range was selected forward or backward.
|
|
97
|
+
* @param operations Operations which has been applied since selection has been stored.
|
|
98
|
+
*/
|
|
99
|
+
_restoreSelection(ranges, isBackward, operations) {
|
|
100
|
+
const model = this.editor.model;
|
|
101
|
+
const document = model.document;
|
|
102
|
+
// This will keep the transformed selection ranges.
|
|
103
|
+
const selectionRanges = [];
|
|
104
|
+
// Transform all ranges from the restored selection.
|
|
105
|
+
const transformedRangeGroups = ranges.map(range => range.getTransformedByOperations(operations));
|
|
106
|
+
const allRanges = transformedRangeGroups.flat();
|
|
107
|
+
for (const rangeGroup of transformedRangeGroups) {
|
|
108
|
+
// While transforming there could appear ranges that are contained by other ranges, we shall ignore them.
|
|
109
|
+
const transformed = rangeGroup
|
|
110
|
+
.filter(range => range.root != document.graveyard)
|
|
111
|
+
.filter(range => !isRangeContainedByAnyOtherRange(range, allRanges));
|
|
112
|
+
// All the transformed ranges ended up in graveyard.
|
|
113
|
+
if (!transformed.length) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
// After the range got transformed, we have an array of ranges. Some of those
|
|
117
|
+
// ranges may be "touching" -- they can be next to each other and could be merged.
|
|
118
|
+
normalizeRanges(transformed);
|
|
119
|
+
// For each `range` from `ranges`, we take only one transformed range.
|
|
120
|
+
// This is because we want to prevent situation where single-range selection
|
|
121
|
+
// got transformed to multi-range selection.
|
|
122
|
+
selectionRanges.push(transformed[0]);
|
|
123
|
+
}
|
|
124
|
+
// @if CK_DEBUG_ENGINE // console.log( `Restored selection by undo: ${ selectionRanges.join( ', ' ) }` );
|
|
125
|
+
// `selectionRanges` may be empty if all ranges ended up in graveyard. If that is the case, do not restore selection.
|
|
126
|
+
if (selectionRanges.length) {
|
|
127
|
+
model.change(writer => {
|
|
128
|
+
writer.setSelection(selectionRanges, { backward: isBackward });
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Undoes a batch by reversing that batch, transforming reversed batch and finally applying it.
|
|
134
|
+
* This is a helper method for {@link #execute}.
|
|
135
|
+
*
|
|
136
|
+
* @param batchToUndo The batch to be undone.
|
|
137
|
+
* @param undoingBatch The batch that will contain undoing changes.
|
|
138
|
+
*/
|
|
139
|
+
_undo(batchToUndo, undoingBatch) {
|
|
140
|
+
const model = this.editor.model;
|
|
141
|
+
const document = model.document;
|
|
142
|
+
// All changes done by the command execution will be saved as one batch.
|
|
143
|
+
this._createdBatches.add(undoingBatch);
|
|
144
|
+
const operationsToUndo = batchToUndo.operations.slice().filter(operation => operation.isDocumentOperation);
|
|
145
|
+
operationsToUndo.reverse();
|
|
146
|
+
// We will process each operation from `batchToUndo`, in reverse order. If there were operations A, B and C in undone batch,
|
|
147
|
+
// we need to revert them in reverse order, so first C' (reversed C), then B', then A'.
|
|
148
|
+
for (const operationToUndo of operationsToUndo) {
|
|
149
|
+
const nextBaseVersion = operationToUndo.baseVersion + 1;
|
|
150
|
+
const historyOperations = Array.from(document.history.getOperations(nextBaseVersion));
|
|
151
|
+
const transformedSets = transformSets([operationToUndo.getReversed()], historyOperations, {
|
|
152
|
+
useRelations: true,
|
|
153
|
+
document: this.editor.model.document,
|
|
154
|
+
padWithNoOps: false,
|
|
155
|
+
forceWeakRemove: true
|
|
156
|
+
});
|
|
157
|
+
const reversedOperations = transformedSets.operationsA;
|
|
158
|
+
// After reversed operation has been transformed by all history operations, apply it.
|
|
159
|
+
for (let operation of reversedOperations) {
|
|
160
|
+
// Do not apply any operation on non-editable space.
|
|
161
|
+
const affectedSelectable = operation.affectedSelectable;
|
|
162
|
+
if (affectedSelectable && !model.canEditAt(affectedSelectable)) {
|
|
163
|
+
operation = new NoOperation(operation.baseVersion);
|
|
164
|
+
}
|
|
165
|
+
// Before applying, add the operation to the `undoingBatch`.
|
|
166
|
+
undoingBatch.addOperation(operation);
|
|
167
|
+
model.applyOperation(operation);
|
|
168
|
+
document.history.setOperationAsUndone(operationToUndo, operation);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Normalizes list of ranges by joining intersecting or "touching" ranges.
|
|
175
|
+
*
|
|
176
|
+
* @param ranges Ranges to be normalized.
|
|
177
|
+
*/
|
|
178
|
+
function normalizeRanges(ranges) {
|
|
179
|
+
ranges.sort((a, b) => a.start.isBefore(b.start) ? -1 : 1);
|
|
180
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
181
|
+
const previousRange = ranges[i - 1];
|
|
182
|
+
const joinedRange = previousRange.getJoined(ranges[i], true);
|
|
183
|
+
if (joinedRange) {
|
|
184
|
+
// Replace the ranges on the list with the new joined range.
|
|
185
|
+
i--;
|
|
186
|
+
ranges.splice(i, 2, joinedRange);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function isRangeContainedByAnyOtherRange(range, ranges) {
|
|
191
|
+
return ranges.some(otherRange => otherRange !== range && otherRange.containsRange(range, true));
|
|
192
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo
|
|
7
|
+
*/
|
|
8
|
+
export { default as Undo } from './undo.js';
|
|
9
|
+
export { default as UndoEditing } from './undoediting.js';
|
|
10
|
+
export { default as UndoUI } from './undoui.js';
|
|
11
|
+
export type { default as UndoCommand } from './undocommand.js';
|
|
12
|
+
export type { default as RedoCommand } from './redocommand.js';
|
|
13
|
+
import './augmentation.js';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo
|
|
7
|
+
*/
|
|
8
|
+
export { default as Undo } from './undo.js';
|
|
9
|
+
export { default as UndoEditing } from './undoediting.js';
|
|
10
|
+
export { default as UndoUI } from './undoui.js';
|
|
11
|
+
import './augmentation.js';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/redocommand
|
|
7
|
+
*/
|
|
8
|
+
import BaseCommand from './basecommand.js';
|
|
9
|
+
/**
|
|
10
|
+
* The redo command stores {@link module:engine/model/batch~Batch batches} that were used to undo a batch by
|
|
11
|
+
* {@link module:undo/undocommand~UndoCommand}. It is able to redo a previously undone batch by reversing the undoing
|
|
12
|
+
* batches created by `UndoCommand`. The reversed batch is transformed by all the batches from
|
|
13
|
+
* {@link module:engine/model/document~Document#history history} that happened after the reversed undo batch.
|
|
14
|
+
*
|
|
15
|
+
* The redo command also takes care of restoring the {@link module:engine/model/document~Document#selection document selection}.
|
|
16
|
+
*/
|
|
17
|
+
export default class RedoCommand extends BaseCommand {
|
|
18
|
+
/**
|
|
19
|
+
* Executes the command. This method reverts the last {@link module:engine/model/batch~Batch batch} added to
|
|
20
|
+
* the command's stack, applies the reverted and transformed version on the
|
|
21
|
+
* {@link module:engine/model/document~Document document} and removes the batch from the stack.
|
|
22
|
+
* Then, it restores the {@link module:engine/model/document~Document#selection document selection}.
|
|
23
|
+
*
|
|
24
|
+
* @fires execute
|
|
25
|
+
*/
|
|
26
|
+
execute(): void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/redocommand
|
|
7
|
+
*/
|
|
8
|
+
import BaseCommand from './basecommand.js';
|
|
9
|
+
/**
|
|
10
|
+
* The redo command stores {@link module:engine/model/batch~Batch batches} that were used to undo a batch by
|
|
11
|
+
* {@link module:undo/undocommand~UndoCommand}. It is able to redo a previously undone batch by reversing the undoing
|
|
12
|
+
* batches created by `UndoCommand`. The reversed batch is transformed by all the batches from
|
|
13
|
+
* {@link module:engine/model/document~Document#history history} that happened after the reversed undo batch.
|
|
14
|
+
*
|
|
15
|
+
* The redo command also takes care of restoring the {@link module:engine/model/document~Document#selection document selection}.
|
|
16
|
+
*/
|
|
17
|
+
export default class RedoCommand extends BaseCommand {
|
|
18
|
+
/**
|
|
19
|
+
* Executes the command. This method reverts the last {@link module:engine/model/batch~Batch batch} added to
|
|
20
|
+
* the command's stack, applies the reverted and transformed version on the
|
|
21
|
+
* {@link module:engine/model/document~Document document} and removes the batch from the stack.
|
|
22
|
+
* Then, it restores the {@link module:engine/model/document~Document#selection document selection}.
|
|
23
|
+
*
|
|
24
|
+
* @fires execute
|
|
25
|
+
*/
|
|
26
|
+
execute() {
|
|
27
|
+
const item = this._stack.pop();
|
|
28
|
+
const redoingBatch = this.editor.model.createBatch({ isUndo: true });
|
|
29
|
+
// All changes have to be done in one `enqueueChange` callback so other listeners will not step between consecutive
|
|
30
|
+
// operations, or won't do changes to the document before selection is properly restored.
|
|
31
|
+
this.editor.model.enqueueChange(redoingBatch, () => {
|
|
32
|
+
const lastOperation = item.batch.operations[item.batch.operations.length - 1];
|
|
33
|
+
const nextBaseVersion = lastOperation.baseVersion + 1;
|
|
34
|
+
const operations = this.editor.model.document.history.getOperations(nextBaseVersion);
|
|
35
|
+
this._restoreSelection(item.selection.ranges, item.selection.isBackward, operations);
|
|
36
|
+
this._undo(item.batch, redoingBatch);
|
|
37
|
+
});
|
|
38
|
+
this.refresh();
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/undo.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/undo
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
import UndoEditing from './undoediting.js';
|
|
10
|
+
import UndoUI from './undoui.js';
|
|
11
|
+
/**
|
|
12
|
+
* The undo feature.
|
|
13
|
+
*
|
|
14
|
+
* This is a "glue" plugin which loads the {@link module:undo/undoediting~UndoEditing undo editing feature}
|
|
15
|
+
* and the {@link module:undo/undoui~UndoUI undo UI feature}.
|
|
16
|
+
*
|
|
17
|
+
* Below is an explanation of the undo mechanism working together with {@link module:engine/model/history~History History}:
|
|
18
|
+
*
|
|
19
|
+
* Whenever an {@link module:engine/model/operation/operation~Operation operation} is applied to the
|
|
20
|
+
* {@link module:engine/model/document~Document document}, it is saved to `History` as is.
|
|
21
|
+
* The {@link module:engine/model/batch~Batch batch} that owns that operation is also saved, in
|
|
22
|
+
* {@link module:undo/undocommand~UndoCommand}, together with the selection that was present in the document before the
|
|
23
|
+
* operation was applied. A batch is saved instead of the operation because changes are undone batch-by-batch, not operation-by-operation
|
|
24
|
+
* and a batch is seen as one undo step.
|
|
25
|
+
*
|
|
26
|
+
* After changes happen to the document, the `History` and `UndoCommand` stack can be represented as follows:
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
* History Undo stack
|
|
30
|
+
* ============== ==================================
|
|
31
|
+
* [operation A1] [ batch A ]
|
|
32
|
+
* [operation B1] [ batch B ]
|
|
33
|
+
* [operation B2] [ batch C ]
|
|
34
|
+
* [operation C1]
|
|
35
|
+
* [operation C2]
|
|
36
|
+
* [operation B3]
|
|
37
|
+
* [operation C3]
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Where operations starting with the same letter are from same batch.
|
|
41
|
+
*
|
|
42
|
+
* Undoing a batch means that a set of operations which will reverse the effects of that batch needs to be generated.
|
|
43
|
+
* For example, if a batch added several letters, undoing the batch should remove them. It is important to apply undoing
|
|
44
|
+
* operations in the reversed order, so if a batch has operation `X`, `Y`, `Z`, reversed operations `Zr`, `Yr` and `Xr`
|
|
45
|
+
* need to be applied. Otherwise reversed operation `Xr` would operate on a wrong document state, because operation `X`
|
|
46
|
+
* does not know that operations `Y` and `Z` happened.
|
|
47
|
+
*
|
|
48
|
+
* After operations from an undone batch got {@link module:engine/model/operation/operation~Operation#getReversed reversed},
|
|
49
|
+
* one needs to make sure if they are ready to be applied. In the scenario above, operation `C3` is the last operation and `C3r`
|
|
50
|
+
* bases on up-to-date document state, so it can be applied to the document.
|
|
51
|
+
*
|
|
52
|
+
* ```
|
|
53
|
+
* History Undo stack
|
|
54
|
+
* ================= ==================================
|
|
55
|
+
* [ operation A1 ] [ batch A ]
|
|
56
|
+
* [ operation B1 ] [ batch B ]
|
|
57
|
+
* [ operation B2 ] [ processing undoing batch C ]
|
|
58
|
+
* [ operation C1 ]
|
|
59
|
+
* [ operation C2 ]
|
|
60
|
+
* [ operation B3 ]
|
|
61
|
+
* [ operation C3 ]
|
|
62
|
+
* [ operation C3r ]
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* Next is operation `C2`, reversed to `C2r`. `C2r` bases on `C2`, so it bases on the wrong document state. It needs to be
|
|
66
|
+
* transformed by operations from history that happened after it, so it "knows" about them. Let us assume that `C2' = C2r * B3 * C3 * C3r`,
|
|
67
|
+
* where `*` means "transformed by". Rest of operations from that batch are processed in the same fashion.
|
|
68
|
+
*
|
|
69
|
+
* ```
|
|
70
|
+
* History Undo stack Redo stack
|
|
71
|
+
* ================= ================================== ==================================
|
|
72
|
+
* [ operation A1 ] [ batch A ] [ batch Cr ]
|
|
73
|
+
* [ operation B1 ] [ batch B ]
|
|
74
|
+
* [ operation B2 ]
|
|
75
|
+
* [ operation C1 ]
|
|
76
|
+
* [ operation C2 ]
|
|
77
|
+
* [ operation B3 ]
|
|
78
|
+
* [ operation C3 ]
|
|
79
|
+
* [ operation C3r ]
|
|
80
|
+
* [ operation C2' ]
|
|
81
|
+
* [ operation C1' ]
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* Selective undo works on the same basis, however, instead of undoing the last batch in the undo stack, any batch can be undone.
|
|
85
|
+
* The same algorithm applies: operations from a batch (i.e. `A1`) are reversed and then transformed by operations stored in history.
|
|
86
|
+
*
|
|
87
|
+
* Redo also is very similar to undo. It has its own stack that is filled with undoing (reversed batches). Operations from
|
|
88
|
+
* the batch that is re-done are reversed-back, transformed in proper order and applied to the document.
|
|
89
|
+
*
|
|
90
|
+
* ```
|
|
91
|
+
* History Undo stack Redo stack
|
|
92
|
+
* ================= ================================== ==================================
|
|
93
|
+
* [ operation A1 ] [ batch A ]
|
|
94
|
+
* [ operation B1 ] [ batch B ]
|
|
95
|
+
* [ operation B2 ] [ batch Crr ]
|
|
96
|
+
* [ operation C1 ]
|
|
97
|
+
* [ operation C2 ]
|
|
98
|
+
* [ operation B3 ]
|
|
99
|
+
* [ operation C3 ]
|
|
100
|
+
* [ operation C3r ]
|
|
101
|
+
* [ operation C2' ]
|
|
102
|
+
* [ operation C1' ]
|
|
103
|
+
* [ operation C1'r]
|
|
104
|
+
* [ operation C2'r]
|
|
105
|
+
* [ operation C3rr]
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export default class Undo extends Plugin {
|
|
109
|
+
/**
|
|
110
|
+
* @inheritDoc
|
|
111
|
+
*/
|
|
112
|
+
static get requires(): readonly [typeof UndoEditing, typeof UndoUI];
|
|
113
|
+
/**
|
|
114
|
+
* @inheritDoc
|
|
115
|
+
*/
|
|
116
|
+
static get pluginName(): "Undo";
|
|
117
|
+
/**
|
|
118
|
+
* @inheritDoc
|
|
119
|
+
*/
|
|
120
|
+
static get isOfficialPlugin(): true;
|
|
121
|
+
}
|
package/src/undo.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/undo
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
import UndoEditing from './undoediting.js';
|
|
10
|
+
import UndoUI from './undoui.js';
|
|
11
|
+
/**
|
|
12
|
+
* The undo feature.
|
|
13
|
+
*
|
|
14
|
+
* This is a "glue" plugin which loads the {@link module:undo/undoediting~UndoEditing undo editing feature}
|
|
15
|
+
* and the {@link module:undo/undoui~UndoUI undo UI feature}.
|
|
16
|
+
*
|
|
17
|
+
* Below is an explanation of the undo mechanism working together with {@link module:engine/model/history~History History}:
|
|
18
|
+
*
|
|
19
|
+
* Whenever an {@link module:engine/model/operation/operation~Operation operation} is applied to the
|
|
20
|
+
* {@link module:engine/model/document~Document document}, it is saved to `History` as is.
|
|
21
|
+
* The {@link module:engine/model/batch~Batch batch} that owns that operation is also saved, in
|
|
22
|
+
* {@link module:undo/undocommand~UndoCommand}, together with the selection that was present in the document before the
|
|
23
|
+
* operation was applied. A batch is saved instead of the operation because changes are undone batch-by-batch, not operation-by-operation
|
|
24
|
+
* and a batch is seen as one undo step.
|
|
25
|
+
*
|
|
26
|
+
* After changes happen to the document, the `History` and `UndoCommand` stack can be represented as follows:
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
* History Undo stack
|
|
30
|
+
* ============== ==================================
|
|
31
|
+
* [operation A1] [ batch A ]
|
|
32
|
+
* [operation B1] [ batch B ]
|
|
33
|
+
* [operation B2] [ batch C ]
|
|
34
|
+
* [operation C1]
|
|
35
|
+
* [operation C2]
|
|
36
|
+
* [operation B3]
|
|
37
|
+
* [operation C3]
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Where operations starting with the same letter are from same batch.
|
|
41
|
+
*
|
|
42
|
+
* Undoing a batch means that a set of operations which will reverse the effects of that batch needs to be generated.
|
|
43
|
+
* For example, if a batch added several letters, undoing the batch should remove them. It is important to apply undoing
|
|
44
|
+
* operations in the reversed order, so if a batch has operation `X`, `Y`, `Z`, reversed operations `Zr`, `Yr` and `Xr`
|
|
45
|
+
* need to be applied. Otherwise reversed operation `Xr` would operate on a wrong document state, because operation `X`
|
|
46
|
+
* does not know that operations `Y` and `Z` happened.
|
|
47
|
+
*
|
|
48
|
+
* After operations from an undone batch got {@link module:engine/model/operation/operation~Operation#getReversed reversed},
|
|
49
|
+
* one needs to make sure if they are ready to be applied. In the scenario above, operation `C3` is the last operation and `C3r`
|
|
50
|
+
* bases on up-to-date document state, so it can be applied to the document.
|
|
51
|
+
*
|
|
52
|
+
* ```
|
|
53
|
+
* History Undo stack
|
|
54
|
+
* ================= ==================================
|
|
55
|
+
* [ operation A1 ] [ batch A ]
|
|
56
|
+
* [ operation B1 ] [ batch B ]
|
|
57
|
+
* [ operation B2 ] [ processing undoing batch C ]
|
|
58
|
+
* [ operation C1 ]
|
|
59
|
+
* [ operation C2 ]
|
|
60
|
+
* [ operation B3 ]
|
|
61
|
+
* [ operation C3 ]
|
|
62
|
+
* [ operation C3r ]
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* Next is operation `C2`, reversed to `C2r`. `C2r` bases on `C2`, so it bases on the wrong document state. It needs to be
|
|
66
|
+
* transformed by operations from history that happened after it, so it "knows" about them. Let us assume that `C2' = C2r * B3 * C3 * C3r`,
|
|
67
|
+
* where `*` means "transformed by". Rest of operations from that batch are processed in the same fashion.
|
|
68
|
+
*
|
|
69
|
+
* ```
|
|
70
|
+
* History Undo stack Redo stack
|
|
71
|
+
* ================= ================================== ==================================
|
|
72
|
+
* [ operation A1 ] [ batch A ] [ batch Cr ]
|
|
73
|
+
* [ operation B1 ] [ batch B ]
|
|
74
|
+
* [ operation B2 ]
|
|
75
|
+
* [ operation C1 ]
|
|
76
|
+
* [ operation C2 ]
|
|
77
|
+
* [ operation B3 ]
|
|
78
|
+
* [ operation C3 ]
|
|
79
|
+
* [ operation C3r ]
|
|
80
|
+
* [ operation C2' ]
|
|
81
|
+
* [ operation C1' ]
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* Selective undo works on the same basis, however, instead of undoing the last batch in the undo stack, any batch can be undone.
|
|
85
|
+
* The same algorithm applies: operations from a batch (i.e. `A1`) are reversed and then transformed by operations stored in history.
|
|
86
|
+
*
|
|
87
|
+
* Redo also is very similar to undo. It has its own stack that is filled with undoing (reversed batches). Operations from
|
|
88
|
+
* the batch that is re-done are reversed-back, transformed in proper order and applied to the document.
|
|
89
|
+
*
|
|
90
|
+
* ```
|
|
91
|
+
* History Undo stack Redo stack
|
|
92
|
+
* ================= ================================== ==================================
|
|
93
|
+
* [ operation A1 ] [ batch A ]
|
|
94
|
+
* [ operation B1 ] [ batch B ]
|
|
95
|
+
* [ operation B2 ] [ batch Crr ]
|
|
96
|
+
* [ operation C1 ]
|
|
97
|
+
* [ operation C2 ]
|
|
98
|
+
* [ operation B3 ]
|
|
99
|
+
* [ operation C3 ]
|
|
100
|
+
* [ operation C3r ]
|
|
101
|
+
* [ operation C2' ]
|
|
102
|
+
* [ operation C1' ]
|
|
103
|
+
* [ operation C1'r]
|
|
104
|
+
* [ operation C2'r]
|
|
105
|
+
* [ operation C3rr]
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export default class Undo extends Plugin {
|
|
109
|
+
/**
|
|
110
|
+
* @inheritDoc
|
|
111
|
+
*/
|
|
112
|
+
static get requires() {
|
|
113
|
+
return [UndoEditing, UndoUI];
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @inheritDoc
|
|
117
|
+
*/
|
|
118
|
+
static get pluginName() {
|
|
119
|
+
return 'Undo';
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @inheritDoc
|
|
123
|
+
*/
|
|
124
|
+
static get isOfficialPlugin() {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/undocommand
|
|
7
|
+
*/
|
|
8
|
+
import BaseCommand from './basecommand.js';
|
|
9
|
+
import type { Batch } from '@ckeditor/ckeditor5-engine';
|
|
10
|
+
/**
|
|
11
|
+
* The undo command stores {@link module:engine/model/batch~Batch batches} applied to the
|
|
12
|
+
* {@link module:engine/model/document~Document document} and is able to undo a batch by reversing it and transforming by
|
|
13
|
+
* batches from {@link module:engine/model/document~Document#history history} that happened after the reversed batch.
|
|
14
|
+
*
|
|
15
|
+
* The undo command also takes care of restoring the {@link module:engine/model/document~Document#selection document selection}.
|
|
16
|
+
*/
|
|
17
|
+
export default class UndoCommand extends BaseCommand {
|
|
18
|
+
/**
|
|
19
|
+
* Executes the command. This method reverts a {@link module:engine/model/batch~Batch batch} added to the command's stack, transforms
|
|
20
|
+
* and applies the reverted version on the {@link module:engine/model/document~Document document} and removes the batch from the stack.
|
|
21
|
+
* Then, it restores the {@link module:engine/model/document~Document#selection document selection}.
|
|
22
|
+
*
|
|
23
|
+
* @fires execute
|
|
24
|
+
* @fires revert
|
|
25
|
+
* @param batch A batch that should be undone. If not set, the last added batch will be undone.
|
|
26
|
+
*/
|
|
27
|
+
execute(batch?: Batch | null): void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Fired when execution of the command reverts some batch.
|
|
31
|
+
*
|
|
32
|
+
* @eventName ~UndoCommand#revert
|
|
33
|
+
*/
|
|
34
|
+
export type UndoCommandRevertEvent = {
|
|
35
|
+
name: 'revert';
|
|
36
|
+
args: [batch: Batch, undoingBatch: Batch];
|
|
37
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/undocommand
|
|
7
|
+
*/
|
|
8
|
+
import BaseCommand from './basecommand.js';
|
|
9
|
+
/**
|
|
10
|
+
* The undo command stores {@link module:engine/model/batch~Batch batches} applied to the
|
|
11
|
+
* {@link module:engine/model/document~Document document} and is able to undo a batch by reversing it and transforming by
|
|
12
|
+
* batches from {@link module:engine/model/document~Document#history history} that happened after the reversed batch.
|
|
13
|
+
*
|
|
14
|
+
* The undo command also takes care of restoring the {@link module:engine/model/document~Document#selection document selection}.
|
|
15
|
+
*/
|
|
16
|
+
export default class UndoCommand extends BaseCommand {
|
|
17
|
+
/**
|
|
18
|
+
* Executes the command. This method reverts a {@link module:engine/model/batch~Batch batch} added to the command's stack, transforms
|
|
19
|
+
* and applies the reverted version on the {@link module:engine/model/document~Document document} and removes the batch from the stack.
|
|
20
|
+
* Then, it restores the {@link module:engine/model/document~Document#selection document selection}.
|
|
21
|
+
*
|
|
22
|
+
* @fires execute
|
|
23
|
+
* @fires revert
|
|
24
|
+
* @param batch A batch that should be undone. If not set, the last added batch will be undone.
|
|
25
|
+
*/
|
|
26
|
+
execute(batch = null) {
|
|
27
|
+
// If batch is not given, set `batchIndex` to the last index in command stack.
|
|
28
|
+
const batchIndex = batch ? this._stack.findIndex(a => a.batch == batch) : this._stack.length - 1;
|
|
29
|
+
const item = this._stack.splice(batchIndex, 1)[0];
|
|
30
|
+
const undoingBatch = this.editor.model.createBatch({ isUndo: true });
|
|
31
|
+
// All changes have to be done in one `enqueueChange` callback so other listeners will not
|
|
32
|
+
// step between consecutive operations, or won't do changes to the document before selection is properly restored.
|
|
33
|
+
this.editor.model.enqueueChange(undoingBatch, () => {
|
|
34
|
+
this._undo(item.batch, undoingBatch);
|
|
35
|
+
const operations = this.editor.model.document.history.getOperations(item.batch.baseVersion);
|
|
36
|
+
this._restoreSelection(item.selection.ranges, item.selection.isBackward, operations);
|
|
37
|
+
});
|
|
38
|
+
// Firing `revert` event after the change block to make sure that it includes all changes from post-fixers
|
|
39
|
+
// and make sure that the selection is "stabilized" (the selection range is saved after undo is executed and then
|
|
40
|
+
// restored on redo, so it is important that the selection range is saved after post-fixers are done).
|
|
41
|
+
this.fire('revert', item.batch, undoingBatch);
|
|
42
|
+
this.refresh();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module undo/undoediting
|
|
7
|
+
*/
|
|
8
|
+
import { Plugin } from '@ckeditor/ckeditor5-core';
|
|
9
|
+
/**
|
|
10
|
+
* The undo engine feature.
|
|
11
|
+
*
|
|
12
|
+
* It introduces the `'undo'` and `'redo'` commands to the editor.
|
|
13
|
+
*/
|
|
14
|
+
export default class UndoEditing extends Plugin {
|
|
15
|
+
/**
|
|
16
|
+
* The command that manages the undo {@link module:engine/model/batch~Batch batches} stack (history).
|
|
17
|
+
* Created and registered during the {@link #init feature initialization}.
|
|
18
|
+
*/
|
|
19
|
+
private _undoCommand;
|
|
20
|
+
/**
|
|
21
|
+
* The command that manages the redo {@link module:engine/model/batch~Batch batches} stack (history).
|
|
22
|
+
* Created and registered during the {@link #init feature initialization}.
|
|
23
|
+
*/
|
|
24
|
+
private _redoCommand;
|
|
25
|
+
/**
|
|
26
|
+
* Keeps track of which batches were registered in undo.
|
|
27
|
+
*/
|
|
28
|
+
private _batchRegistry;
|
|
29
|
+
/**
|
|
30
|
+
* @inheritDoc
|
|
31
|
+
*/
|
|
32
|
+
static get pluginName(): "UndoEditing";
|
|
33
|
+
/**
|
|
34
|
+
* @inheritDoc
|
|
35
|
+
*/
|
|
36
|
+
static get isOfficialPlugin(): true;
|
|
37
|
+
/**
|
|
38
|
+
* @inheritDoc
|
|
39
|
+
*/
|
|
40
|
+
init(): void;
|
|
41
|
+
}
|