瀏覽代碼

Implement editor events.

Piotrek Koszuliński 9 年之前
父節點
當前提交
8659e7444c
共有 2 個文件被更改,包括 85 次插入1 次删除
  1. 5 0
      packages/ckeditor5-editor-classic/src/classic.js
  2. 80 1
      packages/ckeditor5-editor-classic/tests/classic.js

+ 5 - 0
packages/ckeditor5-editor-classic/src/classic.js

@@ -82,8 +82,13 @@ export default class ClassicEditor extends StandardEditor {
 				editor.initPlugins()
 					.then( () => editor._elementReplacer.replace( element, editor.ui.view.element ) )
 					.then( () => editor.ui.init() )
+					.then( () => editor.fire( 'uiReady' ) )
 					.then( () => editor.editing.view.attachDomRoot( editor.ui.view.editableElement ) )
 					.then( () => editor.loadDataFromEditorElement() )
+					.then( () => {
+						editor.fire( 'dataReady' );
+						editor.fire( 'ready' );
+					} )
 					.then( () => editor )
 			);
 		} );

+ 80 - 1
packages/ckeditor5-editor-classic/tests/classic.js

@@ -12,6 +12,7 @@ import ClassicEditorUIView from 'ckeditor5/editor-classic/classiceditoruiview.js
 import HtmlDataProcessor from 'ckeditor5/engine/dataprocessor/htmldataprocessor.js';
 
 import ClassicEditor from 'ckeditor5/editor-classic/classic.js';
+import Plugin from 'ckeditor5/core/plugin.js';
 import Paragraph from 'ckeditor5/paragraph/paragraph.js';
 import Bold from 'ckeditor5/basic-styles/bold.js';
 
@@ -58,7 +59,7 @@ describe( 'ClassicEditor', () => {
 		} );
 	} );
 
-	describe( 'create', () => {
+	describe( 'create()', () => {
 		beforeEach( function() {
 			return ClassicEditor.create( editorElement, {
 					plugins: [ Paragraph, Bold ]
@@ -68,6 +69,10 @@ describe( 'ClassicEditor', () => {
 				} );
 		} );
 
+		afterEach( () => {
+			return editor.destroy();
+		} );
+
 		it( 'creates an instance which inherits from the ClassicEditor', () => {
 			expect( editor ).to.be.instanceof( ClassicEditor );
 		} );
@@ -85,6 +90,80 @@ describe( 'ClassicEditor', () => {
 		} );
 	} );
 
+	describe( 'create - events', () => {
+		afterEach( () => {
+			return editor.destroy();
+		} );
+
+		it( 'fires all events in the right order', () => {
+			const fired = [];
+
+			function spy( evt ) {
+				fired.push( evt.name );
+			}
+
+			class EventWatcher extends Plugin {
+				init() {
+					this.editor.on( 'pluginsReady', spy );
+					this.editor.on( 'uiReady', spy );
+					this.editor.on( 'dataReady', spy );
+					this.editor.on( 'ready', spy );
+				}
+			}
+
+			return ClassicEditor.create( editorElement, {
+					plugins: [ EventWatcher ]
+				} )
+				.then( ( newEditor ) => {
+					expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
+
+					editor = newEditor;
+				} );
+		} );
+
+		it( 'fires dataReady once data is loaded', () => {
+			let data;
+
+			class EventWatcher extends Plugin {
+				init() {
+					this.editor.on( 'dataReady', () => {
+						data = this.editor.getData();
+					} );
+				}
+			}
+
+			return ClassicEditor.create( editorElement, {
+					plugins: [ EventWatcher, Paragraph, Bold ]
+				} )
+				.then( ( newEditor ) => {
+					expect( data ).to.equal( '<p><strong>foo</strong> bar</p>' );
+
+					editor = newEditor;
+				} );
+		} );
+
+		it( 'fires uiReady once UI is ready', () => {
+			let isReady;
+
+			class EventWatcher extends Plugin {
+				init() {
+					this.editor.on( 'uiReady', () => {
+						isReady = this.editor.ui.view.ready;
+					} );
+				}
+			}
+
+			return ClassicEditor.create( editorElement, {
+					plugins: [ EventWatcher ]
+				} )
+				.then( ( newEditor ) => {
+					expect( isReady ).to.be.true;
+
+					editor = newEditor;
+				} );
+		} );
+	} );
+
 	describe( 'destroy', () => {
 		beforeEach( function() {
 			return ClassicEditor.create( editorElement, { plugins: [ Paragraph ] } )