/
imageresizecommand.js
58 lines (51 loc) · 1.41 KB
/
imageresizecommand.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
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/imageresize/imageresizecommand
*/
import Command from '@ckeditor/ckeditor5-core/src/command';
import { isImage } from '../image/utils';
/**
* The image resize command. Currently, it supports only the width attribute.
*
* @extends module:core/command~Command
*/
export default class ImageResizeCommand extends Command {
/**
* @inheritDoc
*/
refresh() {
const element = this.editor.model.document.selection.getSelectedElement();
this.isEnabled = isImage( element );
if ( !element || !element.hasAttribute( 'width' ) ) {
this.value = null;
} else {
this.value = {
width: element.getAttribute( 'width' ),
height: null
};
}
}
/**
* Executes the command.
*
* // Sets the width to 50%:
* editor.execute( 'imageResize', { width: '50%' } );
*
* // Removes the width attribute:
* editor.execute( 'imageResize', { width: null } );
*
* @param {Object} options
* @param {String|null} options.width The new width of the image.
* @fires execute
*/
execute( options ) {
const model = this.editor.model;
const imageElement = model.document.selection.getSelectedElement();
model.change( writer => {
writer.setAttribute( 'width', options.width, imageElement );
} );
}
}