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

Commit

Permalink
Merge pull request #248 from ckeditor/t/246
Browse files Browse the repository at this point in the history
Feature: Support for uploading images with base64 source. Closes #246. Closes ckeditor/ckeditor5-paste-from-office#24.
  • Loading branch information
Reinmar authored Nov 27, 2018
2 parents cc1e7a3 + cd2a8fd commit 89ab27e
Show file tree
Hide file tree
Showing 6 changed files with 498 additions and 41 deletions.
123 changes: 95 additions & 28 deletions src/imageupload/imageuploadediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
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 UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
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, fetchLocalImage } from '../../src/imageupload/utils';

/**
* The editing part of the image upload feature. It registers the `'imageUpload'` command.
Expand All @@ -34,6 +36,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 +47,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 +94,48 @@ 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 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();

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 );

const loader = fileRepository.createLoader( item.file );

if ( loader ) {
writer.setAttribute( 'src', '', item.image );
writer.setAttribute( 'uploadId', loader.id, item.image );
}
}
}

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 +218,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 +255,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
79 changes: 79 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,80 @@ 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.
* @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 fetchLocalImage( image ) {
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 = `image.${ ext }`;
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 ) {
if ( !node.is( 'element', 'img' ) || !node.getAttribute( 'src' ) ) {
return false;
}

return node.getAttribute( 'src' ).match( /^data:image\/\w+;base64,/g ) ||
node.getAttribute( 'src' ).match( /^blob:/g );
}

// 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

0 comments on commit 89ab27e

Please sign in to comment.