transform.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/delta/transform
  7. */
  8. import operationTransform from '../operation/transform.js';
  9. import arrayUtils from '../../../utils/lib/lodash/array.js';
  10. const specialCases = new Map();
  11. /**
  12. * Transforms given {@link module:engine/model/delta/delta~Delta delta} by another {@link module:engine/model/delta/delta~Delta delta} and
  13. * returns the result of that transformation as an array containing one or more {@link module:engine/model/delta/delta~Delta delta}
  14. * instances.
  15. *
  16. * Delta transformations heavily base on {@link module:engine/model/operation/transform~transform operational transformations}. Since
  17. * delta is a list of operations most situations can be handled thanks to operational transformation. Unfortunately,
  18. * deltas are more complicated than operations and have they semantic meaning, as they represent user's editing intentions.
  19. *
  20. * Sometimes, simple operational transformation on deltas' operations might result in some unexpected results. Those
  21. * results would be fine from OT point of view, but would not reflect user's intentions. Because of such conflicts
  22. * we need to handle transformations in special cases in a custom way.
  23. *
  24. * The function itself looks whether two given delta types have a special case function registered. If so, the deltas are
  25. * transformed using that function. If not, {@link module:engine/model/delta/transform~defaultTransform default transformation algorithm}
  26. * is used.
  27. *
  28. * @param {module:engine/model/delta/delta~Delta} a Delta that will be transformed.
  29. * @param {module:engine/model/delta/delta~Delta} b Delta to transform by.
  30. * @param {Boolean} isAMoreImportantThanB Flag indicating whether the delta which will be transformed (`a`) should be treated
  31. * as more important when resolving conflicts. Note that this flag is used only if provided deltas have same
  32. * {@link module:engine/model/delta/delta~Delta._priority priority}. If deltas have different priorities, their importance is resolved
  33. * automatically and overwrites this flag.
  34. * @returns {Array.<module:engine/model/delta/delta~Delta>} Result of the transformation.
  35. */
  36. export default function transform( a, b, isAMoreImportantThanB ) {
  37. const transformAlgorithm = getTransformationCase( a, b ) || defaultTransform;
  38. const transformed = transformAlgorithm( a, b, isAMoreImportantThanB );
  39. const baseVersion = arrayUtils.last( b.operations ).baseVersion;
  40. return updateBaseVersion( baseVersion, transformed );
  41. }
  42. // Updates base versions of operations inside deltas (which are the results of delta transformation).
  43. function updateBaseVersion( baseVersion, deltas ) {
  44. for ( let delta of deltas ) {
  45. for ( let op of delta.operations ) {
  46. op.baseVersion = ++baseVersion;
  47. }
  48. }
  49. return deltas;
  50. }
  51. /**
  52. * The default delta transformation function. It is used for those deltas that are not in special case conflict.
  53. *
  54. * This algorithm is similar to a popular `dOPT` algorithm used in operational transformation, as we are in fact
  55. * transforming two sets of operations by each other.
  56. *
  57. * @param {module:engine/model/delta/delta~Delta} a Delta that will be transformed.
  58. * @param {module:engine/model/delta/delta~Delta} b Delta to transform by.
  59. * @param {Boolean} isAMoreImportantThanB Flag indicating whether the delta which will be transformed (`a`) should be treated
  60. * as more important when resolving conflicts. Note that this flag is used only if provided deltas have same
  61. * {@link module:engine/model/delta/delta~Delta._priority priority}. If deltas have different priorities, their importance is resolved
  62. * automatically and overwrites this flag.
  63. * @returns {Array.<module:engine/model/delta/delta~Delta>} Result of the transformation, that is an array with single delta instance.
  64. */
  65. export function defaultTransform( a, b, isAMoreImportantThanB ) {
  66. // First, resolve the flag real value.
  67. isAMoreImportantThanB = getPriority( a.constructor, b.constructor, isAMoreImportantThanB );
  68. // Create a new delta instance. Make sure that the new delta is of same type as transformed delta.
  69. // We will transform operations in that delta but it doesn't mean the delta's "meaning" which is connected to
  70. // the delta's type. Since the delta's type is heavily used in transformations and probably other parts
  71. // of system it is important to keep proper delta type through all transformation process.
  72. const transformed = new a.constructor();
  73. // Array containing operations that we will transform by. At the beginning these are just operations from
  74. let byOps = b.operations;
  75. // This array is storing operations from `byOps` which got transformed by operation from delta `a`.
  76. let newByOps = [];
  77. // We take each operation from original set of operations to transform.
  78. for ( let opA of a.operations ) {
  79. // We wrap the operation in the array. This is important, because operation transformation algorithm returns
  80. // an array of operations so we need to make sure that our algorithm is ready to handle arrays.
  81. const ops = [ opA ];
  82. // Now the real algorithm takes place.
  83. for ( let opB of byOps ) {
  84. // For each operation that we need transform by...
  85. for ( let i = 0; i < ops.length; i++ ) {
  86. // We take each operation to transform...
  87. const op = ops[ i ];
  88. // And transform both of them by themselves.
  89. // The result of transforming operation from delta B by operation from delta A is saved in
  90. // `newByOps` array. We will use that array for transformations in next loops. We need delta B
  91. // operations after transformed by delta A operations to get correct results of transformations
  92. // of next operations from delta A.
  93. //
  94. // It's like this because 2nd operation from delta A assumes that 1st operation from delta A
  95. // is "already applied". When we transform 2nd operation from delta A by operations from delta B
  96. // we have to be sure that operations from delta B are in a state that acknowledges 1st operation
  97. // from delta A.
  98. //
  99. // This can be easier understood when operations sets to transform are represented by diamond diagrams:
  100. // http://www.codecommit.com/blog/java/understanding-and-applying-operational-transformation
  101. // Using push.apply because operationTransform function is returning an array with one or multiple results.
  102. Array.prototype.push.apply( newByOps, operationTransform( opB, op, !isAMoreImportantThanB ) );
  103. // Then, we transform operation from delta A by operation from delta B.
  104. const results = operationTransform( op, opB, isAMoreImportantThanB );
  105. // We replace currently processed operation from `ops` array by the results of transformation.
  106. // Note, that we process single operation but the operationTransform result might be an array, so we
  107. // might splice-in more operations. We will process them further in next iterations. Right now we
  108. // just save them in `ops` array and move `i` pointer by proper offset.
  109. Array.prototype.splice.apply( ops, [ i, 1 ].concat( results ) );
  110. i += results.length - 1;
  111. }
  112. // At this point a single operation from delta A got transformed by a single operation from delta B.
  113. // The transformation result is in `ops` array and it may be one or more operations. This was just the first step.
  114. // Operation from delta A has to be further transformed by the other operations from delta B.
  115. // So in next iterator loop we will take another operation from delta B and use transformed delta A (`ops`)
  116. // to transform it further.
  117. }
  118. // We got through all delta B operations and have a final transformed state of an operation from delta A.
  119. // As previously mentioned, we substitute operations from delta B by their transformed equivalents.
  120. byOps = newByOps;
  121. newByOps = [];
  122. // We add transformed operation from delta A to newly created delta.
  123. // Remember that transformed operation from delta A may consist of multiple operations.
  124. for ( let op of ops ) {
  125. transformed.addOperation( op );
  126. }
  127. // In next loop, we will take another operation from delta A and transform it through (transformed) operations
  128. // from delta B...
  129. }
  130. return [ transformed ];
  131. }
  132. /**
  133. * Adds a special case callback for given delta classes.
  134. *
  135. * @external module:engine/model/delta/transform~transform
  136. * @function module:engine/model/delta/transform~transform.addTransformationCase
  137. * @param {Function} A Delta constructor which instance will get transformed.
  138. * @param {Function} B Delta constructor which instance will be transformed by.
  139. * @param {Function} resolver A callback that will handle custom special case transformation for instances of given delta classes.
  140. */
  141. export function addTransformationCase( A, B, resolver ) {
  142. let casesA = specialCases.get( A );
  143. if ( !casesA ) {
  144. casesA = new Map();
  145. specialCases.set( A, casesA );
  146. }
  147. casesA.set( B, resolver );
  148. }
  149. /**
  150. * Gets a special case callback which was previously {@link module:engine/model/delta/transform~transform.addTransformationCase added}.
  151. *
  152. * @param {module:engine/model/delta/delta~Delta} a Delta to transform.
  153. * @param {module:engine/model/delta/delta~Delta} b Delta to be transformed by.
  154. */
  155. export function getTransformationCase( a, b ) {
  156. let casesA = specialCases.get( a.constructor );
  157. // If there are no special cases registered for class which `a` is instance of, we will
  158. // check if there are special cases registered for any parent class.
  159. if ( !casesA || !casesA.get( b.constructor ) ) {
  160. const cases = specialCases.keys();
  161. for ( let caseClass of cases ) {
  162. if ( a instanceof caseClass && specialCases.get( caseClass ).get( b.constructor ) ) {
  163. casesA = specialCases.get( caseClass );
  164. break;
  165. }
  166. }
  167. }
  168. if ( casesA ) {
  169. return casesA.get( b.constructor );
  170. }
  171. return undefined;
  172. }
  173. // Checks priorities of passed constructors and decides which one is more important.
  174. // If both priorities are same, value passed in `isAMoreImportantThanB` parameter is used.
  175. function getPriority( A, B, isAMoreImportantThanB ) {
  176. if ( A._priority > B._priority ) {
  177. return true;
  178. } else if ( A._priority < B._priority ) {
  179. return false;
  180. } else {
  181. return isAMoreImportantThanB;
  182. }
  183. }