view.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/view/view
  7. */
  8. import Document from './document';
  9. import DowncastWriter from './downcastwriter';
  10. import Renderer from './renderer';
  11. import DomConverter from './domconverter';
  12. import Position from './position';
  13. import Range from './range';
  14. import Selection from './selection';
  15. import MutationObserver from './observer/mutationobserver';
  16. import KeyObserver from './observer/keyobserver';
  17. import FakeSelectionObserver from './observer/fakeselectionobserver';
  18. import SelectionObserver from './observer/selectionobserver';
  19. import FocusObserver from './observer/focusobserver';
  20. import CompositionObserver from './observer/compositionobserver';
  21. import InputObserver from './observer/inputobserver';
  22. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  23. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  24. import { scrollViewportToShowTarget } from '@ckeditor/ckeditor5-utils/src/dom/scroll';
  25. import { injectUiElementHandling } from './uielement';
  26. import { injectQuirksHandling } from './filler';
  27. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  28. import env from '@ckeditor/ckeditor5-utils/src/env';
  29. /**
  30. * Editor's view controller class. Its main responsibility is DOM - View management for editing purposes, to provide
  31. * abstraction over the DOM structure and events and hide all browsers quirks.
  32. *
  33. * View controller renders view document to DOM whenever view structure changes. To determine when view can be rendered,
  34. * all changes need to be done using the {@link module:engine/view/view~View#change} method, using
  35. * {@link module:engine/view/downcastwriter~DowncastWriter}:
  36. *
  37. * view.change( writer => {
  38. * writer.insert( position, writer.createText( 'foo' ) );
  39. * } );
  40. *
  41. * View controller also register {@link module:engine/view/observer/observer~Observer observers} which observes changes
  42. * on DOM and fire events on the {@link module:engine/view/document~Document Document}.
  43. * Note that the following observers are added by the class constructor and are always available:
  44. *
  45. * * {@link module:engine/view/observer/mutationobserver~MutationObserver},
  46. * * {@link module:engine/view/observer/selectionobserver~SelectionObserver},
  47. * * {@link module:engine/view/observer/focusobserver~FocusObserver},
  48. * * {@link module:engine/view/observer/keyobserver~KeyObserver},
  49. * * {@link module:engine/view/observer/fakeselectionobserver~FakeSelectionObserver}.
  50. * * {@link module:engine/view/observer/compositionobserver~CompositionObserver}.
  51. *
  52. * This class also {@link module:engine/view/view~View#attachDomRoot binds the DOM and the view elements}.
  53. *
  54. * If you do not need full a DOM - view management, and only want to transform a tree of view elements to a tree of DOM
  55. * elements you do not need this controller. You can use the {@link module:engine/view/domconverter~DomConverter DomConverter} instead.
  56. *
  57. * @mixes module:utils/observablemixin~ObservableMixin
  58. */
  59. export default class View {
  60. constructor() {
  61. /**
  62. * Instance of the {@link module:engine/view/document~Document} associated with this view controller.
  63. *
  64. * @readonly
  65. * @type {module:engine/view/document~Document}
  66. */
  67. this.document = new Document();
  68. /**
  69. * Instance of the {@link module:engine/view/domconverter~DomConverter domConverter} used by
  70. * {@link module:engine/view/view~View#_renderer renderer}
  71. * and {@link module:engine/view/observer/observer~Observer observers}.
  72. *
  73. * @readonly
  74. * @type {module:engine/view/domconverter~DomConverter}
  75. */
  76. this.domConverter = new DomConverter();
  77. /**
  78. * Roots of the DOM tree. Map on the `HTMLElement`s with roots names as keys.
  79. *
  80. * @readonly
  81. * @type {Map.<String, HTMLElement>}
  82. */
  83. this.domRoots = new Map();
  84. /**
  85. * Used to prevent calling {@link #forceRender} and {@link #change} during rendering view to the DOM.
  86. *
  87. * @readonly
  88. * @member {Boolean} #isRenderingInProgress
  89. */
  90. this.set( 'isRenderingInProgress', false );
  91. /**
  92. * Instance of the {@link module:engine/view/renderer~Renderer renderer}.
  93. *
  94. * @protected
  95. * @type {module:engine/view/renderer~Renderer}
  96. */
  97. this._renderer = new Renderer( this.domConverter, this.document.selection );
  98. this._renderer.bind( 'isFocused' ).to( this.document );
  99. /**
  100. * A DOM root attributes cache. It saves the initial values of DOM root attributes before the DOM element
  101. * is {@link module:engine/view/view~View#attachDomRoot attached} to the view so later on, when
  102. * the view is destroyed ({@link module:engine/view/view~View#detachDomRoot}), they can be easily restored.
  103. * This way, the DOM element can go back to the (clean) state as if the editing view never used it.
  104. *
  105. * @private
  106. * @member {WeakMap.<HTMLElement,Object>}
  107. */
  108. this._initialDomRootAttributes = new WeakMap();
  109. /**
  110. * Map of registered {@link module:engine/view/observer/observer~Observer observers}.
  111. *
  112. * @private
  113. * @type {Map.<Function, module:engine/view/observer/observer~Observer>}
  114. */
  115. this._observers = new Map();
  116. /**
  117. * Is set to `true` when {@link #change view changes} are currently in progress.
  118. *
  119. * @private
  120. * @type {Boolean}
  121. */
  122. this._ongoingChange = false;
  123. /**
  124. * Used to prevent calling {@link #forceRender} and {@link #change} during rendering view to the DOM.
  125. *
  126. * @private
  127. * @type {Boolean}
  128. */
  129. this._postFixersInProgress = false;
  130. /**
  131. * Internal flag to temporary disable rendering. See the usage in the {@link #_disableRendering}.
  132. *
  133. * @private
  134. * @type {Boolean}
  135. */
  136. this._renderingDisabled = false;
  137. /**
  138. * Internal flag that disables rendering when there are no changes since the last rendering.
  139. * It stores information about changed selection and changed elements from attached document roots.
  140. *
  141. * @private
  142. * @type {Boolean}
  143. */
  144. this._hasChangedSinceTheLastRendering = false;
  145. /**
  146. * DowncastWriter instance used in {@link #change change method} callbacks.
  147. *
  148. * @private
  149. * @type {module:engine/view/downcastwriter~DowncastWriter}
  150. */
  151. this._writer = new DowncastWriter( this.document );
  152. // Add default observers.
  153. this.addObserver( MutationObserver );
  154. this.addObserver( SelectionObserver );
  155. this.addObserver( FocusObserver );
  156. this.addObserver( KeyObserver );
  157. this.addObserver( FakeSelectionObserver );
  158. this.addObserver( CompositionObserver );
  159. if ( env.isAndroid ) {
  160. this.addObserver( InputObserver );
  161. }
  162. // Inject quirks handlers.
  163. injectQuirksHandling( this );
  164. injectUiElementHandling( this );
  165. // Use 'normal' priority so that rendering is performed as first when using that priority.
  166. this.on( 'render', () => {
  167. this._render();
  168. // Informs that layout has changed after render.
  169. this.document.fire( 'layoutChanged' );
  170. // Reset the `_hasChangedSinceTheLastRendering` flag after rendering.
  171. this._hasChangedSinceTheLastRendering = false;
  172. } );
  173. // Listen to the document selection changes directly.
  174. this.listenTo( this.document.selection, 'change', () => {
  175. this._hasChangedSinceTheLastRendering = true;
  176. } );
  177. }
  178. /**
  179. * Attaches a DOM root element to the view element and enable all observers on that element.
  180. * Also {@link module:engine/view/renderer~Renderer#markToSync mark element} to be synchronized
  181. * with the view what means that all child nodes will be removed and replaced with content of the view root.
  182. *
  183. * This method also will change view element name as the same as tag name of given dom root.
  184. * Name is always transformed to lower case.
  185. *
  186. * **Note:** Use {@link #detachDomRoot `detachDomRoot()`} to revert this action.
  187. *
  188. * @param {Element} domRoot DOM root element.
  189. * @param {String} [name='main'] Name of the root.
  190. */
  191. attachDomRoot( domRoot, name = 'main' ) {
  192. const viewRoot = this.document.getRoot( name );
  193. // Set view root name the same as DOM root tag name.
  194. viewRoot._name = domRoot.tagName.toLowerCase();
  195. const initialDomRootAttributes = {};
  196. // 1. Copy and cache the attributes to remember the state of the element before attaching.
  197. // The cached attributes will be restored in detachDomRoot() so the element goes to the
  198. // clean state as if the editing view never used it.
  199. // 2. Apply the attributes using the view writer, so they all go under the control of the engine.
  200. // The editing view takes over the attribute management completely because various
  201. // features (e.g. addPlaceholder()) require dynamic changes of those attributes and they
  202. // cannot be managed by the engine and the UI library at the same time.
  203. for ( const { name, value } of Array.from( domRoot.attributes ) ) {
  204. initialDomRootAttributes[ name ] = value;
  205. // Do not use writer.setAttribute() for the class attribute. The EditableUIView class
  206. // and its descendants could have already set some using the writer.addClass() on the view
  207. // document root. They haven't been rendered yet so they are not present in the DOM root.
  208. // Using writer.setAttribute( 'class', ... ) would override them completely.
  209. if ( name === 'class' ) {
  210. this._writer.addClass( value.split( ' ' ), viewRoot );
  211. } else {
  212. this._writer.setAttribute( name, value, viewRoot );
  213. }
  214. }
  215. this._initialDomRootAttributes.set( domRoot, initialDomRootAttributes );
  216. const updateContenteditableAttribute = () => {
  217. this._writer.setAttribute( 'contenteditable', !viewRoot.isReadOnly, viewRoot );
  218. if ( viewRoot.isReadOnly ) {
  219. this._writer.addClass( 'ck-read-only', viewRoot );
  220. } else {
  221. this._writer.removeClass( 'ck-read-only', viewRoot );
  222. }
  223. };
  224. // Set initial value.
  225. updateContenteditableAttribute();
  226. this.domRoots.set( name, domRoot );
  227. this.domConverter.bindElements( domRoot, viewRoot );
  228. this._renderer.markToSync( 'children', viewRoot );
  229. this._renderer.markToSync( 'attributes', viewRoot );
  230. this._renderer.domDocuments.add( domRoot.ownerDocument );
  231. viewRoot.on( 'change:children', ( evt, node ) => this._renderer.markToSync( 'children', node ) );
  232. viewRoot.on( 'change:attributes', ( evt, node ) => this._renderer.markToSync( 'attributes', node ) );
  233. viewRoot.on( 'change:text', ( evt, node ) => this._renderer.markToSync( 'text', node ) );
  234. viewRoot.on( 'change:isReadOnly', () => this.change( updateContenteditableAttribute ) );
  235. viewRoot.on( 'change', () => {
  236. this._hasChangedSinceTheLastRendering = true;
  237. } );
  238. for ( const observer of this._observers.values() ) {
  239. observer.observe( domRoot, name );
  240. }
  241. }
  242. /**
  243. * Detaches a DOM root element from the view element and restores its attributes to the state before
  244. * {@link #attachDomRoot `attachDomRoot()`}.
  245. *
  246. * @param {String} name Name of the root to detach.
  247. */
  248. detachDomRoot( name ) {
  249. const domRoot = this.domRoots.get( name );
  250. // Remove all root attributes so the DOM element is "bare".
  251. Array.from( domRoot.attributes ).forEach( ( { name } ) => domRoot.removeAttribute( name ) );
  252. const initialDomRootAttributes = this._initialDomRootAttributes.get( domRoot );
  253. // Revert all view root attributes back to the state before attachDomRoot was called.
  254. for ( const attribute in initialDomRootAttributes ) {
  255. domRoot.setAttribute( attribute, initialDomRootAttributes[ attribute ] );
  256. }
  257. this.domRoots.delete( name );
  258. this.domConverter.unbindDomElement( domRoot );
  259. }
  260. /**
  261. * Gets DOM root element.
  262. *
  263. * @param {String} [name='main'] Name of the root.
  264. * @returns {Element} DOM root element instance.
  265. */
  266. getDomRoot( name = 'main' ) {
  267. return this.domRoots.get( name );
  268. }
  269. /**
  270. * Creates observer of the given type if not yet created, {@link module:engine/view/observer/observer~Observer#enable enables} it
  271. * and {@link module:engine/view/observer/observer~Observer#observe attaches} to all existing and future
  272. * {@link #domRoots DOM roots}.
  273. *
  274. * Note: Observers are recognized by their constructor (classes). A single observer will be instantiated and used only
  275. * when registered for the first time. This means that features and other components can register a single observer
  276. * multiple times without caring whether it has been already added or not.
  277. *
  278. * @param {Function} Observer The constructor of an observer to add.
  279. * Should create an instance inheriting from {@link module:engine/view/observer/observer~Observer}.
  280. * @returns {module:engine/view/observer/observer~Observer} Added observer instance.
  281. */
  282. addObserver( Observer ) {
  283. let observer = this._observers.get( Observer );
  284. if ( observer ) {
  285. return observer;
  286. }
  287. observer = new Observer( this );
  288. this._observers.set( Observer, observer );
  289. for ( const [ name, domElement ] of this.domRoots ) {
  290. observer.observe( domElement, name );
  291. }
  292. observer.enable();
  293. return observer;
  294. }
  295. /**
  296. * Returns observer of the given type or `undefined` if such observer has not been added yet.
  297. *
  298. * @param {Function} Observer The constructor of an observer to get.
  299. * @returns {module:engine/view/observer/observer~Observer|undefined} Observer instance or undefined.
  300. */
  301. getObserver( Observer ) {
  302. return this._observers.get( Observer );
  303. }
  304. /**
  305. * Disables all added observers.
  306. */
  307. disableObservers() {
  308. for ( const observer of this._observers.values() ) {
  309. observer.disable();
  310. }
  311. }
  312. /**
  313. * Enables all added observers.
  314. */
  315. enableObservers() {
  316. for ( const observer of this._observers.values() ) {
  317. observer.enable();
  318. }
  319. }
  320. /**
  321. * Scrolls the page viewport and {@link #domRoots} with their ancestors to reveal the
  322. * caret, if not already visible to the user.
  323. */
  324. scrollToTheSelection() {
  325. const range = this.document.selection.getFirstRange();
  326. if ( range ) {
  327. scrollViewportToShowTarget( {
  328. target: this.domConverter.viewRangeToDom( range ),
  329. viewportOffset: 20
  330. } );
  331. }
  332. }
  333. /**
  334. * It will focus DOM element representing {@link module:engine/view/editableelement~EditableElement EditableElement}
  335. * that is currently having selection inside.
  336. */
  337. focus() {
  338. if ( !this.document.isFocused ) {
  339. const editable = this.document.selection.editableElement;
  340. if ( editable ) {
  341. this.domConverter.focus( editable );
  342. this.forceRender();
  343. } else {
  344. // Before focusing view document, selection should be placed inside one of the view's editables.
  345. // Normally its selection will be converted from model document (which have default selection), but
  346. // when using view document on its own, we need to manually place selection before focusing it.
  347. //
  348. // @if CK_DEBUG // console.warn( 'There is no selection in any editable to focus.' );
  349. }
  350. }
  351. }
  352. /**
  353. * The `change()` method is the primary way of changing the view. You should use it to modify any node in the view tree.
  354. * It makes sure that after all changes are made the view is rendered to the DOM (assuming that the view will be changed
  355. * inside the callback). It prevents situations when the DOM is updated when the view state is not yet correct. It allows
  356. * to nest calls one inside another and still performs a single rendering after all those changes are made.
  357. * It also returns the return value of its callback.
  358. *
  359. * const text = view.change( writer => {
  360. * const newText = writer.createText( 'foo' );
  361. * writer.insert( position1, newText );
  362. *
  363. * view.change( writer => {
  364. * writer.insert( position2, writer.createText( 'bar' ) );
  365. * } );
  366. *
  367. * writer.remove( range );
  368. *
  369. * return newText;
  370. * } );
  371. *
  372. * When the outermost change block is done and rendering to the DOM is over the
  373. * {@link module:engine/view/view~View#event:render `View#render`} event is fired.
  374. *
  375. * This method throws a `applying-view-changes-on-rendering` error when
  376. * the change block is used after rendering to the DOM has started.
  377. *
  378. * @param {Function} callback Callback function which may modify the view.
  379. * @returns {*} Value returned by the callback.
  380. */
  381. change( callback ) {
  382. if ( this.isRenderingInProgress || this._postFixersInProgress ) {
  383. /**
  384. * Thrown when there is an attempt to make changes to the view tree when it is in incorrect state. This may
  385. * cause some unexpected behaviour and inconsistency between the DOM and the view.
  386. * This may be caused by:
  387. *
  388. * * calling {@link #change} or {@link #forceRender} during rendering process,
  389. * * calling {@link #change} or {@link #forceRender} inside of
  390. * {@link module:engine/view/document~Document#registerPostFixer post-fixer function}.
  391. *
  392. * @error cannot-change-view-tree
  393. */
  394. throw new CKEditorError(
  395. 'cannot-change-view-tree: ' +
  396. 'Attempting to make changes to the view when it is in an incorrect state: rendering or post-fixers are in progress. ' +
  397. 'This may cause some unexpected behavior and inconsistency between the DOM and the view.',
  398. this
  399. );
  400. }
  401. try {
  402. // Recursive call to view.change() method - execute listener immediately.
  403. if ( this._ongoingChange ) {
  404. return callback( this._writer );
  405. }
  406. // This lock will assure that all recursive calls to view.change() will end up in same block - one "render"
  407. // event for all nested calls.
  408. this._ongoingChange = true;
  409. const callbackResult = callback( this._writer );
  410. this._ongoingChange = false;
  411. // This lock is used by editing controller to render changes from outer most model.change() once. As plugins might call
  412. // view.change() inside model.change() block - this will ensures that postfixers and rendering are called once after all
  413. // changes. Also, we don't need to render anything if there're no changes since last rendering.
  414. if ( !this._renderingDisabled && this._hasChangedSinceTheLastRendering ) {
  415. this._postFixersInProgress = true;
  416. this.document._callPostFixers( this._writer );
  417. this._postFixersInProgress = false;
  418. this.fire( 'render' );
  419. }
  420. return callbackResult;
  421. } catch ( err ) {
  422. // @if CK_DEBUG // throw err;
  423. CKEditorError.rethrowUnexpectedError( err, this );
  424. }
  425. }
  426. /**
  427. * Forces rendering {@link module:engine/view/document~Document view document} to DOM. If any view changes are
  428. * currently in progress, rendering will start after all {@link #change change blocks} are processed.
  429. *
  430. * Note that this method is dedicated for special cases. All view changes should be wrapped in the {@link #change}
  431. * block and the view will automatically check whether it needs to render DOM or not.
  432. *
  433. * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `applying-view-changes-on-rendering` when
  434. * trying to re-render when rendering to DOM has already started.
  435. */
  436. forceRender() {
  437. this._hasChangedSinceTheLastRendering = true;
  438. this.change( () => {} );
  439. }
  440. /**
  441. * Destroys this instance. Makes sure that all observers are destroyed and listeners removed.
  442. */
  443. destroy() {
  444. for ( const observer of this._observers.values() ) {
  445. observer.destroy();
  446. }
  447. this.document.destroy();
  448. this.stopListening();
  449. }
  450. /**
  451. * Creates position at the given location. The location can be specified as:
  452. *
  453. * * a {@link module:engine/view/position~Position position},
  454. * * parent element and offset (offset defaults to `0`),
  455. * * parent element and `'end'` (sets position at the end of that element),
  456. * * {@link module:engine/view/item~Item view item} and `'before'` or `'after'` (sets position before or after given view item).
  457. *
  458. * This method is a shortcut to other constructors such as:
  459. *
  460. * * {@link #createPositionBefore},
  461. * * {@link #createPositionAfter},
  462. *
  463. * @param {module:engine/view/item~Item|module:engine/model/position~Position} itemOrPosition
  464. * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
  465. * first parameter is a {@link module:engine/view/item~Item view item}.
  466. */
  467. createPositionAt( itemOrPosition, offset ) {
  468. return Position._createAt( itemOrPosition, offset );
  469. }
  470. /**
  471. * Creates a new position after given view item.
  472. *
  473. * @param {module:engine/view/item~Item} item View item after which the position should be located.
  474. * @returns {module:engine/view/position~Position}
  475. */
  476. createPositionAfter( item ) {
  477. return Position._createAfter( item );
  478. }
  479. /**
  480. * Creates a new position before given view item.
  481. *
  482. * @param {module:engine/view/item~Item} item View item before which the position should be located.
  483. * @returns {module:engine/view/position~Position}
  484. */
  485. createPositionBefore( item ) {
  486. return Position._createBefore( item );
  487. }
  488. /**
  489. * Creates a range spanning from `start` position to `end` position.
  490. *
  491. * **Note:** This factory method creates it's own {@link module:engine/view/position~Position} instances basing on passed values.
  492. *
  493. * @param {module:engine/view/position~Position} start Start position.
  494. * @param {module:engine/view/position~Position} [end] End position. If not set, range will be collapsed at `start` position.
  495. * @returns {module:engine/view/range~Range}
  496. */
  497. createRange( start, end ) {
  498. return new Range( start, end );
  499. }
  500. /**
  501. * Creates a range that starts before given {@link module:engine/view/item~Item view item} and ends after it.
  502. *
  503. * @param {module:engine/view/item~Item} item
  504. * @returns {module:engine/view/range~Range}
  505. */
  506. createRangeOn( item ) {
  507. return Range._createOn( item );
  508. }
  509. /**
  510. * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
  511. * that element and ends after the last child of that element.
  512. *
  513. * @param {module:engine/view/element~Element} element Element which is a parent for the range.
  514. * @returns {module:engine/view/range~Range}
  515. */
  516. createRangeIn( element ) {
  517. return Range._createIn( element );
  518. }
  519. /**
  520. Creates new {@link module:engine/view/selection~Selection} instance.
  521. *
  522. * // Creates empty selection without ranges.
  523. * const selection = view.createSelection();
  524. *
  525. * // Creates selection at the given range.
  526. * const range = view.createRange( start, end );
  527. * const selection = view.createSelection( range );
  528. *
  529. * // Creates selection at the given ranges
  530. * const ranges = [ view.createRange( start1, end2 ), view.createRange( star2, end2 ) ];
  531. * const selection = view.createSelection( ranges );
  532. *
  533. * // Creates selection from the other selection.
  534. * const otherSelection = view.createSelection();
  535. * const selection = view.createSelection( otherSelection );
  536. *
  537. * // Creates selection from the document selection.
  538. * const selection = view.createSelection( editor.editing.view.document.selection );
  539. *
  540. * // Creates selection at the given position.
  541. * const position = view.createPositionFromPath( root, path );
  542. * const selection = view.createSelection( position );
  543. *
  544. * // Creates collapsed selection at the position of given item and offset.
  545. * const paragraph = view.createContainerElement( 'paragraph' );
  546. * const selection = view.createSelection( paragraph, offset );
  547. *
  548. * // Creates a range inside an {@link module:engine/view/element~Element element} which starts before the
  549. * // first child of that element and ends after the last child of that element.
  550. * const selection = view.createSelection( paragraph, 'in' );
  551. *
  552. * // Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends
  553. * // just after the item.
  554. * const selection = view.createSelection( paragraph, 'on' );
  555. *
  556. * `Selection`'s factory method allow passing additional options (`backward`, `fake` and `label`) as the last argument.
  557. *
  558. * // Creates backward selection.
  559. * const selection = view.createSelection( range, { backward: true } );
  560. *
  561. * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
  562. * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
  563. * represented in other way, for example by applying proper CSS class.
  564. *
  565. * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
  566. * (and be properly handled by screen readers).
  567. *
  568. * // Creates fake selection with label.
  569. * const selection = view.createSelection( range, { fake: true, label: 'foo' } );
  570. *
  571. * @param {module:engine/view/selection~Selectable} [selectable=null]
  572. * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
  573. * @param {Object} [options]
  574. * @param {Boolean} [options.backward] Sets this selection instance to be backward.
  575. * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
  576. * @param {String} [options.label] Label for the fake selection.
  577. * @returns {module:engine/view/selection~Selection}
  578. */
  579. createSelection( selectable, placeOrOffset, options ) {
  580. return new Selection( selectable, placeOrOffset, options );
  581. }
  582. /**
  583. * Disables or enables rendering. If the flag is set to `true` then the rendering will be disabled.
  584. * If the flag is set to `false` and if there was some change in the meantime, then the rendering action will be performed.
  585. *
  586. * @protected
  587. * @param {Boolean} flag A flag indicates whether the rendering should be disabled.
  588. */
  589. _disableRendering( flag ) {
  590. this._renderingDisabled = flag;
  591. if ( flag == false ) {
  592. // Render when you stop blocking rendering.
  593. this.change( () => {} );
  594. }
  595. }
  596. /**
  597. * Renders all changes. In order to avoid triggering the observers (e.g. mutations) all observers are disabled
  598. * before rendering and re-enabled after that.
  599. *
  600. * @private
  601. */
  602. _render() {
  603. this.isRenderingInProgress = true;
  604. this.disableObservers();
  605. this._renderer.render();
  606. this.enableObservers();
  607. this.isRenderingInProgress = false;
  608. }
  609. /**
  610. * Fired after a topmost {@link module:engine/view/view~View#change change block} and all
  611. * {@link module:engine/view/document~Document#registerPostFixer post-fixers} are executed.
  612. *
  613. * Actual rendering is performed as a first listener on 'normal' priority.
  614. *
  615. * view.on( 'render', () => {
  616. * // Rendering to the DOM is complete.
  617. * } );
  618. *
  619. * This event is useful when you want to update interface elements after the rendering, e.g. position of the
  620. * balloon panel. If you wants to change view structure use
  621. * {@link module:engine/view/document~Document#registerPostFixer post-fixers}.
  622. *
  623. * @event module:engine/view/view~View#event:render
  624. */
  625. }
  626. mix( View, ObservableMixin );