batch.js 2.5 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 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 (which has base version set)
  50. * in the batch. If there are no deltas in the batch or neither delta has base version set, it returns `null`.
  51. *
  52. * @readonly
  53. * @type {Number|null}
  54. */
  55. get baseVersion() {
  56. for ( const delta of this.deltas ) {
  57. if ( delta.baseVersion !== null ) {
  58. return delta.baseVersion;
  59. }
  60. }
  61. return null;
  62. }
  63. /**
  64. * Adds delta to the batch instance. All modification methods (insert, remove, split, etc.) use this method
  65. * to add created deltas.
  66. *
  67. * @param {module:engine/model/delta/delta~Delta} delta Delta to add.
  68. * @return {module:engine/model/delta/delta~Delta} Added delta.
  69. */
  70. addDelta( delta ) {
  71. delta.batch = this;
  72. this.deltas.push( delta );
  73. return delta;
  74. }
  75. /**
  76. * Gets an iterable collection of operations.
  77. *
  78. * @returns {Iterable.<module:engine/model/operation/operation~Operation>}
  79. */
  80. * getOperations() {
  81. for ( const delta of this.deltas ) {
  82. yield* delta.operations;
  83. }
  84. }
  85. }