8
0

liverange.js 4.5 KB

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