diff --git a/src/imageuploadcommand.js b/src/imageuploadcommand.js index 8ebbbc3..99acd97 100644 --- a/src/imageuploadcommand.js +++ b/src/imageuploadcommand.js @@ -41,8 +41,15 @@ export default class ImageUploadCommand extends Command { const fileRepository = editor.plugins.get( FileRepository ); doc.enqueueChanges( () => { + const loader = fileRepository.createLoader( file ); + + // Do not throw when upload adapter is not set. FileRepository will log an error anyway. + if ( !loader ) { + return; + } + const imageElement = new ModelElement( 'image', { - uploadId: fileRepository.createLoader( file ).id + uploadId: loader.id } ); let insertAtSelection; diff --git a/src/imageuploadengine.js b/src/imageuploadengine.js index e15280b..712a1ee 100644 --- a/src/imageuploadengine.js +++ b/src/imageuploadengine.js @@ -64,7 +64,7 @@ export default class ImageUploadEngine extends Plugin { } } ); - // Prevents from browser redirecting to drag-end-dropped image. + // Prevents from browser redirecting to the dropped image. editor.editing.view.on( 'dragover', ( evt, data ) => { data.preventDefault(); } ); diff --git a/tests/imageuploadcommand.js b/tests/imageuploadcommand.js index 027fcb0..251f420 100644 --- a/tests/imageuploadcommand.js +++ b/tests/imageuploadcommand.js @@ -17,9 +17,15 @@ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter'; import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position'; +import log from '@ckeditor/ckeditor5-utils/src/log'; + +import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; + describe( 'ImageUploadCommand', () => { let editor, command, doc, fileRepository; + testUtils.createSinonSandbox(); + class UploadAdapterPluginMock extends Plugin { init() { fileRepository = this.editor.plugins.get( FileRepository ); @@ -105,5 +111,22 @@ describe( 'ImageUploadCommand', () => { expect( getModelData( doc ) ).to.equal( '[]' ); } ); + + it( 'should not throw when upload adapter is not set (FileRepository will log an error anyway)', () => { + const file = createNativeFileMock(); + + fileRepository.createAdapter = undefined; + + const logStub = testUtils.sinon.stub( log, 'error' ); + + setModelData( doc, 'fo[]o' ); + + expect( () => { + command.execute( { file } ); + } ).to.not.throw(); + + expect( getModelData( doc ) ).to.equal( 'fo[]o' ); + expect( logStub.calledOnce ).to.be.true; + } ); } ); } );