8
0
Просмотр исходного кода

Fix: The ImageUploadCommand should check whether it can be executed in the selection context.

Aleksander Nowodzinski 7 лет назад
Родитель
Сommit
355326e116

+ 17 - 0
packages/ckeditor5-image/src/imageupload/imageuploadcommand.js

@@ -18,6 +18,23 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
  * @extends module:core/command~Command
  */
 export default class ImageUploadCommand extends Command {
+	/**
+	 * @inheritDoc
+	 */
+	refresh() {
+		const model = this.editor.model;
+		const selection = model.document.selection;
+		const schema = model.schema;
+		const position = selection.getFirstPosition();
+		let parent = position.parent;
+
+		if ( parent != parent.root ) {
+			parent = parent.parent;
+		}
+
+		this.isEnabled = schema.checkChild( parent, 'image' );
+	}
+
 	/**
 	 * Executes the command.
 	 *

+ 33 - 0
packages/ckeditor5-image/tests/imageupload/imageuploadcommand.js

@@ -56,6 +56,39 @@ describe( 'ImageUploadCommand', () => {
 		return editor.destroy();
 	} );
 
+	describe( 'isEnabled', () => {
+		it( 'should be true when the selection directly in the root', () => {
+			setModelData( model, '[]' );
+			expect( command.isEnabled ).to.be.true;
+		} );
+
+		it( 'should be true when the selection directly in a paragraph', () => {
+			setModelData( model, '<paragraph>foo[]</paragraph>' );
+			expect( command.isEnabled ).to.be.true;
+		} );
+
+		it( 'should be true when the selection directly in a block', () => {
+			model.schema.register( 'block', { inheritAllFrom: '$block' } );
+			model.schema.extend( '$text', { allowIn: 'block' } );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
+
+			setModelData( model, '<block>foo[]</block>' );
+			expect( command.isEnabled ).to.be.true;
+		} );
+
+		it( 'should be false when the selection in a limit element', () => {
+			model.schema.register( 'block', { inheritAllFrom: '$block' } );
+			model.schema.register( 'limit', { allowIn: 'block', isLimit: true } );
+			model.schema.extend( '$text', { allowIn: 'limit' } );
+
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'limit', view: 'limit' } ) );
+
+			setModelData( model, '<block><limit>foo[]</limit></block>' );
+			expect( command.isEnabled ).to.be.false;
+		} );
+	} );
+
 	describe( 'execute()', () => {
 		it( 'should insert image at selection position (includes deleting selected content)', () => {
 			const file = createNativeFileMock();