1 | /**
|
2 | * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
3 | * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4 | */
|
5 |
|
6 | /**
|
7 | * @module image/imageresize
|
8 | */
|
9 |
|
10 | import { Plugin } from 'ckeditor5/src/core';
|
11 | import ImageResizeButtons from './imageresize/imageresizebuttons';
|
12 | import ImageResizeEditing from './imageresize/imageresizeediting';
|
13 | import ImageResizeHandles from './imageresize/imageresizehandles';
|
14 |
|
15 | import '../theme/imageresize.css';
|
16 |
|
17 | /**
|
18 | * The image resize plugin.
|
19 | *
|
20 | * It adds a possibility to resize each image using handles.
|
21 | *
|
22 | * @extends module:core/plugin~Plugin
|
23 | */
|
24 | export default class ImageResize extends Plugin {
|
25 | /**
|
26 | * @inheritDoc
|
27 | */
|
28 | static get requires() {
|
29 | return [ ImageResizeEditing, ImageResizeHandles, ImageResizeButtons ];
|
30 | }
|
31 |
|
32 | /**
|
33 | * @inheritDoc
|
34 | */
|
35 | static get pluginName() {
|
36 | return 'ImageResize';
|
37 | }
|
38 | }
|
39 |
|
40 | /**
|
41 | * The available options are `'px'` or `'%'`.
|
42 | *
|
43 | * Determines the size unit applied to the resized image.
|
44 | *
|
45 | * ClassicEditor
|
46 | * .create( editorElement, {
|
47 | * image: {
|
48 | * resizeUnit: 'px'
|
49 | * }
|
50 | * } )
|
51 | * .then( ... )
|
52 | * .catch( ... );
|
53 | *
|
54 | *
|
55 | * This option is used by the {@link module:image/imageresize~ImageResize} feature.
|
56 | *
|
57 | * @default '%'
|
58 | * @member {String} module:image/image~ImageConfig#resizeUnit
|
59 | */
|
60 |
|
61 | /**
|
62 | * The image resize options.
|
63 | *
|
64 | * Each option should have at least these two properties:
|
65 | *
|
66 | * * name: The name of the UI component registered in the global
|
67 | * {@link module:core/editor/editorui~EditorUI#componentFactory component factory} of the editor,
|
68 | * representing the button a user can click to change the size of an image,
|
69 | * * value: An actual image width applied when a user clicks the mentioned button
|
70 | * ({@link module:image/imageresize/resizeimagecommand~ResizeImageCommand} gets executed).
|
71 | * The value property is combined with the {@link module:image/image~ImageConfig#resizeUnit `config.image.resizeUnit`} (`%` by default).
|
72 | * For instance: `value: '50'` and `resizeUnit: '%'` will render as `'50%'` in the UI.
|
73 | *
|
74 | * **Resetting the image size**
|
75 | *
|
76 | * If you want to set an option that will reset image to its original size, you need to pass a `null` value
|
77 | * to one of the options. The `:original` token is not mandatory, you can call it anything you wish, but it must reflect
|
78 | * in the standalone buttons configuration for the image toolbar.
|
79 | *
|
80 | * ClassicEditor
|
81 | * .create( editorElement, {
|
82 | * image: {
|
83 | * resizeUnit: "%",
|
84 | * resizeOptions: [ {
|
85 | * name: 'resizeImage:original',
|
86 | * value: null
|
87 | * },
|
88 | * {
|
89 | * name: 'resizeImage:50',
|
90 | * value: '50'
|
91 | * },
|
92 | * {
|
93 | * name: 'resizeImage:75',
|
94 | * value: '75'
|
95 | * } ]
|
96 | * }
|
97 | * } )
|
98 | * .then( ... )
|
99 | * .catch( ... );
|
100 | *
|
101 | * **Resizing images using a dropdown**
|
102 | *
|
103 | * With resize options defined, you can decide whether you want to display them as a dropdown or as standalone buttons.
|
104 | * For the dropdown, you need to pass only the `resizeImage` token to the
|
105 | {@link module:image/image~ImageConfig#toolbar `config.image.toolbar`}. The dropdown contains all defined options by default:
|
106 | *
|
107 | * ClassicEditor
|
108 | * .create( editorElement, {
|
109 | * image: {
|
110 | * resizeUnit: "%",
|
111 | * resizeOptions: [ {
|
112 | * name: 'resizeImage:original',
|
113 | * value: null
|
114 | * },
|
115 | * {
|
116 | * name: 'resizeImage:50',
|
117 | * value: '50'
|
118 | * },
|
119 | * {
|
120 | * name: 'resizeImage:75',
|
121 | * value: '75'
|
122 | * } ],
|
123 | * toolbar: [ 'resizeImage', ... ],
|
124 | * }
|
125 | * } )
|
126 | * .then( ... )
|
127 | * .catch( ... );
|
128 | *
|
129 | * **Resizing images using individual buttons**
|
130 | *
|
131 | * If you want to have separate buttons for {@link module:image/imageresize/imageresizebuttons~ImageResizeOption each option},
|
132 | * pass their names to the {@link module:image/image~ImageConfig#toolbar `config.image.toolbar`} instead. Please keep in mind
|
133 | * that this time **you must define the additional
|
134 | * {@link module:image/imageresize/imageresizebuttons~ImageResizeOption `icon` property}**:
|
135 | *
|
136 | * ClassicEditor
|
137 | * .create( editorElement, {
|
138 | * image: {
|
139 | * resizeUnit: "%",
|
140 | * resizeOptions: [ {
|
141 | * name: 'resizeImage:original',
|
142 | * value: null,
|
143 | * icon: 'original'
|
144 | * },
|
145 | * {
|
146 | * name: 'resizeImage:25',
|
147 | * value: '25',
|
148 | * icon: 'small'
|
149 | * },
|
150 | * {
|
151 | * name: 'resizeImage:50',
|
152 | * value: '50',
|
153 | * icon: 'medium'
|
154 | * },
|
155 | * {
|
156 | * name: 'resizeImage:75',
|
157 | * value: '75',
|
158 | * icon: 'large'
|
159 | * } ],
|
160 | * toolbar: [ 'resizeImage:25', 'resizeImage:50', 'resizeImage:75', 'resizeImage:original', ... ],
|
161 | * }
|
162 | * } )
|
163 | * .then( ... )
|
164 | * .catch( ... );
|
165 | *
|
166 | * **Customizing resize button labels**
|
167 | *
|
168 | * You can set your own label for each resize button. To do that, add the `label` property like in the example below.
|
169 | *
|
170 | * * When using the **dropdown**, the labels are displayed on the list of all options when you open the dropdown.
|
171 | * * When using **standalone buttons**, the labels will are displayed as tooltips when a user hovers over the button.
|
172 | *
|
173 | * ClassicEditor
|
174 | * .create( editorElement, {
|
175 | * image: {
|
176 | * resizeUnit: "%",
|
177 | * resizeOptions: [ {
|
178 | * name: 'resizeImage:original',
|
179 | * value: null,
|
180 | * label: 'Original size'
|
181 | * // Note: add the "icon" property if you're configuring a standalone button.
|
182 | * },
|
183 | * {
|
184 | * name: 'resizeImage:50',
|
185 | * value: '50',
|
186 | * label: 'Medium size'
|
187 | * // Note: add the "icon" property if you're configuring a standalone button.
|
188 | * },
|
189 | * {
|
190 | * name: 'resizeImage:75',
|
191 | * value: '75',
|
192 | * label: 'Large size'
|
193 | * // Note: add the "icon" property if you're configuring a standalone button.
|
194 | * } ]
|
195 | * }
|
196 | * } )
|
197 | * .then( ... )
|
198 | * .catch( ... );
|
199 | *
|
200 | * **Default value**
|
201 | *
|
202 | * The following configuration is used by default:
|
203 | *
|
204 | * resizeOptions = [
|
205 | * {
|
206 | * name: 'resizeImage:original',
|
207 | * value: null,
|
208 | * icon: 'original'
|
209 | * },
|
210 | * {
|
211 | * name: 'resizeImage:25',
|
212 | * value: '25',
|
213 | * icon: 'small'
|
214 | * },
|
215 | * {
|
216 | * name: 'resizeImage:50',
|
217 | * value: '50',
|
218 | * icon: 'medium'
|
219 | * },
|
220 | * {
|
221 | * name: 'resizeImage:75',
|
222 | * value: '75',
|
223 | * icon: 'large'
|
224 | * }
|
225 | * ];
|
226 | *
|
227 | * @member {Array.<module:image/imageresize/imageresizebuttons~ImageResizeOption>} module:image/image~ImageConfig#resizeOptions
|
228 | */
|