This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
imageuploadcommand.js
87 lines (77 loc) · 2.49 KB
/
imageuploadcommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
import Command from '@ckeditor/ckeditor5-core/src/command';
import { insertImage, isImageAllowed } from '../image/utils';
/**
* @module image/imageupload/imageuploadcommand
*/
/**
* The image upload command.
*
* The command is registered by the {@link module:image/imageupload/imageuploadediting~ImageUploadEditing} plugin as `'imageUpload'`.
*
* In order to upload an image at the current selection position
* (according to the {@link module:widget/utils~findOptimalInsertionPosition} algorithm),
* execute the command and pass the native image file instance:
*
* this.listenTo( editor.editing.view.document, 'clipboardInput', ( evt, data ) => {
* // Assuming that only images were pasted:
* const images = Array.from( data.dataTransfer.files );
*
* // Upload the first image:
* editor.execute( 'imageUpload', { file: images[ 0 ] } );
* } );
*
* It is also possible to insert multiple images at once:
*
* editor.execute( 'imageUpload', {
* file: [
* file1,
* file2
* ]
* } );
*
* @extends module:core/command~Command
*/
export default class ImageUploadCommand extends Command {
/**
* @inheritDoc
*/
refresh() {
this.isEnabled = isImageAllowed( this.editor.model );
}
/**
* Executes the command.
*
* @fires execute
* @param {Object} options Options for the executed command.
* @param {File|Array.<File>} options.file The image file or an array of image files to upload.
*/
execute( options ) {
const editor = this.editor;
const model = editor.model;
const fileRepository = editor.plugins.get( FileRepository );
model.change( writer => {
const filesToUpload = Array.isArray( options.file ) ? options.file : [ options.file ];
for ( const file of filesToUpload ) {
uploadImage( writer, model, fileRepository, file );
}
} );
}
}
// Handles uploading single file.
//
// @param {module:engine/model/writer~writer} writer
// @param {module:engine/model/model~Model} model
// @param {File} file
function uploadImage( writer, model, fileRepository, file ) {
const loader = fileRepository.createLoader( file );
// Do not throw when upload adapter is not set. FileRepository will log an error anyway.
if ( !loader ) {
return;
}
insertImage( writer, model, { uploadId: loader.id } );
}