Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Support for uploading images with base64 source #248

Merged
merged 12 commits into from
Nov 27, 2018
128 changes: 100 additions & 28 deletions src/imageupload/imageuploadediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
import { upcastAttributeToAttribute } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';

import ImageUploadCommand from '../../src/imageupload/imageuploadcommand';
import { isImageType } from '../../src/imageupload/utils';
import { isImageType, isLocalImage, wrapImageToFetch } from '../../src/imageupload/utils';

/**
* The editing part of the image upload feature. It registers the `'imageUpload'` command.
Expand All @@ -34,6 +35,7 @@ export default class ImageUploadEditing extends Plugin {
const editor = this.editor;
const doc = editor.model.document;
const schema = editor.model.schema;
const conversion = editor.conversion;
const fileRepository = editor.plugins.get( FileRepository );

// Setup schema to allow uploadId and uploadStatus for images.
Expand All @@ -44,6 +46,16 @@ export default class ImageUploadEditing extends Plugin {
// Register imageUpload command.
editor.commands.add( 'imageUpload', new ImageUploadCommand( editor ) );

// Register upcast converter for uploadId.
conversion.for( 'upcast' )
.add( upcastAttributeToAttribute( {
view: {
name: 'img',
key: 'uploadId'
},
model: 'uploadId'
} ) );

// Handle pasted images.
// 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
Expand Down Expand Up @@ -81,6 +93,54 @@ export default class ImageUploadEditing extends Plugin {
} );
} );

// Handle HTML pasted with images with base64 or blob sources.
// 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 view = editor.editing.view;

const fetchableImages = Array.from( view.createRangeIn( data.content ) )
.filter( value => isLocalImage( value.item ) && !value.item.getAttribute( 'uploadProcessed' ) )
.map( ( value, index ) => wrapImageToFetch( value.item, index ) );

if ( !fetchableImages.length ) {
return;
}

evt.stop();

Promise.all( fetchableImages ).then( items => {
for ( const item of items ) {
if ( !item.file ) {
// Failed to fetch image or create a file instance, remove image element.
view.change( writer => {
f1ames marked this conversation as resolved.
Show resolved Hide resolved
writer.remove( item.image );
} );
} else {
const loader = fileRepository.createLoader( item.file );

if ( loader ) {
view.change( writer => {
writer.setAttribute( 'src', '', item.image );
writer.setAttribute( 'uploadId', loader.id, item.image );
} );
} else {
view.change( writer => {
// Set attribute so the image will not be processed 2nd time.
writer.setAttribute( 'uploadProcessed', true, item.image );
f1ames marked this conversation as resolved.
Show resolved Hide resolved
} );
}
}
}

editor.plugins.get( 'Clipboard' ).fire( 'inputTransformation', {
content: data.content,
dataTransfer: data.dataTransfer
} );
} );
} );

// Prevents from the browser redirecting to the dropped image.
editor.editing.view.document.on( 'dragover', ( evt, data ) => {
data.preventDefault();
Expand Down Expand Up @@ -163,33 +223,7 @@ export default class ImageUploadEditing extends Plugin {
.then( data => {
model.enqueueChange( 'transparent', writer => {
writer.setAttributes( { uploadStatus: 'complete', src: data.default }, imageElement );

// Srcset attribute for responsive images support.
let maxWidth = 0;
const srcsetAttribute = Object.keys( data )
// Filter out keys that are not integers.
.filter( key => {
const width = parseInt( key, 10 );

if ( !isNaN( width ) ) {
maxWidth = Math.max( maxWidth, width );

return true;
}
} )

// Convert each key to srcset entry.
.map( key => `${ data[ key ] } ${ key }w` )

// Join all entries.
.join( ', ' );

if ( srcsetAttribute != '' ) {
writer.setAttribute( 'srcset', {
data: srcsetAttribute,
width: maxWidth
}, imageElement );
}
this._parseAndSetSrcsetAttributeOnImage( data, imageElement, writer );
} );

clean();
Expand Down Expand Up @@ -226,6 +260,44 @@ export default class ImageUploadEditing extends Plugin {
fileRepository.destroyLoader( loader );
}
}

/**
* Creates `srcset` attribute based on a given file upload response and sets it as an attribute to a specific image element.
*
* @protected
* @param {Object} data Data object from which `srcset` will be created.
* @param {module:engine/model/element~Element} image The image element on which `srcset` attribute will be set.
* @param {module:engine/model/writer~Writer} writer
*/
_parseAndSetSrcsetAttributeOnImage( data, image, writer ) {
// Srcset attribute for responsive images support.
let maxWidth = 0;

const srcsetAttribute = Object.keys( data )
// Filter out keys that are not integers.
.filter( key => {
const width = parseInt( key, 10 );

if ( !isNaN( width ) ) {
maxWidth = Math.max( maxWidth, width );

return true;
}
} )

// Convert each key to srcset entry.
.map( key => `${ data[ key ] } ${ key }w` )

// Join all entries.
.join( ', ' );

if ( srcsetAttribute != '' ) {
writer.setAttribute( 'srcset', {
data: srcsetAttribute,
width: maxWidth
}, image );
}
}
}

