undoengine.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module undo/undoengine
  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. * Undo brings in possibility to undo and redo changes done in the model by deltas through
  15. * the {@link module:engine/model/writer~Writer Writer API}.
  16. *
  17. * @extends module:core/plugin~Plugin
  18. */
  19. export default class UndoEngine extends Plugin {
  20. /**
  21. * @inheritDoc
  22. */
  23. constructor( editor ) {
  24. super( editor );
  25. /**
  26. * The command that manages undo {@link module:engine/model/batch~Batch batches} stack (history).
  27. * Created and registered during the {@link #init feature initialization}.
  28. *
  29. * @private
  30. * @member {undo.UndoEngineCommand} #_undoCommand
  31. */
  32. /**
  33. * The command that manages redo {@link module:engine/model/batch~Batch batches} stack (history).
  34. * Created and registered during the {@link #init feature initialization}.
  35. *
  36. * @private
  37. * @member {undo.UndoEngineCommand} #_redoCommand
  38. */
  39. /**
  40. * Keeps track of which batches were registered in undo.
  41. *
  42. * @private
  43. * @member {WeakSet.<module:engine/model/batch~Batch>}
  44. */
  45. this._batchRegistry = new WeakSet();
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. init() {
  51. // Create commands.
  52. this._undoCommand = new UndoCommand( this.editor );
  53. this._redoCommand = new RedoCommand( this.editor );
  54. // Register command to the editor.
  55. this.editor.commands.add( 'undo', this._undoCommand );
  56. this.editor.commands.add( 'redo', this._redoCommand );
  57. this.listenTo( this.editor.model, 'applyOperation', ( evt, args ) => {
  58. const operation = args[ 0 ];
  59. const batch = operation.delta.batch;
  60. // If changes are not a part of a batch or this is not a new batch, omit those changes.
  61. if ( this._batchRegistry.has( batch ) || batch.type == 'transparent' ) {
  62. return;
  63. } else {
  64. if ( this._redoCommand._createdBatches.has( batch ) ) {
  65. // If this batch comes from `redoCommand`, add it to `undoCommand` stack.
  66. this._undoCommand.addBatch( batch );
  67. } else if ( !this._undoCommand._createdBatches.has( batch ) ) {
  68. // A default batch - these are new changes in the document, not introduced by undo feature.
  69. // Add them to `undoCommand` stack and clear `redoCommand` stack.
  70. this._undoCommand.addBatch( batch );
  71. this._redoCommand.clearStack();
  72. }
  73. }
  74. // Add the batch to the registry so it will not be processed again.
  75. this._batchRegistry.add( batch );
  76. }, { priority: 'highest' } );
  77. this.listenTo( this._undoCommand, 'revert', ( evt, undoneBatch, undoingBatch ) => {
  78. this._redoCommand.addBatch( undoingBatch );
  79. } );
  80. }
  81. }