wrapoperation.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/operation/wrapoperation
  7. */
  8. import Operation from './operation';
  9. import UnwrapOperation from './unwrapoperation';
  10. import Position from '../position';
  11. import Range from '../range';
  12. import Element from '../element';
  13. import { _insert, _move } from './utils';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. /**
  16. * Operation to wrap a range of {@link module:engine/model/node~Node nodes} with an {@link module:engine/model/element~Element element}.
  17. *
  18. * @extends module:engine/model/operation/operation~Operation
  19. */
  20. export default class WrapOperation extends Operation {
  21. /**
  22. * Creates a wrap operation.
  23. *
  24. * @param {module:engine/model/position~Position} position Position before
  25. * the first {@link module:engine/model/item~Item model item} to wrap.
  26. * @param {Number} howMany Offset size of wrapped range. Wrapped range will start at `position.offset` and end at
  27. * `position.offset + howMany`.
  28. * @param {module:engine/model/element~Element|module:engine/model/position~Position} elementOrPosition Wrapper
  29. * element or position in graveyard before the element which should be used as a wrapper.
  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( position, howMany, elementOrPosition, baseVersion ) {
  34. super( baseVersion );
  35. /**
  36. * Position before the first {@link module:engine/model/node~Node node} to wrap.
  37. *
  38. * @member {module:engine/model/position~Position} module:engine/model/operation/wrapoperation~WrapOperation#position
  39. */
  40. this.position = Position.createFromPosition( position );
  41. // `'toNext'` because `position` is a bit like a start of the wrapped range.
  42. this.position.stickiness = 'toNext';
  43. /**
  44. * Total offset size of the wrapped range.
  45. *
  46. * Wrapped range will start at `position.offset` and end at `position.offset + howMany`.
  47. *
  48. * @member {Number} module:engine/model/operation/wrapoperation~WrapOperation#howMany
  49. */
  50. this.howMany = howMany;
  51. /**
  52. * Wrapper element that will be used to wrap nodes.
  53. *
  54. * Is `null` if `elementOrPosition` was a {@link module:engine/model/position~Position}.
  55. *
  56. * @member {module:engine/model/element~Element} module:engine/model/operation/wrapoperation~WrapOperation#element
  57. */
  58. this.element = elementOrPosition instanceof Element ? elementOrPosition : null;
  59. /**
  60. * Position in the graveyard root before the element that will be used as a wrapper element.
  61. *
  62. * Is `null` if `elementOrPosition` was a {@link module:engine/model/element~Element}.
  63. *
  64. * @member {module:engine/model/element~Element} module:engine/model/operation/wrapoperation~WrapOperation#graveyardPosition
  65. */
  66. this.graveyardPosition = elementOrPosition instanceof Element ? null : Position.createFromPosition( elementOrPosition );
  67. if ( this.graveyardPosition ) {
  68. this.graveyardPosition.stickiness = 'toNext';
  69. }
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. get type() {
  75. return 'wrap';
  76. }
  77. /**
  78. * Position to which the wrapped elements will be moved. This is a position at the beginning of the wrapping element.
  79. *
  80. * @readonly
  81. * @type {module:engine/model/position~Position}
  82. */
  83. get targetPosition() {
  84. const path = this.position.path.slice();
  85. path.push( 0 );
  86. return new Position( this.position.root, path );
  87. }
  88. /**
  89. * A range containing all nodes that will be wrapped.
  90. *
  91. * @readonly
  92. * @type {module:engine/model/range~Range}
  93. */
  94. get wrappedRange() {
  95. return Range.createFromPositionAndShift( this.position, this.howMany );
  96. }
  97. /**
  98. * Creates and returns an operation that has the same parameters as this operation.
  99. *
  100. * @returns {module:engine/model/operation/wrapoperation~WrapOperation} Clone of this operation.
  101. */
  102. clone() {
  103. const elementOrPosition = this.element ? this.element._clone() : this.graveyardPosition;
  104. return new this.constructor( this.position, this.howMany, elementOrPosition, this.baseVersion );
  105. }
  106. /**
  107. * See {@link module:engine/model/operation/operation~Operation#getReversed `Operation#getReversed()`}.
  108. *
  109. * @returns {module:engine/model/operation/unwrapoperation~UnwrapOperation}
  110. */
  111. getReversed() {
  112. const graveyard = this.position.root.document.graveyard;
  113. const graveyardPosition = new Position( graveyard, [ 0 ] );
  114. return new UnwrapOperation( this.targetPosition, this.howMany, graveyardPosition, this.baseVersion + 1 );
  115. }
  116. /**
  117. * @inheritDoc
  118. */
  119. _validate() {
  120. const element = this.position.parent;
  121. // Validate whether wrap operation has correct parameters.
  122. if ( !element || this.position.offset > element.maxOffset ) {
  123. /**
  124. * Wrap position is invalid.
  125. *
  126. * @error wrap-operation-position-invalid
  127. */
  128. throw new CKEditorError( 'wrap-operation-position-invalid: Wrap position is invalid.' );
  129. } else if ( this.position.offset + this.howMany > element.maxOffset ) {
  130. /**
  131. * Invalid number of nodes to wrap.
  132. *
  133. * @error wrap-operation-how-many-invalid
  134. */
  135. throw new CKEditorError( 'wrap-operation-how-many-invalid: Invalid number of nodes to wrap.' );
  136. } else if ( this.graveyardPosition && !this.graveyardPosition.nodeAfter ) {
  137. /**
  138. * Graveyard position invalid.
  139. *
  140. * @error wrap-operation-graveyard-position-invalid
  141. */
  142. throw new CKEditorError( 'wrap-operation-graveyard-position-invalid: Graveyard position invalid.' );
  143. }
  144. }
  145. /**
  146. * @inheritDoc
  147. */
  148. _execute() {
  149. const wrappedRange = this.wrappedRange;
  150. const insertPosition = Position.createFromPosition( wrappedRange.end );
  151. const targetPath = insertPosition.path.slice();
  152. targetPath.push( 0 );
  153. const targetPosition = new Position( this.position.root, targetPath );
  154. if ( this.element ) {
  155. const originalElement = this.element;
  156. this.element = this.element._clone();
  157. _insert( insertPosition, originalElement );
  158. } else {
  159. _move( Range.createFromPositionAndShift( this.graveyardPosition, 1 ), insertPosition );
  160. }
  161. _move( wrappedRange, targetPosition );
  162. }
  163. /**
  164. * @inheritDoc
  165. */
  166. toJSON() {
  167. const json = super.toJSON();
  168. json.position = this.position.toJSON();
  169. if ( this.element ) {
  170. json.element = this.element.toJSON();
  171. delete json.graveyardPosition;
  172. } else {
  173. json.graveyardPosition = this.graveyardPosition.toJSON();
  174. delete json.element;
  175. }
  176. return json;
  177. }
  178. /**
  179. * @inheritDoc
  180. */
  181. static get className() {
  182. return 'WrapOperation';
  183. }
  184. /**
  185. * Creates `WrapOperation` object from deserilized object, i.e. from parsed JSON string.
  186. *
  187. * @param {Object} json Deserialized JSON object.
  188. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  189. * @returns {module:engine/model/operation/wrapoperation~WrapOperation}
  190. */
  191. static fromJSON( json, document ) {
  192. const position = Position.fromJSON( json.position, document );
  193. const elementOrPosition = json.element ? Element.fromJSON( json.element ) : Position.fromJSON( json.graveyardPosition, document );
  194. return new this( position, json.howMany, elementOrPosition, json.baseVersion );
  195. }
  196. }