Browse Source

Fixed schema and tests in heading command.

Szymon Kupś 8 years ago
parent
commit
07f9a5ba0a

+ 14 - 13
packages/ckeditor5-heading/src/headingcommand.js

@@ -10,6 +10,8 @@
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import Command from '@ckeditor/ckeditor5-core/src/command/command';
 import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
+import Position from '@ckeditor/ckeditor5-engine/src/model/position';
+import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
  * The heading command. It is used by the {@link module:heading/heading~Heading heading feature} to apply headings.
@@ -114,7 +116,7 @@ export default class HeadingCommand extends Command {
 	 * @private
 	 */
 	_updateValue() {
-		const block = this._getSelectedBlock();
+		const block = first( this.editor.document.selection.getSelectedBlocks() );
 
 		this.value = !!block && block.is( this.modelElement );
 	}
@@ -123,20 +125,19 @@ export default class HeadingCommand extends Command {
 	 * @inheritDoc
 	 */
 	_checkEnabled() {
-		const block = this._getSelectedBlock();
-		const schema = this.editor.document.schema;
+		const block = first( this.editor.document.selection.getSelectedBlocks() );
 
-		return !!block && schema.check( { name: this.modelElement, inside: block.parent.name } ) && !schema.objects.has( block.name );
-	}
+		if ( !block ) {
+			return false;
+		}
 
-	/**
-	 * Returns currently selected block.
-	 *
-	 * @private
-	 * @returns {module:engine/model/element~Element}
-	 */
-	_getSelectedBlock() {
-		return this.editor.document.selection.getSelectedBlocks().next().value;
+		const schema = this.editor.document.schema;
+		const isAllowed = schema.check( {
+			name: this.modelElement,
+			inside: Position.createBefore( block )
+		} );
+
+		return isAllowed && !schema.objects.has( block.name );
 	}
 }
 

+ 8 - 4
packages/ckeditor5-heading/tests/heading.js

@@ -11,6 +11,7 @@ import HeadingEngine from '../src/headingengine';
 import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
 import { add } from '@ckeditor/ckeditor5-utils/src/translation-service';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 add( 'pl', {
 	'Choose heading': 'Wybierz nagłówek',
@@ -36,6 +37,9 @@ describe( 'Heading', () => {
 		.then( newEditor => {
 			editor = newEditor;
 			dropdown = editor.ui.componentFactory.create( 'headings' );
+
+			// Set data so the commands will be enabled.
+			setData( editor.document, '<paragraph>f{}oo</paragraph>' );
 		} );
 	} );
 
@@ -56,11 +60,11 @@ describe( 'Heading', () => {
 	describe( 'init()', () => {
 		it( 'should register options feature component', () => {
 			const dropdown = editor.ui.componentFactory.create( 'headings' );
-
+			//
 			expect( dropdown ).to.be.instanceOf( DropdownView );
-			expect( dropdown.buttonView.isEnabled ).to.be.false;
+			expect( dropdown.buttonView.isEnabled ).to.be.true;
 			expect( dropdown.buttonView.isOn ).to.be.undefined;
-			expect( dropdown.buttonView.label ).to.equal( 'Choose heading' );
+			expect( dropdown.buttonView.label ).to.equal( 'Paragraph' );
 			expect( dropdown.buttonView.tooltip ).to.equal( 'Heading' );
 		} );
 
@@ -236,7 +240,7 @@ describe( 'Heading', () => {
 			it( 'reflects the #value of the commands', () => {
 				const listView = dropdown.listView;
 
-				editor.commands.get( 'heading2' ).value = true;
+				setData( editor.document, '<heading2>f{}oo</heading2>' );
 
 				expect( listView.items.map( item => item.isActive ) ).to.deep.equal( [
 					false,

+ 6 - 2
packages/ckeditor5-heading/tests/headingcommand.js

@@ -80,12 +80,16 @@ describe( 'HeadingCommand', () => {
 				expect( commands[ modelElement ].value ).to.be.true;
 			} );
 
+			it( `equals false if inside to non-block element`, () => {
+				setData( document, `<notBlock>[foo]</notBlock>` );
+
+				expect( commands[ modelElement ].value ).to.be.false;
+			} );
+
 			it( `equals false if moved from ${ modelElement } to non-block element`, () => {
 				setData( document, `<${ modelElement }>[foo]</${ modelElement }><notBlock>foo</notBlock>` );
 				const element = document.getRoot().getChild( 1 );
 
-				expect( commands[ modelElement ].value ).to.be.true;
-
 				document.enqueueChanges( () => {
 					document.selection.setRanges( [ Range.createIn( element ) ] );
 				} );