moveoperation.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/operation/moveoperation
  7. */
  8. import Operation from './operation';
  9. import Position from '../position';
  10. import Range from '../range';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
  13. import writer from './../writer';
  14. /**
  15. * Operation to move a range of {@link module:engine/model/item~Item model items}
  16. * to given {@link module:engine/model/position~Position target position}.
  17. */
  18. export default class MoveOperation extends Operation {
  19. /**
  20. * Creates a move operation.
  21. *
  22. * @param {module:engine/model/position~Position} sourcePosition
  23. * Position before the first {@link module:engine/model/item~Item model item} to move.
  24. * @param {Number} howMany Offset size of moved range. Moved range will start from `sourcePosition` and end at
  25. * `sourcePosition` with offset shifted by `howMany`.
  26. * @param {module:engine/model/position~Position} targetPosition Position at which moved nodes will be inserted.
  27. * @param {Number} baseVersion {@link module:engine/model/document~Document#version} on which operation can be applied.
  28. */
  29. constructor( sourcePosition, howMany, targetPosition, baseVersion ) {
  30. super( baseVersion );
  31. /**
  32. * Position before the first {@link module:engine/model/item~Item model item} to move.
  33. *
  34. * @member {module:engine/model/position~Position} module:engine/model/operation/moveoperation~MoveOperation#sourcePosition
  35. */
  36. this.sourcePosition = Position.createFromPosition( sourcePosition );
  37. /**
  38. * Offset size of moved range.
  39. *
  40. * @member {Number} module:engine/model/operation/moveoperation~MoveOperation#howMany
  41. */
  42. this.howMany = howMany;
  43. /**
  44. * Position at which moved nodes will be inserted.
  45. *
  46. * @member {module:engine/model/position~Position} module:engine/model/operation/moveoperation~MoveOperation#targetPosition
  47. */
  48. this.targetPosition = Position.createFromPosition( targetPosition );
  49. /**
  50. * Defines whether `MoveOperation` is sticky. If `MoveOperation` is sticky, during
  51. * {@link module:engine/model/operation/transform~transform operational transformation} if there will be an operation that
  52. * inserts some nodes at the position equal to the boundary of this `MoveOperation`, that operation will
  53. * get their insertion path updated to the position where this `MoveOperation` moves the range.
  54. *
  55. * @member {Boolean} module:engine/model/operation/moveoperation~MoveOperation#isSticky
  56. */
  57. this.isSticky = false;
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. get type() {
  63. return 'move';
  64. }
  65. /**
  66. * @inheritDoc
  67. * @returns {module:engine/model/operation/moveoperation~MoveOperation}
  68. */
  69. clone() {
  70. const op = new this.constructor( this.sourcePosition, this.howMany, this.targetPosition, this.baseVersion );
  71. op.isSticky = this.isSticky;
  72. return op;
  73. }
  74. /**
  75. * Returns the start position of the moved range after it got moved. This may be different than
  76. * {@link module:engine/model/operation/moveoperation~MoveOperation#targetPosition} in some cases, i.e. when a range is moved
  77. * inside the same parent but {@link module:engine/model/operation/moveoperation~MoveOperation#targetPosition targetPosition}
  78. * is after {@link module:engine/model/operation/moveoperation~MoveOperation#sourcePosition sourcePosition}.
  79. *
  80. * vv vv
  81. * abcdefg ===> adefbcg
  82. * ^ ^
  83. * targetPos movedRangeStart
  84. * offset 6 offset 4
  85. *
  86. * @returns {module:engine/model/position~Position}
  87. */
  88. getMovedRangeStart() {
  89. return this.targetPosition._getTransformedByDeletion( this.sourcePosition, this.howMany );
  90. }
  91. /**
  92. * @inheritDoc
  93. * @returns {module:engine/model/operation/moveoperation~MoveOperation}
  94. */
  95. getReversed() {
  96. const newTargetPosition = this.sourcePosition._getTransformedByInsertion( this.targetPosition, this.howMany );
  97. const op = new this.constructor( this.getMovedRangeStart(), this.howMany, newTargetPosition, this.baseVersion + 1 );
  98. op.isSticky = this.isSticky;
  99. return op;
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. _execute() {
  105. const sourceElement = this.sourcePosition.parent;
  106. const targetElement = this.targetPosition.parent;
  107. const sourceOffset = this.sourcePosition.offset;
  108. const targetOffset = this.targetPosition.offset;
  109. // Validate whether move operation has correct parameters.
  110. // Validation is pretty complex but move operation is one of the core ways to manipulate the document state.
  111. // We expect that many errors might be connected with one of scenarios described below.
  112. if ( !sourceElement || !targetElement ) {
  113. /**
  114. * Source position or target position is invalid.
  115. *
  116. * @error move-operation-position-invalid
  117. */
  118. throw new CKEditorError(
  119. 'move-operation-position-invalid: Source position or target position is invalid.'
  120. );
  121. } else if ( sourceOffset + this.howMany > sourceElement.maxOffset ) {
  122. /**
  123. * The nodes which should be moved do not exist.
  124. *
  125. * @error move-operation-nodes-do-not-exist
  126. */
  127. throw new CKEditorError(
  128. 'move-operation-nodes-do-not-exist: The nodes which should be moved do not exist.'
  129. );
  130. } else if ( sourceElement === targetElement && sourceOffset < targetOffset && targetOffset < sourceOffset + this.howMany ) {
  131. /**
  132. * Trying to move a range of nodes into the middle of that range.
  133. *
  134. * @error move-operation-range-into-itself
  135. */
  136. throw new CKEditorError(
  137. 'move-operation-range-into-itself: Trying to move a range of nodes to the inside of that range.'
  138. );
  139. } else if ( this.sourcePosition.root == this.targetPosition.root ) {
  140. if ( compareArrays( this.sourcePosition.getParentPath(), this.targetPosition.getParentPath() ) == 'prefix' ) {
  141. const i = this.sourcePosition.path.length - 1;
  142. if ( this.targetPosition.path[ i ] >= sourceOffset && this.targetPosition.path[ i ] < sourceOffset + this.howMany ) {
  143. /**
  144. * Trying to move a range of nodes into one of nodes from that range.
  145. *
  146. * @error move-operation-node-into-itself
  147. */
  148. throw new CKEditorError(
  149. 'move-operation-node-into-itself: Trying to move a range of nodes into one of nodes from that range.'
  150. );
  151. }
  152. }
  153. }
  154. const range = writer.move( Range.createFromPositionAndShift( this.sourcePosition, this.howMany ), this.targetPosition );
  155. return {
  156. sourcePosition: this.sourcePosition,
  157. range
  158. };
  159. }
  160. /**
  161. * @inheritDoc
  162. */
  163. static get className() {
  164. return 'engine.model.operation.MoveOperation';
  165. }
  166. /**
  167. * Creates `MoveOperation` object from deserilized object, i.e. from parsed JSON string.
  168. *
  169. * @param {Object} json Deserialized JSON object.
  170. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  171. * @returns {module:engine/model/operation/moveoperation~MoveOperation}
  172. */
  173. static fromJSON( json, document ) {
  174. const sourcePosition = Position.fromJSON( json.sourcePosition, document );
  175. const targetPosition = Position.fromJSON( json.targetPosition, document );
  176. const move = new this( sourcePosition, json.howMany, targetPosition, json.baseVersion );
  177. if ( json.isSticky ) {
  178. move.isSticky = true;
  179. }
  180. return move;
  181. }
  182. }