1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { Plugin } from 'ckeditor5/src/core';
|
11 | import ImageUtils from '../imageutils';
|
12 | import ResizeImageCommand from './resizeimagecommand';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export default class ImageResizeEditing extends Plugin {
|
23 | |
24 |
|
25 |
|
26 | static get requires() {
|
27 | return [ ImageUtils ];
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 | static get pluginName() {
|
34 | return 'ImageResizeEditing';
|
35 | }
|
36 |
|
37 | |
38 |
|
39 |
|
40 | constructor( editor ) {
|
41 | super( editor );
|
42 |
|
43 | editor.config.define( 'image', {
|
44 | resizeUnit: '%',
|
45 | resizeOptions: [ {
|
46 | name: 'resizeImage:original',
|
47 | value: null,
|
48 | icon: 'original'
|
49 | },
|
50 | {
|
51 | name: 'resizeImage:25',
|
52 | value: '25',
|
53 | icon: 'small'
|
54 | },
|
55 | {
|
56 | name: 'resizeImage:50',
|
57 | value: '50',
|
58 | icon: 'medium'
|
59 | },
|
60 | {
|
61 | name: 'resizeImage:75',
|
62 | value: '75',
|
63 | icon: 'large'
|
64 | } ]
|
65 | } );
|
66 | }
|
67 |
|
68 | |
69 |
|
70 |
|
71 | init() {
|
72 | const editor = this.editor;
|
73 | const resizeImageCommand = new ResizeImageCommand( editor );
|
74 |
|
75 | this._registerSchema();
|
76 | this._registerConverters( 'imageBlock' );
|
77 | this._registerConverters( 'imageInline' );
|
78 |
|
79 |
|
80 | editor.commands.add( 'resizeImage', resizeImageCommand );
|
81 | editor.commands.add( 'imageResize', resizeImageCommand );
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 | _registerSchema() {
|
88 | if ( this.editor.plugins.has( 'ImageBlockEditing' ) ) {
|
89 | this.editor.model.schema.extend( 'imageBlock', { allowAttributes: 'width' } );
|
90 | }
|
91 |
|
92 | if ( this.editor.plugins.has( 'ImageInlineEditing' ) ) {
|
93 | this.editor.model.schema.extend( 'imageInline', { allowAttributes: 'width' } );
|
94 | }
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | _registerConverters( imageType ) {
|
104 | const editor = this.editor;
|
105 |
|
106 |
|
107 | editor.conversion.for( 'downcast' ).add( dispatcher =>
|
108 | dispatcher.on( `attribute:width:${ imageType }`, ( evt, data, conversionApi ) => {
|
109 | if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
|
110 | return;
|
111 | }
|
112 |
|
113 | const viewWriter = conversionApi.writer;
|
114 | const figure = conversionApi.mapper.toViewElement( data.item );
|
115 |
|
116 | if ( data.attributeNewValue !== null ) {
|
117 | viewWriter.setStyle( 'width', data.attributeNewValue, figure );
|
118 | viewWriter.addClass( 'image_resized', figure );
|
119 | } else {
|
120 | viewWriter.removeStyle( 'width', figure );
|
121 | viewWriter.removeClass( 'image_resized', figure );
|
122 | }
|
123 | } )
|
124 | );
|
125 |
|
126 | editor.conversion.for( 'upcast' )
|
127 | .attributeToAttribute( {
|
128 | view: {
|
129 | name: imageType === 'imageBlock' ? 'figure' : 'img',
|
130 | styles: {
|
131 | width: /.+/
|
132 | }
|
133 | },
|
134 | model: {
|
135 | key: 'width',
|
136 | value: viewElement => viewElement.getStyle( 'width' )
|
137 | }
|
138 | } );
|
139 | }
|
140 | }
|