Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Updated heading after the removal of Controller class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksander Nowodziński authored and Aleksander Nowodziński committed Nov 4, 2016
1 parent e33fe55 commit 6081dd4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
29 changes: 14 additions & 15 deletions src/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import HeadingEngine from './headingengine.js';
import Feature from '../core/feature.js';

import Model from '../ui/model.js';
import ListDropdownController from '../ui/dropdown/list/listdropdown.js';
import ListDropdownView from '../ui/dropdown/list/listdropdownview.js';
import createListDropdown from '../ui/dropdown/list/createlistdropdown.js';

import Collection from '../utils/collection.js';

Expand Down Expand Up @@ -40,7 +39,7 @@ export default class Heading extends Feature {
// Add formats to collection.
for ( let format of formats ) {
collection.add( new Model( {
id: format.id,
formatId: format.id,
label: format.label
} ) );
}
Expand All @@ -51,24 +50,24 @@ export default class Heading extends Feature {
isOn: false,
label: 'Heading',
withText: true,

// Create item list model.
content: new Model( {
items: collection
} )
items: collection
} );

// Bind dropdown model to command.
dropdownModel.bind( 'isEnabled' ).to( command, 'isEnabled' );
dropdownModel.bind( 'label' ).to( command, 'value', format => format.label );

// Execute command when an item from the dropdown is selected.
this.listenTo( dropdownModel, 'execute', ( evt ) => {
editor.execute( 'heading', { formatId: evt.source.id } );
editor.editing.view.focus();
} );

// Register UI component.
editor.ui.featureComponents.add( 'headings', ListDropdownController, ListDropdownView, dropdownModel );
editor.ui.featureComponents.add( 'headings', ( locale ) => {
const dropdown = createListDropdown( dropdownModel, locale );

// Execute command when an item from the dropdown is selected.
this.listenTo( dropdown, 'execute', ( { source: { formatId } } ) => {
editor.execute( 'heading', { formatId } );
editor.editing.view.focus();
} );

return dropdown;
} );
}
}
33 changes: 15 additions & 18 deletions tests/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import ClassicTestEditor from '/tests/core/_utils/classictesteditor.js';
import Heading from '/ckeditor5/heading/heading.js';
import HeadingEngine from '/ckeditor5/heading/headingengine.js';
import ListDropdown from '/ckeditor5/ui/dropdown/list/listdropdown.js';
import DropdownView from '/ckeditor5/ui/dropdown/dropdownview.js';
import testUtils from '/tests/core/_utils/utils.js';

testUtils.createSinonSandbox();

describe( 'Heading', () => {
let editor, controller;
let editor, dropdown;

beforeEach( () => {
const editorElement = document.createElement( 'div' );
Expand All @@ -26,7 +26,7 @@ describe( 'Heading', () => {
} )
.then( newEditor => {
editor = newEditor;
controller = editor.ui.featureComponents.create( 'headings' );
dropdown = editor.ui.featureComponents.create( 'headings' );
} );
} );

Expand All @@ -43,51 +43,48 @@ describe( 'Heading', () => {
} );

it( 'should register formats feature component', () => {
const controller = editor.ui.featureComponents.create( 'headings' );
const dropdown = editor.ui.featureComponents.create( 'headings' );

expect( controller ).to.be.instanceOf( ListDropdown );
expect( dropdown ).to.be.instanceOf( DropdownView );
} );

it( 'should execute format command on model execute event', () => {
const executeSpy = testUtils.sinon.spy( editor, 'execute' );
const controller = editor.ui.featureComponents.create( 'headings' );
const model = controller.model;
const dropdown = editor.ui.featureComponents.create( 'headings' );

model.id = 'foo';
model.fire( 'execute' );
dropdown.formatId = 'foo';
dropdown.fire( 'execute' );

sinon.assert.calledOnce( executeSpy );
sinon.assert.calledWithExactly( executeSpy, 'heading', { formatId: 'foo' } );
} );

it( 'should focus view after command execution', () => {
const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
const controller = editor.ui.featureComponents.create( 'headings' );
const model = controller.model;
const dropdown = editor.ui.featureComponents.create( 'headings' );

model.fire( 'execute' );
dropdown.fire( 'execute' );

sinon.assert.calledOnce( focusSpy );
} );

describe( 'model to command binding', () => {
let model, command;
let command;

beforeEach( () => {
model = controller.model;
command = editor.commands.get( 'heading' );
} );

it( 'isEnabled', () => {
expect( model.isEnabled ).to.be.true;
expect( dropdown.buttonView.isEnabled ).to.be.true;
command.isEnabled = false;
expect( model.isEnabled ).to.be.false;
expect( dropdown.buttonView.isEnabled ).to.be.false;
} );

it( 'label', () => {
expect( model.label ).to.equal( 'Paragraph' );
expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
command.value = command.formats[ 1 ];
expect( model.label ).to.equal( 'Heading 1' );
expect( dropdown.buttonView.label ).to.equal( 'Heading 1' );
} );
} );
} );

0 comments on commit 6081dd4

Please sign in to comment.