undoengine.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module undo/undoengine
  7. */
  8. import Plugin from '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/document~Document#batch Batch 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.set( 'undo', this._undoCommand );
  56. this.editor.commands.set( 'redo', this._redoCommand );
  57. this.listenTo( this.editor.document, 'change', ( evt, type, changes, batch ) => {
  58. // If changes are not a part of a batch or this is not a new batch, omit those changes.
  59. if ( this._batchRegistry.has( batch ) || batch.type == 'transparent' ) {
  60. return;
  61. } else {
  62. if ( this._redoCommand._createdBatches.has( batch ) ) {
  63. // If this batch comes from `redoCommand`, add it to `undoCommand` stack.
  64. this._undoCommand.addBatch( batch );
  65. } else if ( !this._undoCommand._createdBatches.has( batch ) ) {
  66. // A default batch - these are new changes in the document, not introduced by undo feature.
  67. // Add them to `undoCommand` stack and clear `redoCommand` stack.
  68. this._undoCommand.addBatch( batch );
  69. this._redoCommand.clearStack();
  70. }
  71. }
  72. // Add the batch to the registry so it will not be processed again.
  73. this._batchRegistry.add( batch );
  74. }, { priority: 'highest' } );
  75. this.listenTo( this._undoCommand, 'revert', ( evt, undoneBatch, undoingBatch ) => {
  76. this._redoCommand.addBatch( undoingBatch );
  77. } );
  78. }
  79. }