@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/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 '@ckeditor/ckeditor5-core/dist/index.js';
6
- import { transformOperationSets, NoOperation } from '@ckeditor/ckeditor5-engine/dist/index.js';
7
- import { ButtonView, MenuBarMenuListItemButtonView } from '@ckeditor/ckeditor5-ui/dist/index.js';
8
- import { IconUndo, IconRedo } from '@ckeditor/ckeditor5-icons/dist/index.js';
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
- * Base class for the undo feature commands: {@link module:undo/undocommand~UndoCommand} and {@link module:undo/redocommand~RedoCommand}.
12
- */ class UndoRedoBaseCommand extends Command {
13
- /**
14
- * Stack of items stored by the command. These are pairs of:
15
- *
16
- * * {@link module:engine/model/batch~Batch batch} saved by the command,
17
- * * {@link module:engine/model/selection~ModelSelection selection} state at the moment of saving the batch.
18
- */ _stack = [];
19
- /**
20
- * Stores all batches that were created by this command.
21
- *
22
- * @internal
23
- */ _createdBatches = new WeakSet();
24
- /**
25
- * @inheritDoc
26
- */ constructor(editor){
27
- super(editor);
28
- // Refresh state, so the command is inactive right after initialization.
29
- this.refresh();
30
- // This command should not depend on selection change.
31
- this._isEnabledBasedOnSelection = false;
32
- // Set the transparent batch for the `editor.data.set()` call if the
33
- // batch type is not set already.
34
- this.listenTo(editor.data, 'set', (evt, data)=>{
35
- // Create a shallow copy of the options to not change the original args.
36
- // And make sure that an object is assigned to data[ 1 ].
37
- data[1] = {
38
- ...data[1]
39
- };
40
- const options = data[1];
41
- // If batch type is not set, default to non-undoable batch.
42
- if (!options.batchType) {
43
- options.batchType = {
44
- isUndoable: false
45
- };
46
- }
47
- }, {
48
- priority: 'high'
49
- });
50
- // Clear the stack for the `transparent` batches.
51
- this.listenTo(editor.data, 'set', (evt, data)=>{
52
- // We can assume that the object exists and it has a `batchType` property.
53
- // It was ensured with a higher priority listener before.
54
- const options = data[1];
55
- if (!options.batchType.isUndoable) {
56
- this.clearStack();
57
- }
58
- });
59
- }
60
- /**
61
- * @inheritDoc
62
- */ refresh() {
63
- this.isEnabled = this._stack.length > 0;
64
- }
65
- /**
66
- * Returns all batches created by this command.
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~ModelDocument document}
72
- * created by the editor which this command is registered to.
73
- *
74
- * @param batch The batch to add.
75
- */ addBatch(batch) {
76
- const docSelection = this.editor.model.document.selection;
77
- const selection = {
78
- ranges: docSelection.hasOwnRange ? Array.from(docSelection.getRanges()) : [],
79
- isBackward: docSelection.isBackward
80
- };
81
- this._stack.push({
82
- batch,
83
- selection
84
- });
85
- this.refresh();
86
- }
87
- /**
88
- * Removes all items from the stack.
89
- */ clearStack() {
90
- this._stack = [];
91
- this.refresh();
92
- }
93
- /**
94
- * Restores the {@link module:engine/model/document~ModelDocument#selection document selection} state after a batch was undone.
95
- *
96
- * @param ranges Ranges to be restored.
97
- * @param isBackward A flag describing whether the restored range was selected forward or backward.
98
- * @param operations Operations which has been applied since selection has been stored.
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.filter((range)=>range.root != document.graveyard).filter((range)=>!isRangeContainedByAnyOtherRange(range, allRanges));
110
- // All the transformed ranges ended up in graveyard.
111
- if (!transformed.length) {
112
- continue;
113
- }
114
- // After the range got transformed, we have an array of ranges. Some of those
115
- // ranges may be "touching" -- they can be next to each other and could be merged.
116
- normalizeRanges(transformed);
117
- // For each `range` from `ranges`, we take only one transformed range.
118
- // This is because we want to prevent situation where single-range selection
119
- // got transformed to multi-range selection.
120
- selectionRanges.push(transformed[0]);
121
- }
122
- // @if CK_DEBUG_ENGINE // console.log( `Restored selection by undo: ${ selectionRanges.join( ', ' ) }` );
123
- // `selectionRanges` may be empty if all ranges ended up in graveyard. If that is the case, do not restore selection.
124
- if (selectionRanges.length) {
125
- model.change((writer)=>{
126
- writer.setSelection(selectionRanges, {
127
- backward: isBackward
128
- });
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
- */ _undo(batchToUndo, undoingBatch) {
139
- const model = this.editor.model;
140
- const document = model.document;
141
- // All changes done by the command execution will be saved as one batch.
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
- * Normalizes list of ranges by joining intersecting or "touching" ranges.
176
- *
177
- * @param ranges Ranges to be normalized.
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
- }
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
- return ranges.some((otherRange)=>otherRange !== range && otherRange.containsRange(range, true));
158
+ return ranges.some((otherRange) => otherRange !== range && otherRange.containsRange(range, true));
192
159
  }
