8
0
Просмотр исходного кода

Tests: Added tests to check the placeholder feature of the UI. Code refactoring.

Aleksander Nowodzinski 7 лет назад
Родитель
Сommit
4d7c8529c7

+ 1 - 1
packages/ckeditor5-editor-decoupled/src/decouplededitor.js

@@ -73,7 +73,7 @@ export default class DecoupledEditor extends Editor {
 
 		this.model.document.createRoot();
 
-		const view = new DecoupledEditorUIView( this.locale, this.sourceElement, this.editing.view );
+		const view = new DecoupledEditorUIView( this.locale, this.editing.view, this.sourceElement );
 		this.ui = new DecoupledEditorUI( this, view );
 	}
 

+ 2 - 1
packages/ckeditor5-editor-decoupled/src/decouplededitoruiview.js

@@ -28,10 +28,11 @@ export default class DecoupledEditorUIView extends EditorUIView {
 	 * Creates an instance of the decoupled editor UI view.
 	 *
 	 * @param {module:utils/locale~Locale} locale The {@link module:core/editor/editor~Editor#locale} instance.
+	 * @param {module:engine/view/view~View} editingView The editing view instance this view is related to.
 	 * @param {HTMLElement} [editableElement] The editable element. If not specified, it will be automatically created by
 	 * {@link module:ui/editableui/editableuiview~EditableUIView}. Otherwise, the given element will be used.
 	 */
-	constructor( locale, editableElement, editingView ) {
+	constructor( locale, editingView, editableElement ) {
 		super( locale );
 
 		/**

+ 73 - 20
packages/ckeditor5-editor-decoupled/tests/decouplededitorui.js

@@ -10,11 +10,13 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import DecoupledEditorUI from '../src/decouplededitorui';
 import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import DecoupledEditorUIView from '../src/decouplededitoruiview';
 
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
+import { isElement } from 'lodash-es';
 
 describe( 'DecoupledEditorUI', () => {
 	let editor, view, ui, viewElement;
@@ -23,7 +25,7 @@ describe( 'DecoupledEditorUI', () => {
 
 	beforeEach( () => {
 		return VirtualDecoupledTestEditor
-			.create( {
+			.create( '', {
 				toolbar: [ 'foo', 'bar' ],
 			} )
 			.then( newEditor => {
@@ -69,34 +71,81 @@ describe( 'DecoupledEditorUI', () => {
 					view.editable,
 					{ isFocused: false },
 					[
-						[ editor.editing.view.document, { isFocused: true } ]
+						[ ui.focusTracker, { isFocused: true } ]
 					],
 					{ isFocused: true }
 				);
 			} );
 
-			it( 'binds view.editable#isReadOnly', () => {
-				const editable = editor.editing.view.document.getRoot();
+			it( 'attaches editable UI as view\'s DOM root', () => {
+				expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
+			} );
+		} );
 
-				utils.assertBinding(
-					view.editable,
-					{ isReadOnly: false },
-					[
-						[ editable, { isReadOnly: true } ]
-					],
-					{ isReadOnly: true }
-				);
+		describe( 'placeholder', () => {
+			it( 'sets placeholder from editor.config.placeholder', () => {
+				return VirtualDecoupledTestEditor
+					.create( 'foo', {
+						extraPlugins: [ Paragraph ],
+						placeholder: 'placeholder-text',
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
+
+						return editor.destroy();
+					} );
 			} );
 
-			it( 'attaches editable UI as view\'s DOM root', () => {
-				expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
+			it( 'sets placeholder from "placeholder" attribute of a passed element', () => {
+				const element = document.createElement( 'div' );
+
+				element.setAttribute( 'placeholder', 'placeholder-text' );
+
+				return VirtualDecoupledTestEditor
+					.create( element, {
+						extraPlugins: [ Paragraph ]
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
+
+						return editor.destroy();
+					} );
+			} );
+
+			it( 'uses editor.config.placeholder rather than "placeholder" attribute of a passed element', () => {
+				const element = document.createElement( 'div' );
+
+				element.setAttribute( 'placeholder', 'placeholder-text' );
+
+				return VirtualDecoupledTestEditor
+					.create( element, {
+						placeholder: 'config takes precedence',
+						extraPlugins: [ Paragraph ]
+					} )
+					.then( newEditor => {
+						editor = newEditor;
+
+						const firstChild = editor.editing.view.document.getRoot().getChild( 0 );
+
+						expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
+
+						return editor.destroy();
+					} );
 			} );
 		} );
 
 		describe( 'view.toolbar#items', () => {
 			it( 'are filled with the config.toolbar (specified as an Array)', () => {
 				return VirtualDecoupledTestEditor
-					.create( {
+					.create( '', {
 						toolbar: [ 'foo', 'bar' ]
 					} )
 					.then( editor => {
@@ -111,7 +160,7 @@ describe( 'DecoupledEditorUI', () => {
 
 			it( 'are filled with the config.toolbar (specified as an Object)', () => {
 				return VirtualDecoupledTestEditor
-					.create( {
+					.create( '', {
 						toolbar: {
 							items: [ 'foo', 'bar' ],
 							viewportTopOffset: 100
@@ -185,10 +234,14 @@ function viewCreator( name ) {
 }
 
 class VirtualDecoupledTestEditor extends VirtualTestEditor {
-	constructor( config ) {
+	constructor( sourceElementOrData, config ) {
 		super( config );
 
-		const view = new DecoupledEditorUIView( this.locale );
+		if ( isElement( sourceElementOrData ) ) {
+			this.sourceElement = sourceElementOrData;
+		}
+
+		const view = new DecoupledEditorUIView( this.locale, this.editing.view );
 		this.ui = new DecoupledEditorUI( this, view );
 
 		this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
@@ -201,9 +254,9 @@ class VirtualDecoupledTestEditor extends VirtualTestEditor {
 		return super.destroy();
 	}
 
-	static create( config ) {
+	static create( sourceElementOrData, config ) {
 		return new Promise( resolve => {
-			const editor = new this( config );
+			const editor = new this( sourceElementOrData, config );
 
 			resolve(
 				editor.initPlugins()

+ 15 - 3
packages/ckeditor5-editor-decoupled/tests/decouplededitoruiview.js

@@ -6,6 +6,8 @@
 /* globals document */
 
 import DecoupledEditorUIView from '../src/decouplededitoruiview';
+import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
+import ViewRootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
 import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
 import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
 import Locale from '@ckeditor/ckeditor5-utils/src/locale';
@@ -13,13 +15,15 @@ import Locale from '@ckeditor/ckeditor5-utils/src/locale';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
 describe( 'DecoupledEditorUIView', () => {
-	let locale, view;
+	let locale, view, editingView, editingViewRoot;
 
 	testUtils.createSinonSandbox();
 
 	beforeEach( () => {
 		locale = new Locale( 'en' );
-		view = new DecoupledEditorUIView( locale );
+		setUpEditingView();
+		view = new DecoupledEditorUIView( locale, editingView );
+		view.editable.name = editingViewRoot.rootName;
 	} );
 
 	describe( 'constructor()', () => {
@@ -57,7 +61,8 @@ describe( 'DecoupledEditorUIView', () => {
 
 			it( 'can be created out of an existing DOM element', () => {
 				const editableElement = document.createElement( 'div' );
-				const testView = new DecoupledEditorUIView( locale, editableElement );
+				const testView = new DecoupledEditorUIView( locale, editingView, editableElement );
+				testView.editable.name = editingViewRoot.rootName;
 
 				testView.render();
 
@@ -125,4 +130,11 @@ describe( 'DecoupledEditorUIView', () => {
 			view.editable.element.remove();
 		} );
 	} );
+
+	function setUpEditingView() {
+		editingView = new EditingView();
+		editingViewRoot = new ViewRootEditableElement( 'div' );
+		editingViewRoot._document = editingView.document;
+		editingView.document.roots.add( editingViewRoot );
+	}
 } );