liveposition.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/liveposition
  7. */
  8. import Position from './position';
  9. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  10. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. /**
  13. * `LivePosition` is a type of {@link module:engine/model/position~Position Position}
  14. * that updates itself as {@link module:engine/model/document~Document document}
  15. * is changed through operations. It may be used as a bookmark.
  16. *
  17. * **Note:** Contrary to {@link module:engine/model/position~Position}, `LivePosition` works only in roots that are
  18. * {@link module:engine/model/rootelement~RootElement}.
  19. * If {@link module:engine/model/documentfragment~DocumentFragment} is passed, error will be thrown.
  20. *
  21. * **Note:** Be very careful when dealing with `LivePosition`. Each `LivePosition` instance bind events that might
  22. * have to be unbound.
  23. * Use {@link module:engine/model/liveposition~LivePosition#detach} whenever you don't need `LivePosition` anymore.
  24. *
  25. * @extends module:engine/model/position~Position
  26. */
  27. export default class LivePosition extends Position {
  28. /**
  29. * Creates a live position.
  30. *
  31. * @see module:engine/model/position~Position
  32. * @param {module:engine/model/rootelement~RootElement} root
  33. * @param {Array.<Number>} path
  34. * @param {module:engine/model/position~PositionStickiness} [stickiness]
  35. */
  36. constructor( root, path, stickiness = 'toNone' ) {
  37. super( root, path, stickiness );
  38. if ( !this.root.is( 'rootElement' ) ) {
  39. /**
  40. * LivePosition's root has to be an instance of RootElement.
  41. *
  42. * @error liveposition-root-not-rootelement
  43. */
  44. throw new CKEditorError(
  45. 'model-liveposition-root-not-rootelement: LivePosition\'s root has to be an instance of RootElement.'
  46. );
  47. }
  48. bindWithDocument.call( this );
  49. }
  50. /**
  51. * Unbinds all events previously bound by `LivePosition`. Use it whenever you don't need `LivePosition` instance
  52. * anymore (i.e. when leaving scope in which it was declared or before re-assigning variable that was
  53. * referring to it).
  54. */
  55. detach() {
  56. this.stopListening();
  57. }
  58. /**
  59. * @static
  60. * @method module:engine/model/liveposition~LivePosition.createAfter
  61. * @see module:engine/model/position~Position.createAfter
  62. * @param {module:engine/model/node~Node} node
  63. * @returns {module:engine/model/liveposition~LivePosition}
  64. */
  65. /**
  66. * @static
  67. * @method module:engine/model/liveposition~LivePosition.createBefore
  68. * @see module:engine/model/position~Position.createBefore
  69. * @param {module:engine/model/node~Node} node
  70. * @returns {module:engine/model/liveposition~LivePosition}
  71. */
  72. /**
  73. * @static
  74. * @method module:engine/model/liveposition~LivePosition.createFromParentAndOffset
  75. * @see module:engine/model/position~Position.createFromParentAndOffset
  76. * @param {module:engine/model/element~Element} parent
  77. * @param {Number} offset
  78. * @returns {module:engine/model/liveposition~LivePosition}
  79. */
  80. /**
  81. * @static
  82. * @method module:engine/model/liveposition~LivePosition.createFromPosition
  83. * @see module:engine/model/position~Position.createFromPosition
  84. * @param {module:engine/model/position~Position} position
  85. * @returns {module:engine/model/liveposition~LivePosition}
  86. */
  87. /**
  88. * Fired when `LivePosition` instance is changed due to changes on {@link module:engine/model/document~Document}.
  89. *
  90. * @event module:engine/model/liveposition~LivePosition#change
  91. * @param {module:engine/model/position~Position} oldPosition Position equal to this live position before it got changed.
  92. */
  93. }
  94. // Binds this `LivePosition` to the {@link module:engine/model/document~Document document} that owns
  95. // this position's {@link module:engine/model/position~Position#root root}.
  96. //
  97. // @private
  98. function bindWithDocument() {
  99. this.listenTo(
  100. this.root.document.model,
  101. 'applyOperation',
  102. ( event, args ) => {
  103. const operation = args[ 0 ];
  104. if ( !operation.isDocumentOperation ) {
  105. return;
  106. }
  107. transform.call( this, operation );
  108. },
  109. { priority: 'low' }
  110. );
  111. }
  112. // Updates this position accordingly to the updates applied to the model. Bases on change events.
  113. //
  114. // @private
  115. // @param {module:engine/model/operation/operation~Operation} operation Executed operation.
  116. function transform( operation ) {
  117. const result = this.getTransformedByOperation( operation );
  118. if ( !this.isEqual( result ) ) {
  119. const oldPosition = Position.createFromPosition( this );
  120. this.path = result.path;
  121. this.root = result.root;
  122. this.fire( 'change', oldPosition );
  123. }
  124. }
  125. mix( LivePosition, EmitterMixin );