selectionobserver.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/observer/selectionobserver
  7. */
  8. /* global setInterval, clearInterval */
  9. import Observer from './observer';
  10. import MutationObserver from './mutationobserver';
  11. import log from '@ckeditor/ckeditor5-utils/src/log';
  12. import debounce from '@ckeditor/ckeditor5-utils/src/lib/lodash/debounce';
  13. /**
  14. * Selection observer class observes selection changes in the document. If selection changes on the document this
  15. * observer checks if there are any mutations and if DOM selection is different than the
  16. * {@link module:engine/view/document~Document#selection view selection}. Selection observer fires
  17. * {@link module:engine/view/document~Document#event:selectionChange} event only if selection change was the only change in the document
  18. * and DOM selection is different then the view selection.
  19. *
  20. * Note that this observer is attached by the {@link module:engine/view/view~View} and is available by default.
  21. *
  22. * @see module:engine/view/observer/mutationobserver~MutationObserver
  23. * @extends module:engine/view/observer/observer~Observer
  24. */
  25. export default class SelectionObserver extends Observer {
  26. constructor( view ) {
  27. super( view );
  28. /**
  29. * Instance of the mutation observer. Selection observer calls
  30. * {@link module:engine/view/observer/mutationobserver~MutationObserver#flush} to ensure that the mutations will be handled
  31. * before the {@link module:engine/view/document~Document#event:selectionChange} event is fired.
  32. *
  33. * @readonly
  34. * @member {module:engine/view/observer/mutationobserver~MutationObserver}
  35. * module:engine/view/observer/selectionobserver~SelectionObserver#mutationObserver
  36. */
  37. this.mutationObserver = view.getObserver( MutationObserver );
  38. /**
  39. * Reference to the view {@link module:engine/view/selection~Selection} object used to compare new selection with it.
  40. *
  41. * @readonly
  42. * @member {module:engine/view/selection~Selection} module:engine/view/observer/selectionobserver~SelectionObserver#selection
  43. */
  44. this.selection = this.document.selection;
  45. /* eslint-disable max-len */
  46. /**
  47. * Reference to the {@link module:engine/view/view~View#domConverter}.
  48. *
  49. * @readonly
  50. * @member {module:engine/view/domconverter~DomConverter} module:engine/view/observer/selectionobserver~SelectionObserver#domConverter
  51. */
  52. /* eslint-enable max-len */
  53. this.domConverter = view.domConverter;
  54. /**
  55. * Set of documents which have added "selectionchange" listener to avoid adding listener twice to the same
  56. * document.
  57. *
  58. * @private
  59. * @member {WeakSet.<Document>} module:engine/view/observer/selectionobserver~SelectionObserver#_documents
  60. */
  61. this._documents = new WeakSet();
  62. /**
  63. * Fires debounced event `selectionChangeDone`. It uses `lodash#debounce` method to delay function call.
  64. *
  65. * @private
  66. * @param {Object} data Selection change data.
  67. * @method #_fireSelectionChangeDoneDebounced
  68. */
  69. this._fireSelectionChangeDoneDebounced = debounce( data => this.document.fire( 'selectionChangeDone', data ), 200 );
  70. this._clearInfiniteLoopInterval = setInterval( () => this._clearInfiniteLoop(), 1000 );
  71. /**
  72. * Private property to check if the code does not enter infinite loop.
  73. *
  74. * @private
  75. * @member {Number} module:engine/view/observer/selectionobserver~SelectionObserver#_loopbackCounter
  76. */
  77. this._loopbackCounter = 0;
  78. }
  79. /**
  80. * @inheritDoc
  81. */
  82. observe( domElement ) {
  83. const domDocument = domElement.ownerDocument;
  84. // Add listener once per each document.
  85. if ( this._documents.has( domDocument ) ) {
  86. return;
  87. }
  88. this.listenTo( domDocument, 'selectionchange', () => {
  89. this._handleSelectionChange( domDocument );
  90. } );
  91. this._documents.add( domDocument );
  92. }
  93. /**
  94. * @inheritDoc
  95. */
  96. destroy() {
  97. super.destroy();
  98. clearInterval( this._clearInfiniteLoopInterval );
  99. this._fireSelectionChangeDoneDebounced.cancel();
  100. }
  101. /**
  102. * Selection change listener. {@link module:engine/view/observer/mutationobserver~MutationObserver#flush Flush} mutations, check if
  103. * selection changes and fires {@link module:engine/view/document~Document#event:selectionChange} event on every change
  104. * and {@link module:engine/view/document~Document#event:selectionChangeDone} when selection stop changing.
  105. *
  106. * @private
  107. * @param {Document} domDocument DOM document.
  108. */
  109. _handleSelectionChange( domDocument ) {
  110. // Selection is handled when document is not focused but is read-only. This is because in read-only
  111. // mode contenteditable is set as false and editor won't receive focus but we still need to know
  112. // selection position.
  113. if ( !this.isEnabled || ( !this.document.isFocused && !this.document.isReadOnly ) ) {
  114. return;
  115. }
  116. // Ensure the mutation event will be before selection event on all browsers.
  117. this.mutationObserver.flush();
  118. // If there were mutations then the view will be re-rendered by the mutation observer and selection
  119. // will be updated, so selections will equal and event will not be fired, as expected.
  120. const domSelection = domDocument.defaultView.getSelection();
  121. const newViewSelection = this.domConverter.domSelectionToView( domSelection );
  122. if ( this.selection.isEqual( newViewSelection ) && this.domConverter.isDomSelectionCorrect( domSelection ) ) {
  123. return;
  124. }
  125. // Ensure we are not in the infinite loop (#400).
  126. // This counter is reset each second. 60 selection changes in 1 second is enough high number
  127. // to be very difficult (impossible) to achieve using just keyboard keys (during normal editor use).
  128. if ( ++this._loopbackCounter > 60 ) {
  129. /**
  130. * Selection change observer detected an infinite rendering loop.
  131. * Most probably you try to put the selection in the position which is not allowed
  132. * by the browser and browser fixes it automatically what causes `selectionchange` event on
  133. * which a loopback through a model tries to re-render the wrong selection and again.
  134. *
  135. * @error selectionchange-infinite-loop
  136. */
  137. log.warn( 'selectionchange-infinite-loop: Selection change observer detected an infinite rendering loop.' );
  138. return;
  139. }
  140. if ( this.selection.isSimilar( newViewSelection ) ) {
  141. // If selection was equal and we are at this point of algorithm, it means that it was incorrect.
  142. // Just re-render it, no need to fire any events, etc.
  143. this.view.render();
  144. } else {
  145. const data = {
  146. oldSelection: this.selection,
  147. newSelection: newViewSelection,
  148. domSelection
  149. };
  150. // Prepare data for new selection and fire appropriate events.
  151. this.document.fire( 'selectionChange', data );
  152. // Call` #_fireSelectionChangeDoneDebounced` every time when `selectionChange` event is fired.
  153. // This function is debounced what means that `selectionChangeDone` event will be fired only when
  154. // defined int the function time will elapse since the last time the function was called.
  155. // So `selectionChangeDone` will be fired when selection will stop changing.
  156. this._fireSelectionChangeDoneDebounced( data );
  157. }
  158. }
  159. /**
  160. * Clears `SelectionObserver` internal properties connected with preventing infinite loop.
  161. *
  162. * @protected
  163. */
  164. _clearInfiniteLoop() {
  165. this._loopbackCounter = 0;
  166. }
  167. }
  168. /**
  169. * Fired when selection has changed. This event is fired only when the selection change was the only change that happened
  170. * in the document, and old selection is different then the new selection.
  171. *
  172. * Introduced by {@link module:engine/view/observer/selectionobserver~SelectionObserver}.
  173. *
  174. * Note that because {@link module:engine/view/observer/selectionobserver~SelectionObserver} is attached by the
  175. * {@link module:engine/view/view~View} this event is available by default.
  176. *
  177. * @see module:engine/view/observer/selectionobserver~SelectionObserver
  178. * @event module:engine/view/document~Document#event:selectionChange
  179. * @param {Object} data
  180. * @param {module:engine/view/selection~Selection} data.oldSelection Old View selection which is
  181. * {@link module:engine/view/document~Document#selection}.
  182. * @param {module:engine/view/selection~Selection} data.newSelection New View selection which is converted DOM selection.
  183. * @param {Selection} data.domSelection Native DOM selection.
  184. */
  185. /**
  186. * Fired when selection stops changing.
  187. *
  188. * Introduced by {@link module:engine/view/observer/selectionobserver~SelectionObserver}.
  189. *
  190. * Note that because {@link module:engine/view/observer/selectionobserver~SelectionObserver} is attached by the
  191. * {@link module:engine/view/view~View} this event is available by default.
  192. *
  193. * @see module:engine/view/observer/selectionobserver~SelectionObserver
  194. * @event module:engine/view/document~Document#event:selectionChangeDone
  195. * @param {Object} data
  196. * @param {module:engine/view/selection~Selection} data.oldSelection Old View selection which is
  197. * {@link module:engine/view/document~Document#selection}.
  198. * @param {module:engine/view/selection~Selection} data.newSelection New View selection which is converted DOM selection.
  199. * @param {Selection} data.domSelection Native DOM selection.
  200. */