Răsfoiți Sursa

More things moved from view document to view controller. Added event.

Szymon Kupś 8 ani în urmă
părinte
comite
d4973311fc

+ 0 - 12
packages/ckeditor5-engine/src/view/document.js

@@ -8,8 +8,6 @@
  */
 
 import Selection from './selection';
-import { injectQuirksHandling } from './filler';
-import { injectUiElementHandling } from './uielement';
 import Collection from '@ckeditor/ckeditor5-utils/src/collection';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
@@ -81,9 +79,6 @@ export default class Document {
 		 * @member {Boolean} module:engine/view/document~Document#isFocused
 		 */
 		this.set( 'isFocused', false );
-
-		injectQuirksHandling( this );
-		injectUiElementHandling( this );
 	}
 
 	/**
@@ -112,10 +107,3 @@ mix( Document, ObservableMixin );
  *
  * @typedef {String} module:engine/view/document~ChangeType
  */
-
-/**
- * Fired when {@link #render render} method is called. Actual rendering is executed as a listener to
- * this event with default priority. This way other listeners can be used to run code before or after rendering.
- *
- * @event render
- */

+ 3 - 3
packages/ckeditor5-engine/src/view/filler.js

@@ -146,10 +146,10 @@ export function isBlockFiller( domNode, blockFiller ) {
  * Assign key observer which move cursor from the end of the inline filler to the beginning of it when
  * the left arrow is pressed, so the filler does not break navigation.
  *
- * @param {module:engine/view/document~Document} document Document instance we should inject quirks handling on.
+ * @param {module:engine/view/view~View} view View controller instance we should inject quirks handling on.
  */
-export function injectQuirksHandling( document ) {
-	document.on( 'keydown', jumpOverInlineFiller );
+export function injectQuirksHandling( view ) {
+	view.document.on( 'keydown', jumpOverInlineFiller );
 }
 
 // Move cursor from the end of the inline filler to the beginning of it when, so the filler does not break navigation.

+ 3 - 3
packages/ckeditor5-engine/src/view/uielement.js

@@ -90,10 +90,10 @@ export default class UIElement extends Element {
  * The callback handles the situation when right arrow key is pressed and selection is collapsed before a UI element.
  * Without this handler, it would be impossible to "jump over" UI element using right arrow key.
  *
- * @param {module:engine/view/document~Document} document Document to which the quirks handling will be injected.
+ * @param {module:engine/view/view~View} view View controller to which the quirks handling will be injected.
  */
-export function injectUiElementHandling( document ) {
-	document.on( 'keydown', ( evt, data ) => jumpOverUiElement( evt, data, document.domConverter ) );
+export function injectUiElementHandling( view ) {
+	view.on( 'keydown', ( evt, data ) => jumpOverUiElement( evt, data, view.domConverter ) );
 }
 
 // Returns `null` because block filler is not needed for UIElements.

+ 28 - 13
packages/ckeditor5-engine/src/view/view.js

@@ -22,6 +22,8 @@ import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 import log from '@ckeditor/ckeditor5-utils/src/log';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import { scrollViewportToShowTarget } from '@ckeditor/ckeditor5-utils/src/dom/scroll';
+import { injectUiElementHandling } from './uielement';
+import { injectQuirksHandling } from './filler';
 
 export default class View {
 	constructor() {
@@ -49,7 +51,6 @@ export default class View {
 		 */
 		this._renderer = new Renderer( this.domConverter, this.document.selection );
 		this._renderer.bind( 'isFocused' ).to( this.document );
-		// this.decorate( 'render' );
 
 		/**
 		 * Roots of the DOM tree. Map on the `HTMLElement`s with roots names as keys.
@@ -74,6 +75,9 @@ export default class View {
 		this.addObserver( KeyObserver );
 		this.addObserver( FakeSelectionObserver );
 
+		injectQuirksHandling( this );
+		injectUiElementHandling( this );
+
 		this._ongoingChange = false;
 		this._renderingInProgress = false;
 	}
@@ -228,10 +232,11 @@ export default class View {
 			 */
 			log.warn(
 				'applying-view-changes-on-rendering: ' +
-				'Attempting to make changes in the view during rendering process. ' +
-				'Your changes will not be rendered in DOM.'
+				'Attempting to make changes in the view during rendering process.' +
+				'This may cause some unexpected behaviour and inconsistency between the DOM and the view.'
 			);
 		}
+
 		// If other changes are in progress wait with rendering until every ongoing change is over.
 		if ( this._ongoingChange ) {
 			callback( this._writer );
@@ -242,15 +247,34 @@ export default class View {
 			this._render();
 
 			this._ongoingChange = false;
+
+			// TODO: docs for the event.
+			this.fire( 'change' );
+		}
+	}
+
+	render() {
+		// Render only if no ongoing changes in progress. If there are some, view document will be rendered after all
+		// changes are done. This way view document will not be rendered in the middle of some changes.
+		if ( !this._ongoingChange ) {
+			this._render();
 		}
 	}
 
+	destroy() {
+		for ( const observer of this._observers.values() ) {
+			observer.destroy();
+		}
+
+		this.document.destroy();
+		this.stopListening();
+	}
+
 	/**
 	 * Renders all changes. In order to avoid triggering the observers (e.g. mutations) all observers are disabled
 	 * before rendering and re-enabled after that.
 	 *
 	 * @private
-	 * @fires render
 	 */
 	_render() {
 		this._renderingInProgress = true;
@@ -261,15 +285,6 @@ export default class View {
 
 		this._renderingInProgress = false;
 	}
-
-	destroy() {
-		for ( const observer of this._observers.values() ) {
-			observer.destroy();
-		}
-
-		this.document.destroy();
-		this.stopListening();
-	}
 }
 
 mix( View, ObservableMixin );