8
0

delta.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * Base class for all deltas.
  8. *
  9. * Delta is a single, from the user action point of view, change in the editable document, like insert, split or
  10. * rename element. Delta is composed of operations, which are unit changes needed to be done to execute user action.
  11. *
  12. * Multiple deltas are grouped into a single {@link core.treeModel.Batch}.
  13. *
  14. * @memberOf core.treeModel.delta
  15. */
  16. export default class Delta {
  17. /**
  18. * Creates a delta instance.
  19. */
  20. constructor() {
  21. /**
  22. * {@link core.treeModel.Batch} which delta is a part of. This property is null by default and set by the
  23. * {@link core.treeModel.Batch#addDelta} method.
  24. *
  25. * @readonly
  26. * @member {core.treeModel.Batch} core.treeModel.delta.Delta#batch
  27. */
  28. this.batch = null;
  29. /**
  30. * Array of operations which compose delta.
  31. *
  32. * @readonly
  33. * @member {core.treeModel.operation.Operation[]} core.treeModel.delta.Delta#operations
  34. */
  35. this.operations = [];
  36. }
  37. /**
  38. * A class that will be used when creating reversed delta.
  39. *
  40. * @private
  41. * @type {Object}
  42. */
  43. get _reverseDeltaClass() {
  44. return Delta;
  45. }
  46. /**
  47. * Add operation to the delta.
  48. *
  49. * @param {core.treeModel.operation.Operation} operation Operation instance.
  50. */
  51. addOperation( operation ) {
  52. operation.delta = this;
  53. this.operations.push( operation );
  54. return operation;
  55. }
  56. /**
  57. * Creates and returns a delta that has the same parameters as this delta.
  58. *
  59. * @returns {core.treeModel.delta.Delta} Clone of this delta.
  60. */
  61. clone() {
  62. let delta = new this.constructor();
  63. delta.batch = this.batch;
  64. for ( let op of this.operations ) {
  65. delta.addOperation( op.clone() );
  66. }
  67. return delta;
  68. }
  69. /**
  70. * Creates and returns a reverse delta. Reverse delta when executed right after the original delta will bring back
  71. * tree model state to the point before the original delta execution. In other words, it reverses changes done
  72. * by the original delta.
  73. *
  74. * Keep in mind that tree model state may change since executing the original delta, so reverse delta may be "outdated".
  75. * In that case you will need to {@link core.treeModel.delta.transform} it by all deltas that were executed after
  76. * the original delta.
  77. *
  78. * @returns {core.treeModel.delta.Delta} Reversed delta.
  79. */
  80. getReversed() {
  81. let delta = new this._reverseDeltaClass();
  82. for ( let op of this.operations ) {
  83. let reversedOp = op.getReversed();
  84. reversedOp.baseVersion += this.operations.length - 1;
  85. delta.addOperation( reversedOp );
  86. }
  87. delta.operations.reverse();
  88. return delta;
  89. }
  90. /**
  91. * Delta priority. Used in {@link core.treeModel.delta.transform delta transformations}. Delta with the higher
  92. * priority will be treated as more important when resolving transformation conflicts. If deltas have same
  93. * priority, other factors will be used to determine which delta is more important.
  94. *
  95. * @private
  96. * @type {Number}
  97. */
  98. static get _priority() {
  99. return 0;
  100. }
  101. }