8
0

batch.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/batch
  7. */
  8. /**
  9. * `Batch` instance groups model changes ({@link module:engine/model/delta/delta~Delta deltas}). All deltas grouped in a single `Batch`
  10. * can be reverted together, so you can think about `Batch` as of a single undo step. If you want to extend given undo step you
  11. * can add more changes to the batch using {@link module:engine/model/model~Model#enqueueChange}:
  12. *
  13. * model.enqueueChange( batch, writer => {
  14. * writer.insertText( 'foo', paragraph, 'end' );
  15. * } );
  16. *
  17. * @see module:engine/model/model~Model#enqueueChange
  18. * @see module:engine/model/model~Model#change
  19. */
  20. export default class Batch {
  21. /**
  22. * Creates `Batch` instance.
  23. *
  24. * @see module:engine/model/model~Model#enqueueChange
  25. * @see module:engine/model/model~Model#change
  26. * @param {'transparent'|'default'} [type='default'] Type of the batch.
  27. */
  28. constructor( type = 'default' ) {
  29. /**
  30. * Array of deltas which compose this batch.
  31. *
  32. * @readonly
  33. * @type {Array.<module:engine/model/delta/delta~Delta>}
  34. */
  35. this.deltas = [];
  36. /**
  37. * Type of the batch.
  38. *
  39. * Can be one of the following values:
  40. * * `'default'` - all "normal" batches, most commonly used type.
  41. * * `'transparent'` - batch that should be ignored by other features, i.e. initial batch or collaborative editing changes.
  42. *
  43. * @readonly
  44. * @type {'transparent'|'default'}
  45. */
  46. this.type = type;
  47. }
  48. /**
  49. * Returns this batch base version, which is equal to the base version of first delta in the batch.
  50. * If there are no deltas in the batch, it returns `null`.
  51. *
  52. * @readonly
  53. * @type {Number|null}
  54. */
  55. get baseVersion() {
  56. return this.deltas.length > 0 ? this.deltas[ 0 ].baseVersion : null;
  57. }
  58. /**
  59. * Adds delta to the batch instance. All modification methods (insert, remove, split, etc.) use this method
  60. * to add created deltas.
  61. *
  62. * @param {module:engine/model/delta/delta~Delta} delta Delta to add.
  63. * @return {module:engine/model/delta/delta~Delta} Added delta.
  64. */
  65. addDelta( delta ) {
  66. delta.batch = this;
  67. this.deltas.push( delta );
  68. return delta;
  69. }
  70. /**
  71. * Gets an iterable collection of operations.
  72. *
  73. * @returns {Iterable.<module:engine/model/operation/operation~Operation>}
  74. */
  75. * getOperations() {
  76. for ( const delta of this.deltas ) {
  77. yield* delta.operations;
  78. }
  79. }
  80. }