liveposition.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * Creates a {@link module:engine/model/position~Position position instance}, which is equal to this live position.
  60. *
  61. * @returns {module:engine/model/position~Position}
  62. */
  63. toPosition() {
  64. return new Position( this.root, this.path.slice(), this.stickiness );
  65. }
  66. /**
  67. * Creates a `LivePosition` instance that is equal to position.
  68. *
  69. * @param {module:engine/model/position~Position} position
  70. * @param {module:engine/model/position~PositionStickiness} [stickiness]
  71. * @returns {module:engine/model/position~Position}
  72. */
  73. static fromPosition( position, stickiness ) {
  74. return new this( position.root, position.path.slice(), stickiness ? stickiness : position.stickiness );
  75. }
  76. /**
  77. * @static
  78. * @protected
  79. * @method module:engine/model/liveposition~LivePosition._createAfter
  80. * @see module:engine/model/position~Position._createAfter
  81. * @param {module:engine/model/node~Node} node
  82. * @returns {module:engine/model/liveposition~LivePosition}
  83. */
  84. /**
  85. * @static
  86. * @protected
  87. * @method module:engine/model/liveposition~LivePosition._createBefore
  88. * @see module:engine/model/position~Position._createBefore
  89. * @param {module:engine/model/node~Node} node
  90. * @returns {module:engine/model/liveposition~LivePosition}
  91. */
  92. /**
  93. * Fired when `LivePosition` instance is changed due to changes on {@link module:engine/model/document~Document}.
  94. *
  95. * @event module:engine/model/liveposition~LivePosition#change
  96. * @param {module:engine/model/position~Position} oldPosition Position equal to this live position before it got changed.
  97. */
  98. }
  99. // Binds this `LivePosition` to the {@link module:engine/model/document~Document document} that owns
  100. // this position's {@link module:engine/model/position~Position#root root}.
  101. //
  102. // @private
  103. function bindWithDocument() {
  104. this.listenTo(
  105. this.root.document.model,
  106. 'applyOperation',
  107. ( event, args ) => {
  108. const operation = args[ 0 ];
  109. if ( !operation.isDocumentOperation ) {
  110. return;
  111. }
  112. transform.call( this, operation );
  113. },
  114. { priority: 'low' }
  115. );
  116. }
  117. // Updates this position accordingly to the updates applied to the model. Bases on change events.
  118. //
  119. // @private
  120. // @param {module:engine/model/operation/operation~Operation} operation Executed operation.
  121. function transform( operation ) {
  122. const result = this.getTransformedByOperation( operation );
  123. if ( !this.isEqual( result ) ) {
  124. const oldPosition = this.toPosition();
  125. this.path = result.path;
  126. this.root = result.root;
  127. this.fire( 'change', oldPosition );
  128. }
  129. }
  130. mix( LivePosition, EmitterMixin );