redocommand.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/redocommand
  7. */
  8. import BaseCommand from './basecommand';
  9. /**
  10. * The redo command stores {@link module:engine/model/batch~Batch batches} that were used to undo a batch by
  11. * {@link module:undo/undocommand~UndoCommand}. It is able to redo a previously undone batch by reversing the undoing
  12. * batches created by `UndoCommand`. The reversed batch is transformed by all the batches from
  13. * {@link module:engine/model/document~Document#history history} that happened after the reversed undo batch.
  14. *
  15. * The redo command also takes care of restoring the {@link module:engine/model/document~Document#selection document selection}.
  16. *
  17. * @extends module:undo/basecommand~BaseCommand
  18. */
  19. export default class RedoCommand extends BaseCommand {
  20. /**
  21. * Executes the command. This method reverts the last {@link module:engine/model/batch~Batch batch} added to
  22. * the command's stack, applies the reverted and transformed version on the
  23. * {@link module:engine/model/document~Document document} and removes the batch from the stack.
  24. * Then, it restores the {@link module:engine/model/document~Document#selection document selection}.
  25. *
  26. * @fires execute
  27. */
  28. execute() {
  29. const item = this._stack.pop();
  30. const redoingBatch = this.editor.model.createBatch( 'transparent' );
  31. // All changes have to be done in one `enqueueChange` callback so other listeners will not step between consecutive
  32. // operations, or won't do changes to the document before selection is properly restored.
  33. this.editor.model.enqueueChange( redoingBatch, () => {
  34. const lastOperation = item.batch.operations[ item.batch.operations.length - 1 ];
  35. const nextBaseVersion = lastOperation.baseVersion + 1;
  36. const operations = this.editor.model.document.history.getOperations( nextBaseVersion );
  37. this._restoreSelection( item.selection.ranges, item.selection.isBackward, operations );
  38. this._undo( item.batch, redoingBatch );
  39. } );
  40. this.refresh();
  41. }
  42. }