8
0

mutationobserver.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Observer from './observer.js';
  7. /**
  8. * Mutation observer class observes changes in the DOM, fires {@link engine.treeView.TreeView#mutations} event, mark view elements
  9. * as changed and call {@link engine.treeView.render}. Because all mutated nodes are marked as "to be rendered" and the
  10. * {@link engine.treeView.render} is called, all changes will be reverted, unless the mutation will be handled by the
  11. * {@link engine.treeView.TreeView#mutations} event listener. It means user will see only handled changes, and the editor will
  12. * block all changes which are not handled.
  13. *
  14. * Mutation Observer also take care of reducing number of mutations which are fired. It removes duplicates and
  15. * mutations on elements which do not have corresponding view elements. Also
  16. * {@link engine.treeView.TreeView.MutatatedText text mutation} is fired only if parent element do not change child list.
  17. *
  18. * @memberOf engine.treeView.observer
  19. * @extends engine.treeView.observer.Observer
  20. */
  21. export default class MutationObserver extends Observer {
  22. constructor( treeView ) {
  23. super( treeView );
  24. /**
  25. * Native mutation observer config.
  26. *
  27. * @private
  28. * @member {Object} engine.treeView.observer.MutationObserver#_config
  29. */
  30. this._config = {
  31. childList: true,
  32. characterData: true,
  33. characterDataOldValue: true,
  34. subtree: true
  35. };
  36. /**
  37. * Reference to the {@link engine.treeView.TreeView#domConverter}.
  38. *
  39. * @member {engine.treeView.DomConverter} engine.treeView.observer.MutationObserver#domConverter
  40. */
  41. this.domConverter = treeView.domConverter;
  42. /**
  43. * Reference to the {@link engine.treeView.TreeView#renderer}.
  44. *
  45. * @member {engine.treeView.Renderer} engine.treeView.observer.MutationObserver#renderer
  46. */
  47. this.renderer = treeView.renderer;
  48. /**
  49. * Observed DOM elements.
  50. *
  51. * @private
  52. * @member {Array.<HTMLElement>} engine.treeView.observer.MutationObserver#_domElements
  53. */
  54. this._domElements = [];
  55. /**
  56. * Native mutation observer.
  57. *
  58. * @private
  59. * @member {MutationObserver} engine.treeView.observer.MutationObserver#_mutationObserver
  60. */
  61. this._mutationObserver = new window.MutationObserver( this._onMutations.bind( this ) );
  62. }
  63. /**
  64. * Synchronously fires {@link engine.treeView.TreeView#mutations} event with all mutations in record queue.
  65. * At the same time empties the queue so mutations will not be fired twice.
  66. *
  67. * @returns {[type]} [description]
  68. */
  69. flush() {
  70. this._onMutations( this._mutationObserver.takeRecords() );
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. observe( domElement ) {
  76. this._domElements.push( domElement );
  77. if ( this.isEnabled ) {
  78. this._mutationObserver.observe( domElement, this._config );
  79. }
  80. }
  81. /**
  82. * @inheritDoc
  83. */
  84. enable() {
  85. super.enable();
  86. for ( let domElement of this._domElements ) {
  87. this._mutationObserver.observe( domElement, this._config );
  88. }
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. disable() {
  94. super.disable();
  95. this._mutationObserver.disconnect();
  96. }
  97. /**
  98. * Handles mutations. Deduplicates, mark view elements to sync, fire event and call render.
  99. *
  100. * @protected
  101. * @method engine.treeView.observer.MutationObserver#_onMutations
  102. * @param {Array.<Object>} domMutations Array of native mutations.
  103. */
  104. _onMutations( domMutations ) {
  105. // As a result of this.flush() we can have an empty collection.
  106. if ( domMutations.length === 0 ) {
  107. return;
  108. }
  109. const domConverter = this.domConverter;
  110. // Use map and set for deduplication.
  111. const mutatedTexts = new Map();
  112. const mutatedElements = new Set();
  113. // Handle `childList` mutations first, so we will be able to check if the `characterData` mutation is in the
  114. // element with changed structure anyway.
  115. for ( let mutation of domMutations ) {
  116. if ( mutation.type === 'childList' ) {
  117. const element = domConverter.getCorrespondingViewElement( mutation.target );
  118. if ( element ) {
  119. mutatedElements.add( element );
  120. }
  121. }
  122. }
  123. // Handle `characterData` mutations later, when we have the full list of nodes which changed structure.
  124. for ( let mutation of domMutations ) {
  125. if ( mutation.type === 'characterData' ) {
  126. const text = domConverter.getCorrespondingViewText( mutation.target );
  127. if ( text && !mutatedElements.has( text.parent ) ) {
  128. // Use text as a key, for deduplication. If there will be another mutation on the same text element
  129. // we will have only one in the map.
  130. mutatedTexts.set( text, {
  131. type: 'text',
  132. oldText: text.data,
  133. newText: domConverter.getDataWithoutFiller( mutation.target ),
  134. node: text
  135. } );
  136. }
  137. // When we added first letter to the text node which had only inline filler, for the DOM it is mutation
  138. // on text, but for the view, where filler text node did not existed, new text node was created, so we
  139. // need to fire 'children' mutation instead of 'text'.
  140. else if ( !text && domConverter.startsWithFiller( mutation.target ) ) {
  141. mutatedElements.add( domConverter.getCorrespondingViewElement( mutation.target.parentNode ) );
  142. }
  143. }
  144. }
  145. // Now we build the list of mutations to fire and mark elements. We did not do it earlier to avoid marking the
  146. // same node multiple times in case of duplication.
  147. // List of mutations we will fire.
  148. const viewMutations = [];
  149. for ( let mutatedText of mutatedTexts.values() ) {
  150. this.renderer.markToSync( 'TEXT', mutatedText.node );
  151. viewMutations.push( mutatedText );
  152. }
  153. for ( let viewElement of mutatedElements ) {
  154. const domElement = domConverter.getCorrespondingDomElement( viewElement );
  155. const viewChildren = viewElement.getChildren();
  156. const newViewChildren = domConverter.domChildrenToView( domElement );
  157. this.renderer.markToSync( 'CHILDREN', viewElement );
  158. viewMutations.push( {
  159. type: 'children',
  160. oldChildren: Array.from( viewChildren ),
  161. newChildren: Array.from( newViewChildren ),
  162. node: viewElement
  163. } );
  164. }
  165. this.treeView.fire( 'mutations', viewMutations );
  166. this.treeView.render();
  167. }
  168. }
  169. /**
  170. * Fired when mutation occurred. If tree view is not changed on this event, DOM will be reverter to the state before
  171. * mutation, so all changes which should be applied, should be handled on this event.
  172. *
  173. * @event engine.treeView.TreeView#mutations
  174. * @param {Array.<engine.treeView.TreeView~MutatatedText|engine.treeView.TreeView~MutatatedChildren>} viewMutations
  175. * Array of mutations.
  176. * For mutated texts it will be {@link engine.treeView.TreeView~MutatatedText} and for mutated elements it will be
  177. * {@link engine.treeView.TreeView~MutatatedElement}. You can recognize the type based on the `type` property.
  178. */
  179. /**
  180. * Mutation item for text.
  181. *
  182. * @see engine.treeView.TreeView#mutations
  183. * @see engine.treeView.MutatatedChildren
  184. *
  185. * @typedef {Object} engine.treeView.MutatatedText
  186. *
  187. * @property {String} type For text mutations it is always 'text'.
  188. * @property {engine.treeView.Text} node Mutated text node.
  189. * @property {String} oldText Old text.
  190. * @property {String} newText New text.
  191. */
  192. /**
  193. * Mutation item for child nodes.
  194. *
  195. * @see engine.treeView.TreeView#mutations
  196. * @see engine.treeView.MutatatedText
  197. *
  198. * @typedef {Object} engine.treeView.MutatatedChildren
  199. *
  200. * @property {String} type For child nodes mutations it is always 'children'.
  201. * @property {engine.treeView.Element} node Parent of the mutated children.
  202. * @property {Array.<engine.treeView.Node>} oldChildren Old child nodes.
  203. * @property {Array.<engine.treeView.Node>} newChildren New child nodes.
  204. */