Browse Source

Merge pull request #43 from ckeditor/t/ckeditor5/1449

Other: Editor UI classes API refactoring.

BREAKING CHANGE: Removed `InlineEditor#element` property. The `InlineEditorUI#element` property should be used instead.
BREAKING CHANGE: Removed `InlineEditorUIView#editableElement`. Instead `InlineEditorUI#getEditableElement()` method should be used.
Piotr Jasiun 6 years ago
parent
commit
4bbb4524d1

+ 0 - 8
packages/ckeditor5-editor-inline/src/inlineeditor.js

@@ -76,13 +76,6 @@ export default class InlineEditor extends Editor {
 	}
 
 	/**
-	 * @inheritDoc
-	 */
-	get element() {
-		return this.ui.view.editable.element;
-	}
-
-	/**
 	 * Destroys the editor instance, releasing all resources used by it.
 	 *
 	 * Updates the original editor element with the data.
@@ -179,7 +172,6 @@ export default class InlineEditor extends Editor {
 				editor.initPlugins()
 					.then( () => {
 						editor.ui.init();
-						editor.fire( 'uiReady' );
 					} )
 					.then( () => {
 						const initialData = isElement( sourceElementOrData ) ?

+ 46 - 5
packages/ckeditor5-editor-inline/src/inlineeditorui.js

@@ -18,10 +18,21 @@ import normalizeToolbarConfig from '@ckeditor/ckeditor5-ui/src/toolbar/normalize
  */
 export default class InlineEditorUI extends EditorUI {
 	/**
-	 * @inheritDoc
+	 * Creates an instance of the inline editor UI class.
+	 *
+	 * @param {module:core/editor/editor~Editor} editor The editor instance.
+	 * @param {module:ui/editorui/editoruiview~EditorUIView} view The view of the UI.
 	 */
 	constructor( editor, view ) {
-		super( editor, view );
+		super( editor );
+
+		/**
+		 * The main (top–most) view of the editor UI.
+		 *
+		 * @private
+		 * @member {module:ui/editorui/editoruiview~EditorUIView} #_view
+		 */
+		this._view = view;
 
 		/**
 		 * A normalized `config.toolbar` object.
@@ -33,6 +44,23 @@ export default class InlineEditorUI extends EditorUI {
 	}
 
 	/**
+	 * The main (top–most) view of the editor UI.
+	 *
+	 * @readonly
+	 * @member {module:ui/editorui/editoruiview~EditorUIView} #view
+	 */
+	get view() {
+		return this._view;
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	get element() {
+		return this.view.editable.element;
+	}
+
+	/**
 	 * Initializes the UI.
 	 */
 	init() {
@@ -54,7 +82,7 @@ export default class InlineEditorUI extends EditorUI {
 			// showing up when there's no focus in the UI.
 			if ( view.panel.isVisible ) {
 				view.panel.pin( {
-					target: view.editableElement,
+					target: view.editable.element,
 					positions: view.panelPositions
 				} );
 			}
@@ -67,10 +95,12 @@ export default class InlineEditorUI extends EditorUI {
 		// Bind to focusTracker instead of editor.editing.view because otherwise
 		// focused editable styles disappear when view#toolbar is focused.
 		view.editable.bind( 'isFocused' ).to( this.focusTracker );
-		editor.editing.view.attachDomRoot( view.editableElement );
+		editor.editing.view.attachDomRoot( view.editable.element );
 		view.editable.name = editingRoot.rootName;
 
-		this.focusTracker.add( view.editableElement );
+		this._editableElements.set( view.editable.name, view.editable.element );
+
+		this.focusTracker.add( view.editable.element );
 
 		view.toolbar.fillFromConfig( this._toolbarConfig.items, this.componentFactory );
 
@@ -80,5 +110,16 @@ export default class InlineEditorUI extends EditorUI {
 			originKeystrokeHandler: editor.keystrokes,
 			toolbar: view.toolbar
 		} );
+
+		this.fire( 'ready' );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	destroy() {
+		this._view.destroy();
+
+		super.destroy();
 	}
 }

+ 2 - 9
packages/ckeditor5-editor-inline/src/inlineeditoruiview.js

@@ -72,7 +72,7 @@ export default class InlineEditorUIView extends EditorUIView {
 
 		/**
 		 * A set of positioning functions used by the {@link #panel} to float around
-		 * {@link #editableElement}.
+		 * {@link #element editableElement}.
 		 *
 		 * The positioning functions are as follows:
 		 *
@@ -144,17 +144,10 @@ export default class InlineEditorUIView extends EditorUIView {
 	}
 
 	/**
-	 * @inheritDoc
-	 */
-	get editableElement() {
-		return this.editable.element;
-	}
-
-	/**
 	 * Determines the panel top position of the {@link #panel} in {@link #panelPositions}.
 	 *
 	 * @private
-	 * @param {module:utils/dom/rect~Rect} editableRect Rect of the {@link #editableElement}.
+	 * @param {module:utils/dom/rect~Rect} editableRect Rect of the {@link #element}.
 	 * @param {module:utils/dom/rect~Rect} panelRect Rect of the {@link #panel}.
 	 */
 	_getPanelPositionTop( editableRect, panelRect ) {

+ 10 - 6
packages/ckeditor5-editor-inline/tests/inlineeditor.js

@@ -19,6 +19,8 @@ import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementap
 import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
 
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import log from '@ckeditor/ckeditor5-utils/src/log';
+
 import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
 import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';
 
@@ -32,6 +34,8 @@ describe( 'InlineEditor', () => {
 		editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';
 
 		document.body.appendChild( editorElement );
+
+		testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
 	} );
 
 	afterEach( () => {
@@ -111,11 +115,11 @@ describe( 'InlineEditor', () => {
 			} );
 		} );
 
-		it( 'editor.element should contain the whole editor (with UI) element', () => {
+		it( 'editor.ui.element should contain the whole editor (with UI) element', () => {
 			return InlineEditor.create( '<p>Hello world!</p>', {
 				plugins: [ Paragraph ]
 			} ).then( editor => {
-				expect( editor.editing.view.getDomRoot() ).to.equal( editor.element );
+				expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.element );
 			} );
 		} );
 	} );
@@ -194,7 +198,7 @@ describe( 'InlineEditor', () => {
 			class EventWatcher extends Plugin {
 				init() {
 					this.editor.on( 'pluginsReady', spy );
-					this.editor.on( 'uiReady', spy );
+					this.editor.ui.on( 'ready', spy );
 					this.editor.on( 'dataReady', spy );
 					this.editor.on( 'ready', spy );
 				}
@@ -205,7 +209,7 @@ describe( 'InlineEditor', () => {
 					plugins: [ EventWatcher ]
 				} )
 				.then( newEditor => {
-					expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
+					expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'dataReady', 'ready' ] );
 
 					editor = newEditor;
 				} );
@@ -233,12 +237,12 @@ describe( 'InlineEditor', () => {
 				} );
 		} );
 
-		it( 'fires uiReady once UI is ready', () => {
+		it( 'fires ready once UI is ready', () => {
 			let isReady;
 
 			class EventWatcher extends Plugin {
 				init() {
-					this.editor.on( 'uiReady', () => {
+					this.editor.ui.on( 'ready', () => {
 						isReady = this.editor.ui.view.isRendered;
 					} );
 				}

+ 23 - 3
packages/ckeditor5-editor-inline/tests/inlineeditorui.js

@@ -17,7 +17,7 @@ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 describe( 'InlineEditorUI', () => {
-	let editor, view, ui;
+	let editor, view, ui, viewElement;
 
 	testUtils.createSinonSandbox();
 
@@ -31,6 +31,7 @@ describe( 'InlineEditorUI', () => {
 
 				ui = editor.ui;
 				view = ui.view;
+				viewElement = view.editable.element;
 			} );
 	} );
 
@@ -93,7 +94,7 @@ describe( 'InlineEditorUI', () => {
 				editor.ui.fire( 'update' );
 				sinon.assert.calledOnce( spy );
 				sinon.assert.calledWithExactly( spy, {
-					target: view.editableElement,
+					target: view.editable.element,
 					positions: sinon.match.array
 				} );
 			} );
@@ -198,6 +199,26 @@ describe( 'InlineEditorUI', () => {
 				} );
 		} );
 	} );
+
+	describe( 'element()', () => {
+		it( 'returns correct element instance', () => {
+			expect( ui.element ).to.equal( viewElement );
+		} );
+	} );
+
+	describe( 'getEditableElement()', () => {
+		it( 'returns editable element (default)', () => {
+			expect( ui.getEditableElement() ).to.equal( view.editable.element );
+		} );
+
+		it( 'returns editable element (root name passed)', () => {
+			expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
+		} );
+
+		it( 'returns undefined if editable with the given name is absent', () => {
+			expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
+		} );
+	} );
 } );
 
 function viewCreator( name ) {
@@ -236,7 +257,6 @@ class VirtualInlineTestEditor extends VirtualTestEditor {
 				editor.initPlugins()
 					.then( () => {
 						editor.ui.init();
-						editor.fire( 'uiReady' );
 						editor.fire( 'dataReady' );
 						editor.fire( 'ready' );
 					} )

+ 4 - 8
packages/ckeditor5-editor-inline/tests/inlineeditoruiview.js

@@ -9,9 +9,13 @@ import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpa
 import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
 import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+
 describe( 'InlineEditorUIView', () => {
 	let locale, view;
 
+	testUtils.createSinonSandbox();
+
 	beforeEach( () => {
 		locale = new Locale( 'en' );
 		view = new InlineEditorUIView( locale );
@@ -117,14 +121,6 @@ describe( 'InlineEditorUIView', () => {
 		} );
 	} );
 
-	describe( 'editableElement', () => {
-		it( 'returns editable\'s view element', () => {
-			view.render();
-			expect( view.editableElement.getAttribute( 'contentEditable' ) ).to.equal( 'true' );
-			view.destroy();
-		} );
-	} );
-
 	describe( 'panelPositions', () => {
 		it( 'returns the positions in the right order', () => {
 			const positions = view.panelPositions;