batch-base.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import CKEditorError from '../ckeditorerror.js';
  7. /**
  8. * Creates Batch instance. Not recommended to use directly, use {@link treeModel.Document#batch} instead.
  9. *
  10. * @param {treeModel.Document} doc Document which this Batch changes.
  11. *
  12. * @class core.treeModel.Batch
  13. * @classdesc
  14. * The Batch class groups document changes (deltas). All deltas grouped in a single Batch can be
  15. * reverted together, so you can think about the Batch as a single undo step. If you want to extend one
  16. * undo step you can call another method on the same Batch object. If you want to create a separate undo step
  17. * you can create a new Batch.
  18. *
  19. * For example to create two separate undo steps you can call:
  20. *
  21. * doc.batch().insert( firstPosition, 'foo' );
  22. * doc.batch().insert( secondPosition, 'bar' );
  23. *
  24. * To create a single undo step:
  25. *
  26. * const batch = doc.batch();
  27. * batch.insert( firstPosition, 'foo' );
  28. * batch.insert( secondPosition, 'bar' );
  29. *
  30. * Note that all document modification methods (insert, remove, split, etc.) are chainable so you can shorten code to:
  31. *
  32. * doc.batch().insert( firstPosition, 'foo' ).insert( secondPosition, 'bar' );
  33. *
  34. */
  35. export default class Batch {
  36. constructor( doc ) {
  37. /**
  38. * Document which this Batch changes.
  39. *
  40. * @readonly
  41. * @type {treeModel.Document}
  42. */
  43. this.doc = doc;
  44. /**
  45. * Array of deltas which compose Batch.
  46. *
  47. * @readonly
  48. * @type {Array.<treeModel.delta.Delta>}
  49. */
  50. this.deltas = [];
  51. }
  52. /**
  53. * Adds delta to the Batch instance. All modification methods (insert, remove, split, etc.) use this method
  54. * to add created deltas.
  55. *
  56. * @param {treeModel.delta.Delta} delta Delta to add.
  57. * @return {treeModel.delta.Delta} Added delta.
  58. */
  59. addDelta( delta ) {
  60. delta.batch = this;
  61. this.deltas.push( delta );
  62. return delta;
  63. }
  64. }
  65. /**
  66. * Function to register Batch methods. To make code scalable Batch do not have modification
  67. * methods built in. They can be registered using this method.
  68. *
  69. * This method checks if there is no naming collision and throws `batch-register-taken` if the method name
  70. * is already taken.
  71. *
  72. * Besides that no magic happens here, the method is added to the `Batch` class prototype.
  73. *
  74. * For example:
  75. *
  76. * Batch.register( 'insert', function( position, nodes ) {
  77. * // You can use a class inherit from Delta if that class should handle OT in the special way.
  78. * const delta = new Delta();
  79. *
  80. * // Create operations which should be components of this delta.
  81. * const operation = new InsertOperation( position, nodes, this.doc.version );
  82. *
  83. * // Remember to apply every operation, no magic, you need to do it manually.
  84. * this.doc.applyOperation( operation );
  85. *
  86. * // Add operation to the delta.
  87. * delta.addOperation( operation );
  88. *
  89. * // Add delta to the Batch instance.
  90. * this.addDelta( delta );
  91. *
  92. * // Make this method chainable.
  93. * return this;
  94. * } );
  95. *
  96. * @param {String} name Method name.
  97. * @param {Function} creator Method body.
  98. */
  99. export function register( name, creator ) {
  100. if ( Batch.prototype[ name ] ) {
  101. /**
  102. * This batch method name is already taken.
  103. *
  104. * @error batch-register-taken
  105. * @param {String} name
  106. */
  107. throw new CKEditorError(
  108. 'batch-register-taken: This batch method name is already taken.',
  109. { name: name } );
  110. }
  111. Batch.prototype[ name ] = creator;
  112. }