8
0

document.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 DomConverter from './domconverter.js';
  9. import RootEditableElement from './rooteditableelement.js';
  10. import { injectQuirksHandling } from './filler.js';
  11. import log from '../../utils/log.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. * Instance of the {@link engine.view.DomConverter domConverter} use by
  47. * {@link engine.view.Document#renderer renderer} and {@link engine.view.observer.Observer observers}.
  48. *
  49. * @readonly
  50. * @member {engine.view.DomConverter} engine.view.Document#domConverter
  51. */
  52. this.domConverter = new DomConverter();
  53. /**
  54. * Roots of the view tree. Map of the {engine.view.Element view elements} with roots names as keys.
  55. *
  56. * @readonly
  57. * @member {Map} engine.view.Document#roots
  58. */
  59. this.roots = new Map();
  60. /**
  61. * True if document is focused.
  62. *
  63. * This property is updated by the {@link engine.view.obsever.FocusObserver}.
  64. * If the {@link engine.view.obsever.FocusObserver} is disabled this property will not change.
  65. *
  66. * @readonly
  67. * @observable
  68. * @member {Boolean} engine.view.Document#isFocused
  69. */
  70. this.set( 'isFocused', false );
  71. /**
  72. * Instance of the {@link engine.view.Document#renderer renderer}.
  73. *
  74. * @readonly
  75. * @member {engine.view.Renderer} engine.view.Document#renderer
  76. */
  77. this.renderer = new Renderer( this.domConverter, this.selection );
  78. this.renderer.bind( 'isFocused' ).to( this, 'isFocused' );
  79. /**
  80. * Map of registered {@link engine.view.Observer observers}.
  81. *
  82. * @private
  83. * @member {Map.<Function, engine.view.Observer>} engine.view.Document#_observers
  84. */
  85. this._observers = new Map();
  86. injectQuirksHandling( this );
  87. // Listens `render` event on default priority.
  88. // This way we can attach other listeners before or after rendering execution.
  89. this.on( 'render', () => {
  90. this.disableObservers();
  91. this.renderer.render();
  92. this.enableObservers();
  93. } );
  94. }
  95. /**
  96. * Creates observer of the given type if not yet created, {@link engine.view.Observer#enable enables} it
  97. * and {@link engine.view.observer.Observer#observe attaches} to all existing and future
  98. * {@link engine.view.Document#domRoots DOM roots}.
  99. *
  100. * Note: Observers are recognized by their constructor (classes). A single observer will be instantiated and used only
  101. * when registered for the first time. This means that features and other components can register a single observer
  102. * multiple times without caring whether it has been already added or not.
  103. *
  104. * @param {Function} Observer The constructor of an observer to add.
  105. * Should create an instance inheriting from {@link engine.view.observer.Observer}.
  106. * @returns {engine.view.observer.Observer} Added observer instance.
  107. */
  108. addObserver( Observer ) {
  109. let observer = this._observers.get( Observer );
  110. if ( observer ) {
  111. return observer;
  112. }
  113. observer = new Observer( this );
  114. this._observers.set( Observer, observer );
  115. for ( let [ name, domElement ] of this.domRoots ) {
  116. observer.observe( domElement, name );
  117. }
  118. observer.enable();
  119. return observer;
  120. }
  121. /**
  122. * Returns observer of the given type or `undefined` if such observer has not been added yet.
  123. *
  124. * @param {Function} Observer The constructor of an observer to get.
  125. * @returns {engine.view.observer.Observer|undefined} Observer instance or undefined.
  126. */
  127. getObserver( Observer ) {
  128. return this._observers.get( Observer );
  129. }
  130. /**
  131. * Creates a {@link engine.view.Document#roots view root element}.
  132. *
  133. * If the DOM element is passed as a first parameter it will be automatically
  134. * {@link engine.view.Document#attachDomRoot attached}:
  135. *
  136. * document.createRoot( document.querySelector( 'div#editor' ) ); // Will call document.attachDomRoot.
  137. *
  138. * However, if the string is passed, then only the view element will be created and the DOM element have to be
  139. * attached separately:
  140. *
  141. * document.createRoot( 'body' );
  142. * document.attachDomRoot( document.querySelector( 'body#editor' ) );
  143. *
  144. * In both cases, {@link engine.view.RootEditableElement#name element name} is always transformed to lower case.
  145. *
  146. * @param {Element|String} domRoot DOM root element or the tag name of view root element if the DOM element will be
  147. * attached later.
  148. * @param {String} [name='main'] Name of the root.
  149. * @returns {engine.view.RootEditableElement} The created view root element.
  150. */
  151. createRoot( domRoot, name = 'main' ) {
  152. const rootTag = typeof domRoot == 'string' ? domRoot : domRoot.tagName;
  153. const viewRoot = new RootEditableElement( this, rootTag.toLowerCase(), name );
  154. this.roots.set( name, viewRoot );
  155. // Mark changed nodes in the renderer.
  156. viewRoot.on( 'change:children', ( evt, node ) => this.renderer.markToSync( 'children', node ) );
  157. viewRoot.on( 'change:attributes', ( evt, node ) => this.renderer.markToSync( 'attributes', node ) );
  158. viewRoot.on( 'change:text', ( evt, node ) => this.renderer.markToSync( 'text', node ) );
  159. if ( this.domConverter.isElement( domRoot ) ) {
  160. this.attachDomRoot( domRoot, name );
  161. }
  162. return viewRoot;
  163. }
  164. /**
  165. * Attaches DOM root element to the view element and enable all observers on that element. This method also
  166. * {@link engine.view.Renderer#markToSync mark element} to be synchronized with the view what means that all child
  167. * nodes will be removed and replaced with content of the view root.
  168. *
  169. * Note that {@link engine.view.Document#createRoot} will call this method automatically if the DOM element is
  170. * passed to it.
  171. *
  172. * @param {Element|String} domRoot DOM root element.
  173. * @param {String} [name='main'] Name of the root.
  174. */
  175. attachDomRoot( domRoot, name = 'main' ) {
  176. const viewRoot = this.getRoot( name );
  177. this.domRoots.set( name, domRoot );
  178. this.domConverter.bindElements( domRoot, viewRoot );
  179. this.renderer.markToSync( 'children', viewRoot );
  180. this.renderer.domDocuments.add( domRoot.ownerDocument );
  181. for ( let observer of this._observers.values() ) {
  182. observer.observe( domRoot, name );
  183. }
  184. }
  185. /**
  186. * Gets a {@link engine.view.Document#roots view root element} with the specified name. If the name is not
  187. * specific "main" root is returned.
  188. *
  189. * @param {String} [name='main'] Name of the root.
  190. * @returns {engine.view.RootEditableElement} The view root element with the specified name.
  191. */
  192. getRoot( name = 'main' ) {
  193. return this.roots.get( name );
  194. }
  195. /**
  196. * Gets DOM root element.
  197. *
  198. * @param {String} [name='main'] Name of the root.
  199. * @returns {Element} DOM root element instance.
  200. */
  201. getDomRoot( name = 'main' ) {
  202. return this.domRoots.get( name );
  203. }
  204. /**
  205. * Renders all changes. In order to avoid triggering the observers (e.g. mutations) all observers are disabled
  206. * before rendering and re-enabled after that.
  207. *
  208. * @fires engine.view.Document#render
  209. */
  210. render() {
  211. this.fire( 'render' );
  212. }
  213. /**
  214. * Focuses document. It will focus {@link engine.view.EditableElement EditableElement} that is currently having
  215. * selection inside.
  216. */
  217. focus() {
  218. if ( !this.isFocused ) {
  219. const editable = this.selection.getEditableElement();
  220. if ( editable ) {
  221. this.domConverter.focus( editable );
  222. this.render();
  223. } else {
  224. /**
  225. * Before focusing view document, selection should be placed inside one of the view's editables.
  226. * Normally its selection will be converted from model document (which have default selection), but
  227. * when using view document on its own, we need to manually place selection before focusing it.
  228. *
  229. * @error view-focus-no-selection
  230. */
  231. log.warn( 'view-focus-no-selection: There is no selection in any editable to focus.' );
  232. }
  233. }
  234. }
  235. /**
  236. * Disables all added observers.
  237. */
  238. disableObservers() {
  239. for ( let observer of this._observers.values() ) {
  240. observer.disable();
  241. }
  242. }
  243. /**
  244. * Enables all added observers.
  245. */
  246. enableObservers() {
  247. for ( let observer of this._observers.values() ) {
  248. observer.enable();
  249. }
  250. }
  251. }
  252. mix( Document, ObservableMixin );
  253. /**
  254. * Enum representing type of the change.
  255. *
  256. * Possible values:
  257. *
  258. * * `children` - for child list changes,
  259. * * `attributes` - for element attributes changes,
  260. * * `text` - for text nodes changes.
  261. *
  262. * @typedef {String} engine.view.ChangeType
  263. */
  264. /**
  265. * Fired when {@link engine.view.Document#render render} method is called. Actual rendering is executed as a listener to
  266. * this event with default priority. This way other listeners can be used to run code before or after rendering.
  267. *
  268. * @event engine.view.Document.render
  269. */