liverange.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Range from './range.js';
  7. import LivePosition from './liveposition.js';
  8. import EmitterMixin from '../../utils/emittermixin.js';
  9. import utils from '../../utils/utils.js';
  10. /**
  11. * LiveRange is a Range in the Tree Model that updates itself as the tree changes. It may be used as a bookmark.
  12. *
  13. * **Note:** Constructor creates it's own {@link core.treeModel.LivePosition} instances basing on passed values.
  14. *
  15. * **Note:** Be very careful when dealing with LiveRange. Each LiveRange instance bind events that might
  16. * have to be unbound. Use {@link core.treeModel.LiveRange#detach detach} whenever you don't need LiveRange anymore.
  17. *
  18. * @memberOf core.treeModel
  19. */
  20. export default class LiveRange extends Range {
  21. /**
  22. * Creates a live range.
  23. *
  24. * @see core.treeModel.Range
  25. */
  26. constructor( start, end ) {
  27. super( start, end );
  28. this.start = new LivePosition( this.start.root, this.start.path.slice(), 'STICKS_TO_NEXT' );
  29. this.end = new LivePosition( this.end.root, this.end.path.slice(), 'STICKS_TO_PREVIOUS' );
  30. bindWithDocument.call( this );
  31. }
  32. /**
  33. * Unbinds all events previously bound by LiveRange. Use it whenever you don't need LiveRange instance
  34. * anymore (i.e. when leaving scope in which it was declared or before re-assigning variable that was
  35. * referring to it).
  36. */
  37. detach() {
  38. this.start.detach();
  39. this.end.detach();
  40. this.stopListening();
  41. }
  42. /**
  43. * @see core.treeModel.Range.createFromElement
  44. * @static
  45. * @method core.treeModel.LiveRange.createFromElement
  46. * @param {core.treeModel.Element} element
  47. * @returns {core.treeModel.LiveRange}
  48. */
  49. /**
  50. * @see core.treeModel.Range.createFromPositionAndShift
  51. * @static
  52. * @method core.treeModel.LiveRange.createFromPositionAndShift
  53. * @param {core.treeModel.Position} position
  54. * @param {Number} shift
  55. * @returns {core.treeModel.LiveRange}
  56. */
  57. /**
  58. * @see core.treeModel.Range.createFromParentsAndOffsets
  59. * @static
  60. * @method core.treeModel.LiveRange.createFromParentsAndOffsets
  61. * @param {core.treeModel.Element} startElement
  62. * @param {Number} startOffset
  63. * @param {core.treeModel.Element} endElement
  64. * @param {Number} endOffset
  65. * @returns {core.treeModel.LiveRange}
  66. */
  67. /**
  68. * @see core.treeModel.Range.createFromRange
  69. * @static
  70. * @method core.treeModel.LiveRange.createFromRange
  71. * @param {core.treeModel.Range} range
  72. * @returns {core.treeModel.LiveRange}
  73. */
  74. }
  75. /**
  76. * Binds this LiveRange to the {@link core.treeModel.Document} that owns this range.
  77. *
  78. * @ignore
  79. * @private
  80. * @method core.treeModel.LiveRange#bindWithDocument
  81. */
  82. function bindWithDocument() {
  83. /*jshint validthis: true */
  84. this.listenTo(
  85. this.root.document,
  86. 'change',
  87. ( event, type, changes ) => {
  88. fixBoundaries.call( this, type, changes.range, changes.sourcePosition );
  89. },
  90. this
  91. );
  92. }
  93. /**
  94. * LiveRange boundaries are instances of {@link core.treeModel.LivePosition}, so it is updated thanks to them. This method
  95. * additionally fixes the results of updating live positions taking into account that those live positions
  96. * are boundaries of a range. An example case for fixing live positions is end boundary is moved before start boundary.
  97. *
  98. * @ignore
  99. * @private
  100. * @method fixBoundaries
  101. * @param {String} type Type of changes applied to the Tree Model.
  102. * @param {core.treeModel.Range} range Range containing the result of applied change.
  103. * @param {core.treeModel.Position} [position] Additional position parameter provided by some change events.
  104. */
  105. function fixBoundaries( type, range, position ) {
  106. /* jshint validthis: true */
  107. if ( type == 'move' || type == 'remove' || type == 'reinsert' ) {
  108. let containsStart = range.containsPosition( this.start ) || range.start.isEqual( this.start );
  109. let containsEnd = range.containsPosition( this.end ) || range.end.isEqual( this.end );
  110. position = position.getTransformedByInsertion( range.start, range.end.offset - range.start.offset, true );
  111. // If the range contains both start and end, don't do anything - LivePositions that are boundaries of
  112. // this LiveRange are in correct places, they got correctly transformed.
  113. if ( containsStart && !containsEnd && !range.end.isTouching( position ) ) {
  114. this.start.path = position.path.slice();
  115. this.start.root = position.root;
  116. }
  117. if ( containsEnd && !containsStart && !range.start.isTouching( position ) ) {
  118. this.end.path = position.path.slice();
  119. this.end.root = position.root;
  120. }
  121. }
  122. }
  123. utils.mix( LiveRange, EmitterMixin );