document.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 '@ckeditor/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 '@ckeditor/ckeditor5-utils/src/mix';
  20. import ObservableMixin from '@ckeditor/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}
  62. * and {@link module:engine/view/observer/observer~Observer observers}.
  63. *
  64. * @readonly
  65. * @member {module:engine/view/domconverter~DomConverter} module:engine/view/document~Document#domConverter
  66. */
  67. this.domConverter = new DomConverter();
  68. /**
  69. * Roots of the view tree. Map of the {module:engine/view/element~Element view elements} with roots names as keys.
  70. *
  71. * @readonly
  72. * @member {Map} module:engine/view/document~Document#roots
  73. */
  74. this.roots = new Map();
  75. /**
  76. * True if document is focused.
  77. *
  78. * This property is updated by the {@link module:engine/view/observer/focusobserver~FocusObserver}.
  79. * If the {@link module:engine/view/observer/focusobserver~FocusObserver} is disabled this property will not change.
  80. *
  81. * @readonly
  82. * @observable
  83. * @member {Boolean} module:engine/view/document~Document#isFocused
  84. */
  85. this.set( 'isFocused', false );
  86. /**
  87. * Instance of the {@link module:engine/view/document~Document#renderer renderer}.
  88. *
  89. * @readonly
  90. * @member {module:engine/view/renderer~Renderer} module:engine/view/document~Document#renderer
  91. */
  92. this.renderer = new Renderer( this.domConverter, this.selection );
  93. this.renderer.bind( 'isFocused' ).to( this, 'isFocused' );
  94. /**
  95. * Map of registered {@link module:engine/view/observer/observer~Observer observers}.
  96. *
  97. * @private
  98. * @member {Map.<Function, module:engine/view/observer/observer~Observer>} module:engine/view/document~Document#_observers
  99. */
  100. this._observers = new Map();
  101. // Add default observers.
  102. this.addObserver( MutationObserver );
  103. this.addObserver( SelectionObserver );
  104. this.addObserver( FocusObserver );
  105. this.addObserver( KeyObserver );
  106. this.addObserver( FakeSelectionObserver );
  107. injectQuirksHandling( this );
  108. this.decorate( 'render' );
  109. }
  110. /**
  111. * Creates observer of the given type if not yet created, {@link module:engine/view/observer/observer~Observer#enable enables} it
  112. * and {@link module:engine/view/observer/observer~Observer#observe attaches} to all existing and future
  113. * {@link module:engine/view/document~Document#domRoots DOM roots}.
  114. *
  115. * Note: Observers are recognized by their constructor (classes). A single observer will be instantiated and used only
  116. * when registered for the first time. This means that features and other components can register a single observer
  117. * multiple times without caring whether it has been already added or not.
  118. *
  119. * @param {Function} Observer The constructor of an observer to add.
  120. * Should create an instance inheriting from {@link module:engine/view/observer/observer~Observer}.
  121. * @returns {module:engine/view/observer/observer~Observer} Added observer instance.
  122. */
  123. addObserver( Observer ) {
  124. let observer = this._observers.get( Observer );
  125. if ( observer ) {
  126. return observer;
  127. }
  128. observer = new Observer( this );
  129. this._observers.set( Observer, observer );
  130. for ( const [ name, domElement ] of this.domRoots ) {
  131. observer.observe( domElement, name );
  132. }
  133. observer.enable();
  134. return observer;
  135. }
  136. /**
  137. * Returns observer of the given type or `undefined` if such observer has not been added yet.
  138. *
  139. * @param {Function} Observer The constructor of an observer to get.
  140. * @returns {module:engine/view/observer/observer~Observer|undefined} Observer instance or undefined.
  141. */
  142. getObserver( Observer ) {
  143. return this._observers.get( Observer );
  144. }
  145. /**
  146. * Creates a {@link module:engine/view/document~Document#roots view root element}.
  147. *
  148. * If the DOM element is passed as a first parameter it will be automatically
  149. * {@link module:engine/view/document~Document#attachDomRoot attached}:
  150. *
  151. * document.createRoot( document.querySelector( 'div#editor' ) ); // Will call document.attachDomRoot.
  152. *
  153. * However, if the string is passed, then only the view element will be created and the DOM element have to be
  154. * attached separately:
  155. *
  156. * document.createRoot( 'body' );
  157. * document.attachDomRoot( document.querySelector( 'body#editor' ) );
  158. *
  159. * In both cases, {@link module:engine/view/rooteditableelement~RootEditableElement#rootName element name} is always
  160. * transformed to lower
  161. * case.
  162. *
  163. * @param {Element|String} domRoot DOM root element or the tag name of view root element if the DOM element will be
  164. * attached later.
  165. * @param {String} [name='main'] Name of the root.
  166. * @returns {module:engine/view/rooteditableelement~RootEditableElement} The created view root element.
  167. */
  168. createRoot( domRoot, name = 'main' ) {
  169. const rootTag = typeof domRoot == 'string' ? domRoot : domRoot.tagName;
  170. const viewRoot = new RootEditableElement( rootTag.toLowerCase(), name );
  171. viewRoot.document = this;
  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 module:engine/view/renderer~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 module:engine/view/document~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 ( const observer of this._observers.values() ) {
  200. observer.observe( domRoot, name );
  201. }
  202. }
  203. /**
  204. * Gets a {@link module:engine/view/document~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 {module:engine/view/rooteditableelement~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 render
  227. */
  228. render() {
  229. this.disableObservers();
  230. this.renderer.render();
  231. this.enableObservers();
  232. }
  233. /**
  234. * Focuses document. It will focus {@link module:engine/view/editableelement~EditableElement EditableElement} that is currently having
  235. * selection inside.
  236. */
  237. focus() {
  238. if ( !this.isFocused ) {
  239. const editable = this.selection.editableElement;
  240. if ( editable ) {
  241. this.domConverter.focus( editable );
  242. this.render();
  243. } else {
  244. /**
  245. * Before focusing view document, selection should be placed inside one of the view's editables.
  246. * Normally its selection will be converted from model document (which have default selection), but
  247. * when using view document on its own, we need to manually place selection before focusing it.
  248. *
  249. * @error view-focus-no-selection
  250. */
  251. log.warn( 'view-focus-no-selection: There is no selection in any editable to focus.' );
  252. }
  253. }
  254. }
  255. /**
  256. * Disables all added observers.
  257. */
  258. disableObservers() {
  259. for ( const observer of this._observers.values() ) {
  260. observer.disable();
  261. }
  262. }
  263. /**
  264. * Enables all added observers.
  265. */
  266. enableObservers() {
  267. for ( const observer of this._observers.values() ) {
  268. observer.enable();
  269. }
  270. }
  271. /**
  272. * Destroys all observers created by view `Document`.
  273. */
  274. destroy() {
  275. for ( const observer of this._observers.values() ) {
  276. observer.destroy();
  277. }
  278. }
  279. }
  280. mix( Document, ObservableMixin );
  281. /**
  282. * Enum representing type of the change.
  283. *
  284. * Possible values:
  285. *
  286. * * `children` - for child list changes,
  287. * * `attributes` - for element attributes changes,
  288. * * `text` - for text nodes changes.
  289. *
  290. * @typedef {String} module:engine/view/document~ChangeType
  291. */
  292. /**
  293. * Fired when {@link #render render} method is called. Actual rendering is executed as a listener to
  294. * this event with default priority. This way other listeners can be used to run code before or after rendering.
  295. *
  296. * @event render
  297. */