Преглед на файлове

Fixed _updateValue and added _checkEnabled method to paragraph command.

Szymon Kupś преди 8 години
родител
ревизия
116c16319f
променени са 2 файла, в които са добавени 74 реда и са изтрити 7 реда
  1. 26 5
      packages/ckeditor5-paragraph/src/paragraphcommand.js
  2. 48 2
      packages/ckeditor5-paragraph/tests/paragraphcommand.js

+ 26 - 5
packages/ckeditor5-paragraph/src/paragraphcommand.js

@@ -34,7 +34,10 @@ export default class ParagraphCommand extends Command {
 		this.set( 'value', false );
 
 		// Update current value each time changes are done on document.
-		this.listenTo( editor.document, 'changesDone', () => this._updateValue() );
+		this.listenTo( editor.document, 'changesDone', () => {
+			this._updateValue();
+			this.refreshState();
+		} );
 	}
 
 	/**
@@ -69,10 +72,28 @@ export default class ParagraphCommand extends Command {
 	 * @private
 	 */
 	_updateValue() {
-		const block = this.editor.document.selection.getSelectedBlocks().next().value;
+		const block = this._getSelectedBlock();
+
+		this.value = !!block && block.is( 'paragraph' );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	_checkEnabled() {
+		const block = this._getSelectedBlock();
+		const schema = this.editor.document.schema;
 
-		if ( block ) {
-			this.value = block.is( 'paragraph' );
-		}
+		return !!block && schema.check( { name: 'paragraph', inside: block.parent.name } ) && !schema.objects.has( block.name );
+	}
+
+	/**
+	 * Returns currently selected block.
+	 *
+	 * @private
+	 * @returns {module:engine/model/element~Element}
+	 */
+	_getSelectedBlock() {
+		return this.editor.document.selection.getSelectedBlocks().next().value;
 	}
 }

+ 48 - 2
packages/ckeditor5-paragraph/tests/paragraphcommand.js

@@ -23,6 +23,15 @@ describe( 'ParagraphCommand', () => {
 			editor.commands.set( 'paragraph', command );
 			schema.registerItem( 'paragraph', '$block' );
 			schema.registerItem( 'heading1', '$block' );
+
+			schema.registerItem( 'notBlock' );
+			schema.allow( { name: 'notBlock', inside: '$root' } );
+			schema.allow( { name: '$text', inside: 'notBlock' } );
+
+			schema.registerItem( 'object' );
+			schema.allow( { name: 'object', inside: '$root' } );
+			schema.allow( { name: '$text', inside: 'object' } );
+			schema.objects.add( 'object' );
 		} );
 	} );
 
@@ -32,8 +41,6 @@ describe( 'ParagraphCommand', () => {
 
 	describe( 'value', () => {
 		it( 'has default value', () => {
-			setData( document, '' );
-
 			expect( command.value ).to.be.false;
 		} );
 
@@ -61,6 +68,19 @@ describe( 'ParagraphCommand', () => {
 			setData( document, '<paragraph>[bar</paragraph><heading1>foo]</heading1>' );
 			expect( command.value ).to.be.true;
 		} );
+
+		it( 'has proper value when moved to element that is not a block', () => {
+			setData( document, '<paragraph>[foo]</paragraph><notBlock>foo</notBlock>' );
+			const element = document.getRoot().getChild( 1 );
+
+			expect( command.value ).to.be.true;
+
+			document.enqueueChanges( () => {
+				document.selection.setRanges( [ Range.createIn( element ) ] );
+			} );
+
+			expect( command.value ).to.be.false;
+		} );
 	} );
 
 	describe( '_doExecute', () => {
@@ -147,4 +167,30 @@ describe( 'ParagraphCommand', () => {
 			} );
 		} );
 	} );
+
+	describe( 'isEnabled', () => {
+		it( 'should be enabled when inside another block', () => {
+			setData( document, '<heading1>f{}oo</heading1>' );
+
+			expect( command.isEnabled ).to.be.true;
+		} );
+
+		it( 'should be disabled if inside non-block', () => {
+			setData( document, '<notBlock>f{}oo</notBlock>' );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+
+		it( 'should be disabled if inside object', () => {
+			setData( document, '<object>f{}oo</object>' );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+
+		it( 'should be disabled if selection is placed on object', () => {
+			setData( document, '[<object>foo</object>]' );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+	} );
 } );