liverange.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [
  7. 'treemodel/range',
  8. 'treemodel/liveposition',
  9. 'emittermixin',
  10. 'utils'
  11. ], ( Range, LivePosition, EmitterMixin, utils ) => {
  12. /**
  13. * LiveRange is a Range in the Tree Model that updates itself as the tree changes. It may be used as a bookmark.
  14. * **Note:** Be very careful when dealing with LiveRange. Each LiveRange instance bind events that might
  15. * have to be unbound. Use {@link #detach} whenever you don't need LiveRange anymore.
  16. *
  17. * @class treeModel.LiveRange
  18. */
  19. class LiveRange extends Range {
  20. /**
  21. * Creates a smart range.
  22. *
  23. * @see {treeModel.Range}
  24. * @constructor
  25. */
  26. constructor( start, end ) {
  27. super( start, end );
  28. this.start = new LivePosition( this.start.root, this.start.path.slice(), false );
  29. this.end = new LivePosition( this.end.root, this.end.path.slice(), true );
  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 {@link treeModel.Range#createFromElement}
  44. * @static
  45. * @method createFromElement
  46. * @param {treeModel.Element} element
  47. * @returns {treeModel.LiveRange}
  48. */
  49. /**
  50. * @see {@link treeModel.Range#createFromPositionAndShift}
  51. * @static
  52. * @method createFromPositionAndShift
  53. * @param {treeModel.Position} position
  54. * @param {Number} shift
  55. * @returns {treeModel.LiveRange}
  56. */
  57. /**
  58. * @see {@link treeModel.Range#createFromParentsAndOffsets}
  59. * @static
  60. * @method createFromParentsAndOffsets
  61. * @param {treeModel.Element} startElement
  62. * @param {Number} startOffset
  63. * @param {treeModel.Element} endElement
  64. * @param {Number} endOffset
  65. * @returns {treeModel.LiveRange}
  66. */
  67. /**
  68. * @see {@link treeModel.Range#createFromRange}
  69. * @static
  70. * @method createFromRange
  71. * @param {treeModel.Range} range
  72. * @returns {treeModel.LiveRange}
  73. */
  74. }
  75. /**
  76. * Binds this LiveRange to the {@link treeModel.Document} that owns this range.
  77. *
  78. * @private
  79. * @method bindWithDocument
  80. */
  81. function bindWithDocument() {
  82. /*jshint validthis: true */
  83. this.listenTo(
  84. this.root.document,
  85. 'change',
  86. ( event, type, changes ) => {
  87. fixBoundaries.call( this, type, changes.range, changes.sourcePosition );
  88. },
  89. this
  90. );
  91. }
  92. /**
  93. * LiveRange is partially updated by "automatic" updates of LivePositions that are boundaries of LiveRange.
  94. * However there are cases when the boundaries have to be fixed because they end up in wrong places.
  95. *
  96. * @private
  97. * @method fixBoundaries
  98. * @param {String} type Type of changes applied to the Tree Model.
  99. * @param {treeModel.Range} range Range containing the result of applied change.
  100. * @param {treeModel.Position} [position] Additional position parameter provided by some change events.
  101. */
  102. function fixBoundaries( type, range, position ) {
  103. /*jshint validthis: true */
  104. if ( type == 'move' || type == 'remove' || type == 'reinsert' ) {
  105. let containsStart = range.containsPosition( this.start ) || range.start.isEqual( this.start );
  106. let containsEnd = range.containsPosition( this.end ) || range.end.isEqual( this.end );
  107. position = position.getTransformedByInsertion( range.start, range.end.offset - range.start.offset, true );
  108. // If the range contains both start and end, don't do anything - LivePositions that are boundaries of
  109. // this LiveRange are in correct places, they got correctly transformed.
  110. if ( containsStart && !containsEnd && !range.end.isTouching( position ) ) {
  111. this.start.path = position.path.slice();
  112. this.start.root = position.root;
  113. }
  114. if ( containsEnd && !containsStart && !range.start.isTouching( position ) ) {
  115. this.end.path = position.path.slice();
  116. this.end.root = position.root;
  117. }
  118. }
  119. }
  120. utils.extend( LiveRange.prototype, EmitterMixin );
  121. return LiveRange;
  122. } );