8
0

selectionobserver.js 9.3 KB

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