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

Added #state observable to the Editor class.

Oskar Wróbel 7 лет назад
Родитель
Сommit
1464041df8

+ 15 - 0
packages/ckeditor5-core/src/editor/editor.js

@@ -104,6 +104,19 @@ export default class Editor {
 		 */
 		this.t = this.locale.t;
 
+		/**
+		 * Indicates editor initialization status.
+		 *
+		 * The following statuses are available:
+		 * * initializing -  during initialization chain
+		 * * ready - after initializing chain, the editor is ready to work
+		 * * destroyed - after destroy, editor and all plugins are destroyed
+		 *
+		 * @observable
+		 * @member {'initializing'|'ready'|'destroyed'} #state
+		 */
+		this.set( 'state', 'initializing' );
+
 		/**
 		 * Defines whether this editor is in read-only mode.
 		 *
@@ -225,6 +238,7 @@ export default class Editor {
 	 * @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
 	 */
 	destroy() {
+		this.state = 'destroyed';
 		this.fire( 'destroy' );
 
 		this.stopListening();
@@ -270,6 +284,7 @@ export default class Editor {
 				editor.initPlugins()
 					.then( () => {
 						editor.fire( 'dataReady' );
+						editor.state = 'ready';
 						editor.fire( 'ready' );
 					} )
 					.then( () => editor )

+ 35 - 0
packages/ckeditor5-core/tests/editor/editor.js

@@ -181,6 +181,41 @@ describe( 'Editor', () => {
 		} );
 	} );
 
+	describe( 'state', () => {
+		it( 'is `initializing` initially', () => {
+			const editor = new Editor();
+
+			expect( editor.state ).to.equal( 'initializing' );
+		} );
+
+		it( 'is `ready` after initialization chain', () => {
+			return Editor.create().then( editor => {
+				expect( editor.state ).to.equal( 'ready' );
+
+				return editor.destroy();
+			} );
+		} );
+
+		it( 'is `destroyed` after editor destroy', () => {
+			const editor = new Editor();
+
+			return editor.destroy().then( () => {
+				expect( editor.state ).to.equal( 'destroyed' );
+			} );
+		} );
+
+		it( 'is observable', () => {
+			const editor = new Editor();
+			const spy = sinon.spy();
+
+			editor.on( 'change:state', spy );
+
+			editor.state = 'ready';
+
+			sinon.assert.calledOnce( spy );
+		} );
+	} );
+
 	describe( 'isReadOnly', () => {
 		it( 'is false initially', () => {
 			const editor = new Editor();