8
0

document.js 10 KB

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