undoediting.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. 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/src/plugin';
  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. * @extends module:core/plugin~Plugin
  17. */
  18. export default class UndoEditing extends Plugin {
  19. /**
  20. * @inheritDoc
  21. */
  22. static get pluginName() {
  23. return 'UndoEditing';
  24. }
  25. /**
  26. * @inheritDoc
  27. */
  28. constructor( editor ) {
  29. super( editor );
  30. /**
  31. * The command that manages undo {@link module:engine/model/batch~Batch batches} stack (history).
  32. * Created and registered during the {@link #init feature initialization}.
  33. *
  34. * @private
  35. * @member {module:undo/undocommand~UndoCommand} #_undoCommand
  36. */
  37. /**
  38. * The command that manages redo {@link module:engine/model/batch~Batch batches} stack (history).
  39. * Created and registered during the {@link #init feature initialization}.
  40. *
  41. * @private
  42. * @member {module:undo/undocommand~UndoCommand} #_redoCommand
  43. */
  44. /**
  45. * Keeps track of which batches were registered in undo.
  46. *
  47. * @private
  48. * @member {WeakSet.<module:engine/model/batch~Batch>}
  49. */
  50. this._batchRegistry = new WeakSet();
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. init() {
  56. const editor = this.editor;
  57. // Create commands.
  58. this._undoCommand = new UndoCommand( editor );
  59. this._redoCommand = new RedoCommand( editor );
  60. // Register command to the editor.
  61. editor.commands.add( 'undo', this._undoCommand );
  62. editor.commands.add( 'redo', this._redoCommand );
  63. this.listenTo( editor.model, 'applyOperation', ( evt, args ) => {
  64. const operation = args[ 0 ];
  65. // Do not register batch if the operation is not a document operation.
  66. // This prevents from creating empty undo steps, where all operations where non-document operations.
  67. // Non-document operations creates and alters content in detached tree fragments (for example, document fragments).
  68. // Most of time this is preparing data before it is inserted into actual tree (for example during copy & paste).
  69. // Such operations should not be reversed.
  70. if ( !operation.isDocumentOperation ) {
  71. return;
  72. }
  73. const batch = operation.batch;
  74. const isRedoBatch = this._redoCommand._createdBatches.has( batch );
  75. const isUndoBatch = this._undoCommand._createdBatches.has( batch );
  76. const isRegisteredBatch = this._batchRegistry.has( batch );
  77. // If changes are not a part of a batch or this is not a new batch, omit those changes.
  78. if ( isRegisteredBatch || ( batch.type == 'transparent' && !isRedoBatch && !isUndoBatch ) ) {
  79. return;
  80. } else {
  81. if ( isRedoBatch ) {
  82. // If this batch comes from `redoCommand`, add it to `undoCommand` stack.
  83. this._undoCommand.addBatch( batch );
  84. } else if ( !isUndoBatch ) {
  85. // A default batch - these are new changes in the document, not introduced by undo feature.
  86. // Add them to `undoCommand` stack and clear `redoCommand` stack.
  87. this._undoCommand.addBatch( batch );
  88. this._redoCommand.clearStack();
  89. }
  90. }
  91. // Add the batch to the registry so it will not be processed again.
  92. this._batchRegistry.add( batch );
  93. }, { priority: 'highest' } );
  94. this.listenTo( this._undoCommand, 'revert', ( evt, undoneBatch, undoingBatch ) => {
  95. this._redoCommand.addBatch( undoingBatch );
  96. } );
  97. editor.keystrokes.set( 'CTRL+Z', 'undo' );
  98. editor.keystrokes.set( 'CTRL+Y', 'redo' );
  99. editor.keystrokes.set( 'CTRL+SHIFT+Z', 'redo' );
  100. }
  101. }