193
160
 
194
161
  /**
195
- * The undo command stores {@link module:engine/model/batch~Batch batches} applied to the
196
- * {@link module:engine/model/document~ModelDocument document} and is able to undo a batch by reversing it and transforming by
197
- * batches from {@link module:engine/model/document~ModelDocument#history history} that happened after the reversed batch.
198
- *
199
- * The undo command also takes care of restoring the {@link module:engine/model/document~ModelDocument#selection document selection}.
200
- */ class UndoCommand extends UndoRedoBaseCommand {
201
- /**
202
- * Executes the command. This method reverts a {@link module:engine/model/batch~Batch batch} added to the command's stack, transforms
203
- * and applies the reverted version on the {@link module:engine/model/document~ModelDocument document} and removes the batch from
204
- * the stack. Then, it restores the {@link module:engine/model/document~ModelDocument#selection document selection}.
205
- *
206
- * @fires execute
207
- * @fires revert
208
- * @param batch A batch that should be undone. If not set, the last added batch will be undone.
209
- */ execute(batch = null) {
210
- // If batch is not given, set `batchIndex` to the last index in command stack.
211
- const batchIndex = batch ? this._stack.findIndex((a)=>a.batch == batch) : this._stack.length - 1;
212
- const item = this._stack.splice(batchIndex, 1)[0];
213
- const undoingBatch = this.editor.model.createBatch({
214
- isUndo: true
215
- });
216
- // All changes have to be done in one `enqueueChange` callback so other listeners will not
217
- // step between consecutive operations, or won't do changes to the document before selection is properly restored.
218
- this.editor.model.enqueueChange(undoingBatch, ()=>{
219
- this._undo(item.batch, undoingBatch);
220
- const operations = this.editor.model.document.history.getOperations(item.batch.baseVersion);
221
- this._restoreSelection(item.selection.ranges, item.selection.isBackward, operations);
222
- });
223
- // Firing `revert` event after the change block to make sure that it includes all changes from post-fixers
224
- // and make sure that the selection is "stabilized" (the selection range is saved after undo is executed and then
225
- // restored on redo, so it is important that the selection range is saved after post-fixers are done).
226
- this.fire('revert', item.batch, undoingBatch);
227
- this.refresh();
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
- * The redo command stores {@link module:engine/model/batch~Batch batches} that were used to undo a batch by
233
- * {@link module:undo/undocommand~UndoCommand}. It is able to redo a previously undone batch by reversing the undoing
234
- * batches created by `UndoCommand`. The reversed batch is transformed by all the batches from
235
- * {@link module:engine/model/document~ModelDocument#history history} that happened after the reversed undo batch.
236
- *
237
- * The redo command also takes care of restoring the {@link module:engine/model/document~ModelDocument#selection document selection}.
238
- */ class RedoCommand extends UndoRedoBaseCommand {
239
- /**
240
- * Executes the command. This method reverts the last {@link module:engine/model/batch~Batch batch} added to
241
- * the command's stack, applies the reverted and transformed version on the
242
- * {@link module:engine/model/document~ModelDocument document} and removes the batch from the stack.
243
- * Then, it restores the {@link module:engine/model/document~ModelDocument#selection document selection}.
244
- *
245
- * @fires execute
246
- */ execute() {
247
- const item = this._stack.pop();
248
- const redoingBatch = this.editor.model.createBatch({
249
- isUndo: true
250
- });
251
- // All changes have to be done in one `enqueueChange` callback so other listeners will not step between consecutive
252
- // operations, or won't do changes to the document before selection is properly restored.
253
- this.editor.model.enqueueChange(redoingBatch, ()=>{
254
- const lastOperation = item.batch.operations[item.batch.operations.length - 1];
255
- const nextBaseVersion = lastOperation.baseVersion + 1;
256
- const operations = this.editor.model.document.history.getOperations(nextBaseVersion);
257
- this._restoreSelection(item.selection.ranges, item.selection.isBackward, operations);
258
- this._undo(item.batch, redoingBatch);
259
- });
260
- // Firing `revert` event after the change block to make sure that it includes all changes from post-fixers
261
- // and make sure that the selection is "stabilized" (the selection range is saved after undo is executed and then
262
- // restored on redo, so it is important that the selection range is saved after post-fixers are done).
263
- this.fire('revert', item.batch, redoingBatch);
264
- this.refresh();
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
- * The undo engine feature.
270
- *
271
- * It introduces the `'undo'` and `'redo'` commands to the editor.
272
- */ class UndoEditing extends Plugin {
273
- /**
274
- * The command that manages the undo {@link module:engine/model/batch~Batch batches} stack (history).
275
- * Created and registered during the {@link #init feature initialization}.
276
- */ _undoCommand;
277
- /**
278
- * The command that manages the redo {@link module:engine/model/batch~Batch batches} stack (history).
279
- * Created and registered during the {@link #init feature initialization}.
280
- */ _redoCommand;
281
- /**
282
- * Keeps track of which batches were registered in undo.
283
- */ _batchRegistry = new WeakSet();
284
- /**
285
- * @inheritDoc
286
- */ static get pluginName() {
287
- return 'UndoEditing';
288
- }
289
- /**
290
- * @inheritDoc
291
- */ static get isOfficialPlugin() {
292
- return true;
293
- }
294
- /**
295
- * @inheritDoc
296
- */ init() {
297
- const editor = this.editor;
298
- const t = editor.t;
299
- // Create commands.
300
- this._undoCommand = new UndoCommand(editor);
301
- this._redoCommand = new RedoCommand(editor);
302
- // Register command to the editor.
303
- editor.commands.add('undo', this._undoCommand);
304
- editor.commands.add('redo', this._redoCommand);
305
- this.listenTo(editor.model, 'applyOperation', (evt, args)=>{
306
- const operation = args[0];
307
- // Do not register batch if the operation is not a document operation.
308
- // This prevents from creating empty undo steps, where all operations where non-document operations.
309
- // Non-document operations creates and alters content in detached tree fragments (for example, document fragments).
310
- // Most of time this is preparing data before it is inserted into actual tree (for example during copy & paste).
311
- // Such operations should not be reversed.
312
- if (!operation.isDocumentOperation) {
313
- return;
314
- }
315
- const batch = operation.batch;
316
- const isRedoBatch = this._redoCommand.createdBatches.has(batch);
317
- const isUndoBatch = this._undoCommand.createdBatches.has(batch);
318
- const wasProcessed = this._batchRegistry.has(batch);
319
- // Skip the batch if it was already processed.
320
- if (wasProcessed) {
321
- return;
322
- }
323
- // Add the batch to the registry so it will not be processed again.
324
- this._batchRegistry.add(batch);
325
- if (!batch.isUndoable) {
326
- return;
327
- }
328
- if (isRedoBatch) {
329
- // If this batch comes from `redoCommand`, add it to the `undoCommand` stack.
330
- this._undoCommand.addBatch(batch);
331
- } else if (!isUndoBatch) {
332
- // If the batch comes neither from `redoCommand` nor from `undoCommand` then it is a new, regular batch.
333
- // Add the batch to the `undoCommand` stack and clear the `redoCommand` stack.
334
- this._undoCommand.addBatch(batch);
335
- this._redoCommand.clearStack();
336
- }
337
- }, {
338
- priority: 'highest'
339
- });
340
- this.listenTo(this._undoCommand, 'revert', (evt, undoneBatch, undoingBatch)=>{
341
- this._redoCommand.addBatch(undoingBatch);
342
- });
343
- editor.keystrokes.set('CTRL+Z', 'undo');
344
- editor.keystrokes.set('CTRL+Y', 'redo');
345
- editor.keystrokes.set('CTRL+SHIFT+Z', 'redo');
346
- // Add the information about the keystrokes to the accessibility database.
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
- * The undo UI feature. It introduces the `'undo'` and `'redo'` buttons to the editor.
371
- */ class UndoUI extends Plugin {
372
- /**
373
- * @inheritDoc
374
- */ static get pluginName() {
375
- return 'UndoUI';
376
- }
377
- /**
378
- * @inheritDoc
379
- */ static get isOfficialPlugin() {
380
- return true;
381
- }
382
- /**
383
- * @inheritDoc
384
- */ init() {
385
- const editor = this.editor;
386
- const locale = editor.locale;
387
- const t = editor.t;
388
- const localizedUndoIcon = locale.uiLanguageDirection == 'ltr' ? IconUndo : IconRedo;
389
- const localizedRedoIcon = locale.uiLanguageDirection == 'ltr' ? IconRedo : IconUndo;
390
- this._addButtonsToFactory('undo', t('Undo'), 'CTRL+Z', localizedUndoIcon);
391
- this._addButtonsToFactory('redo', t('Redo'), 'CTRL+Y', localizedRedoIcon);
392
- }
393
- /**
394
- * Creates a button for the specified command.
395
- *
396
- * @param name Command name.
397
- * @param label Button label.
398
- * @param keystroke Command keystroke.
399
- * @param Icon Source of the icon.
400
- */ _addButtonsToFactory(name, label, keystroke, Icon) {
401
- const editor = this.editor;
402
- editor.ui.componentFactory.add(name, ()=>{
403
- const buttonView = this._createButton(ButtonView, name, label, keystroke, Icon);
404
- buttonView.set({
405
- tooltip: true
406
- });
407
- return buttonView;
408
- });
409
- editor.ui.componentFactory.add('menuBar:' + name, ()=>{
410
- return this._createButton(MenuBarMenuListItemButtonView, name, label, keystroke, Icon);
411
- });
412
- }
413
- /**
414
- * TODO
415
- */ _createButton(ButtonClass, name, label, keystroke, Icon) {
416
- const editor = this.editor;
417
- const locale = editor.locale;
418
- const command = editor.commands.get(name);
419
- const view = new ButtonClass(locale);
420
- view.set({
421
- label,
422
- icon: Icon,
423
- keystroke
424
- });
425
- view.bind('isEnabled').to(command, 'isEnabled');
426
- this.listenTo(view, 'execute', ()=>{
427
- editor.execute(name);
428
- editor.editing.view.focus();
429
- });
430
- return view;
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
- * The undo feature.
436
- *
437
- * This is a "glue" plugin which loads the {@link module:undo/undoediting~UndoEditing undo editing feature}
438
- * and the {@link module:undo/undoui~UndoUI undo UI feature}.
439
- *
440
- * Below is an explanation of the undo mechanism working together with {@link module:engine/model/history~History History}:
441
- *
442
- * Whenever an {@link module:engine/model/operation/operation~Operation operation} is applied to the
443
- * {@link module:engine/model/document~ModelDocument document}, it is saved to `History` as is.
444
- * The {@link module:engine/model/batch~Batch batch} that owns that operation is also saved, in
445
- * {@link module:undo/undocommand~UndoCommand}, together with the selection that was present in the document before the
446
- * operation was applied. A batch is saved instead of the operation because changes are undone batch-by-batch, not operation-by-operation
447
- * and a batch is seen as one undo step.
448
- *
449
- * After changes happen to the document, the `History` and `UndoCommand` stack can be represented as follows:
450
- *
451
- * ```
452
- * History Undo stack
453
- * ============== ==================================
454
- * [operation A1] [ batch A ]
455
- * [operation B1] [ batch B ]
456
- * [operation B2] [ batch C ]
457
- * [operation C1]
458
- * [operation C2]
459
- * [operation B3]
460
- * [operation C3]
461
- * ```
462
- *
463
- * Where operations starting with the same letter are from same batch.
464
- *
465
- * Undoing a batch means that a set of operations which will reverse the effects of that batch needs to be generated.
466
- * For example, if a batch added several letters, undoing the batch should remove them. It is important to apply undoing
467
- * operations in the reversed order, so if a batch has operation `X`, `Y`, `Z`, reversed operations `Zr`, `Yr` and `Xr`
468
- * need to be applied. Otherwise reversed operation `Xr` would operate on a wrong document state, because operation `X`
469
- * does not know that operations `Y` and `Z` happened.
470
- *
471
- * After operations from an undone batch got {@link module:engine/model/operation/operation~Operation#getReversed reversed},
472
- * one needs to make sure if they are ready to be applied. In the scenario above, operation `C3` is the last operation and `C3r`
473
- * bases on up-to-date document state, so it can be applied to the document.
474
- *
475
- * ```
476
- * History Undo stack
477
- * ================= ==================================
478
- * [ operation A1 ] [ batch A ]
479
- * [ operation B1 ] [ batch B ]
480
- * [ operation B2 ] [ processing undoing batch C ]
481
- * [ operation C1 ]
482
- * [ operation C2 ]
483
- * [ operation B3 ]
484
- * [ operation C3 ]
485
- * [ operation C3r ]
486
- * ```
487
- *
488
- * Next is operation `C2`, reversed to `C2r`. `C2r` bases on `C2`, so it bases on the wrong document state. It needs to be
489
- * transformed by operations from history that happened after it, so it "knows" about them. Let us assume that `C2' = C2r * B3 * C3 * C3r`,
490
- * where `*` means "transformed by". Rest of operations from that batch are processed in the same fashion.
491
- *
492
- * ```
493
- * History Undo stack Redo stack
494
- * ================= ================================== ==================================
495
- * [ operation A1 ] [ batch A ] [ batch Cr ]
496
- * [ operation B1 ] [ batch B ]
497
- * [ operation B2 ]
498
- * [ operation C1 ]
499
- * [ operation C2 ]
500
- * [ operation B3 ]
501
- * [ operation C3 ]
502
- * [ operation C3r ]
503
- * [ operation C2' ]
504
- * [ operation C1' ]
505
- * ```
506
- *
507
- * Selective undo works on the same basis, however, instead of undoing the last batch in the undo stack, any batch can be undone.
508
- * The same algorithm applies: operations from a batch (i.e. `A1`) are reversed and then transformed by operations stored in history.
509
- *
510
- * Redo also is very similar to undo. It has its own stack that is filled with undoing (reversed batches). Operations from
511
- * the batch that is re-done are reversed-back, transformed in proper order and applied to the document.
512
- *
513
- * ```
514
- * History Undo stack Redo stack
515
- * ================= ================================== ==================================
516
- * [ operation A1 ] [ batch A ]
517
- * [ operation B1 ] [ batch B ]
518
- * [ operation B2 ] [ batch Crr ]
519
- * [ operation C1 ]
520
- * [ operation C2 ]
521
- * [ operation B3 ]
522
- * [ operation C3 ]
523
- * [ operation C3r ]
524
- * [ operation C2' ]
525
- * [ operation C1' ]
526
- * [ operation C1'r]
527
- * [ operation C2'r]
528
- * [ operation C3rr]
529
- * ```
530
- */ class Undo extends Plugin {
531
- /**
532
- * @inheritDoc
533
- */ static get requires() {
534
- return [
535
- UndoEditing,
536
- UndoUI
537
- ];
538
- }
539
- /**
540
- * @inheritDoc
541
- */ static get pluginName() {
542
- return 'Undo';
543
- }
544
- /**
545
- * @inheritDoc
546
- */ static get isOfficialPlugin() {
547
- return true;
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