소스 검색

Moved EditingController and DataController from StandardEditor to Editor and create main root at default.

Oskar Wróbel 8 년 전
부모
커밋
bd53ad75a7

+ 28 - 25
packages/ckeditor5-core/src/editor/editor.js

@@ -8,6 +8,7 @@
  */
 
 import Config from '@ckeditor/ckeditor5-utils/src/config';
+import EditingController from '@ckeditor/ckeditor5-engine/src/controller/editingcontroller';
 import PluginCollection from '../plugincollection';
 import CommandCollection from '../commandcollection';
 import Locale from '@ckeditor/ckeditor5-utils/src/locale';
@@ -18,7 +19,13 @@ import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 
 /**
- * Class representing a basic editor. It contains a base architecture, without much additional logic.
+ * Class representing editor. It contains basic editor architecture and provides API needed by plugins
+ * like {@link module:engine/controller/editingcontroller~EditingController editing pipeline} and
+ * {@link module:engine/controller/datacontroller~DataController data pipeline}.
+ *
+ * Besides it creates main {@link module:engine/model/rootelement~RootElement} using
+ * {@link module:engine/model/document~Document#createRoot createRoot} method what automatically creates
+ * corresponding {@link module:engine/view/rooteditableelement~RootEditableElement view root} element.
  *
  * See also {@link module:core/editor/standardeditor~StandardEditor}.
  *
@@ -73,53 +80,48 @@ export default class Editor {
 		 */
 		this.t = this.locale.t;
 
+		/**
+		 * Defines whether this editor is in read-only mode.
+		 *
+		 * In read-only mode the editor {@link #commands commands} are disabled so it is not possible
+		 * to modify document using them.
+		 *
+		 * @observable
+		 * @member {Boolean} #isReadOnly
+		 */
+		this.set( 'isReadOnly', false );
+
 		/**
 		 * The editor's model.
 		 *
 		 * The center of the editor's abstract data model.
 		 *
-		 * Besides the model, the editor usually contains two controllers –
-		 * {@link #data data controller} and {@link #editing editing controller}.
-		 * The former is used e.g. when setting or retrieving editor data and contains a useful
-		 * set of methods for operating on the content. The latter controls user input and rendering
-		 * the content for editing.
-		 *
 		 * @readonly
 		 * @member {module:engine/model/model~Model}
 		 */
 		this.model = new Model();
 
+		// Creates main root.
+		this.model.document.createRoot();
+
 		/**
 		 * The {@link module:engine/controller/datacontroller~DataController data controller}.
+		 * Used e.g. for setting or retrieving editor data.
 		 *
 		 * @readonly
 		 * @member {module:engine/controller/datacontroller~DataController}
 		 */
 		this.data = new DataController( this.model );
 
-		/**
-		 * Defines whether this editor is in read-only mode.
-		 *
-		 * In read-only mode the editor {@link #commands commands} are disabled so it is not possible
-		 * to modify document using them.
-		 *
-		 * @observable
-		 * @member {Boolean} #isReadOnly
-		 */
-		this.set( 'isReadOnly', false );
-
 		/**
 		 * The {@link module:engine/controller/editingcontroller~EditingController editing controller}.
-		 *
-		 * This property is set by more specialized editor classes (such as {@link module:core/editor/standardeditor~StandardEditor}),
-		 * however, it's required for features to work as their engine-related parts will try to connect converters.
-		 *
-		 * When defining a virtual editor class, like one working in Node.js, it's possible to plug a virtual
-		 * editing controller which only instantiates necessary properties, but without any observers and listeners.
+		 * Controls user input and rendering the content for editing.
 		 *
 		 * @readonly
-		 * @member {module:engine/controller/editingcontroller~EditingController} #editing
+		 * @member {module:engine/controller/editingcontroller~EditingController}
 		 */
+		this.editing = new EditingController( this.model );
+		this.editing.view.bind( 'isReadOnly' ).to( this );
 	}
 
 	/**
@@ -173,6 +175,7 @@ export default class Editor {
 			.then( () => {
 				this.model.destroy();
 				this.data.destroy();
+				this.editing.destroy();
 			} );
 	}
 

+ 1 - 8
packages/ckeditor5-core/src/editor/standardeditor.js

@@ -9,15 +9,13 @@
 
 import Editor from './editor';
 import EditingKeystrokeHandler from '../editingkeystrokehandler';
-import EditingController from '@ckeditor/ckeditor5-engine/src/controller/editingcontroller';
 import isFunction from '@ckeditor/ckeditor5-utils/src/lib/lodash/isFunction';
 
 import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
 import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
 
 /**
- * Class representing a typical browser-based editor. It handles a single source element and
- * uses {@link module:engine/controller/editingcontroller~EditingController}.
+ * Class representing a typical browser-based editor.
  *
  * @extends module:core/editor/editor~Editor
  */
