batch.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/model/batch
  7. */
  8. /**
  9. * A batch instance groups model changes ({@link module:engine/model/operation/operation~Operation operations}). All operations
  10. * grouped in a single batch can be reverted together, so you can also think about a batch as of a single undo step. If you want
  11. * to extend a given undo step, you 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 a 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'] The type of the batch.
  27. */
  28. constructor( type = 'default' ) {
  29. /**
  30. * An array of operations that compose this batch.
  31. *
  32. * @readonly
  33. * @type {Array.<module:engine/model/operation/operation~Operation>}
  34. */
  35. this.operations = [];
  36. /**
  37. * The type of the batch.
  38. *
  39. * It can be one of the following values:
  40. * * `'default'` &ndash; All "normal" batches. This is the most commonly used type.
  41. * * `'transparent'` &ndash; A batch that should be ignored by other features, i.e. an initial batch or collaborative editing
  42. * changes.
  43. *
  44. * @readonly
  45. * @type {'transparent'|'default'}
  46. */
  47. this.type = type;
  48. }
  49. /**
  50. * Returns the base version of this batch, which is equal to the base version of the first operation in the batch.
  51. * If there are no operations in the batch or neither operation has the base version set, it returns `null`.
  52. *
  53. * @readonly
  54. * @type {Number|null}
  55. */
  56. get baseVersion() {
  57. for ( const op of this.operations ) {
  58. if ( op.baseVersion !== null ) {
  59. return op.baseVersion;
  60. }
  61. }
  62. return null;
  63. }
  64. /**
  65. * Adds an operation to the batch instance.
  66. *
  67. * @param {module:engine/model/operation/operation~Operation} operation An operation to add.
  68. * @returns {module:engine/model/operation/operation~Operation} The added operation.
  69. */
  70. addOperation( operation ) {
  71. operation.batch = this;
  72. this.operations.push( operation );
  73. return operation;
  74. }
  75. }