// Returns `true` if non-empty `text/html` is included in the data transfer.
Expand Down
77 changes: 77 additions & 0 deletions src/imageupload/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @module image/imageupload/utils
*/

/* global fetch, File */

/**
* Checks if a given file is an image.
*
Expand All @@ -18,3 +20,78 @@ export function isImageType( file ) {

return types.test( file.type );
}

/**
* Creates a promise which fetches the image local source (base64 or blob) and returns as a `File` object.
*
* @param {module:engine/view/element~Element} image Image which source to fetch.
* @param {Number} index Image index used as image name suffix.
* @returns {Promise} A promise which resolves when image source is fetched and converted to `File` instance.
* It resolves with object holding initial image element (as `image`) and its file source (as `file`). If
* the `file` attribute is null, it means fetching failed.
*/
export function wrapImageToFetch( image, index ) {
f1ames marked this conversation as resolved.
Show resolved Hide resolved
return new Promise( resolve => {
// Fetch works asynchronously and so does not block browser UI when processing data.
fetch( image.getAttribute( 'src' ) )
.then( resource => resource.blob() )
.then( blob => {
const mimeType = getImageMimeType( blob, image.getAttribute( 'src' ) );
const ext = mimeType.replace( 'image/', '' );
const filename = `${ Number( new Date() ) }-image${ index }.${ ext }`;
f1ames marked this conversation as resolved.
Show resolved Hide resolved
const file = createFileFromBlob( blob, filename, mimeType );

resolve( { image, file } );
} )
.catch( () => {
// We always resolve a promise so `Promise.all` will not reject if one of many fetch fails.
resolve( { image, file: null } );
} );
} );
}

/**
* Checks whether given node is an image element with local source (base64 or blob).
*
* @param {module:engine/view/node~Node} node Node to check.
* @returns {Boolean}
*/
export function isLocalImage( node ) {
return node.is( 'element', 'img' ) && node.getAttribute( 'src' ) &&
( node.getAttribute( 'src' ).match( /data:image\/\w+;base64,/g ) ||
node.getAttribute( 'src' ).match( /blob:/g ) );
f1ames marked this conversation as resolved.
Show resolved Hide resolved
}

// Extracts image type based on its blob representation or its source.
//
// @param {String} src Image src attribute value.
// @param {Blob} blob Image blob representation.
// @returns {String}
function getImageMimeType( blob, src ) {
if ( blob.type ) {
return blob.type;
} else if ( src.match( /data:(image\/\w+);base64/ ) ) {
return src.match( /data:(image\/\w+);base64/ )[ 1 ].toLowerCase();
} else {
// Fallback to 'jpeg' as common extension.
return 'image/jpeg';
}
}

// Creates `File` instance from the given `Blob` instance using specified filename.
//
// @param {Blob} blob The `Blob` instance from which file will be created.
// @param {String} filename Filename used during file creation.
// @param {String} mimeType File mime type.
// @returns {File|null} The `File` instance created from the given blob or `null` if `File API` is not available.
function createFileFromBlob( blob, filename, mimeType ) {
try {
return new File( [ blob ], filename, { type: mimeType } );
} catch ( err ) {
// Edge does not support `File` constructor ATM, see https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9551546/.
// However, the `File` function is present (so cannot be checked with `!window.File` or `typeof File === 'function'`), but
// calling it with `new File( ... )` throws an error. This try-catch prevents that. Also when the function will
// be implemented correctly in Edge the code will start working without any changes (see #247).
return null;
}
}
3 changes: 2 additions & 1 deletion tests/imageupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/* globals document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
import Image from '../src/image';
import ImageUpload from '../src/imageupload';
import ImageUploadEditing from '../src/imageupload/imageuploadediting';
Expand All @@ -23,7 +24,7 @@ describe( 'ImageUpload', () => {

return ClassicEditor
.create( editorElement, {
plugins: [ Image, ImageUpload, UploadAdapterPluginMock ]
plugins: [ Image, ImageUpload, UploadAdapterPluginMock, Clipboard ]
} )
.then( newEditor => {
editor = newEditor;
Expand Down
Loading