瀏覽代碼

Add and update 'is()' method for view components.

Mateusz Samsel 6 年之前
父節點
當前提交
b4079ae19d

+ 3 - 2
packages/ckeditor5-engine/src/view/attributeelement.js

@@ -128,10 +128,11 @@ export default class AttributeElement extends Element {
 	 * @inheritDoc
 	 */
 	is( type, name = null ) {
+		const cutType = type && type.replace( 'view:', '' );
 		if ( !name ) {
-			return type == 'attributeElement' || super.is( type );
+			return cutType == 'attributeElement' || super.is( type );
 		} else {
-			return ( type == 'attributeElement' && name == this.name ) || super.is( type, name );
+			return ( cutType == 'attributeElement' && name == this.name ) || super.is( type, name );
 		}
 	}
 

+ 3 - 2
packages/ckeditor5-engine/src/view/containerelement.js

@@ -54,10 +54,11 @@ export default class ContainerElement extends Element {
 	 * @inheritDoc
 	 */
 	is( type, name = null ) {
+		const cutType = type && type.replace( 'view:', '' );
 		if ( !name ) {
-			return type == 'containerElement' || super.is( type );
+			return cutType == 'containerElement' || super.is( type );
 		} else {
-			return ( type == 'containerElement' && name == this.name ) || super.is( type, name );
+			return ( cutType == 'containerElement' && name == this.name ) || super.is( type, name );
 		}
 	}
 }

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

@@ -133,6 +133,16 @@ export default class Document {
 	}
 
 	/**
+	 * Checks whether given object is of `document` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'document' || type == 'view:document';
+	}
+
+	/**
 	 * Performs post-fixer loops. Executes post-fixer callbacks as long as none of them has done any changes to the model.
 	 *
 	 * @protected

+ 1 - 1
packages/ckeditor5-engine/src/view/documentfragment.js

@@ -102,7 +102,7 @@ export default class DocumentFragment {
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'documentFragment';
+		return type == 'documentFragment' || type == 'view:documentFragment';
 	}
 
 	/**

+ 8 - 1
packages/ckeditor5-engine/src/view/documentselection.js

@@ -281,15 +281,22 @@ export default class DocumentSelection {
 	 *		const selection = new DocumentSelection( ... );
 	 *
 	 *		selection.is( 'selection' ); // true
+	 *		selection.is( 'view:selection' ); // true
 	 *		selection.is( 'documentSelection' ); // true
+	 *		selection.is( 'view:documentSelection' ); // true
 	 *		selection.is( 'node' ); // false
+	 *		selection.is( 'view:node' ); // false
+	 *		selection.is( 'model:selection' ); // false
 	 *		selection.is( 'element' ); // false
 	 *
 	 * @param {String} type
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'selection' || type == 'documentSelection';
+		return type == 'selection' ||
+			type == 'documentSelection' ||
+			type == 'view:selection' ||
+			type == 'view:documentSelection';
 	}
 
 	/**

+ 10 - 0
packages/ckeditor5-engine/src/view/domconverter.js

@@ -106,6 +106,16 @@ export default class DomConverter {
 	}
 
 	/**
+	 * Checks whether given object is of `domConverter` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'domConverter' || type == 'view:domConverter';
+	}
+
+	/**
 	 * Binds given DOM element that represents fake selection to {@link module:engine/view/documentselection~DocumentSelection
 	 * document selection}. Document selection copy is stored and can be retrieved by
 	 * {@link module:engine/view/domconverter~DomConverter#fakeSelectionToView} method.

+ 10 - 0
packages/ckeditor5-engine/src/view/downcastwriter.js

@@ -55,6 +55,16 @@ export default class DowncastWriter {
 	}
 
 	/**
+	 * Checks whether given object is of `downcastWriter` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'downcastWriter' || type == 'view:downcastWriter';
+	}
+
+	/**
 	 * Sets {@link module:engine/view/documentselection~DocumentSelection selection's} ranges and direction to the
 	 * specified location based on the given {@link module:engine/view/selection~Selectable selectable}.
 	 *

+ 3 - 2
packages/ckeditor5-engine/src/view/editableelement.js

@@ -70,10 +70,11 @@ export default class EditableElement extends ContainerElement {
 	 * @inheritDoc
 	 */
 	is( type, name = null ) {
+		const cutType = type && type.replace( 'view:', '' );
 		if ( !name ) {
-			return type == 'editableElement' || super.is( type );
+			return cutType == 'editableElement' || super.is( type );
 		} else {
-			return ( type == 'editableElement' && name == this.name ) || super.is( type, name );
+			return ( cutType == 'editableElement' && name == this.name ) || super.is( type, name );
 		}
 	}
 

+ 3 - 2
packages/ckeditor5-engine/src/view/element.js

@@ -162,10 +162,11 @@ export default class Element extends Node {
 	 * @returns {Boolean}
 	 */
 	is( type, name = null ) {
+		const cutType = type.replace( 'view:', '' );
 		if ( !name ) {
-			return type == 'element' || type == this.name || super.is( type );
+			return cutType == 'element' || cutType == this.name || super.is( type );
 		} else {
-			return type == 'element' && name == this.name;
+			return cutType == 'element' && name == this.name;
 		}
 	}
 

+ 3 - 2
packages/ckeditor5-engine/src/view/emptyelement.js

@@ -47,10 +47,11 @@ export default class EmptyElement extends Element {
 	 * @inheritDoc
 	 */
 	is( type, name = null ) {
+		const cutType = type.replace( 'view:', '' );
 		if ( !name ) {
-			return type == 'emptyElement' || super.is( type );
+			return cutType == 'emptyElement' || super.is( type );
 		} else {
-			return ( type == 'emptyElement' && name == this.name ) || super.is( type, name );
+			return ( cutType == 'emptyElement' && name == this.name ) || super.is( type, name );
 		}
 	}
 

+ 10 - 0
packages/ckeditor5-engine/src/view/matcher.js

@@ -174,6 +174,16 @@ export default class Matcher {
 
 		return ( typeof pattern != 'function' && name && !( name instanceof RegExp ) ) ? name : null;
 	}
+
+	/**
+	 * Checks whether given object is of `matcher` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'matcher' || type == 'view:matcher';
+	}
 }
 
 // Returns match information if {@link module:engine/view/element~Element element} is matching provided pattern.

+ 1 - 1
packages/ckeditor5-engine/src/view/node.js

@@ -308,7 +308,7 @@ export default class Node {
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'node';
+		return type == 'node' || type == 'view:node';
 	}
 
 	/**

+ 10 - 0
packages/ckeditor5-engine/src/view/position.js

@@ -207,6 +207,16 @@ export default class Position {
 	}
 
 	/**
+	 * Checks whether given object is of `position` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'position' || type == 'view:position';
+	}
+
+	/**
 	 * Checks whether this position equals given position.
 	 *
 	 * @param {module:engine/view/position~Position} otherPosition Position to compare with.

+ 10 - 0
packages/ckeditor5-engine/src/view/range.js

@@ -395,6 +395,16 @@ export default class Range {
 	}
 
 	/**
+	 * Checks whether given object is of `range` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'range' || type == 'view:range';
+	}
+
+	/**
 	 * Checks and returns whether this range intersects with the given range.
 	 *
 	 * @param {module:engine/view/range~Range} otherRange Range to compare with.

+ 10 - 0
packages/ckeditor5-engine/src/view/renderer.js

@@ -119,6 +119,16 @@ export default class Renderer {
 	}
 
 	/**
+	 * Checks whether given object is of `renderer` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'renderer' || type == 'view:renderer';
+	}
+
+	/**
 	 * Marks a view node to be updated in the DOM by {@link #render `render()`}.
 	 *
 	 * Note that only view nodes whose parents have corresponding DOM elements need to be marked to be synchronized.

+ 3 - 2
packages/ckeditor5-engine/src/view/rooteditableelement.js

@@ -41,10 +41,11 @@ export default class RootEditableElement extends EditableElement {
 	 * @inheritDoc
 	 */
 	is( type, name = null ) {
+		const cutType = type.replace( 'view:', '' );
 		if ( !name ) {
-			return type == 'rootElement' || super.is( type );
+			return cutType == 'rootElement' || super.is( type );
 		} else {
-			return ( type == 'rootElement' && name == this.name ) || super.is( type, name );
+			return ( cutType == 'rootElement' && name == this.name ) || super.is( type, name );
 		}
 	}
 

+ 1 - 1
packages/ckeditor5-engine/src/view/selection.js

@@ -611,7 +611,7 @@ export default class Selection {
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'selection';
+		return type == 'selection' || type == 'view:selection';
 	}
 
 	/**

+ 1 - 1
packages/ckeditor5-engine/src/view/text.js

@@ -45,7 +45,7 @@ export default class Text extends Node {
 	 * @inheritDoc
 	 */
 	is( type ) {
-		return type == 'text' || super.is( type );
+		return type == 'text' || type == 'view:text' || super.is( type );
 	}
 
 	/**

+ 1 - 1
packages/ckeditor5-engine/src/view/textproxy.js

@@ -149,7 +149,7 @@ export default class TextProxy {
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'textProxy';
+		return type == 'textProxy' || type == 'view:textProxy';
 	}
 
 	/**

+ 10 - 0
packages/ckeditor5-engine/src/view/treewalker.js

@@ -145,6 +145,16 @@ export default class TreeWalker {
 	}
 
 	/**
+	 * Checks whether given object is of `treeWalker` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'treeWalker' || type == 'view:treeWalker';
+	}
+
+	/**
 	 * Moves {@link #position} in the {@link #direction} skipping values as long as the callback function returns `true`.
 	 *
 	 * For example:

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

@@ -60,10 +60,11 @@ export default class UIElement extends Element {
 	 * @inheritDoc
 	 */
 	is( type, name = null ) {
+		const cutType = type.replace( 'view:', '' );
 		if ( !name ) {
-			return type == 'uiElement' || super.is( type );
+			return cutType == 'uiElement' || super.is( type );
 		} else {
-			return ( type == 'uiElement' && name == this.name ) || super.is( type, name );
+			return ( cutType == 'uiElement' && name == this.name ) || super.is( type, name );
 		}
 	}
 

+ 10 - 0
packages/ckeditor5-engine/src/view/upcastwriter.js

@@ -36,6 +36,16 @@ import Selection from './selection';
  */
 export default class UpcastWriter {
 	/**
+	 * Checks whether given object is of `upcastWriter` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'upcastWriter' || type == 'view:upcastWriter';
+	}
+
+	/**
 	 * Creates a new {@link module:engine/view/documentfragment~DocumentFragment} instance.
 	 *
 	 * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]

+ 10 - 0
packages/ckeditor5-engine/src/view/view.js

@@ -653,6 +653,16 @@ export default class View {
 	}
 
 	/**
+	 * Checks whether given object is of `view` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'view';
+	}
+
+	/**
 	 * Disables or enables rendering. If the flag is set to `true` then the rendering will be disabled.
 	 * If the flag is set to `false` and if there was some change in the meantime, then the rendering action will be performed.
 	 *