liverange.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 is changed due to changes on {@link module:engine/model/document~Document}.
  71. *
  72. * @event change
  73. * @param {module:engine/model/range~Range} oldRange
  74. * Range with start and end position equal to start and end position of this live range before it got changed.
  75. */
  76. }
  77. /**
  78. * Binds this `LiveRange` to the {@link module:engine/model/document~Document document}
  79. * that owns this range's {@link module:engine/model/range~Range#root root}.
  80. *
  81. * @ignore
  82. * @private
  83. * @method module:engine/model/liverange~LiveRange#bindWithDocument
  84. */
  85. function bindWithDocument() {
  86. /*jshint validthis: true */
  87. // Operation types that a range can be transformed by.
  88. const supportedTypes = new Set( [ 'insert', 'move', 'remove', 'reinsert' ] );
  89. this.listenTo(
  90. this.root.document,
  91. 'change',
  92. ( event, type, changes ) => {
  93. if ( supportedTypes.has( type ) ) {
  94. transform.call( this, type, changes.range, changes.sourcePosition );
  95. }
  96. },
  97. { priority: 'high' }
  98. );
  99. }
  100. /**
  101. * Updates this range accordingly to the updates applied to the model. Bases on change events.
  102. *
  103. * @ignore
  104. * @private
  105. * @method transform
  106. * @param {String} type Type of changes applied to the model document.
  107. * @param {module:engine/model/range~Range} targetRange Range containing the result of applied change.
  108. * @param {module:engine/model/position~Position} [sourcePosition] Source position for move, remove and reinsert change types.
  109. */
  110. function transform( type, targetRange, sourcePosition ) {
  111. /* jshint validthis: true */
  112. const howMany = targetRange.end.offset - targetRange.start.offset;
  113. let targetPosition = targetRange.start;
  114. if ( type == 'move' ) {
  115. // Range._getTransformedByDocumentChange is expecting `targetPosition` to be "before" move
  116. // (before transformation). `targetRange.start` is already after the move happened.
  117. // We have to revert `targetPosition` to the state before the move.
  118. targetPosition = targetPosition._getTransformedByInsertion( sourcePosition, howMany );
  119. }
  120. const result = this._getTransformedByDocumentChange( type, targetPosition, howMany, sourcePosition );
  121. // Decide whether inserted part should be included in the range.
  122. // This extra step is needed only for `move` change type and only if the ranges have common part.
  123. // If ranges are not intersecting and `targetRange` is moved to this range, it is included (in `_getTransformedByDocumentChange`).
  124. //
  125. // If ranges are intersecting, `result` contains spread range. `targetRange` need to be included if it is moved
  126. // to this range, but only if it is moved to a position that is not touching this range boundary.
  127. //
  128. // First, this concerns only `move` change, because insert change includes inserted part always (type == 'move').
  129. // Second, this is a case only if moved range was intersecting with this range and was inserted into this range (result.length == 3).
  130. // Third, we check range `result[ 1 ]`, that is the range created by splitting this range by inserting `targetRange`.
  131. // If that range is not empty, it means that we are in fact inserting inside `targetRange`.
  132. if ( type == 'move' && result.length == 3 && !result[ 1 ].isEmpty ) {
  133. // `result[ 2 ]` is a "common part" of this range and moved range. We substitute that common part with the whole
  134. // `targetRange` because we want to include whole `targetRange` in this range.
  135. result[ 2 ] = targetRange;
  136. }
  137. const updated = Range.createFromRanges( result );
  138. // If anything changed, update the range and fire an event.
  139. if ( !updated.isEqual( this ) ) {
  140. const oldRange = Range.createFromRange( this );
  141. this.start = updated.start;
  142. this.end = updated.end;
  143. this.fire( 'change', oldRange );
  144. }
  145. }
  146. mix( LiveRange, EmitterMixin );