Parcourir la source

Introduced DataInterface.

Oskar Wróbel il y a 8 ans
Parent
commit
97a56c938a

+ 34 - 0
packages/ckeditor5-core/src/editor/utils/datainterface.js

@@ -0,0 +1,34 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module core/editor/utils/datainterface
+ */
+
+/**
+ * @mixin module:core/editor/utils/datainterface~DataInterface
+ */
+const DataInterface = {
+	/**
+	 * Sets the data in the editor's main root.
+	 *
+	 * @method module:core/editor/utils/datainterface~DataInterface#setData
+	 * @param {*} data The data to load.
+	 */
+	setData( data ) {
+		this.data.set( data );
+	},
+
+	/**
+	 * Gets the data from the editor's main root.
+	 *
+	 * @method module:core/editor/utils/datainterface~DataInterface#getData
+	 */
+	getData() {
+		return this.data.get();
+	}
+};
+
+export default DataInterface;

+ 53 - 0
packages/ckeditor5-core/tests/editor/utils/datainterface.js

@@ -0,0 +1,53 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import DataInterface from '../../../src/editor/utils/datainterface';
+import Editor from '../../../src/editor/editor';
+import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
+import mix from '@ckeditor/ckeditor5-utils/src/mix';
+import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+
+describe( 'DataInterface', () => {
+	let editor;
+
+	beforeEach( () => {
+		class CustomEditor extends Editor {}
+		mix( CustomEditor, DataInterface );
+
+		editor = new CustomEditor();
+		editor.data.processor = new HtmlDataProcessor();
+		editor.model.document.createRoot( '$root', 'main' );
+		editor.model.document.createRoot( '$root', 'secondRoot' );
+		editor.model.schema.extend( '$text', { allowIn: '$root' } );
+	} );
+
+	afterEach( () => {
+		editor.destroy();
+	} );
+
+	describe( 'setData()', () => {
+		it( 'should be added to editor interface', () => {
+			expect( editor ).have.property( 'setData' ).to.be.a( 'function' );
+		} );
+
+		it( 'should set data of the first root', () => {
+			editor.setData( 'foo' );
+
+			expect( getData( editor.model, { rootName: 'main', withoutSelection: true } ) ).to.equal( 'foo' );
+		} );
+	} );
+
+	describe( 'getData()', () => {
+		it( 'should be added to editor interface', () => {
+			expect( editor ).have.property( 'getData' ).to.be.a( 'function' );
+		} );
+
+		it( 'should get data of the first root', () => {
+			setData( editor.model, 'foo' );
+
+			expect( editor.getData() ).to.equal( 'foo' );
+		} );
+	} );
+} );