1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { Command } from 'ckeditor5/src/core';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export default class ResizeImageCommand extends Command {
|
18 | |
19 |
|
20 |
|
21 | refresh() {
|
22 | const editor = this.editor;
|
23 | const imageUtils = editor.plugins.get( 'ImageUtils' );
|
24 | const element = imageUtils.getClosestSelectedImageElement( editor.model.document.selection );
|
25 |
|
26 | this.isEnabled = !!element;
|
27 |
|
28 | if ( !element || !element.hasAttribute( 'width' ) ) {
|
29 | this.value = null;
|
30 | } else {
|
31 | this.value = {
|
32 | width: element.getAttribute( 'width' ),
|
33 | height: null
|
34 | };
|
35 | }
|
36 | }
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | execute( options ) {
|
52 | const editor = this.editor;
|
53 | const model = editor.model;
|
54 | const imageUtils = editor.plugins.get( 'ImageUtils' );
|
55 | const imageElement = imageUtils.getClosestSelectedImageElement( model.document.selection );
|
56 |
|
57 | this.value = {
|
58 | width: options.width,
|
59 | height: null
|
60 | };
|
61 |
|
62 | if ( imageElement ) {
|
63 | model.change( writer => {
|
64 | writer.setAttribute( 'width', options.width, imageElement );
|
65 | } );
|
66 | }
|
67 | }
|
68 | }
|