removeoperation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/operation/removeoperation
  7. */
  8. import MoveOperation from './moveoperation.js';
  9. import Position from '../position.js';
  10. import Element from '../element.js';
  11. import ReinsertOperation from './reinsertoperation.js';
  12. /**
  13. * Operation to remove a range of nodes.
  14. */
  15. export default class RemoveOperation extends MoveOperation {
  16. /**
  17. * Creates a remove operation.
  18. *
  19. * @param {module:engine/model/position~Position} position Position before the first {@link module:engine/model/item~Item model item} to
  20. * remove.
  21. * @param {Number} howMany Offset size of removed range. {@link module:engine/model/item~Item Model items} will be removed starting
  22. * from `sourcePosition`, up to a `sourcePosition` with offset shifted by `howMany`.
  23. * @param {Number} baseVersion {@link module:engine/model/document~Document#version} on which operation can be applied.
  24. */
  25. constructor( position, howMany, baseVersion ) {
  26. const graveyard = position.root.document.graveyard;
  27. const graveyardPosition = new Position( graveyard, [ graveyard.maxOffset, 0 ] );
  28. super( position, howMany, graveyardPosition, baseVersion );
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. get type() {
  34. return 'remove';
  35. }
  36. /**
  37. * Offset of the graveyard "holder" element, in which nodes removed by this operation are stored.
  38. *
  39. * @protected
  40. * @type {Number}
  41. */
  42. get _holderElementOffset() {
  43. return this.targetPosition.path[ 0 ];
  44. }
  45. /**
  46. * Sets {@link module:engine/model/operation/removeoperation~RemoveOperation#_holderElementOffset}.
  47. *
  48. * @protected
  49. * @param {Number} offset
  50. */
  51. set _holderElementOffset( offset ) {
  52. this.targetPosition.path[ 0 ] = offset;
  53. }
  54. /**
  55. * Flag informing whether this operation should insert "holder" element (`true`) or should move removed nodes
  56. * into existing "holder" element (`false`).
  57. *
  58. * It is `true` for each `RemoveOperation` that is the first `RemoveOperation` in it's delta that points to given holder element.
  59. * This way only one `RemoveOperation` in given delta will insert "holder" element.
  60. *
  61. * @protected
  62. * @type {Boolean}
  63. */
  64. get _needsHolderElement() {
  65. if ( this.delta ) {
  66. // Let's look up all operations from this delta in the same order as they are in the delta.
  67. for ( let operation of this.delta.operations ) {
  68. // We are interested only in `RemoveOperation`s.
  69. if ( operation instanceof RemoveOperation ) {
  70. // If the first `RemoveOperation` in the delta is this operation, this operation
  71. // needs to insert holder element in the graveyard.
  72. if ( operation == this ) {
  73. return true;
  74. } else if ( operation._holderElementOffset == this._holderElementOffset ) {
  75. // If there is a `RemoveOperation` in this delta that "points" to the same holder element offset,
  76. // that operation will already insert holder element at that offset. We should not create another holder.
  77. return false;
  78. }
  79. }
  80. }
  81. }
  82. // By default `RemoveOperation` needs holder element, so set it so, if the operation does not have delta.
  83. return true;
  84. }
  85. /**
  86. * @inheritDoc
  87. * @returns {module:engine/model/operation/reinsertoperation~ReinsertOperation}
  88. */
  89. getReversed() {
  90. return new ReinsertOperation( this.targetPosition, this.howMany, this.sourcePosition, this.baseVersion + 1 );
  91. }
  92. /**
  93. * @inheritDoc
  94. * @returns {module:engine/model/operation/removeoperation~RemoveOperation}
  95. */
  96. clone() {
  97. let removeOperation = new RemoveOperation( this.sourcePosition, this.howMany, this.baseVersion );
  98. removeOperation.targetPosition = Position.createFromPosition( this.targetPosition );
  99. return removeOperation;
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. _execute() {
  105. // Insert "holder" element in graveyard root, if the operation needs it.
  106. if ( this._needsHolderElement ) {
  107. const graveyard = this.targetPosition.root;
  108. const holderElement = new Element( '$graveyardHolder' );
  109. graveyard.insertChildren( this._holderElementOffset, holderElement );
  110. // If the operation removes nodes that are already in graveyard, it may happen that
  111. // the operation's source position is invalidated by inserting new holder element into the graveyard.
  112. // If that's the case, we need to fix source position path.
  113. if ( this.sourcePosition.root == graveyard && this.sourcePosition.path[ 0 ] >= this._holderElementOffset ) {
  114. this.sourcePosition.path[ 0 ]++;
  115. }
  116. }
  117. // Then, execute as a move operation.
  118. return super._execute();
  119. }
  120. /**
  121. * @inheritDoc
  122. */
  123. static get className() {
  124. return 'engine.model.operation.RemoveOperation';
  125. }
  126. /**
  127. * Creates `RemoveOperation` object from deserilized object, i.e. from parsed JSON string.
  128. *
  129. * @param {Object} json Deserialized JSON object.
  130. * @param {module:engine/model/document~Document} document Document on which this operation will be applied.
  131. * @returns {module:engine/model/operation/removeoperation~RemoveOperation}
  132. */
  133. static fromJSON( json, document ) {
  134. let sourcePosition = Position.fromJSON( json.sourcePosition, document );
  135. const removeOp = new RemoveOperation( sourcePosition, json.howMany, json.baseVersion );
  136. removeOp.targetPosition = Position.fromJSON( json.targetPosition, document );
  137. return removeOp;
  138. }
  139. }