document.js 11 KB

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