Ver código fonte

Moved additional construtor params to custom properties in EditableElement and RootEditableElement.

Szymon Kupś 9 anos atrás
pai
commit
5d20df1b21

+ 2 - 1
packages/ckeditor5-engine/src/view/document.js

@@ -193,7 +193,8 @@ export default class Document {
 	createRoot( domRoot, name = 'main' ) {
 		const rootTag = typeof domRoot == 'string' ? domRoot : domRoot.tagName;
 
-		const viewRoot = new RootEditableElement( this, rootTag.toLowerCase(), name );
+		const viewRoot = new RootEditableElement( rootTag.toLowerCase(), name );
+		viewRoot.document = this;
 
 		this.roots.set( name, viewRoot );
 

+ 23 - 18
packages/ckeditor5-engine/src/view/editableelement.js

@@ -12,6 +12,8 @@ import ContainerElement from './containerelement';
 import mix from 'ckeditor5-utils/src/mix';
 import ObservableMixin from 'ckeditor5-utils/src/observablemixin';
 
+const documentSymbol = Symbol( 'document' );
+
 /**
  * Editable element which can be a {@link module:engine/view/rooteditableelement~RootEditableElement root}
  * or nested editable area in the editor.
@@ -23,18 +25,10 @@ export default class EditableElement extends ContainerElement {
 	/**
 	 * Creates an editable element.
 	 */
-	constructor( document, name, attrs, children ) {
+	constructor( name, attrs, children ) {
 		super( name, attrs, children );
 
 		/**
-		 * {@link module:engine/view/document~Document} that is an owner of this root.
-		 *
-		 * @private
-		 * @member {module:engine/view/document~Document} module:engine/view/rooteditableelement~RootEditableElement#_document
-		 */
-		this._document = document;
-
-		/**
 		 * Whether the editable is in read-write or read-only mode.
 		 *
 		 * @observable
@@ -52,6 +46,26 @@ export default class EditableElement extends ContainerElement {
 		 * @observable
 		 * @member {Boolean} module:engine/view/editableelement~EditableElement#isFocused
 		 */
+		this.set( 'isFocused', false );
+	}
+
+	/**
+	 * Gets {@link module:engine/view/document~Document View document} reference that owns this editable element.
+	 *
+	 * @type {module:engine/view/document~Document}
+	 */
+	get document() {
+		return this.getCustomProperty( documentSymbol );
+	}
+
+	/**
+	 * Sets {@link module:engine/view/document~Document} that is an owner of this root.
+	 *
+	 * @type {module:engine/view/document~Document}
+	 */
+	set document( document ) {
+		this.setCustomProperty( documentSymbol, document );
+
 		this.bind( 'isFocused' ).to(
 			document,
 			'isFocused',
@@ -64,15 +78,6 @@ export default class EditableElement extends ContainerElement {
 			this.isFocused = document.isFocused && document.selection.editableElement == this;
 		}, { priority: 'low' } );
 	}
-
-	/**
-	 * {@link module:engine/view/document~Document View document} reference that owns this editable element.
-	 *
-	 * @type {module:engine/view/document~Document|null}
-	 */
-	get document() {
-		return this._document;
-	}
 }
 
 mix( EditableElement, ObservableMixin );

+ 23 - 4
packages/ckeditor5-engine/src/view/rooteditableelement.js

@@ -9,6 +9,8 @@
 
 import EditableElement from './editableelement';
 
+const rootNameSymbol = Symbol( 'rootName' );
+
 /**
  * Class representing a single root in the data view. A root can be either {@link #isReadOnly editable or read-only}, but
  * in both cases it is called "an editable". Roots can contain other {@link module:engine/view/editableelement~EditableElement editable
@@ -23,10 +25,9 @@ export default class RootEditableElement extends EditableElement {
 	 *
 	 * @param {module:engine/view/document~Document} document {@link module:engine/view/document~Document} that is an owner of the root.
 	 * @param {String} name Node name.
-	 * @param {String} [rootName='main'] Root name inside parent {@link module:engine/view/document~Document}.
 	 */
-	constructor( document, name, rootName = 'main' ) {
-		super( document, name );
+	constructor( name ) {
+		super( name );
 
 		/**
 		 * Name of this root inside {@link module:engine/view/document~Document} that is an owner of this root.
@@ -34,6 +35,24 @@ export default class RootEditableElement extends EditableElement {
 		 * @readonly
 		 * @member {String}
 		 */
-		this.rootName = rootName;
+		this.rootName = 'main';
+	}
+
+	/**
+	 * Gets root name inside parent {@link module:engine/view/document~Document}.
+	 *
+	 * @type {String}
+	 */
+	get rootName() {
+		return this.getCustomProperty( rootNameSymbol );
+	}
+
+	/**
+	 * Sets root name inside parent {@link module:engine/view/document~Document}.
+	 *
+	 * @type {String}
+	 */
+	set rootName( rootName ) {
+		this.setCustomProperty( rootNameSymbol, rootName );
 	}
 }

+ 3 - 1
packages/ckeditor5-engine/tests/view/domconverter/domconverter.js

@@ -36,7 +36,9 @@ describe( 'DomConverter', () => {
 
 		beforeEach( () => {
 			viewDocument = new ViewDocument();
-			viewEditable = new ViewEditable( viewDocument, 'div' );
+			viewEditable = new ViewEditable( 'div' );
+			viewEditable.document = viewDocument;
+
 			domEditable = document.createElement( 'div' );
 			converter.bindElements( domEditable, viewEditable );
 			domEditable.setAttribute( 'contenteditable', 'true' );

+ 12 - 5
packages/ckeditor5-engine/tests/view/editableelement.js

@@ -14,12 +14,17 @@ describe( 'EditableElement', () => {
 		beforeEach( () => {
 			docMock = createDocumentMock();
 
-			viewMain = new RootEditableElement( docMock, 'div' );
-			viewHeader = new RootEditableElement( docMock, 'h1', 'header' );
+			viewMain = new RootEditableElement( 'div' );
+			viewMain.document = docMock;
+
+			viewHeader = new RootEditableElement( 'h1' );
+			viewHeader.document = docMock;
+			viewHeader.rootName = 'header';
 		} );
 
 		it( 'should be observable', () => {
-			const root = new RootEditableElement( createDocumentMock(), 'div' );
+			const root = new RootEditableElement( 'div' );
+			root.document = createDocumentMock();
 
 			expect( root.isFocused ).to.be.false;
 
@@ -73,7 +78,8 @@ describe( 'EditableElement', () => {
 
 	describe( 'isReadOnly', () => {
 		it( 'should be observable', () => {
-			const root = new RootEditableElement( createDocumentMock(), 'div' );
+			const root = new RootEditableElement( 'div' );
+			root.document = createDocumentMock();
 
 			expect( root.isReadOnly ).to.be.false;
 
@@ -92,7 +98,8 @@ describe( 'EditableElement', () => {
 	describe( 'getDocument', ()=> {
 		it( 'should return document', () => {
 			const docMock = createDocumentMock();
-			const root = new RootEditableElement( docMock, 'div' );
+			const root = new RootEditableElement( 'div' );
+			root.document = docMock;
 
 			expect( root.document ).to.equal( docMock );
 		} );

+ 4 - 2
packages/ckeditor5-engine/tests/view/node.js

@@ -146,7 +146,8 @@ describe( 'Node', () => {
 
 		it( 'should return Document attached to the parent element', () => {
 			const docMock = createDocumentMock();
-			const parent = new RootEditableElement( docMock, 'div' );
+			const parent = new RootEditableElement( 'div' );
+			parent.document = docMock;
 			const child = new Element( 'p' );
 
 			child.parent = parent;
@@ -171,7 +172,8 @@ describe( 'Node', () => {
 		} );
 
 		it( 'should return root element', () => {
-			const parent = new RootEditableElement( createDocumentMock(), 'div' );
+			const parent = new RootEditableElement( 'div' );
+			parent.document = createDocumentMock();
 			const child = new Element( 'p' );
 
 			child.parent = parent;

+ 2 - 1
packages/ckeditor5-engine/tests/view/position.js

@@ -457,7 +457,8 @@ describe( 'Position', () => {
 		it( 'should return EditableElement when position is placed inside', () => {
 			const document = new Document();
 			const p = new Element( 'p' );
-			const editable = new EditableElement( document, 'div', null, p );
+			const editable = new EditableElement( 'div', null, p );
+			editable.document = document;
 			const position = new Position( p, 0 );
 
 			expect( position.editableElement ).to.equal( editable );

+ 5 - 2
packages/ckeditor5-engine/tests/view/rooteditableelement.js

@@ -12,7 +12,8 @@ import createDocumentMock from 'ckeditor5-engine/tests/view/_utils/createdocumen
 describe( 'RootEditableElement', () => {
 	describe( 'constructor()', () => {
 		it( 'should create an element with default root name', () => {
-			const root = new RootEditableElement( createDocumentMock(), 'div' );
+			const root = new RootEditableElement( 'div' );
+			root.document = createDocumentMock();
 
 			expect( root ).to.be.instanceof( EditableElement );
 			expect( root ).to.be.instanceof( ContainerElement );
@@ -25,7 +26,9 @@ describe( 'RootEditableElement', () => {
 		} );
 
 		it( 'should create an element with custom root name', () => {
-			const root = new RootEditableElement( createDocumentMock(), 'h1', 'header' );
+			const root = new RootEditableElement( 'h1' );
+			root.document = createDocumentMock();
+			root.rootName = 'header';
 
 			expect( root.rootName ).to.equal( 'header' );
 			expect( root.name ).to.equal( 'h1' );

+ 4 - 2
packages/ckeditor5-engine/tests/view/textproxy.js

@@ -68,7 +68,8 @@ describe( 'TextProxy', () => {
 
 		it( 'should return Document attached to the parent element', () => {
 			const docMock = createDocumentMock();
-			const root = new RootEditableElement( docMock, 'div' );
+			const root = new RootEditableElement( 'div' );
+			root.document = docMock;
 
 			wrapper.parent = root;
 
@@ -84,7 +85,8 @@ describe( 'TextProxy', () => {
 
 	describe( 'getRoot', () => {
 		it( 'should return root element', () => {
-			const root = new RootEditableElement( createDocumentMock(), 'div' );
+			const root = new RootEditableElement( 'div' );
+			root.document = createDocumentMock();
 
 			wrapper.parent = root;