@@ -40,10 +38,6 @@ export default class StandardEditor extends Editor {
 		 */
 		this.element = element;
 
-		// Documented in Editor.
-		this.editing = new EditingController( this.model );
-		this.editing.view.bind( 'isReadOnly' ).to( this );
-
 		/**
 		 * Instance of the {@link module:core/editingkeystrokehandler~EditingKeystrokeHandler}.
 		 *
@@ -74,7 +68,6 @@ export default class StandardEditor extends Editor {
 	destroy() {
 		return Promise.resolve()
 			.then( () => this.keystrokes.destroy() )
-			.then( () => this.editing.destroy() )
 			.then( super.destroy() );
 	}
 

+ 23 - 1
packages/ckeditor5-core/tests/editor/editor.js

@@ -8,6 +8,7 @@
 import Editor from '../../src/editor/editor';
 import Plugin from '../../src/plugin';
 import Config from '@ckeditor/ckeditor5-utils/src/config';
+import EditingController from '@ckeditor/ckeditor5-engine/src/controller/editingcontroller';
 import PluginCollection from '../../src/plugincollection';
 import CommandCollection from '../../src/commandcollection';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
@@ -103,6 +104,7 @@ describe( 'Editor', () => {
 
 			expect( editor.config ).to.be.an.instanceof( Config );
 			expect( editor.commands ).to.be.an.instanceof( CommandCollection );
+			expect( editor.editing ).to.be.instanceof( EditingController );
 
 			expect( editor.plugins ).to.be.an.instanceof( PluginCollection );
 			expect( getPlugins( editor ) ).to.be.empty;
@@ -133,6 +135,24 @@ describe( 'Editor', () => {
 
 			expect( editor.config.get( 'bar' ) ).to.equal( 'foo' );
 		} );
+
+		it( 'should bind editing.view#isReadOnly to the editor', () => {
+			const editor = new Editor();
+
+			editor.isReadOnly = false;
+
+			expect( editor.editing.view.isReadOnly ).to.false;
+
+			editor.isReadOnly = true;
+
+			expect( editor.editing.view.isReadOnly ).to.true;
+		} );
+
+		it( 'should create main root element', () => {
+			const editor = new Editor();
+
+			expect( editor.model.document.hasRoot( 'main' ) ).to.true;
+		} );
 	} );
 
 	describe( 'plugins', () => {
@@ -194,13 +214,15 @@ describe( 'Editor', () => {
 
 			const spy1 = sinon.spy( editor.data, 'destroy' );
 			const spy2 = sinon.spy( editor.model, 'destroy' );
-			const spy3 = sinon.spy( editor.plugins, 'destroy' );
+			const spy3 = sinon.spy( editor.editing, 'destroy' );
+			const spy4 = sinon.spy( editor.plugins, 'destroy' );
 
 			return editor.destroy()
 				.then( () => {
 					expect( spy1.calledOnce ).to.be.true;
 					expect( spy2.calledOnce ).to.be.true;
 					expect( spy3.calledOnce ).to.be.true;
+					expect( spy4.calledOnce ).to.be.true;
 				} );
 		} );
 	} );

+ 0 - 34
packages/ckeditor5-core/tests/editor/standardeditor.js

@@ -10,7 +10,6 @@ import StandardEditor from '../../src/editor/standardeditor';
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
-import EditingController from '@ckeditor/ckeditor5-engine/src/controller/editingcontroller';
 import EditingKeystrokeHandler from '../../src/editingkeystrokehandler';
 import Plugin from '../../src/plugin';
 
@@ -27,22 +26,9 @@ describe( 'StandardEditor', () => {
 			const editor = new StandardEditor( editorElement, { foo: 1 } );
 
 			expect( editor ).to.have.property( 'element', editorElement );
-			expect( editor.editing ).to.be.instanceof( EditingController );
 			expect( editor.keystrokes ).to.be.instanceof( EditingKeystrokeHandler );
 		} );
 
-		it( 'should bind editing.view#isReadOnly to the editor', () => {
-			const editor = new StandardEditor( editorElement, { foo: 1 } );
-
-			editor.isReadOnly = false;
-
-			expect( editor.editing.view.isReadOnly ).to.false;
-
-			editor.isReadOnly = true;
-
-			expect( editor.editing.view.isReadOnly ).to.true;
-		} );
-
 		it( 'activates #keystrokes', () => {
 			const spy = sinon.spy( EditingKeystrokeHandler.prototype, 'listenTo' );
 			const editor = new StandardEditor( editorElement, { foo: 1 } );
@@ -76,18 +62,6 @@ describe( 'StandardEditor', () => {
 				} );
 		} );
 
-		it( 'destroys the #editing', () => {
-			const editor = new StandardEditor( editorElement, { foo: 1 } );
-			const spy = sinon.spy( editor.editing, 'destroy' );
-
-			sinon.assert.notCalled( spy );
-
-			return editor.destroy()
-				.then( () => {
-					sinon.assert.calledOnce( spy );
-				} );
-		} );
-
 		it( 'destroys the parent', () => {
 			const editor = new StandardEditor( editorElement, { foo: 1 } );
 			const spy = sinon.spy( Editor.prototype, 'destroy' );
@@ -153,12 +127,8 @@ describe( 'StandardEditor', () => {
 		} );
 
 		it( 'should set data of the first root', () => {
-			editor.model.document.createRoot();
 			editor.model.document.createRoot( '$root', 'secondRoot' );
 
-			editor.editing.createRoot( 'div' );
-			editor.editing.createRoot( 'div', 'secondRoot' );
-
 			editor.setData( 'foo' );
 
 			expect( getData( editor.model, { rootName: 'main', withoutSelection: true } ) ).to.equal( 'foo' );
@@ -180,12 +150,8 @@ describe( 'StandardEditor', () => {
 		} );
 
 		it( 'should get data of the first root', () => {
-			editor.model.document.createRoot();
 			editor.model.document.createRoot( '$root', 'secondRoot' );
 
-			editor.editing.createRoot( 'div' );
-			editor.editing.createRoot( 'div', 'secondRoot' );
-
 			setData( editor.model, 'foo' );
 
 			expect( editor.getData() ).to.equal( 'foo' );