Browse Source

Align code to the latest changes in editor.plugin API.

Maciej Gołaszewski 7 years ago
parent
commit
861916834d

+ 28 - 26
packages/ckeditor5-image/src/imageupload/imageuploadediting.js

@@ -98,40 +98,42 @@ export default class ImageUploadEditing extends Plugin {
 		// For every image file, a new file loader is created and a placeholder image is
 		// inserted into the content. Then, those images are uploaded once they appear in the model
 		// (see Document#change listener below).
-		this.listenTo( editor.plugins.get( 'Clipboard' ), 'inputTransformation', ( evt, data ) => {
-			const fetchableImages = Array.from( editor.editing.view.createRangeIn( data.content ) )
-				.filter( value => isLocalImage( value.item ) && !value.item.getAttribute( 'uploadProcessed' ) )
-				.map( value => fetchLocalImage( value.item ) );
-
-			if ( !fetchableImages.length ) {
-				return;
-			}
+		if ( editor.plugins.has( 'Clipboard' ) ) {
+			this.listenTo( editor.plugins.get( 'Clipboard' ), 'inputTransformation', ( evt, data ) => {
+				const fetchableImages = Array.from( editor.editing.view.createRangeIn( data.content ) )
+					.filter( value => isLocalImage( value.item ) && !value.item.getAttribute( 'uploadProcessed' ) )
+					.map( value => fetchLocalImage( value.item ) );
+
+				if ( !fetchableImages.length ) {
+					return;
+				}
 
-			evt.stop();
+				evt.stop();
 
-			Promise.all( fetchableImages ).then( items => {
-				const writer = new UpcastWriter();
+				Promise.all( fetchableImages ).then( items => {
+					const writer = new UpcastWriter();
 
-				for ( const item of items ) {
-					if ( !item.file ) {
-						// Failed to fetch image or create a file instance, remove image element.
-						writer.remove( item.image );
-					} else {
-						// Set attribute marking the image as processed.
-						writer.setAttribute( 'uploadProcessed', true, item.image );
+					for ( const item of items ) {
+						if ( !item.file ) {
+							// Failed to fetch image or create a file instance, remove image element.
+							writer.remove( item.image );
+						} else {
+							// Set attribute marking the image as processed.
+							writer.setAttribute( 'uploadProcessed', true, item.image );
 
-						const loader = fileRepository.createLoader( item.file );
+							const loader = fileRepository.createLoader( item.file );
 
-						if ( loader ) {
-							writer.setAttribute( 'src', '', item.image );
-							writer.setAttribute( 'uploadId', loader.id, item.image );
+							if ( loader ) {
+								writer.setAttribute( 'src', '', item.image );
+								writer.setAttribute( 'uploadId', loader.id, item.image );
+							}
 						}
 					}
-				}
 
-				editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', data );
+					editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', data );
+				} );
 			} );
-		} );
+		}
 
 		// Prevents from the browser redirecting to the dropped image.
 		editor.editing.view.document.on( 'dragover', ( evt, data ) => {
@@ -266,7 +268,7 @@ export default class ImageUploadEditing extends Plugin {
 		let maxWidth = 0;
 
 		const srcsetAttribute = Object.keys( data )
-			// Filter out keys that are not integers.
+		// Filter out keys that are not integers.
 			.filter( key => {
 				const width = parseInt( key, 10 );
 

+ 7 - 0
packages/ckeditor5-image/tests/imageupload/imageuploadediting.js

@@ -97,6 +97,13 @@ describe( 'ImageUploadEditing', () => {
 		expect( editor.commands.get( 'imageUpload' ) ).to.be.instanceOf( ImageUploadCommand );
 	} );
 
+	it( 'should not crash when Clipboard plugin is not available', () => {
+		return VirtualTestEditor
+			.create( {
+				plugins: [ ImageEditing, ImageUploadEditing, Paragraph, UndoEditing, UploadAdapterPluginMock ]
+			} );
+	} );
+
 	it( 'should insert image when is pasted', () => {
 		const fileMock = createNativeFileMock();
 		const dataTransfer = new DataTransfer( { files: [ fileMock ], types: [ 'Files' ] } );