liveposition.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import RootElement from './rootelement.js';
  6. import Position from './position.js';
  7. import Range from './range.js';
  8. import EmitterMixin from '../../utils/emittermixin.js';
  9. import mix from '../../utils/mix.js';
  10. import CKEditorError from '../../utils/ckeditorerror.js';
  11. /**
  12. * `LivePosition` is a type of {@link engine.model.Position Position} that updates itself as {@link engine.model.Document document}
  13. * is changed through operations. It may be used as a bookmark.
  14. *
  15. * **Note:** Contrary to {@link engine.model.Position}, `LivePosition` works only in roots that are
  16. * {@link engine.model.RootElement}. If {@link engine.model.DocumentFragment} is passed, error will be thrown.
  17. *
  18. * **Note:** Be very careful when dealing with `LivePosition`. Each `LivePosition` instance bind events that might
  19. * have to be unbound. Use {@link engine.model.LivePosition#detach} whenever you don't need `LivePosition` anymore.
  20. *
  21. * @memberOf engine.model
  22. * @extends engine.model.Position
  23. */
  24. export default class LivePosition extends Position {
  25. /**
  26. * Creates a live position.
  27. *
  28. * @see engine.model.Position
  29. * @param {engine.model.RootElement} root
  30. * @param {Array.<Number>} path
  31. * @param {engine.model.PositionStickiness} [stickiness] Defaults to `'STICKS_TO_NEXT'`.
  32. * See {@link engine.model.LivePosition#stickiness}.
  33. */
  34. constructor( root, path, stickiness ) {
  35. if ( !( root instanceof RootElement ) ) {
  36. /**
  37. * LivePosition root has to be an instance of RootElement.
  38. *
  39. * @error liveposition-root-not-rootelement
  40. */
  41. throw new CKEditorError( 'model-liveposition-root-not-rootelement: LivePosition root has to be an instance of RootElement.' );
  42. }
  43. super( root, path );
  44. /**
  45. * Flag representing `LivePosition` stickiness. `LivePosition` might be sticking to previous node or next node.
  46. * Whenever some nodes are inserted at the same position as `LivePosition`, `stickiness` is checked to decide if
  47. * LivePosition should be moved. Similar applies when a range of nodes is moved and one of it's boundary
  48. * position is same as `LivePosition`.
  49. *
  50. * Examples:
  51. *
  52. * Insert:
  53. * Position is at | and we insert at the same position, marked as ^:
  54. * - | sticks to previous node: `<p>f|^oo</p>` => `<p>f|baroo</p>`
  55. * - | sticks to next node: `<p>f^|oo</p>` => `<p>fbar|oo</p>`
  56. *
  57. * Move:
  58. * Position is at | and range [ ] is moved to position ^:
  59. * - | sticks to previous node: `<p>f|[oo]</p><p>b^ar</p>` => `<p>f|</p><p>booar</p>`
  60. * - | sticks to next node: `<p>f|[oo]</p><p>b^ar</p>` => `<p>f</p><p>b|ooar</p>`
  61. *
  62. * @member {engine.model.PositionStickiness} engine.model.LivePosition#stickiness
  63. */
  64. this.stickiness = stickiness || 'STICKS_TO_NEXT';
  65. bindWithDocument.call( this );
  66. }
  67. /**
  68. * Unbinds all events previously bound by `LivePosition`. Use it whenever you don't need `LivePosition` instance
  69. * anymore (i.e. when leaving scope in which it was declared or before re-assigning variable that was
  70. * referring to it).
  71. */
  72. detach() {
  73. this.stopListening();
  74. }
  75. /**
  76. * @static
  77. * @method engine.model.LivePosition.createAfter
  78. * @see engine.model.Position.createAfter
  79. * @param {engine.model.Node} node
  80. * @returns {engine.model.LivePosition}
  81. */
  82. /**
  83. * @static
  84. * @method engine.model.LivePosition.createBefore
  85. * @see engine.model.Position.createBefore
  86. * @param {engine.model.Node} node
  87. * @returns {engine.model.LivePosition}
  88. */
  89. /**
  90. * @static
  91. * @method engine.model.LivePosition.createFromParentAndOffset
  92. * @see engine.model.Position.createFromParentAndOffset
  93. * @param {engine.model.Element} parent
  94. * @param {Number} offset
  95. * @returns {engine.model.LivePosition}
  96. */
  97. /**
  98. * @static
  99. * @method engine.model.LivePosition.createFromPosition
  100. * @see engine.model.Position.createFromPosition
  101. * @param {engine.model.Position} position
  102. * @returns {engine.model.LivePosition}
  103. */
  104. }
  105. /**
  106. * Binds this `LivePosition` to the {@link engine.model.Document document} that owns
  107. * this position's {@link engine.model.Position#root root}.
  108. *
  109. * @ignore
  110. * @private
  111. * @method engine.model.LivePosition.bindWithDocument
  112. */
  113. function bindWithDocument() {
  114. /*jshint validthis: true */
  115. this.listenTo(
  116. this.root.document,
  117. 'change',
  118. ( event, type, changes ) => {
  119. transform.call( this, type, changes.range, changes.sourcePosition );
  120. }
  121. );
  122. }
  123. /**
  124. * Updates this position accordingly to the updates applied to the model. Bases on change events.
  125. *
  126. * @ignore
  127. * @private
  128. * @method transform
  129. * @param {String} type Type of changes applied to the Tree Model.
  130. * @param {engine.model.Range} range Range containing the result of applied change.
  131. * @param {engine.model.Position} [position] Additional position parameter provided by some change events.
  132. */
  133. function transform( type, range, position ) {
  134. /*jshint validthis: true */
  135. let howMany = range.end.offset - range.start.offset;
  136. let transformed;
  137. switch ( type ) {
  138. case 'insert':
  139. let insertBefore = this.stickiness == 'STICKS_TO_NEXT';
  140. transformed = this._getTransformedByInsertion( range.start, howMany, insertBefore );
  141. break;
  142. case 'move':
  143. case 'remove':
  144. case 'reinsert':
  145. let originalRange = Range.createFromPositionAndShift( position, howMany );
  146. let gotMoved = originalRange.containsPosition( this ) ||
  147. ( originalRange.start.isEqual( this ) && this.stickiness == 'STICKS_TO_NEXT' ) ||
  148. ( originalRange.end.isEqual( this ) && this.stickiness == 'STICKS_TO_PREVIOUS' );
  149. // We can't use ._getTransformedByMove() because we have a different if-condition.
  150. if ( gotMoved ) {
  151. transformed = this._getCombined( position, range.start );
  152. } else {
  153. let insertBefore = this.stickiness == 'STICKS_TO_NEXT';
  154. transformed = this._getTransformedByMove( position, range.start, howMany, insertBefore );
  155. }
  156. break;
  157. default:
  158. return;
  159. }
  160. this.path = transformed.path;
  161. this.root = transformed.root;
  162. }
  163. mix( LivePosition, EmitterMixin );
  164. /**
  165. * Enum representing how position is "sticking" with their neighbour nodes.
  166. * Possible values: `'STICKS_TO_NEXT'`, `'STICKS_TO_PREVIOUS'`.
  167. *
  168. * @typedef {String} engine.model.PositionStickiness
  169. */