1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { Plugin, icons } from 'ckeditor5/src/core';
|
11 | import { ButtonView, DropdownButtonView, Model, createDropdown, addListToDropdown } from 'ckeditor5/src/ui';
|
12 | import { CKEditorError, Collection } from 'ckeditor5/src/utils';
|
13 |
|
14 | import ImageResizeEditing from './imageresizeediting';
|
15 |
|
16 | const RESIZE_ICONS = {
|
17 | small: icons.objectSizeSmall,
|
18 | medium: icons.objectSizeMedium,
|
19 | large: icons.objectSizeLarge,
|
20 | original: icons.objectSizeFull
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export default class ImageResizeButtons extends Plugin {
|
31 | |
32 |
|
33 |
|
34 | static get requires() {
|
35 | return [ ImageResizeEditing ];
|
36 | }
|
37 |
|
38 | |
39 |
|
40 |
|
41 | static get pluginName() {
|
42 | return 'ImageResizeButtons';
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 | constructor( editor ) {
|
49 | super( editor );
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | this._resizeUnit = editor.config.get( 'image.resizeUnit' );
|
60 | }
|
61 |
|
62 | |
63 |
|
64 |
|
65 | init() {
|
66 | const editor = this.editor;
|
67 | const options = editor.config.get( 'image.resizeOptions' );
|
68 | const command = editor.commands.get( 'resizeImage' );
|
69 |
|
70 | this.bind( 'isEnabled' ).to( command );
|
71 |
|
72 | for ( const option of options ) {
|
73 | this._registerImageResizeButton( option );
|
74 | }
|
75 |
|
76 | this._registerImageResizeDropdown( options );
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | _registerImageResizeButton( option ) {
|
86 | const editor = this.editor;
|
87 | const { name, value, icon } = option;
|
88 | const optionValueWithUnit = value ? value + this._resizeUnit : null;
|
89 |
|
90 | editor.ui.componentFactory.add( name, locale => {
|
91 | const button = new ButtonView( locale );
|
92 | const command = editor.commands.get( 'resizeImage' );
|
93 | const labelText = this._getOptionLabelValue( option, true );
|
94 |
|
95 | if ( !RESIZE_ICONS[ icon ] ) {
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | throw new CKEditorError(
|
107 | 'imageresizebuttons-missing-icon',
|
108 | editor,
|
109 | option
|
110 | );
|
111 | }
|
112 |
|
113 | button.set( {
|
114 |
|
115 | label: labelText,
|
116 | icon: RESIZE_ICONS[ icon ],
|
117 | tooltip: labelText,
|
118 | isToggleable: true
|
119 | } );
|
120 |
|
121 |
|
122 | button.bind( 'isEnabled' ).to( this );
|
123 | button.bind( 'isOn' ).to( command, 'value', getIsOnButtonCallback( optionValueWithUnit ) );
|
124 |
|
125 | this.listenTo( button, 'execute', () => {
|
126 | editor.execute( 'resizeImage', { width: optionValueWithUnit } );
|
127 | } );
|
128 |
|
129 | return button;
|
130 | } );
|
131 | }
|
132 |
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | _registerImageResizeDropdown( options ) {
|
141 | const editor = this.editor;
|
142 | const t = editor.t;
|
143 | const originalSizeOption = options.find( option => !option.value );
|
144 |
|
145 | const componentCreator = locale => {
|
146 | const command = editor.commands.get( 'resizeImage' );
|
147 | const dropdownView = createDropdown( locale, DropdownButtonView );
|
148 | const dropdownButton = dropdownView.buttonView;
|
149 |
|
150 | dropdownButton.set( {
|
151 | tooltip: t( 'Resize image' ),
|
152 | commandValue: originalSizeOption.value,
|
153 | icon: RESIZE_ICONS.medium,
|
154 | isToggleable: true,
|
155 | label: this._getOptionLabelValue( originalSizeOption ),
|
156 | withText: true,
|
157 | class: 'ck-resize-image-button'
|
158 | } );
|
159 |
|
160 | dropdownButton.bind( 'label' ).to( command, 'value', commandValue => {
|
161 | if ( commandValue && commandValue.width ) {
|
162 | return commandValue.width;
|
163 | } else {
|
164 | return this._getOptionLabelValue( originalSizeOption );
|
165 | }
|
166 | } );
|
167 | dropdownView.bind( 'isOn' ).to( command );
|
168 | dropdownView.bind( 'isEnabled' ).to( this );
|
169 |
|
170 | addListToDropdown( dropdownView, this._getResizeDropdownListItemDefinitions( options, command ) );
|
171 |
|
172 | dropdownView.listView.ariaLabel = t( 'Image resize list' );
|
173 |
|
174 |
|
175 | this.listenTo( dropdownView, 'execute', evt => {
|
176 | editor.execute( evt.source.commandName, { width: evt.source.commandValue } );
|
177 | editor.editing.view.focus();
|
178 | } );
|
179 |
|
180 | return dropdownView;
|
181 | };
|
182 |
|
183 |
|
184 | editor.ui.componentFactory.add( 'resizeImage', componentCreator );
|
185 | editor.ui.componentFactory.add( 'imageResize', componentCreator );
|
186 | }
|
187 |
|
188 | |
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | _getOptionLabelValue( option, forTooltip ) {
|
198 | const t = this.editor.t;
|
199 |
|
200 | if ( option.label ) {
|
201 | return option.label;
|
202 | } else if ( forTooltip ) {
|
203 | if ( option.value ) {
|
204 | return t( 'Resize image to %0', option.value + this._resizeUnit );
|
205 | } else {
|
206 | return t( 'Resize image to the original size' );
|
207 | }
|
208 | } else {
|
209 | if ( option.value ) {
|
210 | return option.value + this._resizeUnit;
|
211 | } else {
|
212 | return t( 'Original' );
|
213 | }
|
214 | }
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | _getResizeDropdownListItemDefinitions( options, command ) {
|
226 | const itemDefinitions = new Collection();
|
227 |
|
228 | options.map( option => {
|
229 | const optionValueWithUnit = option.value ? option.value + this._resizeUnit : null;
|
230 | const definition = {
|
231 | type: 'button',
|
232 | model: new Model( {
|
233 | commandName: 'resizeImage',
|
234 | commandValue: optionValueWithUnit,
|
235 | label: this._getOptionLabelValue( option ),
|
236 | withText: true,
|
237 | icon: null
|
238 | } )
|
239 | };
|
240 |
|
241 | definition.model.bind( 'isOn' ).to( command, 'value', getIsOnButtonCallback( optionValueWithUnit ) );
|
242 |
|
243 | itemDefinitions.add( definition );
|
244 | } );
|
245 |
|
246 | return itemDefinitions;
|
247 | }
|
248 | }
|
249 |
|
250 |
|
251 | function getIsOnButtonCallback( value ) {
|
252 | return commandValue => {
|
253 | if ( value === null && commandValue === value ) {
|
254 | return true;
|
255 | }
|
256 |
|
257 | return commandValue && commandValue.width === value;
|
258 | };
|
259 | }
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|