-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
imageresizebuttons.ts
352 lines (302 loc) · 10.3 KB
/
imageresizebuttons.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/imageresize/imageresizebuttons
*/
import { map } from 'lodash-es';
import { Plugin, icons, type Editor } from 'ckeditor5/src/core.js';
import {
ButtonView,
DropdownButtonView,
ViewModel,
createDropdown,
addListToDropdown,
type ListDropdownItemDefinition
} from 'ckeditor5/src/ui.js';
import { CKEditorError, Collection, type Locale } from 'ckeditor5/src/utils.js';
import ImageResizeEditing from './imageresizeediting.js';
import type ResizeImageCommand from './resizeimagecommand.js';
import type { ImageResizeOption } from '../imageconfig.js';
const RESIZE_ICONS = {
small: icons.objectSizeSmall,
medium: icons.objectSizeMedium,
large: icons.objectSizeLarge,
custom: icons.objectSizeCustom,
original: icons.objectSizeFull
};
/**
* The image resize buttons plugin.
*
* It adds a possibility to resize images using the toolbar dropdown or individual buttons, depending on the plugin configuration.
*/
export default class ImageResizeButtons extends Plugin {
/**
* @inheritDoc
*/
public static get requires() {
return [ ImageResizeEditing ] as const;
}
/**
* @inheritDoc
*/
public static get pluginName() {
return 'ImageResizeButtons' as const;
}
/**
* The resize unit.
* @default '%'
*/
private readonly _resizeUnit: 'px' | '%';
/**
* @inheritDoc
*/
constructor( editor: Editor ) {
super( editor );
this._resizeUnit = editor.config.get( 'image.resizeUnit' )!;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const options = editor.config.get( 'image.resizeOptions' )!;
const command: ResizeImageCommand = editor.commands.get( 'resizeImage' )!;
this.bind( 'isEnabled' ).to( command );
for ( const option of options ) {
this._registerImageResizeButton( option );
}
this._registerImageResizeDropdown( options );
}
/**
* A helper function that creates a standalone button component for the plugin.
*
* @param resizeOption A model of the resize option.
*/
private _registerImageResizeButton( option: ImageResizeOption ): void {
const editor = this.editor;
const { name, value, icon } = option;
editor.ui.componentFactory.add( name, locale => {
const button = new ButtonView( locale );
const command: ResizeImageCommand = editor.commands.get( 'resizeImage' )!;
const labelText = this._getOptionLabelValue( option, true );
if ( !RESIZE_ICONS[ icon as keyof typeof RESIZE_ICONS ] ) {
/**
* When configuring {@link module:image/imageconfig~ImageConfig#resizeOptions `config.image.resizeOptions`} for standalone
* buttons, a valid `icon` token must be set for each option.
*
* See all valid options described in the
* {@link module:image/imageconfig~ImageResizeOption plugin configuration}.
*
* @error imageresizebuttons-missing-icon
* @param option Invalid image resize option.
*/
throw new CKEditorError(
'imageresizebuttons-missing-icon',
editor,
option
);
}
button.set( {
// Use the `label` property for a verbose description (because of ARIA).
label: labelText,
icon: RESIZE_ICONS[ icon as keyof typeof RESIZE_ICONS ],
tooltip: labelText,
isToggleable: true
} );
// Bind button to the command.
button.bind( 'isEnabled' ).to( this );
if ( editor.plugins.has( 'ImageCustomResizeUI' ) && isCustomImageResizeOption( option ) ) {
const customResizeUI = editor.plugins.get( 'ImageCustomResizeUI' );
this.listenTo( button, 'execute', () => {
customResizeUI._showForm( this._resizeUnit );
} );
} else {
const optionValueWithUnit = value ? value + this._resizeUnit : null;
button.bind( 'isOn' ).to( command, 'value', getIsOnButtonCallback( optionValueWithUnit ) );
this.listenTo( button, 'execute', () => {
editor.execute( 'resizeImage', { width: optionValueWithUnit } );
} );
}
return button;
} );
}
/**
* A helper function that creates a dropdown component for the plugin containing all the resize options defined in
* the editor configuration.
*
* @param options An array of configured options.
*/
private _registerImageResizeDropdown( options: Array<ImageResizeOption> ): void {
const editor = this.editor;
const t = editor.t;
const originalSizeOption = options.find( option => !option.value )!;
const componentCreator = ( locale: Locale ) => {
const command: ResizeImageCommand = editor.commands.get( 'resizeImage' )!;
const dropdownView = createDropdown( locale, DropdownButtonView );
const dropdownButton: typeof dropdownView.buttonView & { commandValue?: string | null } = dropdownView.buttonView;
const accessibleLabel = t( 'Resize image' );
dropdownButton.set( {
tooltip: accessibleLabel,
commandValue: originalSizeOption.value,
icon: RESIZE_ICONS.medium,
isToggleable: true,
label: this._getOptionLabelValue( originalSizeOption ),
withText: true,
class: 'ck-resize-image-button',
ariaLabel: accessibleLabel,
ariaLabelledBy: undefined
} );
dropdownButton.bind( 'label' ).to( command, 'value', commandValue => {
if ( commandValue && commandValue.width ) {
return commandValue.width;
} else {
return this._getOptionLabelValue( originalSizeOption );
}
} );
dropdownView.bind( 'isEnabled' ).to( this );
addListToDropdown( dropdownView, () => this._getResizeDropdownListItemDefinitions( options, command ), {
ariaLabel: t( 'Image resize list' ),
role: 'menu'
} );
// Execute command when an item from the dropdown is selected.
this.listenTo( dropdownView, 'execute', evt => {
if ( 'onClick' in evt.source ) {
( evt.source as any ).onClick();
} else {
editor.execute( ( evt.source as any ).commandName, { width: ( evt.source as any ).commandValue } );
editor.editing.view.focus();
}
} );
return dropdownView;
};
// Register `resizeImage` dropdown and add `imageResize` dropdown as an alias for backward compatibility.
editor.ui.componentFactory.add( 'resizeImage', componentCreator );
editor.ui.componentFactory.add( 'imageResize', componentCreator );
}
/**
* A helper function for creating an option label value string.
*
* @param option A resize option object.
* @param forTooltip An optional flag for creating a tooltip label.
* @returns A user-defined label combined from the numeric value and the resize unit or the default label
* for reset options (`Original`).
*/
private _getOptionLabelValue( option: ImageResizeOption, forTooltip: boolean = false ): string {
const t = this.editor.t;
if ( option.label ) {
return option.label;
} else if ( forTooltip ) {
if ( isCustomImageResizeOption( option ) ) {
return t( 'Custom image size' );
} else if ( option.value ) {
return t( 'Resize image to %0', option.value + this._resizeUnit );
} else {
return t( 'Resize image to the original size' );
}
} else {
if ( isCustomImageResizeOption( option ) ) {
return t( 'Custom' );
} else if ( option.value ) {
return option.value + this._resizeUnit;
} else {
return t( 'Original' );
}
}
}
/**
* A helper function that parses the resize options and returns list item definitions ready for use in the dropdown.
*
* @param options The resize options.
* @param command The resize image command.
* @returns Dropdown item definitions.
*/
private _getResizeDropdownListItemDefinitions(
options: Array<ImageResizeOption>,
command: ResizeImageCommand
): Collection<ListDropdownItemDefinition> {
const { editor } = this;
const itemDefinitions = new Collection<ListDropdownItemDefinition>();
const optionsWithSerializedValues = options.map( option => {
if ( isCustomImageResizeOption( option ) ) {
return {
...option,
valueWithUnits: 'custom'
};
}
if ( !option.value ) {
return {
...option,
valueWithUnits: null
};
}
return {
...option,
valueWithUnits: `${ option.value }${ this._resizeUnit }`
};
} );
for ( const option of optionsWithSerializedValues ) {
let definition: ListDropdownItemDefinition | null = null;
if ( editor.plugins.has( 'ImageCustomResizeUI' ) && isCustomImageResizeOption( option ) ) {
const customResizeUI = editor.plugins.get( 'ImageCustomResizeUI' );
definition = {
type: 'button',
model: new ViewModel( {
label: this._getOptionLabelValue( option ),
role: 'menuitemradio',
withText: true,
icon: null,
onClick: () => {
customResizeUI._showForm( this._resizeUnit );
}
} )
};
const allDropdownValues = map( optionsWithSerializedValues, 'valueWithUnits' );
definition.model.bind( 'isOn' ).to( command, 'value', getIsOnCustomButtonCallback( allDropdownValues ) );
} else {
definition = {
type: 'button',
model: new ViewModel( {
commandName: 'resizeImage',
commandValue: option.valueWithUnits,
label: this._getOptionLabelValue( option ),
role: 'menuitemradio',
withText: true,
icon: null
} )
};
definition.model.bind( 'isOn' ).to( command, 'value', getIsOnButtonCallback( option.valueWithUnits ) );
}
definition.model.bind( 'isEnabled' ).to( command, 'isEnabled' );
itemDefinitions.add( definition );
}
return itemDefinitions;
}
}
/**
* A helper that checks if provided option triggers custom resize balloon.
*/
function isCustomImageResizeOption( option: ImageResizeOption ) {
return option.value === 'custom';
}
/**
* A helper function for setting the `isOn` state of buttons in value bindings.
*/
function getIsOnButtonCallback( value: string | null ): ( commandValue: unknown ) => boolean {
return ( commandValue: unknown ): boolean => {
const objectCommandValue = commandValue as null | { width: string | null };
if ( value === null && objectCommandValue === value ) {
return true;
}
return objectCommandValue !== null && objectCommandValue.width === value;
};
}
/**
* A helper function for setting the `isOn` state of custom size button in value bindings.
*/
function getIsOnCustomButtonCallback( allDropdownValues: Array<string | null> ): ( commandValue: unknown ) => boolean {
return ( commandValue: unknown ): boolean => !allDropdownValues.some(
dropdownValue => getIsOnButtonCallback( dropdownValue )( commandValue )
);
}