8
0

removeoperation.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import MoveOperation from './moveoperation.js';
  7. import Position from '../position.js';
  8. import Element from '../element.js';
  9. import ReinsertOperation from './reinsertoperation.js';
  10. /**
  11. * Operation to remove a range of nodes.
  12. *
  13. * @memberOf engine.model.operation
  14. * @extends engine.model.operation.Operation
  15. */
  16. export default class RemoveOperation extends MoveOperation {
  17. /**
  18. *
  19. * Creates a remove operation.
  20. *
  21. * @param {engine.model.Position} position Position before the first node to remove.
  22. * @param {Number} howMany How many nodes to remove.
  23. * @param {Number} baseVersion {@link engine.model.Document#version} on which operation can be applied.
  24. */
  25. constructor( position, howMany, baseVersion ) {
  26. const graveyard = position.root.document.graveyard;
  27. super( position, howMany, new Position( graveyard, [ graveyard.getChildCount(), 0 ] ), baseVersion );
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. get type() {
  33. return 'remove';
  34. }
  35. /**
  36. * Offset of the graveyard "holder" element, in which nodes removed by this operation are stored.
  37. *
  38. * @protected
  39. * @type {Number}
  40. */
  41. get _holderElementOffset() {
  42. return this.targetPosition.path[ 0 ];
  43. }
  44. /**
  45. * Sets {@link engine.model.operation.RemoveOperation#_holderElementOffset}.
  46. *
  47. * @protected
  48. * @param {Number} offset
  49. */
  50. set _holderElementOffset( offset ) {
  51. this.targetPosition.path[ 0 ] = offset;
  52. }
  53. /**
  54. * Flag informing whether this operation should insert "holder" element (`true`) or should remove nodes
  55. * into existing "holder" element (`false`). It is `true` for each `RemoveOperation` that is the first `RemoveOperation`
  56. * in it's delta which points to given holder element.
  57. *
  58. * @protected
  59. * @type {Boolean}
  60. */
  61. get _needsHolderElement() {
  62. if ( this.delta ) {
  63. // Let's look up all operations from this delta in the same order as they are in the delta.
  64. for ( let operation of this.delta.operations ) {
  65. // We are interested only in `RemoveOperation`s.
  66. if ( operation instanceof RemoveOperation ) {
  67. // If the first `RemoveOperation` in the delta is this operation, this operation
  68. // needs to insert holder element in the graveyard.
  69. if ( operation == this ) {
  70. return true;
  71. } else if ( operation._holderElementOffset == this._holderElementOffset ) {
  72. // If there is a `RemoveOperation` in this delta that "points" to the same holder element offset,
  73. // that operation will already insert holder element at that offset. We should not create another holder.
  74. return false;
  75. }
  76. }
  77. }
  78. }
  79. // By default `RemoveOperation` needs holder element, so set it so, if the operation does not have delta.
  80. return true;
  81. }
  82. /**
  83. * @returns {engine.model.operation.ReinsertOperation}
  84. */
  85. getReversed() {
  86. return new ReinsertOperation( this.targetPosition, this.howMany, this.sourcePosition, this.baseVersion + 1 );
  87. }
  88. /**
  89. * @returns {engine.model.operation.RemoveOperation}
  90. */
  91. clone() {
  92. let removeOperation = new RemoveOperation( this.sourcePosition, this.howMany, this.baseVersion );
  93. removeOperation.targetPosition = Position.createFromPosition( this.targetPosition );
  94. removeOperation.movedRangeStart = Position.createFromPosition( this.movedRangeStart );
  95. return removeOperation;
  96. }
  97. /**
  98. * @inheritDoc
  99. */
  100. _execute() {
  101. if ( this._needsHolderElement ) {
  102. const graveyard = this.targetPosition.root;
  103. const holderElement = new Element( '$graveyardHolder' );
  104. graveyard.insertChildren( this.targetPosition.path[ 0 ], holderElement );
  105. }
  106. return super._execute();
  107. }
  108. /**
  109. * @inheritDoc
  110. */
  111. static get className() {
  112. return 'engine.model.operation.RemoveOperation';
  113. }
  114. }