-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
imageinsertcommand.js
61 lines (55 loc) · 1.64 KB
/
imageinsertcommand.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import Command from '@ckeditor/ckeditor5-core/src/command';
import { insertImage, isImageAllowed } from './utils';
/**
* @module image/image/imageinsertcommand
*/
/**
* Insert image command.
*
* The command is registered by the {@link module:image/image/imageediting~ImageEditing} plugin as `'imageInsert'`.
*
* In order to insert an image at the current selection position
* (according to the {@link module:widget/utils~findOptimalInsertionPosition} algorithm),
* execute the command and specify the image source:
*
* editor.execute( 'imageInsert', { source: 'http://url.to.the/image' } );
*
* It is also possible to insert multiple images at once:
*
* editor.execute( 'imageInsert', {
* source: [
* 'path/to/image.jpg',
* 'path/to/other-image.jpg'
* ]
* } );
*
* @extends module:core/command~Command
*/
export default class ImageInsertCommand extends Command {
/**
* @inheritDoc
*/
refresh() {
this.isEnabled = isImageAllowed( this.editor.model );
}
/**
* Executes the command.
*
* @fires execute
* @param {Object} options Options for the executed command.
* @param {String|Array.<String>} options.source The image source or an array of image sources to insert.
*/
execute( options ) {
const model = this.editor.model;
model.change( writer => {
const sources = Array.isArray( options.source ) ? options.source : [ options.source ];
for ( const src of sources ) {
insertImage( writer, model, { src } );
}
} );
}
}