8
0

moveoperation.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 { _move } from './utils';
  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. * @extends module:engine/model/operation/operation~Operation
  19. */
  20. export default class MoveOperation extends Operation {
  21. /**
  22. * Creates a move operation.
  23. *
  24. * @param {module:engine/model/position~Position} sourcePosition
  25. * Position before the first {@link module:engine/model/item~Item model item} to move.
  26. * @param {Number} howMany Offset size of moved range. Moved range will start from `sourcePosition` and end at
  27. * `sourcePosition` with offset shifted by `howMany`.
  28. * @param {module:engine/model/position~Position} targetPosition Position at which moved nodes will be inserted.
  29. * @param {Number|null} baseVersion Document {@link module:engine/model/document~Document#version} on which operation
  30. * can be applied or `null` if the operation operates on detached (non-document) tree.
  31. */
  32. constructor( sourcePosition, howMany, targetPosition, baseVersion ) {
  33. super( baseVersion );
  34. /**
  35. * Position before the first {@link module:engine/model/item~Item model item} to move.
  36. *
  37. * @member {module:engine/model/position~Position} module:engine/model/operation/moveoperation~MoveOperation#sourcePosition
  38. */
  39. this.sourcePosition = sourcePosition.clone();
  40. // `'toNext'` because `sourcePosition` is a bit like a start of the moved range.
  41. this.sourcePosition.stickiness = 'toNext';
  42. /**
  43. * Offset size of moved range.
  44. *
  45. * @member {Number} module:engine/model/operation/moveoperation~MoveOperation#howMany
  46. */
  47. this.howMany = howMany;
  48. /**
  49. * Position at which moved nodes will be inserted.
  50. *
  51. * @member {module:engine/model/position~Position} module:engine/model/operation/moveoperation~MoveOperation#targetPosition
  52. */
  53. this.targetPosition = targetPosition.clone();
  54. this.targetPosition.stickiness = 'toNone';
  55. }
  56. /**
  57. * @inheritDoc
  58. */
  59. get type() {
  60. if ( this.targetPosition.root.rootName == '$graveyard' ) {
  61. return 'remove';
  62. } else if ( this.sourcePosition.root.rootName == '$graveyard' ) {
  63. return 'reinsert';
  64. }
  65. return 'move';
  66. }
  67. /**
  68. * Creates and returns an operation that has the same parameters as this operation.
  69. *
  70. * @returns {module:engine/model/operation/moveoperation~MoveOperation} Clone of this operation.
  71. */
  72. clone() {
  73. return new this.constructor( this.sourcePosition, this.howMany, this.targetPosition, this.baseVersion );
  74. }
  75. /**
  76. * Returns the start position of the moved range after it got moved. This may be different than
  77. * {@link module:engine/model/operation/moveoperation~MoveOperation#targetPosition} in some cases, i.e. when a range is moved
  78. * inside the same parent but {@link module:engine/model/operation/moveoperation~MoveOperation#targetPosition targetPosition}
  79. * is after {@link module:engine/model/operation/moveoperation~MoveOperation#sourcePosition sourcePosition}.
  80. *
  81. * vv vv
  82. * abcdefg ===> adefbcg
  83. * ^ ^
  84. * targetPos movedRangeStart
  85. * offset 6 offset 4
  86. *
  87. * @returns {module:engine/model/position~Position}
  88. */
  89. getMovedRangeStart() {
  90. return this.targetPosition._getTransformedByDeletion( this.sourcePosition, this.howMany );
  91. }
  92. /**
  93. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  94. *
  95. * @returns {module:engine/model/operation/moveoperation~MoveOperation}
  96. */
  97. getReversed() {
  98. const newTargetPosition = this.sourcePosition._getTransformedByInsertion( this.targetPosition, this.howMany );
  99. return new this.constructor( this.getMovedRangeStart(), this.howMany, newTargetPosition, this.baseVersion + 1 );
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. _validate() {
  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 ( sourceOffset + this.howMany > sourceElement.maxOffset ) {
  113. /**
  114. * The nodes which should be moved do not exist.
  115. *
  116. * @error move-operation-nodes-do-not-exist
  117. */
  118. throw new CKEditorError(
  119. 'move-operation-nodes-do-not-exist: The nodes which should be moved do not exist.', this
  120. );
  121. } else if ( sourceElement === targetElement && sourceOffset < targetOffset && targetOffset < sourceOffset + this.howMany ) {
  122. /**
  123. * Trying to move a range of nodes into the middle of that range.
  124. *
  125. * @error move-operation-range-into-itself
  126. */
  127. throw new CKEditorError(
  128. 'move-operation-range-into-itself: Trying to move a range of nodes to the inside of that range.', this
  129. );
  130. } else if ( this.sourcePosition.root == this.targetPosition.root ) {
  131. if ( compareArrays( this.sourcePosition.getParentPath(), this.targetPosition.getParentPath() ) == 'prefix' ) {
  132. const i = this.sourcePosition.path.length - 1;
  133. if ( this.targetPosition.path[ i ] >= sourceOffset && this.targetPosition.path[ i ] < sourceOffset + this.howMany ) {
  134. /**
  135. * Trying to move a range of nodes into one of nodes from that range.
  136. *
  137. * @error move-operation-node-into-itself
  138. */
  139. throw new CKEditorError(
  140. 'move-operation-node-into-itself: Trying to move a range of nodes into one of nodes from that range.', this
  141. );
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * @inheritDoc
  148. */
  149. _execute() {
  150. _move( Range._createFromPositionAndShift( this.sourcePosition, this.howMany ), this.targetPosition );
  151. }
  152. /**
  153. * @inheritDoc
  154. */
  155. toJSON() {
  156. const json = super.toJSON();
  157. json.sourcePosition = this.sourcePosition.toJSON();
  158. json.targetPosition = this.targetPosition.toJSON();
  159. return json;
  160. }
  161. /**
  162. * @inheritDoc
  163. */
  164. static get className() {
  165. return 'MoveOperation';
  166. }
  167. /**
  168. * Creates `MoveOperation` object from deserilized object, i.e. from parsed JSON string.
  169. *
  170. * @param {Object} json Deserialized JSON object.
  171. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  172. * @returns {module:engine/model/operation/moveoperation~MoveOperation}
  173. */
  174. static fromJSON( json, document ) {
  175. const sourcePosition = Position.fromJSON( json.sourcePosition, document );
  176. const targetPosition = Position.fromJSON( json.targetPosition, document );
  177. return new this( sourcePosition, json.howMany, targetPosition, json.baseVersion );
  178. }
  179. }