transaction-base.js 3.8 KB

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