8
0

deltareplayer.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/dev-utils/deltareplayer
  7. */
  8. /* global setTimeout, console */
  9. import DeltaFactory from '../model/delta/deltafactory';
  10. /**
  11. * DeltaReplayer is a dev-tool created for easily replaying operations on the document from stringified deltas.
  12. */
  13. export default class DeltaReplayer {
  14. /**
  15. * @param {module:engine/model/document~Document} document.
  16. * @param {String} logSeparator Separator between deltas.
  17. * @param {String} stringifiedDeltas Deltas to replay.
  18. */
  19. constructor( document, logSeparator, stringifiedDeltas ) {
  20. this._document = document;
  21. this._logSeparator = logSeparator;
  22. this.setStringifiedDeltas( stringifiedDeltas );
  23. }
  24. /**
  25. * @param {String} stringifiedDeltas Deltas to replay.
  26. */
  27. setStringifiedDeltas( stringifiedDeltas ) {
  28. if ( stringifiedDeltas === '' ) {
  29. this._deltasToReplay = [];
  30. return;
  31. }
  32. this._deltasToReplay = stringifiedDeltas
  33. .split( this._logSeparator )
  34. .map( stringifiedDelta => JSON.parse( stringifiedDelta ) );
  35. }
  36. getDeltasToReplay() {
  37. return this._deltasToReplay;
  38. }
  39. /**
  40. * Applies all deltas with delay between actions.
  41. *
  42. * @param {Number} timeInterval
  43. */
  44. play( timeInterval = 1000 ) {
  45. if ( this._deltasToReplay.length === 0 ) {
  46. return;
  47. }
  48. this.applyNextDelta().then( () => {
  49. setTimeout( () => this.play(), timeInterval );
  50. } );
  51. }
  52. /**
  53. * @param {Number} numberOfDeltas Number of deltas to apply.
  54. * @returns {Promise}
  55. */
  56. applyDeltas( numberOfDeltas ) {
  57. if ( numberOfDeltas <= 0 ) {
  58. return;
  59. }
  60. return this.applyNextDelta()
  61. .then( () => this.applyDeltas( numberOfDeltas - 1 ) )
  62. .catch( err => console.warn( err ) );
  63. }
  64. /**
  65. * @returns {Promise}
  66. */
  67. applyAllDeltas() {
  68. return this.applyNextDelta()
  69. .then( () => this.applyAllDeltas() )
  70. .catch( err => console.warn( err ) );
  71. }
  72. /**
  73. * @returns {Promise}
  74. */
  75. applyNextDelta() {
  76. const document = this._document;
  77. return new Promise( ( res, rej ) => {
  78. document.enqueueChanges( () => {
  79. const jsonDelta = this._deltasToReplay.shift();
  80. if ( !jsonDelta ) {
  81. return rej( new Error( 'No deltas to replay' ) );
  82. }
  83. const delta = DeltaFactory.fromJSON( jsonDelta, this._document );
  84. const batch = document.batch();
  85. batch.addDelta( delta );
  86. for ( const operation of delta.operations ) {
  87. document.applyOperation( operation );
  88. }
  89. res();
  90. } );
  91. } );
  92. }
  93. }