Browse Source

Added Formats class.

Szymon Kupś 9 years ago
parent
commit
c47fea8796

+ 6 - 4
packages/ckeditor5-heading/src/formats.js

@@ -19,7 +19,6 @@ export default class Formats extends Feature {
 
 	init() {
 		const editor = this.editor;
-		const t = editor.t;
 		const command = editor.commands.get( 'format' );
 		const formats = command.formats;
 		const collection = new Collection();
@@ -28,7 +27,7 @@ export default class Formats extends Feature {
 		for ( let format of formats ) {
 			collection.add( new Model( {
 				id: format.id,
-				label: t( format.label )
+				label: format.label
 			} ) );
 		}
 
@@ -41,17 +40,20 @@ export default class Formats extends Feature {
 		const dropdownModel = new Model( {
 			isEnabled: true,
 			isOn: false,
-			label: t( 'Formats' ),
+			label: 'Formats',
 			content: itemListModel
 		} );
 
 		//Bind dropdown model to command.
 		dropdownModel.bind( 'isEnabled' ).to( command, 'isEnabled' );
+		dropdownModel.bind( 'label' ).to( command, 'format', ( format ) => {
+			return format.label;
+		} );
 
 		// Execute command when item from dropdown is selected.
 		this.listenTo( itemListModel, 'execute', ( evtInfo, itemModel ) => {
 			editor.execute( 'format', itemModel.id );
-			dropdownModel.label = t( itemModel.label );
+			dropdownModel.label = itemModel.label;
 		} );
 
 		editor.ui.featureComponents.add( 'formats', ListDropdownController, ListDropdownView, dropdownModel );

+ 1 - 0
packages/ckeditor5-heading/tests/formats.html

@@ -0,0 +1 @@
+<div id="editor"></div>

+ 57 - 0
packages/ckeditor5-heading/tests/formats.js

@@ -0,0 +1,57 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import ClassicTestEditor from '/tests/ckeditor5/_utils/classictesteditor.js';
+import Formats from '/ckeditor5/formats/formats.js';
+import FormatsEngine from '/ckeditor5/formats/formatsengine.js';
+import ListDropdown from '/ckeditor5/ui/dropdown/list/listdropdown.js';
+import testUtils from '/tests/ckeditor5/_utils/utils.js';
+
+testUtils.createSinonSandbox();
+
+describe( 'Formats', () => {
+	let editor;
+
+	beforeEach( () => {
+		return ClassicTestEditor.create( document.getElementById( 'editor' ), {
+			features: [ Formats ],
+			toolbar: [ 'formats' ]
+		} )
+			.then( newEditor => {
+				editor = newEditor;
+			} );
+	} );
+
+	afterEach( () => {
+		return editor.destroy();
+	} );
+
+	it( 'should be loaded', () => {
+		expect( editor.plugins.get( Formats ) ).to.be.instanceOf( Formats );
+	} );
+
+	it( 'should load FormatsEngine', () => {
+		expect( editor.plugins.get( FormatsEngine ) ).to.be.instanceOf( FormatsEngine );
+	} );
+
+	it( 'should register formats feature component', () => {
+		const controller = editor.ui.featureComponents.create( 'formats' );
+
+		expect( controller ).to.be.instanceOf( ListDropdown );
+	} );
+
+	it( 'should execute format command on model execute event', () => {
+		const executeSpy = testUtils.sinon.spy( editor, 'execute' );
+		const controller = editor.ui.featureComponents.create( 'formats' );
+		const model = controller.model.content;
+
+		model.fire( 'execute', { id: 'paragraph', label: 'Paragraph' } );
+
+		sinon.assert.calledOnce( executeSpy );
+		sinon.assert.calledWithExactly( executeSpy, 'format', 'paragraph' );
+	} );
+} );