document.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 Selection from './selection.js';
  7. import Renderer from './renderer.js';
  8. import Writer from './writer.js';
  9. import DomConverter from './domconverter.js';
  10. import RootEditableElement from './rooteditableelement.js';
  11. import { injectQuirksHandling } from './filler.js';
  12. import mix from '../../utils/mix.js';
  13. import ObservableMixin from '../../utils/observablemixin.js';
  14. /**
  15. * Document class creates an abstract layer over the content editable area.
  16. * It combines the actual tree of view elements, tree of DOM elements,
  17. * {@link engine.view.DomConverter DOM Converter}, {@link engine.view.Renderer renderer} and all
  18. * {@link engine.view.Observer observers}.
  19. *
  20. * If you want to only transform the tree of view elements to the DOM elements you can use the
  21. * {@link engine.view.DomConverter DomConverter}.
  22. *
  23. * @memberOf engine.view
  24. * @mixes utils.EmitterMixin
  25. */
  26. export default class Document {
  27. /**
  28. * Creates a Document instance.
  29. */
  30. constructor() {
  31. /**
  32. * Roots of the DOM tree. Map on the `HTMLElement`s with roots names as keys.
  33. *
  34. * @readonly
  35. * @member {Map} engine.view.Document#domRoots
  36. */
  37. this.domRoots = new Map();
  38. /**
  39. * Selection done on this document.
  40. *
  41. * @readonly
  42. * @member {engine.view.Selection} engine.view.Document#selection
  43. */
  44. this.selection = new Selection();
  45. /**
  46. * Tree View writer.
  47. *
  48. * @readonly
  49. * @member {engine.view.Writer} engine.view.Document#writer
  50. */
  51. this.writer = new Writer();
  52. /**
  53. * Instance of the {@link engine.view.DomConverter domConverter} use by
  54. * {@link engine.view.Document#renderer renderer} and {@link engine.view.observer.Observer observers}.
  55. *
  56. * @readonly
  57. * @member {engine.view.DomConverter} engine.view.Document#domConverter
  58. */
  59. this.domConverter = new DomConverter();
  60. /**
  61. * Roots of the view tree. Map of the {engine.view.Element view elements} with roots names as keys.
  62. *
  63. * @readonly
  64. * @member {Map} engine.view.Document#roots
  65. */
  66. this.roots = new Map();
  67. /**
  68. * True if document is focused.
  69. *
  70. * This property is updated by the {@link engine.view.obsever.FocusObserver}.
  71. * If the {@link engine.view.obsever.FocusObserver} is disabled this property will not change.
  72. *
  73. * @readonly
  74. * @observable
  75. * @member {Boolean} engine.view.Document#isFocused
  76. */
  77. this.set( 'isFocused', false );
  78. /**
  79. * {@link engine.view.EditableElement EditableElement} which is containing selection. It can be `null` if
  80. * there is no selection found.
  81. *
  82. * @readonly
  83. * @observable
  84. * @member {engine.view.EditableElement|null} engine.view.Document#selectedEditable
  85. */
  86. this.set( 'selectedEditable', this.selection.getEditableElement() );
  87. this.selection.on( 'change', () => this.selectedEditable = this.selection.getEditableElement() );
  88. /**
  89. * Instance of the {@link engine.view.Document#renderer renderer}.
  90. *
  91. * @readonly
  92. * @member {engine.view.Renderer} engine.view.Document#renderer
  93. */
  94. this.renderer = new Renderer( this.domConverter, this.selection );
  95. this.renderer.bind( 'isFocused' ).to( this, 'isFocused' );
  96. /**
  97. * Map of registered {@link engine.view.Observer observers}.
  98. *
  99. * @private
  100. * @member {Map.<Function, engine.view.Observer>} engine.view.Document#_observers
  101. */
  102. this._observers = new Map();
  103. injectQuirksHandling( this );
  104. }
  105. /**
  106. * Creates observer of the given type if not yet created, {@link engine.view.Observer#enable enables} it
  107. * and {@link engine.view.observer.Observer#observe attaches} to all existing and future
  108. * {@link engine.view.Document#domRoots DOM roots}.
  109. *
  110. * Note: Observers are recognized by their constructor (classes). A single observer will be instantiated and used only
  111. * when registered for the first time. This means that features and other components can register a single observer
  112. * multiple times without caring whether it has been already added or not.
  113. *
  114. * @param {Function} Observer The constructor of an observer to add.
  115. * Should create an instance inheriting from {@link engine.view.observer.Observer}.
  116. * @returns {engine.view.observer.Observer} Added observer instance.
  117. */
  118. addObserver( Observer ) {
  119. let observer = this._observers.get( Observer );
  120. if ( observer ) {
  121. return observer;
  122. }
  123. observer = new Observer( this );
  124. this._observers.set( Observer, observer );
  125. for ( let [ name, domElement ] of this.domRoots ) {
  126. observer.observe( domElement, name );
  127. }
  128. observer.enable();
  129. return observer;
  130. }
  131. /**
  132. * Returns observer of the given type or `undefined` if such observer has not been added yet.
  133. *
  134. * @param {Function} Observer The constructor of an observer to get.
  135. * @returns {engine.view.observer.Observer|undefined} Observer instance or undefined.
  136. */
  137. getObserver( Observer ) {
  138. return this._observers.get( Observer );
  139. }
  140. /**
  141. * Creates a {@link engine.view.Document#roots view root element}.
  142. *
  143. * If the DOM element is passed as a first parameter it will be automatically
  144. * {@link engine.view.Document#attachDomRoot attached}:
  145. *
  146. * document.createRoot( document.querySelector( 'div#editor' ) ); // Will call document.attachDomRoot.
  147. *
  148. * However, if the string is passed, then only the view element will be created and the DOM element have to be
  149. * attached separately:
  150. *
  151. * document.createRoot( 'body' );
  152. * document.attachDomRoot( document.querySelector( 'body#editor' ) );
  153. *
  154. * @param {Element|String} domRoot DOM root element or the tag name of view root element if the DOM element will be
  155. * attached later.
  156. * @param {String} [name='main'] Name of the root.
  157. * @returns {engine.view.RootEditableElement} The created view root element.
  158. */
  159. createRoot( domRoot, name = 'main' ) {
  160. const rootTag = typeof domRoot == 'string' ? domRoot : domRoot.tagName;
  161. const viewRoot = new RootEditableElement( this, rootTag, name );
  162. this.roots.set( name, viewRoot );
  163. // Mark changed nodes in the renderer.
  164. viewRoot.on( 'change:children', ( evt, node ) => this.renderer.markToSync( 'children', node ) );
  165. viewRoot.on( 'change:attributes', ( evt, node ) => this.renderer.markToSync( 'attributes', node ) );
  166. viewRoot.on( 'change:text', ( evt, node ) => this.renderer.markToSync( 'text', node ) );
  167. if ( domRoot instanceof HTMLElement ) {
  168. this.attachDomRoot( domRoot, name );
  169. }
  170. return viewRoot;
  171. }
  172. /**
  173. * Attaches DOM root element to the view element and enable all observers on that element. This method also
  174. * {@link engine.view.Renderer#markToSync mark element} to be synchronized with the view what means that all child
  175. * nodes will be removed and replaced with content of the view root.
  176. *
  177. * Note that {@link engine.view.Document#createRoot} will call this method automatically if the DOM element is
  178. * passed to it.
  179. *
  180. * @param {Element|String} domRoot DOM root element.
  181. * @param {String} [name='main'] Name of the root.
  182. */
  183. attachDomRoot( domRoot, name = 'main' ) {
  184. const viewRoot = this.getRoot( name );
  185. this.domRoots.set( name, domRoot );
  186. this.domConverter.bindElements( domRoot, viewRoot );
  187. this.renderer.markToSync( 'children', viewRoot );
  188. for ( let observer of this._observers.values() ) {
  189. observer.observe( domRoot, name );
  190. }
  191. }
  192. /**
  193. * Gets a {@link engine.view.Document#roots view root element} with the specified name. If the name is not
  194. * specific "main" root is returned.
  195. *
  196. * @param {String} [name='main'] Name of the root.
  197. * @returns {engine.view.RootEditableElement} The view root element with the specified name.
  198. */
  199. getRoot( name = 'main' ) {
  200. return this.roots.get( name );
  201. }
  202. /**
  203. * Gets DOM root element.
  204. *
  205. * @param {String} [name='main'] Name of the root.
  206. * @returns {Element} DOM root element instance.
  207. */
  208. getDomRoot( name = 'main' ) {
  209. return this.domRoots.get( name );
  210. }
  211. /**
  212. * Renders all changes. In order to avoid triggering the observers (e.g. mutations) all observers are disabled
  213. * before rendering and re-enabled after that.
  214. */
  215. render() {
  216. this.disableObservers();
  217. this.renderer.render();
  218. this.enableObservers();
  219. }
  220. /**
  221. * Focuses document. It will focus {@link engine.view.EditableElement EditableElement} that is currently having
  222. * selection inside.
  223. *
  224. * @fires engine.view.Document#focus
  225. */
  226. focus() {
  227. this.fire( 'focus' );
  228. this.render();
  229. }
  230. /**
  231. * Disables all added observers.
  232. */
  233. disableObservers() {
  234. for ( let observer of this._observers.values() ) {
  235. observer.disable();
  236. }
  237. }
  238. /**
  239. * Enables all added observers.
  240. */
  241. enableObservers() {
  242. for ( let observer of this._observers.values() ) {
  243. observer.enable();
  244. }
  245. }
  246. }
  247. mix( Document, ObservableMixin );
  248. /**
  249. * Enum representing type of the change.
  250. *
  251. * Possible values:
  252. *
  253. * * `children` - for child list changes,
  254. * * `attributes` - for element attributes changes,
  255. * * `text` - for text nodes changes.
  256. *
  257. * @typedef {String} engine.view.ChangeType
  258. */