Browse Source

Disable image upload command when selection is on Object or inside other Object.

Maciej Gołaszewski 7 years ago
parent
commit
b127818126

+ 5 - 5
packages/ckeditor5-image/src/imageupload/imageuploadcommand.js

@@ -25,7 +25,7 @@ export default class ImageUploadCommand extends Command {
 		const selection = model.document.selection;
 		const schema = model.schema;
 
-		this.isEnabled = isImageAllowedInParent( selection, schema ) && checkSelectionWithImage( selection );
+		this.isEnabled = isImageAllowedInParent( selection, schema ) && checkSelectionWithObject( selection, schema );
 	}
 
 	/**
@@ -86,13 +86,13 @@ function isImageAllowedInParent( selection, schema ) {
 // Additional check for when the command should be disabled:
 // - selection is on image
 // - selection is inside image (image caption)
-function checkSelectionWithImage( selection ) {
+function checkSelectionWithObject( selection, schema ) {
 	const selectedElement = selection.getSelectedElement();
 
-	const isSelectionOnImage = !!selectedElement && selectedElement.is( 'image' );
-	const isSelectionInImage = !![ ...selection.focus.parent.getAncestors() ].find( ancestor => ancestor.name == 'image' );
+	const isSelectionOnObject = !!selectedElement && schema.isObject( selectedElement );
+	const isSelectionInObject = !![ ...selection.focus.getAncestors() ].find( ancestor => schema.isObject( ancestor ) );
 
-	return !isSelectionOnImage && !isSelectionInImage;
+	return !isSelectionOnObject && !isSelectionInObject;
 }
 
 // Returns a node that will be used to insert image with `model.insertContent` to check if image can be placed there.

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

@@ -100,6 +100,23 @@ describe( 'ImageUploadCommand', () => {
 			expect( command.isEnabled ).to.be.false;
 		} );
 
+		it( 'should be false when the selection is on other object', () => {
+			model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'object', view: 'object' } ) );
+			setModelData( model, '[<object></object>]' );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+
+		it( 'should be false when the selection is inside other object', () => {
+			model.schema.register( 'object', { isObject: true, allowIn: '$root' } );
+			model.schema.extend( '$text', { allowIn: 'object' } );
+			editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'object', view: 'object' } ) );
+			setModelData( model, '<object>[]</object>' );
+
+			expect( command.isEnabled ).to.be.false;
+		} );
+
 		it( 'should be false when schema disallows image', () => {
 			model.schema.register( 'block', { inheritAllFrom: '$block' } );
 			model.schema.extend( 'paragraph', { allowIn: 'block' } );