-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
uploadimagecommand.ts
114 lines (101 loc) · 4.02 KB
/
uploadimagecommand.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import { FileRepository } from 'ckeditor5/src/upload.js';
import { Command } from 'ckeditor5/src/core.js';
import { toArray, type ArrayOrItem } from 'ckeditor5/src/utils.js';
import type { Position } from 'ckeditor5/src/engine.js';
import type ImageUtils from '../imageutils.js';
/**
* @module image/imageupload/uploadimagecommand
*/
/**
* The upload image command.
*
* The command is registered by the {@link module:image/imageupload/imageuploadediting~ImageUploadEditing} plugin as `uploadImage`
* and it is also available via aliased `imageUpload` name.
*
* In order to upload an image at the current selection position
* (according to the {@link module:widget/utils~findOptimalInsertionRange} algorithm),
* execute the command and pass the native image file instance:
*
* ```ts
* 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( 'uploadImage', { file: images[ 0 ] } );
* } );
* ```
*
* It is also possible to insert multiple images at once:
*
* ```ts
* editor.execute( 'uploadImage', {
* file: [
* file1,
* file2
* ]
* } );
* ```
*/
export default class UploadImageCommand extends Command {
/**
* @inheritDoc
*/
public override refresh(): void {
const editor = this.editor;
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );
const selectedElement = editor.model.document.selection.getSelectedElement()!;
// TODO: This needs refactoring.
this.isEnabled = imageUtils.isImageAllowed() || imageUtils.isImage( selectedElement );
}
/**
* Executes the command.
*
* @fires execute
* @param options Options for the executed command.
* @param options.file The image file or an array of image files to upload.
*/
public override execute( options: { file: ArrayOrItem<File> } ): void {
const files = toArray( options.file );
const selection = this.editor.model.document.selection;
const imageUtils: ImageUtils = this.editor.plugins.get( 'ImageUtils' );
// In case of multiple files, each file (starting from the 2nd) will be inserted at a position that
// follows the previous one. That will move the selection and, to stay on the safe side and make sure
// all images inherit the same selection attributes, they are collected beforehand.
//
// Applying these attributes ensures, for instance, that inserting an (inline) image into a link does
// not split that link but preserves its continuity.
//
// Note: Selection attributes that do not make sense for images will be filtered out by insertImage() anyway.
const selectionAttributes = Object.fromEntries( selection.getAttributes() );
files.forEach( ( file, index ) => {
const selectedElement = selection.getSelectedElement();
// Inserting of an inline image replace the selected element and make a selection on the inserted image.
// Therefore inserting multiple inline images requires creating position after each element.
if ( index && selectedElement && imageUtils.isImage( selectedElement ) ) {
const position = this.editor.model.createPositionAfter( selectedElement );
this._uploadImage( file, selectionAttributes, position );
} else {
this._uploadImage( file, selectionAttributes );
}
} );
}
/**
* Handles uploading single file.
*/
private _uploadImage( file: File, attributes: object, position?: Position ): void {
const editor = this.editor;
const fileRepository = editor.plugins.get( FileRepository );
const loader = fileRepository.createLoader( file );
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );
// Do not throw when upload adapter is not set. FileRepository will log an error anyway.
if ( !loader ) {
return;
}
imageUtils.insertImage( { ...attributes, uploadId: loader.id }, position );
}
}