basic-transformations.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/delta/basic-transformations
  7. */
  8. import deltaTransform from './transform';
  9. const addTransformationCase = deltaTransform.addTransformationCase;
  10. const defaultTransform = deltaTransform.defaultTransform;
  11. import Range from '../range';
  12. import Position from '../position';
  13. import NoOperation from '../operation/nooperation';
  14. import AttributeOperation from '../operation/attributeoperation';
  15. import InsertOperation from '../operation/insertoperation';
  16. import ReinsertOperation from '../operation/reinsertoperation';
  17. import Delta from './delta';
  18. import AttributeDelta from './attributedelta';
  19. import InsertDelta from './insertdelta';
  20. import MarkerDelta from './markerdelta';
  21. import MergeDelta from './mergedelta';
  22. import MoveDelta from './movedelta';
  23. import SplitDelta from './splitdelta';
  24. import WeakInsertDelta from './weakinsertdelta';
  25. import WrapDelta from './wrapdelta';
  26. import UnwrapDelta from './unwrapdelta';
  27. import RenameDelta from './renamedelta';
  28. import RemoveDelta from './removedelta';
  29. import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
  30. // Provide transformations for default deltas.
  31. // Add special case for AttributeDelta x WeakInsertDelta transformation.
  32. addTransformationCase( AttributeDelta, WeakInsertDelta, ( a, b, context ) => {
  33. // If nodes are weak-inserted into attribute delta range, we need to apply changes from attribute delta on them.
  34. // So first we do the normal transformation and if this special cases happens, we will add an extra delta.
  35. const deltas = defaultTransform( a, b, context );
  36. if ( a.range.containsPosition( b.position ) ) {
  37. deltas.push( _getComplementaryAttrDelta( b, a ) );
  38. }
  39. return deltas;
  40. } );
  41. // Add special case for AttributeDelta x SplitDelta transformation.
  42. addTransformationCase( AttributeDelta, SplitDelta, ( a, b, context ) => {
  43. // Do not apply special transformation case if `SplitDelta` has `NoOperation` as the second operation.
  44. if ( !b.position ) {
  45. return defaultTransform( a, b, context );
  46. }
  47. const undoMode = context.undoMode;
  48. const splitPosition = new Position( b.position.root, b.position.path.slice( 0, -1 ) );
  49. const deltas = defaultTransform( a, b, context );
  50. // Special case applies only if undo is not a context and only if `SplitDelta` has `InsertOperation` (not `ReinsertOperation`).
  51. if ( undoMode || !( b._cloneOperation instanceof InsertOperation ) ) {
  52. return deltas;
  53. }
  54. for ( const operation of a.operations ) {
  55. // If a node that has been split has it's attribute updated, we should also update attribute of
  56. // the node created during splitting.
  57. if ( operation.range.containsPosition( splitPosition ) || operation.range.start.isEqual( splitPosition ) ) {
  58. const additionalAttributeDelta = new AttributeDelta();
  59. const rangeStart = splitPosition.getShiftedBy( 1 );
  60. const rangeEndPath = rangeStart.path.slice();
  61. rangeEndPath.push( 0 );
  62. const rangeEnd = new Position( rangeStart.root, rangeEndPath );
  63. const oldValue = b._cloneOperation.nodes.getNode( 0 ).getAttribute( operation.key );
  64. additionalAttributeDelta.addOperation( new AttributeOperation(
  65. new Range( rangeStart, rangeEnd ),
  66. operation.key,
  67. oldValue === undefined ? null : oldValue,
  68. operation.newValue,
  69. 0
  70. ) );
  71. deltas.push( additionalAttributeDelta );
  72. break;
  73. }
  74. }
  75. return deltas;
  76. } );
  77. // Add special case for InsertDelta x MergeDelta transformation.
  78. addTransformationCase( InsertDelta, MergeDelta, ( a, b, context ) => {
  79. // Do not apply special transformation case if `MergeDelta` has `NoOperation` as the second operation.
  80. if ( !b.position ) {
  81. return defaultTransform( a, b, context );
  82. }
  83. const undoMode = context.undoMode;
  84. // If insert is applied at the same position where merge happened, we reverse the merge (we treat it like it
  85. // didn't happen) and then apply the original insert operation. This is "mirrored" in MergeDelta x InsertDelta
  86. // transformation below, where we simply do not apply MergeDelta.
  87. if ( !undoMode && a.position.isEqual( b.position ) ) {
  88. return [
  89. b.getReversed(),
  90. a.clone()
  91. ];
  92. }
  93. return defaultTransform( a, b, context );
  94. } );
  95. function transformMarkerDelta( a, b ) {
  96. const transformedDelta = a.clone();
  97. const transformedOp = transformedDelta.operations[ 0 ];
  98. if ( transformedOp.oldRange ) {
  99. transformedOp.oldRange = transformedOp.oldRange.getTransformedByDelta( b )[ 0 ];
  100. }
  101. if ( transformedOp.newRange ) {
  102. transformedOp.newRange = transformedOp.newRange.getTransformedByDelta( b )[ 0 ];
  103. }
  104. return [ transformedDelta ];
  105. }
  106. addTransformationCase( MarkerDelta, SplitDelta, transformMarkerDelta );
  107. addTransformationCase( MarkerDelta, MergeDelta, transformMarkerDelta );
  108. addTransformationCase( MarkerDelta, WrapDelta, transformMarkerDelta );
  109. addTransformationCase( MarkerDelta, UnwrapDelta, transformMarkerDelta );
  110. addTransformationCase( MarkerDelta, MoveDelta, transformMarkerDelta );
  111. addTransformationCase( MarkerDelta, RenameDelta, transformMarkerDelta );
  112. // Add special case for MoveDelta x MergeDelta transformation.
  113. addTransformationCase( MoveDelta, MergeDelta, ( a, b, context ) => {
  114. const undoMode = context.undoMode;
  115. // Do not apply special transformation case in undo mode or if `MergeDelta` has `NoOperation` as the second operation.
  116. if ( undoMode || !b.position ) {
  117. return defaultTransform( a, b, context );
  118. }
  119. // If move delta is supposed to move a node that has been merged, we reverse the merge (we treat it like it
  120. // didn't happen) and then apply the original move operation. This is "mirrored" in MergeDelta x MoveDelta
  121. // transformation below, where we simply do not apply MergeDelta.
  122. const operateInSameParent =
  123. a.sourcePosition.root == b.position.root &&
  124. compareArrays( a.sourcePosition.getParentPath(), b.position.getParentPath() ) === 'same';
  125. const mergeInsideMoveRange = a.sourcePosition.offset <= b.position.offset && a.sourcePosition.offset + a.howMany > b.position.offset;
  126. if ( operateInSameParent && mergeInsideMoveRange ) {
  127. return [
  128. b.getReversed(),
  129. a.clone()
  130. ];
  131. }
  132. return defaultTransform( a, b, context );
  133. } );
  134. // Add special case for MergeDelta x InsertDelta transformation.
  135. addTransformationCase( MergeDelta, InsertDelta, ( a, b, context ) => {
  136. // Do not apply special transformation case if `MergeDelta` has `NoOperation` as the second operation.
  137. if ( !a.position ) {
  138. return defaultTransform( a, b, context );
  139. }
  140. const undoMode = context.undoMode;
  141. // If merge is applied at the same position where we inserted a range of nodes we cancel the merge as it's results
  142. // may be unexpected and very weird. Even if we do some "magic" we don't know what really are users' expectations.
  143. if ( !undoMode && a.position.isEqual( b.position ) ) {
  144. return [ noDelta() ];
  145. }
  146. return defaultTransform( a, b, context );
  147. } );
  148. // Add special case for MergeDelta x MoveDelta transformation.
  149. addTransformationCase( MergeDelta, MoveDelta, ( a, b, context ) => {
  150. const undoMode = context.undoMode;
  151. // Do not apply special transformation case in undo mode or if `MergeDelta` has `NoOperation` as the second operation.
  152. if ( undoMode || !a.position ) {
  153. return defaultTransform( a, b, context );
  154. }
  155. // If merge is applied at the position between moved nodes we cancel the merge as it's results may be unexpected and
  156. // very weird. Even if we do some "magic" we don't know what really are users' expectations.
  157. const operateInSameParent =
  158. a.position.root == b.sourcePosition.root &&
  159. compareArrays( a.position.getParentPath(), b.sourcePosition.getParentPath() ) === 'same';
  160. const mergeInsideMoveRange = b.sourcePosition.offset <= a.position.offset && b.sourcePosition.offset + b.howMany > a.position.offset;
  161. if ( operateInSameParent && mergeInsideMoveRange ) {
  162. return [ noDelta() ];
  163. }
  164. return defaultTransform( a, b, context );
  165. } );
  166. addTransformationCase( SplitDelta, SplitDelta, ( a, b, context ) => {
  167. const undoMode = context.undoMode;
  168. // Do not apply special transformation case if transformation is in undo mode.
  169. if ( undoMode ) {
  170. return defaultTransform( a, b, context );
  171. }
  172. // Do not apply special transformation case if `SplitDelta` has `NoOperation` as the second operation.
  173. if ( !a.position || !b.position ) {
  174. return defaultTransform( a, b, context );
  175. }
  176. const pathA = a.position.getParentPath();
  177. const pathB = b.position.getParentPath();
  178. // The special case is for splits inside the same parent.
  179. if ( a.position.root == b.position.root && compareArrays( pathA, pathB ) == 'same' ) {
  180. a = a.clone();
  181. if ( a.position.offset < b.position.offset || ( a.position.offset == b.position.offset && context.isStrong ) ) {
  182. // If both first operations are `ReinsertOperation`s, we might need to transform `a._cloneOperation`,
  183. // so it will take correct node from graveyard.
  184. if (
  185. a._cloneOperation instanceof ReinsertOperation && b._cloneOperation instanceof ReinsertOperation &&
  186. a._cloneOperation.sourcePosition.offset > b._cloneOperation.sourcePosition.offset
  187. ) {
  188. a._cloneOperation.sourcePosition = a._cloneOperation.sourcePosition.getShiftedBy( -1 );
  189. }
  190. // `a` splits closer or at same offset.
  191. // Change how many nodes are moved. Do not move nodes that were moved by delta `b`.
  192. const aRange = Range.createFromPositionAndShift( a.position, a._moveOperation.howMany );
  193. const bRange = Range.createFromPositionAndShift( b.position, b._moveOperation.howMany );
  194. const diff = aRange.getDifference( bRange );
  195. let newHowMany = 0;
  196. for ( const range of diff ) {
  197. newHowMany += range.end.offset - range.start.offset;
  198. }
  199. if ( newHowMany === 0 ) {
  200. a.operations.pop(); // Remove last operation (`MoveOperation`).
  201. a.addOperation( new NoOperation( a.operations[ 0 ].baseVersion + 1 ) ); // Add `NoOperation` instead.
  202. } else {
  203. a.operations[ 1 ].howMany = newHowMany;
  204. }
  205. return [ a ];
  206. } else {
  207. // `a` splits further.
  208. // This is more complicated case, thankfully we can solve it using default transformation and setting proper context.
  209. const newContext = Object.assign( {}, context );
  210. newContext.isStrong = true;
  211. newContext.insertBefore = true;
  212. return defaultTransform( a, b, newContext );
  213. }
  214. }
  215. return defaultTransform( a, b, context );
  216. } );
  217. // Add special case for SplitDelta x UnwrapDelta transformation.
  218. addTransformationCase( SplitDelta, UnwrapDelta, ( a, b, context ) => {
  219. // Do not apply special transformation case if `SplitDelta` has `NoOperation` as the second operation.
  220. if ( !a.position ) {
  221. return defaultTransform( a, b, context );
  222. }
  223. // If incoming split delta tries to split a node that just got unwrapped, there is actually nothing to split,
  224. // so we discard that delta.
  225. if ( a.position.root == b.position.root && compareArrays( b.position.path, a.position.getParentPath() ) === 'same' ) {
  226. return [ noDelta() ];
  227. }
  228. return defaultTransform( a, b, context );
  229. } );
  230. // Add special case for SplitDelta x WrapDelta transformation.
  231. addTransformationCase( SplitDelta, WrapDelta, ( a, b, context ) => {
  232. // Do not apply special transformation case if `SplitDelta` has `NoOperation` as the second operation.
  233. if ( !a.position ) {
  234. return defaultTransform( a, b, context );
  235. }
  236. // If split is applied at the position between wrapped nodes, we cancel the split as it's results may be unexpected and
  237. // very weird. Even if we do some "magic" we don't know what really are users' expectations.
  238. const sameRoot = a.position.root == b.range.start.root;
  239. const operateInSameParent = sameRoot && compareArrays( a.position.getParentPath(), b.range.start.getParentPath() ) === 'same';
  240. const splitInsideWrapRange = b.range.start.offset < a.position.offset && b.range.end.offset >= a.position.offset;
  241. if ( operateInSameParent && splitInsideWrapRange ) {
  242. return [ noDelta() ];
  243. } else if ( sameRoot && compareArrays( a.position.getParentPath(), b.range.end.getShiftedBy( -1 ).path ) === 'same' ) {
  244. // Split position is directly inside the last node from wrap range.
  245. // If that's the case, we manually change split delta so it will "target" inside the wrapping element.
  246. // By doing so we will be inserting split node right to the original node which feels natural and is a good UX.
  247. const delta = a.clone();
  248. // 1. Fix insert operation position.
  249. // Node to split is the last children of the wrapping element.
  250. // Wrapping element is the element inserted by WrapDelta (re)insert operation.
  251. // It is inserted after the wrapped range, but the wrapped range will be moved inside it.
  252. // Having this in mind, it is correct to use wrapped range start position as the position before wrapping element.
  253. // Now, `splitNodePos` points before wrapping element.
  254. // To get a position before last children of that element, we expand position's `path` member by proper offset.
  255. const splitPath = b.range.start.path.slice();
  256. splitPath.push( b.howMany - 1 );
  257. const splitNodePos = new Position( b.range.start.root, splitPath );
  258. // SplitDelta insert operation position should be right after the node we split.
  259. delta._cloneOperation.position = splitNodePos.getShiftedBy( 1 );
  260. // 2. Fix move operation source position.
  261. // Nodes moved by SplitDelta will be moved from new position, modified by WrapDelta.
  262. // To obtain that new position, `splitNodePos` will be used, as this is the node we are extracting children from.
  263. // Nothing changed inside split node so it is correct to use the original split position offset.
  264. const sourcePath = splitNodePos.path.slice();
  265. sourcePath.push( a.position.offset );
  266. delta._moveOperation.sourcePosition = new Position( splitNodePos.root, sourcePath );
  267. // 3. Fix move operation target position.
  268. // SplitDelta move operation target position should be inside the node inserted by operation above.
  269. // Since the node is empty, we will insert at offset 0.
  270. const targetPath = splitNodePos.getShiftedBy( 1 ).path.slice();
  271. targetPath.push( 0 );
  272. delta._moveOperation.targetPosition = new Position( splitNodePos.root, targetPath );
  273. return [ delta ];
  274. }
  275. return defaultTransform( a, b, context );
  276. } );
  277. // Add special case for SplitDelta x WrapDelta transformation.
  278. addTransformationCase( SplitDelta, AttributeDelta, ( a, b, context ) => {
  279. // Do not apply special transformation case if `SplitDelta` has `NoOperation` as the second operation.
  280. if ( !a.position ) {
  281. return defaultTransform( a, b, context );
  282. }
  283. a = a.clone();
  284. const undoMode = context.undoMode;
  285. const splitPosition = new Position( a.position.root, a.position.path.slice( 0, -1 ) );
  286. // Special case applies only if undo is not a context and only if `SplitDelta` has `InsertOperation` (not `ReinsertOperation`).
  287. if ( undoMode || !( a._cloneOperation instanceof InsertOperation ) ) {
  288. return [ a ];
  289. }
  290. // If element to split had it's attribute changed, we have to reflect this change in an element
  291. // that is in SplitDelta's InsertOperation.
  292. for ( const operation of b.operations ) {
  293. if ( operation.range.containsPosition( splitPosition ) || operation.range.start.isEqual( splitPosition ) ) {
  294. if ( operation.newValue !== null ) {
  295. a._cloneOperation.nodes.getNode( 0 ).setAttribute( operation.key, operation.newValue );
  296. } else {
  297. a._cloneOperation.nodes.getNode( 0 ).removeAttribute( operation.key );
  298. }
  299. break;
  300. }
  301. }
  302. return [ a ];
  303. } );
  304. // Add special case for UnwrapDelta x SplitDelta transformation.
  305. addTransformationCase( UnwrapDelta, SplitDelta, ( a, b, context ) => {
  306. // Do not apply special transformation case if `SplitDelta` has `NoOperation` as the second operation.
  307. if ( !b.position ) {
  308. return defaultTransform( a, b, context );
  309. }
  310. // If incoming unwrap delta tries to unwrap node that got split we should unwrap the original node and the split copy.
  311. // This can be achieved either by reverting split and applying unwrap to singular node, or creating additional unwrap delta.
  312. if ( a.position.root == b.position.root && compareArrays( a.position.path, b.position.getParentPath() ) === 'same' ) {
  313. return [
  314. b.getReversed(),
  315. a.clone()
  316. ];
  317. }
  318. return defaultTransform( a, b, context );
  319. } );
  320. // Add special case for WeakInsertDelta x AttributeDelta transformation.
  321. addTransformationCase( WeakInsertDelta, AttributeDelta, ( a, b ) => {
  322. // If nodes are weak-inserted into attribute delta range, we need to apply changes from attribute delta on them.
  323. const deltas = [ a.clone() ];
  324. if ( b.range.containsPosition( a.position ) ) {
  325. deltas.push( _getComplementaryAttrDelta( a, b ) );
  326. }
  327. return deltas;
  328. } );
  329. // Add special case for WrapDelta x SplitDelta transformation.
  330. addTransformationCase( WrapDelta, SplitDelta, ( a, b, context ) => {
  331. // Do not apply special transformation case if `SplitDelta` has `NoOperation` as the second operation.
  332. if ( !b.position ) {
  333. return defaultTransform( a, b, context );
  334. }
  335. // If incoming wrap delta tries to wrap range that contains split position, we have to cancel the split and apply
  336. // the wrap. Since split was already applied, we have to revert it.
  337. const sameRoot = a.range.start.root == b.position.root;
  338. const operateInSameParent = sameRoot && compareArrays( a.range.start.getParentPath(), b.position.getParentPath() ) === 'same';
  339. const splitInsideWrapRange = a.range.start.offset < b.position.offset && a.range.end.offset >= b.position.offset;
  340. if ( operateInSameParent && splitInsideWrapRange ) {
  341. return [
  342. b.getReversed(),
  343. a.clone()
  344. ];
  345. } else if ( sameRoot && compareArrays( b.position.getParentPath(), a.range.end.getShiftedBy( -1 ).path ) === 'same' ) {
  346. const delta = a.clone();
  347. // Move wrapping element insert position one node further so it is after the split node insertion.
  348. delta._insertOperation.position = delta._insertOperation.position.getShiftedBy( 1 );
  349. // Include the split node copy.
  350. delta._moveOperation.howMany++;
  351. // Change the path to wrapping element in move operation.
  352. const index = delta._moveOperation.targetPosition.path.length - 2;
  353. const path = delta._moveOperation.targetPosition.path.slice();
  354. path[ index ] += 1;
  355. delta._moveOperation.targetPosition = new Position( delta._moveOperation.targetPosition.root, path );
  356. return [ delta ];
  357. }
  358. return defaultTransform( a, b, context );
  359. } );
  360. // Add special case for RenameDelta x SplitDelta transformation.
  361. addTransformationCase( RenameDelta, SplitDelta, ( a, b, context ) => {
  362. const undoMode = context.undoMode;
  363. const deltas = defaultTransform( a, b, context );
  364. // Special case applies only if undo is not a context and only if `SplitDelta` has `InsertOperation` (not `ReinsertOperation`).
  365. if ( undoMode || !( b._cloneOperation instanceof InsertOperation ) ) {
  366. return deltas;
  367. }
  368. const insertPosition = b._cloneOperation.position.getShiftedBy( -1 );
  369. if ( insertPosition && a.operations[ 0 ].position.isEqual( insertPosition ) ) {
  370. // If a node that has been split has it's name changed, we should also change name of
  371. // the node created during splitting.
  372. const additionalRenameDelta = a.clone();
  373. additionalRenameDelta.operations[ 0 ].position = insertPosition.getShiftedBy( 1 );
  374. deltas.push( additionalRenameDelta );
  375. }
  376. return deltas;
  377. } );
  378. // Add special case for SplitDelta x RenameDelta transformation.
  379. addTransformationCase( SplitDelta, RenameDelta, ( a, b, context ) => {
  380. a = a.clone();
  381. const undoMode = context.undoMode;
  382. // Special case applies only if undo is not a context and only if `SplitDelta` has `InsertOperation` (not `ReinsertOperation`).
  383. if ( undoMode || !( a._cloneOperation instanceof InsertOperation ) ) {
  384. return [ a ];
  385. }
  386. const insertPosition = a._cloneOperation.position.getShiftedBy( -1 );
  387. // If element to split had it's name changed, we have to reflect this by creating additional rename operation.
  388. if ( insertPosition && !undoMode && b.operations[ 0 ].position.isEqual( insertPosition ) ) {
  389. const additionalRenameDelta = b.clone();
  390. additionalRenameDelta.operations[ 0 ].position = insertPosition.getShiftedBy( 1 );
  391. additionalRenameDelta.operations[ 0 ].oldName = a._cloneOperation.nodes.getNode( 0 ).name;
  392. return [ a, additionalRenameDelta ];
  393. }
  394. return [ a ];
  395. } );
  396. // Add special case for RemoveDelta x SplitDelta transformation.
  397. addTransformationCase( RemoveDelta, SplitDelta, ( a, b, context ) => {
  398. const deltas = defaultTransform( a, b, context );
  399. // The "clone operation" may be InsertOperation, ReinsertOperation, MoveOperation or NoOperation.
  400. const insertPosition = b._cloneOperation.position || b._cloneOperation.targetPosition;
  401. // NoOperation.
  402. if ( !insertPosition ) {
  403. return defaultTransform( a, b, context );
  404. }
  405. const undoMode = context.undoMode;
  406. // Special case applies only if undo is not a context.
  407. if ( undoMode ) {
  408. return deltas;
  409. }
  410. // In case if `defaultTransform` returned more than one delta.
  411. for ( const delta of deltas ) {
  412. // "No delta" may be returned in some cases.
  413. if ( delta instanceof RemoveDelta ) {
  414. const operation = delta._moveOperation;
  415. const rangeEnd = operation.sourcePosition.getShiftedBy( operation.howMany );
  416. if ( rangeEnd.isEqual( insertPosition ) ) {
  417. operation.howMany += 1;
  418. }
  419. }
  420. }
  421. return deltas;
  422. } );
  423. // Add special case for SplitDelta x RemoveDelta transformation.
  424. addTransformationCase( SplitDelta, RemoveDelta, ( a, b, context ) => {
  425. const undoMode = context.undoMode;
  426. // Special case applies only if undo is not a context.
  427. if ( undoMode ) {
  428. return defaultTransform( a, b, context );
  429. }
  430. // This case is very trickily solved.
  431. // Instead of fixing `a` delta, we change `b` delta for a while and fire default transformation with fixed `b` delta.
  432. // Thanks to that fixing `a` delta will be differently (correctly) transformed.
  433. //
  434. // The "clone operation" may be InsertOperation, ReinsertOperation, MoveOperation or NoOperation.
  435. const insertPosition = a._cloneOperation.position || a._cloneOperation.targetPosition;
  436. // NoOperation.
  437. if ( !insertPosition ) {
  438. return defaultTransform( a, b, context );
  439. }
  440. b = b.clone();
  441. const operation = b._moveOperation;
  442. const rangeEnd = operation.sourcePosition.getShiftedBy( operation.howMany );
  443. if ( rangeEnd.isEqual( insertPosition ) ) {
  444. operation.howMany += 1;
  445. }
  446. return defaultTransform( a, b, context );
  447. } );
  448. // Helper function for `AttributeDelta` class transformations.
  449. // Creates an attribute delta that sets attribute from given `attributeDelta` on nodes from given `weakInsertDelta`.
  450. function _getComplementaryAttrDelta( weakInsertDelta, attributeDelta ) {
  451. const complementaryAttrDelta = new AttributeDelta();
  452. const nodes = weakInsertDelta.nodes;
  453. // At the beginning we store the attribute value from the first node on `weakInsertDelta` node list.
  454. let val = nodes.getNode( 0 ).getAttribute( attributeDelta.key );
  455. // This stores the last index of `weakInsertDelta` node list where the attribute value was different
  456. // than in the previous node. We need it to create separate `AttributeOperation`s for nodes with different attributes.
  457. let lastOffset = 0;
  458. // Sum of offsets of already processed nodes.
  459. let offsetSum = nodes.getNode( 0 ).offsetSize;
  460. for ( let i = 1; i < nodes.length; i++ ) {
  461. const node = nodes.getNode( i );
  462. const nodeAttrVal = node.getAttribute( attributeDelta.key );
  463. // If previous node has different attribute value, we will create an operation to the point before current node.
  464. // So all nodes with the same attributes up to this point will be included in one `AttributeOperation`.
  465. if ( nodeAttrVal != val ) {
  466. // New operation is created only when it is needed. If given node already has proper value for this
  467. // attribute we simply skip it without adding a new operation.
  468. if ( val != attributeDelta.value ) {
  469. addOperation();
  470. }
  471. val = nodeAttrVal;
  472. lastOffset = offsetSum;
  473. }
  474. offsetSum = offsetSum + node.offsetSize;
  475. }
  476. // At the end we have to add additional `AttributeOperation` for the last part of node list. If all nodes on the
  477. // node list had same attributes, this will be the only operation added to the delta.
  478. addOperation();
  479. return complementaryAttrDelta;
  480. function addOperation() {
  481. const range = new Range(
  482. weakInsertDelta.position.getShiftedBy( lastOffset ),
  483. weakInsertDelta.position.getShiftedBy( offsetSum )
  484. );
  485. const attrOperation = new AttributeOperation( range, attributeDelta.key, val, attributeDelta.value, 0 );
  486. complementaryAttrDelta.addOperation( attrOperation );
  487. }
  488. }
  489. // This is "no-op" delta, it has no type and only no-operation, it basically does nothing.
  490. // It is used when we don't want to apply changes but still we need to return a delta.
  491. function noDelta() {
  492. const noDelta = new Delta();
  493. // BaseVersion will be fixed later anyway.
  494. noDelta.addOperation( new NoOperation( 0 ) );
  495. return noDelta;
  496. }