moveoperation.js 7.4 KB

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