8
0

batch.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 call another method on the same `Batch` object. If you want to create a separate undo step you can create a new `Batch`.
  12. *
  13. * For example to create two separate undo steps you can call:
  14. *
  15. * doc.batch().insert( 'foo', firstPosition );
  16. * doc.batch().insert( 'bar', secondPosition );
  17. *
  18. * To create a single undo step:
  19. *
  20. * const batch = doc.batch();
  21. * batch.insert( 'foo', firstPosition );
  22. * batch.insert( 'bar', secondPosition );
  23. *
  24. */
  25. export default class Batch {
  26. /**
  27. * Creates `Batch` instance. Not recommended to use directly, use {@link module:engine/model~model#change} or
  28. * {@link module:engine/model~model#enqueueChanges} instead.
  29. *
  30. * @param {'transparent'|'default'} [type='default'] Type of the batch.
  31. */
  32. constructor( type = 'default' ) {
  33. /**
  34. * Array of deltas which compose this batch.
  35. *
  36. * @readonly
  37. * @type {Array.<module:engine/model/delta/delta~Delta>}
  38. */
  39. this.deltas = [];
  40. /**
  41. * Type of the batch.
  42. *
  43. * Can be one of the following values:
  44. * * `'default'` - all "normal" batches, most commonly used type.
  45. * * `'transparent'` - batch that should be ignored by other features, i.e. initial batch or collaborative editing changes.
  46. *
  47. * @readonly
  48. * @type {'transparent'|'default'}
  49. */
  50. this.type = type;
  51. }
  52. /**
  53. * Returns this batch base version, which is equal to the base version of first delta in the batch.
  54. * If there are no deltas in the batch, it returns `null`.
  55. *
  56. * @readonly
  57. * @type {Number|null}
  58. */
  59. get baseVersion() {
  60. return this.deltas.length > 0 ? this.deltas[ 0 ].baseVersion : null;
  61. }
  62. /**
  63. * Adds delta to the batch instance. All modification methods (insert, remove, split, etc.) use this method
  64. * to add created deltas.
  65. *
  66. * @param {module:engine/model/delta/delta~Delta} delta Delta to add.
  67. * @return {module:engine/model/delta/delta~Delta} Added delta.
  68. */
  69. addDelta( delta ) {
  70. delta.batch = this;
  71. this.deltas.push( delta );
  72. return delta;
  73. }
  74. /**
  75. * Gets an iterable collection of operations.
  76. *
  77. * @returns {Iterable.<module:engine/model/operation/operation~Operation>}
  78. */
  79. * getOperations() {
  80. for ( const delta of this.deltas ) {
  81. yield* delta.operations;
  82. }
  83. }
  84. }