瀏覽代碼

Show warning if one of link or imageInsert commands is not available.

Maciej Gołaszewski 6 年之前
父節點
當前提交
b77e46da55

+ 28 - 3
packages/ckeditor5-ckfinder/src/ckfindercommand.js

@@ -41,8 +41,11 @@ export default class CKFinderCommand extends Command {
 		const imageCommand = this.editor.commands.get( 'imageInsert' );
 		const linkCommand = this.editor.commands.get( 'link' );
 
+		const canUseImageCommand = !!( imageCommand && imageCommand.isEnabled );
+		const canUseLinkCommand = !!( linkCommand && linkCommand.isEnabled );
+
 		// The CKFinder command is enabled when one of image or link command is enabled.
-		this.isEnabled = imageCommand && imageCommand.isEnabled || linkCommand && linkCommand.isEnabled;
+		this.isEnabled = canUseImageCommand || canUseLinkCommand;
 	}
 
 	/**
@@ -83,8 +86,18 @@ export default class CKFinderCommand extends Command {
 				const links = files.filter( file => !file.isImage() );
 				const images = files.filter( file => file.isImage() );
 
-				for ( const linkFile of links ) {
-					editor.execute( 'link', linkFile.getUrl() );
+				if ( !editor.commands.get( 'link' ) ) {
+					const notification = editor.plugins.get( 'Notification' );
+					const t = editor.locale.t;
+
+					notification.showWarning( t( 'The "link" command must be available to insert links.' ), {
+						title: t( 'Inserting link failed' ),
+						namespace: 'ckfinder'
+					} );
+				} else {
+					for ( const linkFile of links ) {
+						editor.execute( 'link', linkFile.getUrl() );
+					}
 				}
 
 				const imagesUrls = [];
@@ -126,6 +139,18 @@ export default class CKFinderCommand extends Command {
 function insertImages( editor, urls ) {
 	const imageCommand = editor.commands.get( 'imageInsert' );
 
+	if ( !imageCommand ) {
+		const notification = editor.plugins.get( 'Notification' );
+		const t = editor.locale.t;
+
+		notification.showWarning( t( 'The "imageInsert" command must be available to insert images.' ), {
+			title: t( 'Inserting image failed' ),
+			namespace: 'ckfinder'
+		} );
+
+		return;
+	}
+
 	// Check if inserting an image is actually possible - it might be possible to only insert a link.
 	if ( !imageCommand.isEnabled ) {
 		const notification = editor.plugins.get( 'Notification' );

+ 52 - 0
packages/ckeditor5-ckfinder/tests/ckfindercommand.js

@@ -128,6 +128,22 @@ describe( 'CKFinderCommand', () => {
 			command.refresh();
 			expect( command.isEnabled ).to.be.true;
 		} );
+
+		it( 'should be true when imageInsert command is not available', () => {
+			setModelData( model, '<paragraph>[]</paragraph>' );
+			const insertImage = editor.commands.get( 'imageInsert' );
+			editor.commands._commands.delete( 'link' );
+
+			insertImage.isEnabled = false;
+
+			command.refresh();
+			expect( command.isEnabled ).to.be.false;
+
+			insertImage.isEnabled = true;
+
+			command.refresh();
+			expect( command.isEnabled ).to.be.true;
+		} );
 	} );
 
 	describe( 'execute()', () => {
@@ -400,6 +416,42 @@ describe( 'CKFinderCommand', () => {
 				.to.equal( '<paragraph>f[o]o</paragraph>' );
 		} );
 
+		it( 'should show warning notification if link command is not available', done => {
+			// Remove link command.
+			editor.commands._commands.delete( 'link' );
+			const notification = editor.plugins.get( Notification );
+
+			notification.on( 'show:warning', ( evt, data ) => {
+				expect( data.message ).to.equal( 'The "link" command must be available to insert links.' );
+				expect( data.title ).to.equal( 'Inserting link failed' );
+				evt.stop();
+
+				done();
+			}, { priority: 'high' } );
+
+			command.execute();
+
+			mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.pdf', false ) ] );
+		} );
+
+		it( 'should show warning notification if link command is not available', done => {
+			// Remove link command.
+			editor.commands._commands.delete( 'imageInsert' );
+			const notification = editor.plugins.get( Notification );
+
+			notification.on( 'show:warning', ( evt, data ) => {
+				expect( data.message ).to.equal( 'The "imageInsert" command must be available to insert images.' );
+				expect( data.title ).to.equal( 'Inserting image failed' );
+				evt.stop();
+
+				done();
+			}, { priority: 'high' } );
+
+			command.execute();
+
+			mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.png', true ) ] );
+		} );
+
 		it( 'should not insert image nor crash when image could not be inserted', () => {
 			model.schema.register( 'other', {
 				allowIn: '$root',