8
0

batch-base.js 3.6 KB

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