@ckeditor/ckeditor5-undo 48.2.0 → 48.3.0-alpha.1
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/dist/augmentation.d.ts +14 -14
- package/dist/basecommand.d.ts +73 -73
- package/dist/index-content.css +1 -0
- package/dist/index-editor.css +1 -0
- package/dist/index.css +0 -2
- package/dist/index.d.ts +12 -12
- package/dist/index.js +502 -530
- package/dist/index.js.map +1 -1
- package/dist/redocommand.d.ts +22 -22
- package/dist/undo.d.ts +116 -116
- package/dist/undocommand.d.ts +23 -23
- package/dist/undoediting.d.ts +36 -36
- package/dist/undoui.d.ts +33 -33
- package/package.json +6 -6
- package/dist/index.css.map +0 -1
package/dist/index.js
CHANGED
|
@@ -2,551 +2,523 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
4
|
*/
|
|
5
|
-
import { Command, Plugin } from
|
|
6
|
-
import {
|
|
7
|
-
import { ButtonView, MenuBarMenuListItemButtonView } from
|
|
8
|
-
import {
|
|
5
|
+
import { Command, Plugin } from "@ckeditor/ckeditor5-core";
|
|
6
|
+
import { NoOperation, transformOperationSets } from "@ckeditor/ckeditor5-engine";
|
|
7
|
+
import { ButtonView, MenuBarMenuListItemButtonView } from "@ckeditor/ckeditor5-ui";
|
|
8
|
+
import { IconRedo, IconUndo } from "@ckeditor/ckeditor5-icons";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this._createdBatches.add(undoingBatch);
|
|
143
|
-
const operationsToUndo = batchToUndo.operations.slice().filter((operation)=>operation.isDocumentOperation);
|
|
144
|
-
operationsToUndo.reverse();
|
|
145
|
-
// We will process each operation from `batchToUndo`, in reverse order. If there were operations A, B and C in undone batch,
|
|
146
|
-
// we need to revert them in reverse order, so first C' (reversed C), then B', then A'.
|
|
147
|
-
for (const operationToUndo of operationsToUndo){
|
|
148
|
-
const nextBaseVersion = operationToUndo.baseVersion + 1;
|
|
149
|
-
const historyOperations = Array.from(document.history.getOperations(nextBaseVersion));
|
|
150
|
-
const transformedSets = transformOperationSets([
|
|
151
|
-
operationToUndo.getReversed()
|
|
152
|
-
], historyOperations, {
|
|
153
|
-
useRelations: true,
|
|
154
|
-
document: this.editor.model.document,
|
|
155
|
-
padWithNoOps: false,
|
|
156
|
-
forceWeakRemove: true
|
|
157
|
-
});
|
|
158
|
-
const reversedOperations = transformedSets.operationsA;
|
|
159
|
-
// After reversed operation has been transformed by all history operations, apply it.
|
|
160
|
-
for (let operation of reversedOperations){
|
|
161
|
-
// Do not apply any operation on non-editable space.
|
|
162
|
-
const affectedSelectable = operation.affectedSelectable;
|
|
163
|
-
if (affectedSelectable && !model.canEditAt(affectedSelectable)) {
|
|
164
|
-
operation = new NoOperation(operation.baseVersion);
|
|
165
|
-
}
|
|
166
|
-
// Before applying, add the operation to the `undoingBatch`.
|
|
167
|
-
undoingBatch.addOperation(operation);
|
|
168
|
-
model.applyOperation(operation);
|
|
169
|
-
document.history.setOperationAsUndone(operationToUndo, operation);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
11
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
12
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* @module undo/basecommand
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Base class for the undo feature commands: {@link module:undo/undocommand~UndoCommand} and {@link module:undo/redocommand~RedoCommand}.
|
|
19
|
+
*/
|
|
20
|
+
var UndoRedoBaseCommand = class extends Command {
|
|
21
|
+
/**
|
|
22
|
+
* Stack of items stored by the command. These are pairs of:
|
|
23
|
+
*
|
|
24
|
+
* * {@link module:engine/model/batch~Batch batch} saved by the command,
|
|
25
|
+
* * {@link module:engine/model/selection~ModelSelection selection} state at the moment of saving the batch.
|
|
26
|
+
*/
|
|
27
|
+
_stack = [];
|
|
28
|
+
/**
|
|
29
|
+
* Stores all batches that were created by this command.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
_createdBatches = /* @__PURE__ */ new WeakSet();
|
|
34
|
+
/**
|
|
35
|
+
* @inheritDoc
|
|
36
|
+
*/
|
|
37
|
+
constructor(editor) {
|
|
38
|
+
super(editor);
|
|
39
|
+
this.refresh();
|
|
40
|
+
this._isEnabledBasedOnSelection = false;
|
|
41
|
+
this.listenTo(editor.data, "set", (evt, data) => {
|
|
42
|
+
data[1] = { ...data[1] };
|
|
43
|
+
const options = data[1];
|
|
44
|
+
if (!options.batchType) options.batchType = { isUndoable: false };
|
|
45
|
+
}, { priority: "high" });
|
|
46
|
+
this.listenTo(editor.data, "set", (evt, data) => {
|
|
47
|
+
if (!data[1].batchType.isUndoable) this.clearStack();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @inheritDoc
|
|
52
|
+
*/
|
|
53
|
+
refresh() {
|
|
54
|
+
this.isEnabled = this._stack.length > 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns all batches created by this command.
|
|
58
|
+
*/
|
|
59
|
+
get createdBatches() {
|
|
60
|
+
return this._createdBatches;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Stores a batch in the command, together with the selection state of the {@link module:engine/model/document~ModelDocument document}
|
|
64
|
+
* created by the editor which this command is registered to.
|
|
65
|
+
*
|
|
66
|
+
* @param batch The batch to add.
|
|
67
|
+
*/
|
|
68
|
+
addBatch(batch) {
|
|
69
|
+
const docSelection = this.editor.model.document.selection;
|
|
70
|
+
const selection = {
|
|
71
|
+
ranges: docSelection.hasOwnRange ? Array.from(docSelection.getRanges()) : [],
|
|
72
|
+
isBackward: docSelection.isBackward
|
|
73
|
+
};
|
|
74
|
+
this._stack.push({
|
|
75
|
+
batch,
|
|
76
|
+
selection
|
|
77
|
+
});
|
|
78
|
+
this.refresh();
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Removes all items from the stack.
|
|
82
|
+
*/
|
|
83
|
+
clearStack() {
|
|
84
|
+
this._stack = [];
|
|
85
|
+
this.refresh();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Restores the {@link module:engine/model/document~ModelDocument#selection document selection} state after a batch was undone.
|
|
89
|
+
*
|
|
90
|
+
* @param ranges Ranges to be restored.
|
|
91
|
+
* @param isBackward A flag describing whether the restored range was selected forward or backward.
|
|
92
|
+
* @param operations Operations which has been applied since selection has been stored.
|
|
93
|
+
*/
|
|
94
|
+
_restoreSelection(ranges, isBackward, operations) {
|
|
95
|
+
const model = this.editor.model;
|
|
96
|
+
const document = model.document;
|
|
97
|
+
const selectionRanges = [];
|
|
98
|
+
const transformedRangeGroups = ranges.map((range) => range.getTransformedByOperations(operations));
|
|
99
|
+
const allRanges = transformedRangeGroups.flat();
|
|
100
|
+
for (const rangeGroup of transformedRangeGroups) {
|
|
101
|
+
const transformed = rangeGroup.filter((range) => range.root != document.graveyard).filter((range) => !isRangeContainedByAnyOtherRange(range, allRanges));
|
|
102
|
+
if (!transformed.length) continue;
|
|
103
|
+
normalizeRanges(transformed);
|
|
104
|
+
selectionRanges.push(transformed[0]);
|
|
105
|
+
}
|
|
106
|
+
if (selectionRanges.length) model.change((writer) => {
|
|
107
|
+
writer.setSelection(selectionRanges, { backward: isBackward });
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Undoes a batch by reversing that batch, transforming reversed batch and finally applying it.
|
|
112
|
+
* This is a helper method for {@link #execute}.
|
|
113
|
+
*
|
|
114
|
+
* @param batchToUndo The batch to be undone.
|
|
115
|
+
* @param undoingBatch The batch that will contain undoing changes.
|
|
116
|
+
*/
|
|
117
|
+
_undo(batchToUndo, undoingBatch) {
|
|
118
|
+
const model = this.editor.model;
|
|
119
|
+
const document = model.document;
|
|
120
|
+
this._createdBatches.add(undoingBatch);
|
|
121
|
+
const operationsToUndo = batchToUndo.operations.slice().filter((operation) => operation.isDocumentOperation);
|
|
122
|
+
operationsToUndo.reverse();
|
|
123
|
+
for (const operationToUndo of operationsToUndo) {
|
|
124
|
+
const nextBaseVersion = operationToUndo.baseVersion + 1;
|
|
125
|
+
const historyOperations = Array.from(document.history.getOperations(nextBaseVersion));
|
|
126
|
+
const reversedOperations = transformOperationSets([operationToUndo.getReversed()], historyOperations, {
|
|
127
|
+
useRelations: true,
|
|
128
|
+
document: this.editor.model.document,
|
|
129
|
+
padWithNoOps: false,
|
|
130
|
+
forceWeakRemove: true
|
|
131
|
+
}).operationsA;
|
|
132
|
+
for (let operation of reversedOperations) {
|
|
133
|
+
const affectedSelectable = operation.affectedSelectable;
|
|
134
|
+
if (affectedSelectable && !model.canEditAt(affectedSelectable)) operation = new NoOperation(operation.baseVersion);
|
|
135
|
+
undoingBatch.addOperation(operation);
|
|
136
|
+
model.applyOperation(operation);
|
|
137
|
+
document.history.setOperationAsUndone(operationToUndo, operation);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
174
142
|
/**
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
143
|
+
* Normalizes list of ranges by joining intersecting or "touching" ranges.
|
|
144
|
+
*
|
|
145
|
+
* @param ranges Ranges to be normalized.
|
|
146
|
+
*/
|
|
147
|
+
function normalizeRanges(ranges) {
|
|
148
|
+
ranges.sort((a, b) => a.start.isBefore(b.start) ? -1 : 1);
|
|
149
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
150
|
+
const joinedRange = ranges[i - 1].getJoined(ranges[i], true);
|
|
151
|
+
if (joinedRange) {
|
|
152
|
+
i--;
|
|
153
|
+
ranges.splice(i, 2, joinedRange);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
189
156
|
}
|
|
190
157
|
function isRangeContainedByAnyOtherRange(range, ranges) {
|
|
191
|
-
|
|
158
|
+
return ranges.some((otherRange) => otherRange !== range && otherRange.containsRange(range, true));
|
|
192
159
|
}
|
|
193
160
|
|
|
194
161
|
/**
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
162
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
163
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
164
|
+
*/
|
|
165
|
+
/**
|
|
166
|
+
* @module undo/undocommand
|
|
167
|
+
*/
|
|
168
|
+
/**
|
|
169
|
+
* The undo command stores {@link module:engine/model/batch~Batch batches} applied to the
|
|
170
|
+
* {@link module:engine/model/document~ModelDocument document} and is able to undo a batch by reversing it and transforming by
|
|
171
|
+
* batches from {@link module:engine/model/document~ModelDocument#history history} that happened after the reversed batch.
|
|
172
|
+
*
|
|
173
|
+
* The undo command also takes care of restoring the {@link module:engine/model/document~ModelDocument#selection document selection}.
|
|
174
|
+
*/
|
|
175
|
+
var UndoCommand = class extends UndoRedoBaseCommand {
|
|
176
|
+
/**
|
|
177
|
+
* Executes the command. This method reverts a {@link module:engine/model/batch~Batch batch} added to the command's stack, transforms
|
|
178
|
+
* and applies the reverted version on the {@link module:engine/model/document~ModelDocument document} and removes the batch from
|
|
179
|
+
* the stack. Then, it restores the {@link module:engine/model/document~ModelDocument#selection document selection}.
|
|
180
|
+
*
|
|
181
|
+
* @fires execute
|
|
182
|
+
* @fires revert
|
|
183
|
+
* @param batch A batch that should be undone. If not set, the last added batch will be undone.
|
|
184
|
+
*/
|
|
185
|
+
execute(batch = null) {
|
|
186
|
+
const batchIndex = batch ? this._stack.findIndex((a) => a.batch == batch) : this._stack.length - 1;
|
|
187
|
+
const item = this._stack.splice(batchIndex, 1)[0];
|
|
188
|
+
const undoingBatch = this.editor.model.createBatch({ isUndo: true });
|
|
189
|
+
this.editor.model.enqueueChange(undoingBatch, () => {
|
|
190
|
+
this._undo(item.batch, undoingBatch);
|
|
191
|
+
const operations = this.editor.model.document.history.getOperations(item.batch.baseVersion);
|
|
192
|
+
this._restoreSelection(item.selection.ranges, item.selection.isBackward, operations);
|
|
193
|
+
});
|
|
194
|
+
this.fire("revert", item.batch, undoingBatch);
|
|
195
|
+
this.refresh();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
230
198
|
|
|
231
199
|
/**
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
200
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
201
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
202
|
+
*/
|
|
203
|
+
/**
|
|
204
|
+
* @module undo/redocommand
|
|
205
|
+
*/
|
|
206
|
+
/**
|
|
207
|
+
* The redo command stores {@link module:engine/model/batch~Batch batches} that were used to undo a batch by
|
|
208
|
+
* {@link module:undo/undocommand~UndoCommand}. It is able to redo a previously undone batch by reversing the undoing
|
|
209
|
+
* batches created by `UndoCommand`. The reversed batch is transformed by all the batches from
|
|
210
|
+
* {@link module:engine/model/document~ModelDocument#history history} that happened after the reversed undo batch.
|
|
211
|
+
*
|
|
212
|
+
* The redo command also takes care of restoring the {@link module:engine/model/document~ModelDocument#selection document selection}.
|
|
213
|
+
*/
|
|
214
|
+
var RedoCommand = class extends UndoRedoBaseCommand {
|
|
215
|
+
/**
|
|
216
|
+
* Executes the command. This method reverts the last {@link module:engine/model/batch~Batch batch} added to
|
|
217
|
+
* the command's stack, applies the reverted and transformed version on the
|
|
218
|
+
* {@link module:engine/model/document~ModelDocument document} and removes the batch from the stack.
|
|
219
|
+
* Then, it restores the {@link module:engine/model/document~ModelDocument#selection document selection}.
|
|
220
|
+
*
|
|
221
|
+
* @fires execute
|
|
222
|
+
*/
|
|
223
|
+
execute() {
|
|
224
|
+
const item = this._stack.pop();
|
|
225
|
+
const redoingBatch = this.editor.model.createBatch({ isUndo: true });
|
|
226
|
+
this.editor.model.enqueueChange(redoingBatch, () => {
|
|
227
|
+
const nextBaseVersion = item.batch.operations[item.batch.operations.length - 1].baseVersion + 1;
|
|
228
|
+
const operations = this.editor.model.document.history.getOperations(nextBaseVersion);
|
|
229
|
+
this._restoreSelection(item.selection.ranges, item.selection.isBackward, operations);
|
|
230
|
+
this._undo(item.batch, redoingBatch);
|
|
231
|
+
});
|
|
232
|
+
this.fire("revert", item.batch, redoingBatch);
|
|
233
|
+
this.refresh();
|
|
234
|
+
}
|
|
235
|
+
};
|
|
267
236
|
|
|
268
237
|
/**
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
editor.accessibility.addKeystrokeInfos({
|
|
348
|
-
keystrokes: [
|
|
349
|
-
{
|
|
350
|
-
label: t('Undo'),
|
|
351
|
-
keystroke: 'CTRL+Z'
|
|
352
|
-
},
|
|
353
|
-
{
|
|
354
|
-
label: t('Redo'),
|
|
355
|
-
keystroke: [
|
|
356
|
-
[
|
|
357
|
-
'CTRL+Y'
|
|
358
|
-
],
|
|
359
|
-
[
|
|
360
|
-
'CTRL+SHIFT+Z'
|
|
361
|
-
]
|
|
362
|
-
]
|
|
363
|
-
}
|
|
364
|
-
]
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
}
|
|
238
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
239
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
240
|
+
*/
|
|
241
|
+
/**
|
|
242
|
+
* @module undo/undoediting
|
|
243
|
+
*/
|
|
244
|
+
/**
|
|
245
|
+
* The undo engine feature.
|
|
246
|
+
*
|
|
247
|
+
* It introduces the `'undo'` and `'redo'` commands to the editor.
|
|
248
|
+
*/
|
|
249
|
+
var UndoEditing = class extends Plugin {
|
|
250
|
+
/**
|
|
251
|
+
* The command that manages the undo {@link module:engine/model/batch~Batch batches} stack (history).
|
|
252
|
+
* Created and registered during the {@link #init feature initialization}.
|
|
253
|
+
*/
|
|
254
|
+
_undoCommand;
|
|
255
|
+
/**
|
|
256
|
+
* The command that manages the redo {@link module:engine/model/batch~Batch batches} stack (history).
|
|
257
|
+
* Created and registered during the {@link #init feature initialization}.
|
|
258
|
+
*/
|
|
259
|
+
_redoCommand;
|
|
260
|
+
/**
|
|
261
|
+
* Keeps track of which batches were registered in undo.
|
|
262
|
+
*/
|
|
263
|
+
_batchRegistry = /* @__PURE__ */ new WeakSet();
|
|
264
|
+
/**
|
|
265
|
+
* @inheritDoc
|
|
266
|
+
*/
|
|
267
|
+
static get pluginName() {
|
|
268
|
+
return "UndoEditing";
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* @inheritDoc
|
|
272
|
+
*/
|
|
273
|
+
static get isOfficialPlugin() {
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* @inheritDoc
|
|
278
|
+
*/
|
|
279
|
+
init() {
|
|
280
|
+
const editor = this.editor;
|
|
281
|
+
const t = editor.t;
|
|
282
|
+
this._undoCommand = new UndoCommand(editor);
|
|
283
|
+
this._redoCommand = new RedoCommand(editor);
|
|
284
|
+
editor.commands.add("undo", this._undoCommand);
|
|
285
|
+
editor.commands.add("redo", this._redoCommand);
|
|
286
|
+
this.listenTo(editor.model, "applyOperation", (evt, args) => {
|
|
287
|
+
const operation = args[0];
|
|
288
|
+
if (!operation.isDocumentOperation) return;
|
|
289
|
+
const batch = operation.batch;
|
|
290
|
+
const isRedoBatch = this._redoCommand.createdBatches.has(batch);
|
|
291
|
+
const isUndoBatch = this._undoCommand.createdBatches.has(batch);
|
|
292
|
+
if (this._batchRegistry.has(batch)) return;
|
|
293
|
+
this._batchRegistry.add(batch);
|
|
294
|
+
if (!batch.isUndoable) return;
|
|
295
|
+
if (isRedoBatch) this._undoCommand.addBatch(batch);
|
|
296
|
+
else if (!isUndoBatch) {
|
|
297
|
+
this._undoCommand.addBatch(batch);
|
|
298
|
+
this._redoCommand.clearStack();
|
|
299
|
+
}
|
|
300
|
+
}, { priority: "highest" });
|
|
301
|
+
this.listenTo(this._undoCommand, "revert", (evt, undoneBatch, undoingBatch) => {
|
|
302
|
+
this._redoCommand.addBatch(undoingBatch);
|
|
303
|
+
});
|
|
304
|
+
editor.keystrokes.set("CTRL+Z", "undo");
|
|
305
|
+
editor.keystrokes.set("CTRL+Y", "redo");
|
|
306
|
+
editor.keystrokes.set("CTRL+SHIFT+Z", "redo");
|
|
307
|
+
editor.accessibility.addKeystrokeInfos({ keystrokes: [{
|
|
308
|
+
label: t("Undo"),
|
|
309
|
+
keystroke: "CTRL+Z"
|
|
310
|
+
}, {
|
|
311
|
+
label: t("Redo"),
|
|
312
|
+
keystroke: [["CTRL+Y"], ["CTRL+SHIFT+Z"]]
|
|
313
|
+
}] });
|
|
314
|
+
}
|
|
315
|
+
};
|
|
368
316
|
|
|
369
317
|
/**
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
318
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
319
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
320
|
+
*/
|
|
321
|
+
/**
|
|
322
|
+
* @module undo/undoui
|
|
323
|
+
*/
|
|
324
|
+
/**
|
|
325
|
+
* The undo UI feature. It introduces the `'undo'` and `'redo'` buttons to the editor.
|
|
326
|
+
*/
|
|
327
|
+
var UndoUI = class extends Plugin {
|
|
328
|
+
/**
|
|
329
|
+
* @inheritDoc
|
|
330
|
+
*/
|
|
331
|
+
static get pluginName() {
|
|
332
|
+
return "UndoUI";
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* @inheritDoc
|
|
336
|
+
*/
|
|
337
|
+
static get isOfficialPlugin() {
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* @inheritDoc
|
|
342
|
+
*/
|
|
343
|
+
init() {
|
|
344
|
+
const editor = this.editor;
|
|
345
|
+
const locale = editor.locale;
|
|
346
|
+
const t = editor.t;
|
|
347
|
+
const localizedUndoIcon = locale.uiLanguageDirection == "ltr" ? IconUndo : IconRedo;
|
|
348
|
+
const localizedRedoIcon = locale.uiLanguageDirection == "ltr" ? IconRedo : IconUndo;
|
|
349
|
+
this._addButtonsToFactory("undo", t("Undo"), "CTRL+Z", localizedUndoIcon);
|
|
350
|
+
this._addButtonsToFactory("redo", t("Redo"), "CTRL+Y", localizedRedoIcon);
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Creates a button for the specified command.
|
|
354
|
+
*
|
|
355
|
+
* @param name Command name.
|
|
356
|
+
* @param label Button label.
|
|
357
|
+
* @param keystroke Command keystroke.
|
|
358
|
+
* @param Icon Source of the icon.
|
|
359
|
+
*/
|
|
360
|
+
_addButtonsToFactory(name, label, keystroke, Icon) {
|
|
361
|
+
const editor = this.editor;
|
|
362
|
+
editor.ui.componentFactory.add(name, () => {
|
|
363
|
+
const buttonView = this._createButton(ButtonView, name, label, keystroke, Icon);
|
|
364
|
+
buttonView.set({ tooltip: true });
|
|
365
|
+
return buttonView;
|
|
366
|
+
});
|
|
367
|
+
editor.ui.componentFactory.add("menuBar:" + name, () => {
|
|
368
|
+
return this._createButton(MenuBarMenuListItemButtonView, name, label, keystroke, Icon);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* TODO
|
|
373
|
+
*/
|
|
374
|
+
_createButton(ButtonClass, name, label, keystroke, Icon) {
|
|
375
|
+
const editor = this.editor;
|
|
376
|
+
const locale = editor.locale;
|
|
377
|
+
const command = editor.commands.get(name);
|
|
378
|
+
const view = new ButtonClass(locale);
|
|
379
|
+
view.set({
|
|
380
|
+
label,
|
|
381
|
+
icon: Icon,
|
|
382
|
+
keystroke
|
|
383
|
+
});
|
|
384
|
+
view.bind("isEnabled").to(command, "isEnabled");
|
|
385
|
+
this.listenTo(view, "execute", () => {
|
|
386
|
+
editor.execute(name);
|
|
387
|
+
editor.editing.view.focus();
|
|
388
|
+
});
|
|
389
|
+
return view;
|
|
390
|
+
}
|
|
391
|
+
};
|
|
433
392
|
|
|
434
393
|
/**
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
*
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
394
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
395
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
396
|
+
*/
|
|
397
|
+
/**
|
|
398
|
+
* @module undo/undo
|
|
399
|
+
*/
|
|
400
|
+
/**
|
|
401
|
+
* The undo feature.
|
|
402
|
+
*
|
|
403
|
+
* This is a "glue" plugin which loads the {@link module:undo/undoediting~UndoEditing undo editing feature}
|
|
404
|
+
* and the {@link module:undo/undoui~UndoUI undo UI feature}.
|
|
405
|
+
*
|
|
406
|
+
* Below is an explanation of the undo mechanism working together with {@link module:engine/model/history~History History}:
|
|
407
|
+
*
|
|
408
|
+
* Whenever an {@link module:engine/model/operation/operation~Operation operation} is applied to the
|
|
409
|
+
* {@link module:engine/model/document~ModelDocument document}, it is saved to `History` as is.
|
|
410
|
+
* The {@link module:engine/model/batch~Batch batch} that owns that operation is also saved, in
|
|
411
|
+
* {@link module:undo/undocommand~UndoCommand}, together with the selection that was present in the document before the
|
|
412
|
+
* operation was applied. A batch is saved instead of the operation because changes are undone batch-by-batch, not operation-by-operation
|
|
413
|
+
* and a batch is seen as one undo step.
|
|
414
|
+
*
|
|
415
|
+
* After changes happen to the document, the `History` and `UndoCommand` stack can be represented as follows:
|
|
416
|
+
*
|
|
417
|
+
* ```
|
|
418
|
+
* History Undo stack
|
|
419
|
+
* ============== ==================================
|
|
420
|
+
* [operation A1] [ batch A ]
|
|
421
|
+
* [operation B1] [ batch B ]
|
|
422
|
+
* [operation B2] [ batch C ]
|
|
423
|
+
* [operation C1]
|
|
424
|
+
* [operation C2]
|
|
425
|
+
* [operation B3]
|
|
426
|
+
* [operation C3]
|
|
427
|
+
* ```
|
|
428
|
+
*
|
|
429
|
+
* Where operations starting with the same letter are from same batch.
|
|
430
|
+
*
|
|
431
|
+
* Undoing a batch means that a set of operations which will reverse the effects of that batch needs to be generated.
|
|
432
|
+
* For example, if a batch added several letters, undoing the batch should remove them. It is important to apply undoing
|
|
433
|
+
* operations in the reversed order, so if a batch has operation `X`, `Y`, `Z`, reversed operations `Zr`, `Yr` and `Xr`
|
|
434
|
+
* need to be applied. Otherwise reversed operation `Xr` would operate on a wrong document state, because operation `X`
|
|
435
|
+
* does not know that operations `Y` and `Z` happened.
|
|
436
|
+
*
|
|
437
|
+
* After operations from an undone batch got {@link module:engine/model/operation/operation~Operation#getReversed reversed},
|
|
438
|
+
* one needs to make sure if they are ready to be applied. In the scenario above, operation `C3` is the last operation and `C3r`
|
|
439
|
+
* bases on up-to-date document state, so it can be applied to the document.
|
|
440
|
+
*
|
|
441
|
+
* ```
|
|
442
|
+
* History Undo stack
|
|
443
|
+
* ================= ==================================
|
|
444
|
+
* [ operation A1 ] [ batch A ]
|
|
445
|
+
* [ operation B1 ] [ batch B ]
|
|
446
|
+
* [ operation B2 ] [ processing undoing batch C ]
|
|
447
|
+
* [ operation C1 ]
|
|
448
|
+
* [ operation C2 ]
|
|
449
|
+
* [ operation B3 ]
|
|
450
|
+
* [ operation C3 ]
|
|
451
|
+
* [ operation C3r ]
|
|
452
|
+
* ```
|
|
453
|
+
*
|
|
454
|
+
* Next is operation `C2`, reversed to `C2r`. `C2r` bases on `C2`, so it bases on the wrong document state. It needs to be
|
|
455
|
+
* transformed by operations from history that happened after it, so it "knows" about them. Let us assume that `C2' = C2r * B3 * C3 * C3r`,
|
|
456
|
+
* where `*` means "transformed by". Rest of operations from that batch are processed in the same fashion.
|
|
457
|
+
*
|
|
458
|
+
* ```
|
|
459
|
+
* History Undo stack Redo stack
|
|
460
|
+
* ================= ================================== ==================================
|
|
461
|
+
* [ operation A1 ] [ batch A ] [ batch Cr ]
|
|
462
|
+
* [ operation B1 ] [ batch B ]
|
|
463
|
+
* [ operation B2 ]
|
|
464
|
+
* [ operation C1 ]
|
|
465
|
+
* [ operation C2 ]
|
|
466
|
+
* [ operation B3 ]
|
|
467
|
+
* [ operation C3 ]
|
|
468
|
+
* [ operation C3r ]
|
|
469
|
+
* [ operation C2' ]
|
|
470
|
+
* [ operation C1' ]
|
|
471
|
+
* ```
|
|
472
|
+
*
|
|
473
|
+
* Selective undo works on the same basis, however, instead of undoing the last batch in the undo stack, any batch can be undone.
|
|
474
|
+
* The same algorithm applies: operations from a batch (i.e. `A1`) are reversed and then transformed by operations stored in history.
|
|
475
|
+
*
|
|
476
|
+
* Redo also is very similar to undo. It has its own stack that is filled with undoing (reversed batches). Operations from
|
|
477
|
+
* the batch that is re-done are reversed-back, transformed in proper order and applied to the document.
|
|
478
|
+
*
|
|
479
|
+
* ```
|
|
480
|
+
* History Undo stack Redo stack
|
|
481
|
+
* ================= ================================== ==================================
|
|
482
|
+
* [ operation A1 ] [ batch A ]
|
|
483
|
+
* [ operation B1 ] [ batch B ]
|
|
484
|
+
* [ operation B2 ] [ batch Crr ]
|
|
485
|
+
* [ operation C1 ]
|
|
486
|
+
* [ operation C2 ]
|
|
487
|
+
* [ operation B3 ]
|
|
488
|
+
* [ operation C3 ]
|
|
489
|
+
* [ operation C3r ]
|
|
490
|
+
* [ operation C2' ]
|
|
491
|
+
* [ operation C1' ]
|
|
492
|
+
* [ operation C1'r]
|
|
493
|
+
* [ operation C2'r]
|
|
494
|
+
* [ operation C3rr]
|
|
495
|
+
* ```
|
|
496
|
+
*/
|
|
497
|
+
var Undo = class extends Plugin {
|
|
498
|
+
/**
|
|
499
|
+
* @inheritDoc
|
|
500
|
+
*/
|
|
501
|
+
static get requires() {
|
|
502
|
+
return [UndoEditing, UndoUI];
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* @inheritDoc
|
|
506
|
+
*/
|
|
507
|
+
static get pluginName() {
|
|
508
|
+
return "Undo";
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* @inheritDoc
|
|
512
|
+
*/
|
|
513
|
+
static get isOfficialPlugin() {
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
|
|
520
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
521
|
+
*/
|
|
550
522
|
|
|
551
523
|
export { RedoCommand, Undo, UndoCommand, UndoEditing, UndoRedoBaseCommand, UndoUI };
|
|
552
|
-
//# sourceMappingURL=index.js.map
|
|
524
|
+
//# sourceMappingURL=index.js.map
|