basic-transformations.js 24 KB

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