Browse Source

Merge pull request #7 from ckeditor/t/4

Fix: Editor element should be filled up with data once the editor is destroyed. Closes #4.
Piotrek Koszuliński 8 years ago
parent
commit
f049d1f9f5

+ 6 - 2
packages/ckeditor5-editor-balloon/src/balloontoolbareditor.js

@@ -12,6 +12,7 @@ import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/html
 import ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
 import BalloonToolbarEditorUI from './balloontoolbareditorui';
 import BalloonToolbarEditorUIView from './balloontoolbareditoruiview';
+import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
 
 import '../theme/theme.scss';
 
@@ -47,10 +48,13 @@ export default class BalloonToolbarEditor extends StandardEditor {
 	 * @returns {Promise}
 	 */
 	destroy() {
-		this.updateEditorElement();
+		// Cache the data, then destroy.
+		// It's safe to assume that the model->view conversion will not work after super.destroy().
+		const data = this.getData();
 
 		return this.ui.destroy()
-			.then( () => super.destroy() );
+			.then( () => super.destroy() )
+			.then( () => setDataInElement( this.element, data ) );
 	}
 
 	/**

+ 24 - 3
packages/ckeditor5-editor-balloon/tests/balloontoolbareditor.js

@@ -9,6 +9,8 @@ import BalloonToolbarEditorUI from '../src/balloontoolbareditorui';
 import BalloonToolbarEditorUIView from '../src/balloontoolbareditoruiview';
 
 import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
+import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
+import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
 
 import BalloonToolbarEditor from '../src/balloontoolbareditor';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
@@ -175,20 +177,39 @@ describe( 'BalloonToolbarEditor', () => {
 		} );
 	} );
 
-	describe( 'destroy', () => {
+	describe( 'destroy()', () => {
 		beforeEach( function() {
 			return BalloonToolbarEditor.create( editorElement, { plugins: [ Paragraph ] } )
 				.then( newEditor => {
 					editor = newEditor;
+
+					const schema = editor.document.schema;
+
+					schema.registerItem( 'heading' );
+					schema.allow( { name: 'heading', inside: '$root' } );
+					schema.allow( { name: '$text', inside: 'heading' } );
+
+					buildModelConverter().for( editor.data.modelToView )
+						.fromElement( 'heading' )
+						.toElement( 'heading' );
+
+					buildViewConverter().for( editor.data.viewToModel )
+						.fromElement( 'heading' )
+						.toElement( 'heading' );
+
+					buildModelConverter().for( editor.editing.modelToView )
+						.fromElement( 'heading' )
+						.toElement( 'heading-editing-representation' );
 				} );
 		} );
 
 		it( 'sets the data back to the editor element', () => {
-			editor.setData( '<p>foo</p>' );
+			editor.setData( '<p>a</p><heading>b</heading>' );
 
 			return editor.destroy()
 				.then( () => {
-					expect( editorElement.innerHTML ).to.equal( '<p>foo</p>' );
+					expect( editorElement.innerHTML )
+						.to.equal( '<p>a</p><heading>b</heading>' );
 				} );
 		} );
 	} );