@ckeditor/ckeditor5-undo 38.0.1 → 38.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,37 +1,37 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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';
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
- };
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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';
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
+ };
@@ -1,41 +1,41 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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';
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
- this.fire('revert', item.batch, undoingBatch);
38
- });
39
- this.refresh();
40
- }
41
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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';
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
+ this.fire('revert', item.batch, undoingBatch);
38
+ });
39
+ this.refresh();
40
+ }
41
+ }
@@ -1,37 +1,37 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- init(): void;
37
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ init(): void;
37
+ }
@@ -1,82 +1,82 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- import UndoCommand from './undocommand';
10
- import RedoCommand from './redocommand';
11
- /**
12
- * The undo engine feature.
13
- *
14
- * It introduces the `'undo'` and `'redo'` commands to the editor.
15
- */
16
- export default class UndoEditing extends Plugin {
17
- constructor() {
18
- super(...arguments);
19
- /**
20
- * Keeps track of which batches were registered in undo.
21
- */
22
- this._batchRegistry = new WeakSet();
23
- }
24
- /**
25
- * @inheritDoc
26
- */
27
- static get pluginName() {
28
- return 'UndoEditing';
29
- }
30
- /**
31
- * @inheritDoc
32
- */
33
- init() {
34
- const editor = this.editor;
35
- // Create commands.
36
- this._undoCommand = new UndoCommand(editor);
37
- this._redoCommand = new RedoCommand(editor);
38
- // Register command to the editor.
39
- editor.commands.add('undo', this._undoCommand);
40
- editor.commands.add('redo', this._redoCommand);
41
- this.listenTo(editor.model, 'applyOperation', (evt, args) => {
42
- const operation = args[0];
43
- // Do not register batch if the operation is not a document operation.
44
- // This prevents from creating empty undo steps, where all operations where non-document operations.
45
- // Non-document operations creates and alters content in detached tree fragments (for example, document fragments).
46
- // Most of time this is preparing data before it is inserted into actual tree (for example during copy & paste).
47
- // Such operations should not be reversed.
48
- if (!operation.isDocumentOperation) {
49
- return;
50
- }
51
- const batch = operation.batch;
52
- const isRedoBatch = this._redoCommand.createdBatches.has(batch);
53
- const isUndoBatch = this._undoCommand.createdBatches.has(batch);
54
- const wasProcessed = this._batchRegistry.has(batch);
55
- // Skip the batch if it was already processed.
56
- if (wasProcessed) {
57
- return;
58
- }
59
- // Add the batch to the registry so it will not be processed again.
60
- this._batchRegistry.add(batch);
61
- if (!batch.isUndoable) {
62
- return;
63
- }
64
- if (isRedoBatch) {
65
- // If this batch comes from `redoCommand`, add it to the `undoCommand` stack.
66
- this._undoCommand.addBatch(batch);
67
- }
68
- else if (!isUndoBatch) {
69
- // If the batch comes neither from `redoCommand` nor from `undoCommand` then it is a new, regular batch.
70
- // Add the batch to the `undoCommand` stack and clear the `redoCommand` stack.
71
- this._undoCommand.addBatch(batch);
72
- this._redoCommand.clearStack();
73
- }
74
- }, { priority: 'highest' });
75
- this.listenTo(this._undoCommand, 'revert', (evt, undoneBatch, undoingBatch) => {
76
- this._redoCommand.addBatch(undoingBatch);
77
- });
78
- editor.keystrokes.set('CTRL+Z', 'undo');
79
- editor.keystrokes.set('CTRL+Y', 'redo');
80
- editor.keystrokes.set('CTRL+SHIFT+Z', 'redo');
81
- }
82
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ import UndoCommand from './undocommand';
10
+ import RedoCommand from './redocommand';
11
+ /**
12
+ * The undo engine feature.
13
+ *
14
+ * It introduces the `'undo'` and `'redo'` commands to the editor.
15
+ */
16
+ export default class UndoEditing extends Plugin {
17
+ constructor() {
18
+ super(...arguments);
19
+ /**
20
+ * Keeps track of which batches were registered in undo.
21
+ */
22
+ this._batchRegistry = new WeakSet();
23
+ }
24
+ /**
25
+ * @inheritDoc
26
+ */
27
+ static get pluginName() {
28
+ return 'UndoEditing';
29
+ }
30
+ /**
31
+ * @inheritDoc
32
+ */
33
+ init() {
34
+ const editor = this.editor;
35
+ // Create commands.
36
+ this._undoCommand = new UndoCommand(editor);
37
+ this._redoCommand = new RedoCommand(editor);
38
+ // Register command to the editor.
39
+ editor.commands.add('undo', this._undoCommand);
40
+ editor.commands.add('redo', this._redoCommand);
41
+ this.listenTo(editor.model, 'applyOperation', (evt, args) => {
42
+ const operation = args[0];
43
+ // Do not register batch if the operation is not a document operation.
44
+ // This prevents from creating empty undo steps, where all operations where non-document operations.
45
+ // Non-document operations creates and alters content in detached tree fragments (for example, document fragments).
46
+ // Most of time this is preparing data before it is inserted into actual tree (for example during copy & paste).
47
+ // Such operations should not be reversed.
48
+ if (!operation.isDocumentOperation) {
49
+ return;
50
+ }
51
+ const batch = operation.batch;
52
+ const isRedoBatch = this._redoCommand.createdBatches.has(batch);
53
+ const isUndoBatch = this._undoCommand.createdBatches.has(batch);
54
+ const wasProcessed = this._batchRegistry.has(batch);
55
+ // Skip the batch if it was already processed.
56
+ if (wasProcessed) {
57
+ return;
58
+ }
59
+ // Add the batch to the registry so it will not be processed again.
60
+ this._batchRegistry.add(batch);
61
+ if (!batch.isUndoable) {
62
+ return;
63
+ }
64
+ if (isRedoBatch) {
65
+ // If this batch comes from `redoCommand`, add it to the `undoCommand` stack.
66
+ this._undoCommand.addBatch(batch);
67
+ }
68
+ else if (!isUndoBatch) {
69
+ // If the batch comes neither from `redoCommand` nor from `undoCommand` then it is a new, regular batch.
70
+ // Add the batch to the `undoCommand` stack and clear the `redoCommand` stack.
71
+ this._undoCommand.addBatch(batch);
72
+ this._redoCommand.clearStack();
73
+ }
74
+ }, { priority: 'highest' });
75
+ this.listenTo(this._undoCommand, 'revert', (evt, undoneBatch, undoingBatch) => {
76
+ this._redoCommand.addBatch(undoingBatch);
77
+ });
78
+ editor.keystrokes.set('CTRL+Z', 'undo');
79
+ editor.keystrokes.set('CTRL+Y', 'redo');
80
+ editor.keystrokes.set('CTRL+SHIFT+Z', 'redo');
81
+ }
82
+ }
package/src/undoui.d.ts CHANGED
@@ -1,30 +1,30 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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/undoui
7
- */
8
- import { Plugin } from '@ckeditor/ckeditor5-core';
9
- /**
10
- * The undo UI feature. It introduces the `'undo'` and `'redo'` buttons to the editor.
11
- */
12
- export default class UndoUI extends Plugin {
13
- /**
14
- * @inheritDoc
15
- */
16
- static get pluginName(): 'UndoUI';
17
- /**
18
- * @inheritDoc
19
- */
20
- init(): void;
21
- /**
22
- * Creates a button for the specified command.
23
- *
24
- * @param name Command name.
25
- * @param label Button label.
26
- * @param keystroke Command keystroke.
27
- * @param Icon Source of the icon.
28
- */
29
- private _addButton;
30
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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/undoui
7
+ */
8
+ import { Plugin } from '@ckeditor/ckeditor5-core';
9
+ /**
10
+ * The undo UI feature. It introduces the `'undo'` and `'redo'` buttons to the editor.
11
+ */
12
+ export default class UndoUI extends Plugin {
13
+ /**
14
+ * @inheritDoc
15
+ */
16
+ static get pluginName(): "UndoUI";
17
+ /**
18
+ * @inheritDoc
19
+ */
20
+ init(): void;
21
+ /**
22
+ * Creates a button for the specified command.
23
+ *
24
+ * @param name Command name.
25
+ * @param label Button label.
26
+ * @param keystroke Command keystroke.
27
+ * @param Icon Source of the icon.
28
+ */
29
+ private _addButton;
30
+ }
package/src/undoui.js CHANGED
@@ -1,61 +1,61 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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/undoui
7
- */
8
- import { Plugin } from '@ckeditor/ckeditor5-core';
9
- import { ButtonView } from '@ckeditor/ckeditor5-ui';
10
- import undoIcon from '../theme/icons/undo.svg';
11
- import redoIcon from '../theme/icons/redo.svg';
12
- /**
13
- * The undo UI feature. It introduces the `'undo'` and `'redo'` buttons to the editor.
14
- */
15
- export default class UndoUI extends Plugin {
16
- /**
17
- * @inheritDoc
18
- */
19
- static get pluginName() {
20
- return 'UndoUI';
21
- }
22
- /**
23
- * @inheritDoc
24
- */
25
- init() {
26
- const editor = this.editor;
27
- const locale = editor.locale;
28
- const t = editor.t;
29
- const localizedUndoIcon = locale.uiLanguageDirection == 'ltr' ? undoIcon : redoIcon;
30
- const localizedRedoIcon = locale.uiLanguageDirection == 'ltr' ? redoIcon : undoIcon;
31
- this._addButton('undo', t('Undo'), 'CTRL+Z', localizedUndoIcon);
32
- this._addButton('redo', t('Redo'), 'CTRL+Y', localizedRedoIcon);
33
- }
34
- /**
35
- * Creates a button for the specified command.
36
- *
37
- * @param name Command name.
38
- * @param label Button label.
39
- * @param keystroke Command keystroke.
40
- * @param Icon Source of the icon.
41
- */
42
- _addButton(name, label, keystroke, Icon) {
43
- const editor = this.editor;
44
- editor.ui.componentFactory.add(name, locale => {
45
- const command = editor.commands.get(name);
46
- const view = new ButtonView(locale);
47
- view.set({
48
- label,
49
- icon: Icon,
50
- keystroke,
51
- tooltip: true
52
- });
53
- view.bind('isEnabled').to(command, 'isEnabled');
54
- this.listenTo(view, 'execute', () => {
55
- editor.execute(name);
56
- editor.editing.view.focus();
57
- });
58
- return view;
59
- });
60
- }
61
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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/undoui
7
+ */
8
+ import { Plugin } from '@ckeditor/ckeditor5-core';
9
+ import { ButtonView } from '@ckeditor/ckeditor5-ui';
10
+ import undoIcon from '../theme/icons/undo.svg';
11
+ import redoIcon from '../theme/icons/redo.svg';
12
+ /**
13
+ * The undo UI feature. It introduces the `'undo'` and `'redo'` buttons to the editor.
14
+ */
15
+ export default class UndoUI extends Plugin {
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get pluginName() {
20
+ return 'UndoUI';
21
+ }
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ init() {
26
+ const editor = this.editor;
27
+ const locale = editor.locale;
28
+ const t = editor.t;
29
+ const localizedUndoIcon = locale.uiLanguageDirection == 'ltr' ? undoIcon : redoIcon;
30
+ const localizedRedoIcon = locale.uiLanguageDirection == 'ltr' ? redoIcon : undoIcon;
31
+ this._addButton('undo', t('Undo'), 'CTRL+Z', localizedUndoIcon);
32
+ this._addButton('redo', t('Redo'), 'CTRL+Y', localizedRedoIcon);
33
+ }
34
+ /**
35
+ * Creates a button for the specified command.
36
+ *
37
+ * @param name Command name.
38
+ * @param label Button label.
39
+ * @param keystroke Command keystroke.
40
+ * @param Icon Source of the icon.
41
+ */
42
+ _addButton(name, label, keystroke, Icon) {
43
+ const editor = this.editor;
44
+ editor.ui.componentFactory.add(name, locale => {
45
+ const command = editor.commands.get(name);
46
+ const view = new ButtonView(locale);
47
+ view.set({
48
+ label,
49
+ icon: Icon,
50
+ keystroke,
51
+ tooltip: true
52
+ });
53
+ view.bind('isEnabled').to(command, 'isEnabled');
54
+ this.listenTo(view, 'execute', () => {
55
+ editor.execute(name);
56
+ editor.editing.view.focus();
57
+ });
58
+ return view;
59
+ });
60
+ }
61
+ }