/
imagestyle.js
284 lines (276 loc) · 11.4 KB
/
imagestyle.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
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
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module image/imagestyle
*/
import { Plugin } from 'ckeditor5/src/core';
import ImageStyleEditing from './imagestyle/imagestyleediting';
import ImageStyleUI from './imagestyle/imagestyleui';
/**
* The image style plugin.
*
* For a detailed overview of the image styles feature, check the {@glink features/images/images-styles documentation}.
*
* This is a "glue" plugin which loads the following plugins:
* * {@link module:image/imagestyle/imagestyleediting~ImageStyleEditing},
* * {@link module:image/imagestyle/imagestyleui~ImageStyleUI}
*
* It provides a default configuration, which can be extended or overwritten.
* Read more about the {@link module:image/image~ImageConfig#styles image styles configuration}.
*
* @extends module:core/plugin~Plugin
*/
export default class ImageStyle extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ ImageStyleEditing, ImageStyleUI ];
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'ImageStyle';
}
}
/**
* The configuration for the {@link module:image/imagestyle~ImageStyle} plugin that should be provided
* while creating the editor instance.
*
* A detailed information about the default configuration and customization can be found in
* {@link module:image/image~ImageConfig#styles `ImageConfig#styles`}.
*
* @interface ImageStyleConfig
*/
/**
* A list of the image style options.
*
* @member {Array.<module:image/imagestyle~ImageStyleOptionDefinition>} module:image/imagestyle~ImageStyleConfig#options
*/
/**
* The {@link module:image/imagestyle `ImageStyle`} plugin requires a list of the
* {@link module:image/imagestyle~ImageStyleConfig#options image style options} to work properly.
* The default configuration is provided (listed below) and can be customized while creating the editor instance.
*
* # **Command**
*
* The {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand `imageStyleCommand`}
* is configured based on the defined options,
* so you can change the style of the selected image by executing the following command:
*
* editor.execute( 'imageStyle' { value: 'alignLeft' } );
*
* # **Buttons**
*
* All of the image style options provided in the configuration are registered
* in the {@link module:ui/componentfactory~ComponentFactory UI components factory} with the "imageStyle:" prefixes and can be used
* in the {@link module:image/image~ImageConfig#toolbar image toolbar configuration}. The buttons available by default depending
* on the loaded plugins are listed in the next section.
*
* Read more about styling images in the {@glink features/images/images-styles Image styles guide}.
*
* # **Default options and buttons**
*
* If the custom configuration is not provided, the default configuration will be used depending on the loaded
* image editing plugins.
*
* * If both {@link module:image/image/imageblockediting~ImageBlockEditing `ImageBlockEditing`} and
* {@link module:image/image/imageinlineediting~ImageInlineEditing `ImageInlineEditing`} plugins are loaded
* (which is usually the default editor configuration), the following options will be available for the toolbar
* configuration. These options will be registered as the buttons with the "imageStyle:" prefixes.
*
* const imageDefaultConfig = {
* styles: {
* options: [
* 'inline', 'alignLeft', 'alignRight',
* 'alignCenter', 'alignBlockLeft', 'alignBlockRight',
* 'block', 'side'
* ]
* }
* };
*
* * If only the {@link module:image/image/imageblockediting~ImageBlockEditing `ImageBlockEditing`} plugin is loaded,
* the following buttons (options) and groups will be available for the toolbar configuration.
* These options will be registered as the buttons with the "imageStyle:" prefixes.
*
* const imageDefaultConfig = {
* styles: {
* options: [ 'block', 'side' ]
* }
* };
*
* * If only the {@link module:image/image/imageinlineediting~ImageInlineEditing `ImageInlineEditing`} plugin is loaded,
* the following buttons (options) and groups will available for the toolbar configuration.
* These options will be registered as the buttons with the "imageStyle:" prefixes.
*
* const imageDefaultConfig = {
* styles: {
* options: [ 'inline', 'alignLeft', 'alignRight' ]
* }
* };
*
* Read more about the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options}.
*
* # **Custom configuration**
*
* The image styles configuration can be customized in several ways:
*
* * Any of the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options}
* can be loaded by the reference to its name as follows:
*
* ClassicEditor
* .create( editorElement, {
* image: {
* styles: {
* options: [ 'alignLeft', 'alignRight' ]
* }
* }
* } );
*
* * Each of the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default image style options} can be customized,
* e.g. to change the `icon`, `title` or CSS `className` of the style. The feature also provides several
* {@link module:image/imagestyle/utils~DEFAULT_ICONS default icons} to choose from.
*
* import customIcon from 'custom-icon.svg';
*
* // ...
*
* ClassicEditor.create( editorElement, { image:
* styles: {
* options: {
* // This will only customize the icon of the "block" style.
* // Note: 'right' is one of default icons provided by the feature.
* {
* name: 'block',
* icon: 'right'
* },
*
* // This will customize the icon, title and CSS class of the default "side" style.
* {
* name: 'side',
* icon: customIcon,
* title: 'My side style',
* className: 'custom-side-image'
* }
* }
* }
* } );
*
* * If none of the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default image style options}
* works for the integration, it is possible to define independent custom styles, too.
*
* See the documentation about the image style {@link module:image/imagestyle~ImageStyleOptionDefinition options}
* to define the custom image style configuration properly.
*
* import redIcon from 'red-icon.svg';
* import blueIcon from 'blue-icon.svg';
*
* // ...
*
* ClassicEditor.create( editorElement, { image:
* styles: {
* // A list of completely custom styling options.
* options: [
* {
* name: 'regular',
* modelElements: [ 'imageBlock', 'imageInline' ],
* title: 'Regular image',
* icon: 'full',
* isDefault: true
* }, {
* name: 'blue',
* modelElements: [ 'imageInline' ],
* title: 'Blue image',
* icon: blueIcon,
* className: 'image-blue'
* }, {
* name: 'red',
* modelElements: [ 'imageBlock' ],
* title: 'Red image',
* icon: redIcon,
* className: 'image-red'
* }
* ]
* }
* } );
*
* @member {module:image/imagestyle~ImageStyleConfig} module:image/image~ImageConfig#styles
*/
/**
* The image styling option definition descriptor.
*
* This definition should be implemented in the `Image` plugin {@link module:image/image~ImageConfig#styles configuration} for:
*
* * customizing one of the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options} by providing the proper name
* of the default style and the properties that should be overridden,
* * or defining a completely custom styling option by providing a custom name and implementing the following properties.
*
* import fullSizeIcon from 'path/to/icon.svg';
*
* const imageStyleOptionDefinition = {
* name: 'fullSize',
* icon: fullSizeIcon,
* title: 'Full size image',
* className: 'image-full-size',
* modelElements: [ 'imageBlock', 'imageInline' ]
* }
*
* The styling option will be registered as the button under the name `'imageStyle:{name}'` in the
* {@link module:ui/componentfactory~ComponentFactory UI components factory} (this functionality is provided by the
* {@link module:image/imagestyle/imagestyleui~ImageStyleUI} plugin).
*
* @property {String} name The unique name of the styling option. It will be used to:
*
* * refer to one of the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options} or define the custom style,
* * store the chosen style in the model by setting the `imageStyle` attribute of the model image element,
* * as a value of the {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand#execute `imageStyle` command},
* * when registering a button for the style in the following manner: (`'imageStyle:{name}'`).
*
* @property {Boolean} [isDefault] When set, the style will be used as the default one for the model elements
* listed in the `modelElements` property. A default style does not apply any CSS class to the view element.
*
* If this property is not defined, its value is inherited
* from the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options} addressed in the name property.
*
* @property {String} icon One of the following to be used when creating the styles's button:
*
* * an SVG icon source (as an XML string),
* * one of the keys in {@link module:image/imagestyle/utils~DEFAULT_ICONS} to use one of default icons provided by the plugin.
*
* If this property is not defined, its value is inherited
* from the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options} addressed in the name property.
*
* @property {String} title The styles's title. Setting `title` to one of
* {@link module:image/imagestyle/imagestyleui~ImageStyleUI#localizedDefaultStylesTitles}
* will automatically translate it to the language of the editor.
*
* If this property is not defined, its value is inherited
* from the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options} addressed in the name property.
*
* @property {String} [className] The CSS class used to represent the style in the view.
* It should be used only for the non-default styles.
*
* If this property is not defined, its value is inherited
* from the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options} addressed in the name property.
*
* @property {Array.<String>} modelElements The list of the names of the model elements that are supported by the style.
* The possible values are:
* * `[ 'imageBlock' ]` if the style can be applied to the image type introduced by the
* {@link module:image/image/imageblockediting~ImageBlockEditing `ImageBlockEditing`} plugin,
* * `[ 'imageInline' ]` if the style can be applied to the image type introduced by the
* {@link module:image/image/imageinlineediting~ImageInlineEditing `ImageInlineEditing`} plugin,
* * `[ 'imageInline', 'imageBlock' ]` if the style can be applied to both image types introduced by the plugins mentioned above.
*
* This property determines which model element names work with the style. If the model element name of the currently selected
* image is different, upon executing the
* {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand#execute `imageStyle`} command the image type (model element name)
* will automatically change.
*
* If this property is not defined, its value is inherited
* from the {@link module:image/imagestyle/utils~DEFAULT_OPTIONS default styling options} addressed in the name property.
*
* @typedef {Object} module:image/imagestyle~ImageStyleOptionDefinition
*/