liverange.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/liverange
  7. */
  8. import Range from './range';
  9. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  10. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  11. /**
  12. * `LiveRange` is a type of {@link module:engine/model/range~Range Range}
  13. * that updates itself as {@link module:engine/model/document~Document document}
  14. * is changed through operations. It may be used as a bookmark.
  15. *
  16. * **Note:** Be very careful when dealing with `LiveRange`. Each `LiveRange` instance bind events that might
  17. * have to be unbound. Use {@link module:engine/model/liverange~LiveRange#detach detach} whenever you don't need `LiveRange` anymore.
  18. */
  19. export default class LiveRange extends Range {
  20. /**
  21. * Creates a live range.
  22. *
  23. * @see module:engine/model/range~Range
  24. */
  25. constructor( start, end ) {
  26. super( start, end );
  27. bindWithDocument.call( this );
  28. }
  29. /**
  30. * Unbinds all events previously bound by `LiveRange`. Use it whenever you don't need `LiveRange` instance
  31. * anymore (i.e. when leaving scope in which it was declared or before re-assigning variable that was
  32. * referring to it).
  33. */
  34. detach() {
  35. this.stopListening();
  36. }
  37. /**
  38. * @see module:engine/model/range~Range.createIn
  39. * @static
  40. * @method module:engine/model/liverange~LiveRange.createIn
  41. * @param {module:engine/model/element~Element} element
  42. * @returns {module:engine/model/liverange~LiveRange}
  43. */
  44. /**
  45. * @see module:engine/model/range~Range.createFromPositionAndShift
  46. * @static
  47. * @method module:engine/model/liverange~LiveRange.createFromPositionAndShift
  48. * @param {module:engine/model/position~Position} position
  49. * @param {Number} shift
  50. * @returns {module:engine/model/liverange~LiveRange}
  51. */
  52. /**
  53. * @see module:engine/model/range~Range.createFromParentsAndOffsets
  54. * @static
  55. * @method module:engine/model/liverange~LiveRange.createFromParentsAndOffsets
  56. * @param {module:engine/model/element~Element} startElement
  57. * @param {Number} startOffset
  58. * @param {module:engine/model/element~Element} endElement
  59. * @param {Number} endOffset
  60. * @returns {module:engine/model/liverange~LiveRange}
  61. */
  62. /**
  63. * @see module:engine/model/range~Range.createFromRange
  64. * @static
  65. * @method module:engine/model/liverange~LiveRange.createFromRange
  66. * @param {module:engine/model/range~Range} range
  67. * @returns {module:engine/model/liverange~LiveRange}
  68. */
  69. /**
  70. * Fired when `LiveRange` instance boundaries have changed due to changes in the
  71. * {@link module:engine/model/document~Document document}.
  72. *
  73. * @event change:range
  74. * @param {module:engine/model/range~Range} oldRange Range with start and end position equal to start and end position of this live
  75. * range before it got changed.
  76. * @param {Object} data Object with additional information about the change. Those parameters are passed from
  77. * {@link module:engine/model/document~Document#event:change document change event}.
  78. * @param {String} data.type Change type.
  79. * @param {module:engine/model/batch~Batch} data.batch Batch which changed the live range.
  80. * @param {module:engine/model/range~Range} data.range Range containing the result of applied change.
  81. * @param {module:engine/model/position~Position} data.sourcePosition Source position for move, remove and reinsert change types.
  82. */
  83. /**
  84. * Fired when `LiveRange` instance boundaries have not changed after a change in {@link module:engine/model/document~Document document}
  85. * but the change took place inside the range, effectively changing its content.
  86. *
  87. * @event change:content
  88. * @param {module:engine/model/range~Range} range Range with start and end position equal to start and end position of
  89. * change range.
  90. * @param {Object} data Object with additional information about the change. Those parameters are passed from
  91. * {@link module:engine/model/document~Document#event:change document change event}.
  92. * @param {String} data.type Change type.
  93. * @param {module:engine/model/batch~Batch} data.batch Batch which changed the live range.
  94. * @param {module:engine/model/range~Range} data.range Range containing the result of applied change.
  95. * @param {module:engine/model/position~Position} data.sourcePosition Source position for move, remove and reinsert change types.
  96. */
  97. }
  98. /**
  99. * Binds this `LiveRange` to the {@link module:engine/model/document~Document document}
  100. * that owns this range's {@link module:engine/model/range~Range#root root}.
  101. *
  102. * @ignore
  103. * @private
  104. * @method module:engine/model/liverange~LiveRange#bindWithDocument
  105. */
  106. function bindWithDocument() {
  107. // Operation types that a range can be transformed by.
  108. const supportedTypes = new Set( [ 'insert', 'move', 'remove', 'reinsert' ] );
  109. this.listenTo(
  110. this.root.document.model,
  111. 'applyOperation',
  112. ( event, args ) => {
  113. const operation = args[ 0 ];
  114. if ( !operation.isDocumentOperation ) {
  115. return;
  116. }
  117. if ( supportedTypes.has( operation.type ) ) {
  118. transform.call( this, operation );
  119. }
  120. },
  121. { priority: 'low' }
  122. );
  123. }
  124. /**
  125. * Updates this range accordingly to the updates applied to the model. Bases on change events.
  126. *
  127. * @ignore
  128. * @private
  129. * @method transform
  130. * @param {module:engine/model/operation/operation~Operation} operation Executed operation.
  131. */
  132. function transform( operation ) {
  133. const changeType = operation.type;
  134. const batch = operation.delta.batch;
  135. let targetRange;
  136. let sourcePosition;
  137. if ( changeType == 'insert' ) {
  138. targetRange = Range.createFromPositionAndShift( operation.position, operation.nodes.maxOffset );
  139. } else {
  140. targetRange = Range.createFromPositionAndShift( operation.getMovedRangeStart(), operation.howMany );
  141. sourcePosition = operation.sourcePosition;
  142. }
  143. const howMany = targetRange.end.offset - targetRange.start.offset;
  144. let targetPosition = targetRange.start;
  145. if ( changeType == 'move' || changeType == 'remove' || changeType == 'reinsert' ) {
  146. // Range._getTransformedByDocumentChange is expecting `targetPosition` to be "before" move
  147. // (before transformation). `targetRange.start` is already after the move happened.
  148. // We have to revert `targetPosition` to the state before the move.
  149. targetPosition = targetPosition._getTransformedByInsertion( sourcePosition, howMany );
  150. }
  151. const result = this._getTransformedByDocumentChange( changeType, operation.delta.type, targetPosition, howMany, sourcePosition );
  152. // Decide whether moved part should be included in the range.
  153. //
  154. // First, this concerns only `move` change, because insert change includes inserted part always (changeType == 'move').
  155. // Second, this is a case only if moved range was intersecting with this range and was inserted into this range (result.length == 3).
  156. if ( ( changeType == 'move' || changeType == 'remove' || changeType == 'reinsert' ) && result.length == 3 ) {
  157. // `result[ 2 ]` is a "common part" of this range and moved range. We substitute that common part with the whole
  158. // `targetRange` because we want to include whole `targetRange` in this range.
  159. result[ 2 ] = targetRange;
  160. }
  161. const updated = Range.createFromRanges( result );
  162. const boundariesChanged = !updated.isEqual( this );
  163. const rangeExpanded = this.containsPosition( targetPosition );
  164. const rangeShrunk = sourcePosition && ( this.containsPosition( sourcePosition ) || this.start.isEqual( sourcePosition ) );
  165. const contentChanged = rangeExpanded || rangeShrunk;
  166. if ( boundariesChanged ) {
  167. // If range boundaries have changed, fire `change:range` event.
  168. const oldRange = Range.createFromRange( this );
  169. this.start = updated.start;
  170. this.end = updated.end;
  171. this.fire( 'change:range', oldRange, {
  172. type: changeType,
  173. batch,
  174. range: targetRange,
  175. sourcePosition
  176. } );
  177. } else if ( contentChanged ) {
  178. // If range boundaries have not changed, but there was change inside the range, fire `change:content` event.
  179. this.fire( 'change:content', Range.createFromRange( this ), {
  180. type: changeType,
  181. batch,
  182. range: targetRange,
  183. sourcePosition
  184. } );
  185. }
  186. }
  187. mix( LiveRange, EmitterMixin );