ソースを参照

Introduced `getEditableElement()` and `getEditableElementsNames()` methods in `EditorUI`.

Krzysztof Krztoń 7 年 前
コミット
322e718534

+ 24 - 1
packages/ckeditor5-core/src/editor/editorui.js

@@ -54,6 +54,14 @@ export default class EditorUI {
 		 */
 		this.focusTracker = new FocusTracker();
 
+		/**
+		 * Stores all editable elements used by the editor instance.
+		 *
+		 * @protected
+		 * @member {Array.<module:ui/editorui/editoruiview~EditorUIView>}
+		 */
+		this._editableElements = [];
+
 		/**
 		 * **Deprecated** since `v12.0.0`. This property is deprecated and should not be used. Use the property
 		 * from the subclass directly instead, for example
@@ -158,6 +166,8 @@ export default class EditorUI {
 	destroy() {
 		this.stopListening();
 
+		this._editableElements = [];
+
 		if ( this._view ) {
 			this._view.destroy();
 		}
@@ -166,10 +176,23 @@ export default class EditorUI {
 	/**
 	 * Returns the editable editor element with the given name or null if editable does not exist.
 	 *
-	 * @method module:core/editor/editorui~EditorUI#getEditableElement
 	 * @param {String} [rootName=main] The editable name.
 	 * @returns {HTMLElement|null}
 	 */
+	getEditableElement( rootName = 'main' ) {
+		const editable = this._editableElements.find( editable => editable.name === rootName );
+
+		return editable ? editable.element : null;
+	}
+
+	/**
+	 * Returns array of names of all editor editable elements.
+	 *
+	 * @returns {Array.<String>}
+	 */
+	getEditableElementsNames() {
+		return this._editableElements.map( editable => editable.name );
+	}
 
 	/**
 	 * Fired when the editor UI is ready.

+ 50 - 0
packages/ckeditor5-core/tests/editor/editorui.js

@@ -13,6 +13,8 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
 import testUtils from '../_utils/utils';
 import log from '@ckeditor/ckeditor5-utils/src/log';
 
+/* global document */
+
 describe( 'EditorUI', () => {
 	let editor, ui;
 
@@ -136,4 +138,52 @@ describe( 'EditorUI', () => {
 			} ).to.not.throw();
 		} );
 	} );
+
+	describe( 'getEditableElement()', () => {
+		it( 'should return editable element (default root name)', () => {
+			const ui = new EditorUI( editor );
+			const editableMock = { name: 'main', element: document.createElement( 'div' ) };
+
+			ui._editableElements.push( editableMock );
+
+			expect( ui.getEditableElement() ).to.equal( editableMock.element );
+		} );
+
+		it( 'should return editable element (custom root name)', () => {
+			const ui = new EditorUI( editor );
+			const editableMock1 = { name: 'root1', element: document.createElement( 'div' ) };
+			const editableMock2 = { name: 'root2', element: document.createElement( 'p' ) };
+
+			ui._editableElements.push( editableMock1 );
+			ui._editableElements.push( editableMock2 );
+
+			expect( ui.getEditableElement( 'root1' ) ).to.equal( editableMock1.element );
+			expect( ui.getEditableElement( 'root2' ) ).to.equal( editableMock2.element );
+		} );
+
+		it( 'should return null if editable with specified name does not exist', () => {
+			const ui = new EditorUI( editor );
+
+			expect( ui.getEditableElement() ).to.null;
+		} );
+	} );
+
+	describe( 'getEditableElementsNames()', () => {
+		it( 'should return array of names', () => {
+			const ui = new EditorUI( editor );
+			const editableMock1 = { name: 'main', element: document.createElement( 'div' ) };
+			const editableMock2 = { name: 'root2', element: document.createElement( 'p' ) };
+
+			ui._editableElements.push( editableMock1 );
+			ui._editableElements.push( editableMock2 );
+
+			expect( ui.getEditableElementsNames() ).to.deep.equal( [ 'main', 'root2' ] );
+		} );
+
+		it( 'should return empty array if no editables', () => {
+			const ui = new EditorUI( editor );
+
+			expect( ui.getEditableElementsNames() ).to.be.empty;
+		} );
+	} );
 } );