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

Created editors simplified for tests.

Piotrek Koszuliński 9 лет назад
Родитель
Сommit
17cebf7106

+ 18 - 0
src/editor/editor.js

@@ -153,6 +153,24 @@ export default class Editor {
 
 		command._execute( commandParam );
 	}
+
+	/**
+	 * Creates a basic editor instance.
+	 *
+	 * @param {Object} config See {@link ckeditor5.editor.StandardEditor}'s param.
+	 * @returns {Promise} Promise resolved once editor is ready.
+	 * @returns {ckeditor5.editor.StandardEditor} return.editor The editor instance.
+	 */
+	static create( config ) {
+		return new Promise( ( resolve ) => {
+			const editor = new this( config );
+
+			resolve(
+				editor.initPlugins()
+					.then( () => editor )
+			);
+		} );
+	}
 }
 
 mix( Editor, EmitterMixin );

+ 10 - 18
src/editor/standardeditor.js

@@ -9,7 +9,6 @@ import Editor from './editor.js';
 import KeystrokeHandler from '../keystrokehandler.js';
 import EditingController from '../engine/editingcontroller.js';
 
-import CKEditorError from '../utils/ckeditorerror.js';
 import getDataFromElement from '../utils/dom/getdatafromelement.js';
 import setDataInElement from '../utils/dom/setdatainelement.js';
 
@@ -70,16 +69,6 @@ export default class StandardEditor extends Editor {
 	 * @param {*} data The data to load.
 	 */
 	setData( data ) {
-		if ( !this.data ) {
-			/**
-			 * Data controller has not been defined yet, so methods like {@link ckeditor5.editor.StandardEditor#setData} and
-			 * {@link ckeditor5.editor.StandardEditor#getData} cannot be used.
-			 *
-			 * @error editor-no-datacontroller
-			 */
-			throw new CKEditorError( 'editor-no-datacontroller: Data controller has not been defined yet.' );
-		}
-
 		this.data.set( data );
 	}
 
@@ -87,10 +76,6 @@ export default class StandardEditor extends Editor {
 	 * Gets the data from the editor's main root.
 	 */
 	getData() {
-		if ( !this.data ) {
-			throw new CKEditorError( 'editor-no-datacontroller: Data controller has not been defined yet.' );
-		}
-
 		return this.data.get();
 	}
 
@@ -111,12 +96,19 @@ export default class StandardEditor extends Editor {
 	/**
 	 * Creates a standard editor instance.
 	 *
-	 * @abstract
-	 * @static
-	 * @method #create
 	 * @param {HTMLElement} element See {@link ckeditor5.editor.StandardEditor}'s param.
 	 * @param {Object} config See {@link ckeditor5.editor.StandardEditor}'s param.
 	 * @returns {Promise} Promise resolved once editor is ready.
 	 * @returns {ckeditor5.editor.StandardEditor} return.editor The editor instance.
 	 */
+	static create( element, config ) {
+		return new Promise( ( resolve ) => {
+			const editor = new this( element, config );
+
+			resolve(
+				editor.initPlugins()
+					.then( () => editor )
+			);
+		} );
+	}
 }

+ 63 - 0
tests/_utils/classictesteditor.js

@@ -0,0 +1,63 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import StandardEditor from '/ckeditor5/editor/standardeditor.js';
+
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+
+import BoxedEditorUI from '/ckeditor5/ui/editorui/boxed/boxededitorui.js';
+import BoxedEditorUIView from '/ckeditor5/ui/editorui/boxed/boxededitoruiview.js';
+
+export default class ClassicTestEditor extends StandardEditor {
+	constructor( element, config ) {
+		super( element, config );
+
+		const editableElement = document.createElement( 'div' );
+
+		document.body.appendChild( editableElement );
+
+		this.document.createRoot();
+
+		this.editing.createRoot( editableElement );
+
+		this.data.processor = new HtmlDataProcessor();
+	}
+
+	destroy() {
+		return this.ui.destroy()
+			.then( () => super.destroy() );
+	}
+
+	static create( element, config ) {
+		return new Promise( ( resolve ) => {
+			const editor = new this( element, config );
+
+			resolve(
+				editor._createUI()
+					.then( () => editor.initPlugins() )
+					.then( () => editor._initUI() )
+					.then( () => editor.loadDataFromEditorElement() )
+					.then( () => editor )
+			);
+		} );
+	}
+
+	_createUI() {
+		const editorUI = new BoxedEditorUI( this );
+		const editorUIView = new BoxedEditorUIView( editorUI.viewModel, this.locale );
+
+		editorUI.view = editorUIView;
+
+		this.ui = editorUI;
+
+		return Promise.resolve();
+	}
+
+	_initUI() {
+		return this.ui.init();
+	}
+}

+ 23 - 0
tests/_utils/virtualeditingcontroller.js

@@ -0,0 +1,23 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import ModelConversionDispatcher from '/ckeditor5/engine/conversion/modelconversiondispatcher.js';
+import ViewDocument from '/ckeditor5/engine/view/document.js';
+
+export default class VirtualEditingController {
+	constructor( model ) {
+		this.model = model;
+
+		this.view = new ViewDocument();
+
+		this.modelToView = new ModelConversionDispatcher( {
+			writer: this.view.writer,
+			mapper: this.mapper,
+			viewSelection: this.view.selection
+		} );
+	}
+}

+ 27 - 0
tests/_utils/virtualtesteditor.js

@@ -0,0 +1,27 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Editor from '/ckeditor5/editor/editor.js';
+import VirtualEditingController from './virtualeditingcontroller.js';
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+
+export default class VirtualTestEditor extends Editor {
+	constructor( config ) {
+		super( config );
+
+		this.editing = new VirtualEditingController( this.document );
+		this.data.processor = new HtmlDataProcessor();
+	}
+
+	setData( data ) {
+		this.data.set( data );
+	}
+
+	getData() {
+		return this.data.get();
+	}
+}