@ckeditor/ckeditor5-image 39.0.2 → 40.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/build/image.js +1 -1
  2. package/build/image.js.map +1 -0
  3. package/build/translations/pt-br.js +1 -1
  4. package/ckeditor5-metadata.json +12 -0
  5. package/lang/translations/pt-br.po +1 -1
  6. package/package.json +3 -3
  7. package/src/augmentation.d.ts +56 -55
  8. package/src/augmentation.js +5 -5
  9. package/src/autoimage.d.ts +52 -52
  10. package/src/autoimage.js +132 -132
  11. package/src/image/converters.d.ts +66 -66
  12. package/src/image/converters.js +232 -242
  13. package/src/image/imageblockediting.d.ts +58 -55
  14. package/src/image/imageblockediting.js +152 -136
  15. package/src/image/imageediting.d.ts +30 -30
  16. package/src/image/imageediting.js +63 -74
  17. package/src/image/imageinlineediting.d.ts +59 -56
  18. package/src/image/imageinlineediting.js +176 -160
  19. package/src/image/imageloadobserver.d.ts +48 -48
  20. package/src/image/imageloadobserver.js +52 -52
  21. package/src/image/imagetypecommand.d.ts +44 -40
  22. package/src/image/imagetypecommand.js +80 -77
  23. package/src/image/insertimagecommand.d.ts +66 -66
  24. package/src/image/insertimagecommand.js +120 -120
  25. package/src/image/replaceimagesourcecommand.d.ts +34 -34
  26. package/src/image/replaceimagesourcecommand.js +44 -44
  27. package/src/image/ui/utils.d.ts +25 -25
  28. package/src/image/ui/utils.js +44 -44
  29. package/src/image/utils.d.ts +64 -52
  30. package/src/image/utils.js +121 -100
  31. package/src/image.d.ts +34 -34
  32. package/src/image.js +38 -38
  33. package/src/imageblock.d.ts +33 -33
  34. package/src/imageblock.js +37 -37
  35. package/src/imagecaption/imagecaptionediting.d.ts +89 -89
  36. package/src/imagecaption/imagecaptionediting.js +225 -225
  37. package/src/imagecaption/imagecaptionui.d.ts +26 -26
  38. package/src/imagecaption/imagecaptionui.js +61 -61
  39. package/src/imagecaption/imagecaptionutils.d.ts +38 -38
  40. package/src/imagecaption/imagecaptionutils.js +62 -62
  41. package/src/imagecaption/toggleimagecaptioncommand.d.ts +66 -66
  42. package/src/imagecaption/toggleimagecaptioncommand.js +138 -138
  43. package/src/imagecaption.d.ts +26 -26
  44. package/src/imagecaption.js +30 -30
  45. package/src/imageconfig.d.ts +713 -713
  46. package/src/imageconfig.js +5 -5
  47. package/src/imageinline.d.ts +33 -33
  48. package/src/imageinline.js +37 -37
  49. package/src/imageinsert/imageinsertui.d.ts +44 -44
  50. package/src/imageinsert/imageinsertui.js +141 -141
  51. package/src/imageinsert/ui/imageinsertformrowview.d.ts +61 -61
  52. package/src/imageinsert/ui/imageinsertformrowview.js +54 -54
  53. package/src/imageinsert/ui/imageinsertpanelview.d.ts +106 -106
  54. package/src/imageinsert/ui/imageinsertpanelview.js +161 -161
  55. package/src/imageinsert/utils.d.ts +25 -25
  56. package/src/imageinsert/utils.js +58 -58
  57. package/src/imageinsert.d.ts +33 -33
  58. package/src/imageinsert.js +37 -37
  59. package/src/imageinsertviaurl.d.ts +30 -30
  60. package/src/imageinsertviaurl.js +34 -34
  61. package/src/imageresize/imageresizebuttons.d.ts +67 -67
  62. package/src/imageresize/imageresizebuttons.js +217 -217
  63. package/src/imageresize/imageresizeediting.d.ts +37 -37
  64. package/src/imageresize/imageresizeediting.js +165 -114
  65. package/src/imageresize/imageresizehandles.d.ts +31 -30
  66. package/src/imageresize/imageresizehandles.js +114 -107
  67. package/src/imageresize/resizeimagecommand.d.ts +42 -42
  68. package/src/imageresize/resizeimagecommand.js +63 -61
  69. package/src/imageresize.d.ts +27 -27
  70. package/src/imageresize.js +31 -31
  71. package/src/imagesizeattributes.d.ts +34 -0
  72. package/src/imagesizeattributes.js +143 -0
  73. package/src/imagestyle/converters.d.ts +24 -24
  74. package/src/imagestyle/converters.js +79 -79
  75. package/src/imagestyle/imagestylecommand.d.ts +68 -65
  76. package/src/imagestyle/imagestylecommand.js +107 -101
  77. package/src/imagestyle/imagestyleediting.d.ts +50 -50
  78. package/src/imagestyle/imagestyleediting.js +108 -108
  79. package/src/imagestyle/imagestyleui.d.ts +56 -56
  80. package/src/imagestyle/imagestyleui.js +192 -192
  81. package/src/imagestyle/utils.d.ts +101 -101
  82. package/src/imagestyle/utils.js +329 -329
  83. package/src/imagestyle.d.ts +32 -32
  84. package/src/imagestyle.js +36 -36
  85. package/src/imagetextalternative/imagetextalternativecommand.d.ts +34 -34
  86. package/src/imagetextalternative/imagetextalternativecommand.js +44 -44
  87. package/src/imagetextalternative/imagetextalternativeediting.d.ts +28 -28
  88. package/src/imagetextalternative/imagetextalternativeediting.js +35 -35
  89. package/src/imagetextalternative/imagetextalternativeui.d.ts +68 -68
  90. package/src/imagetextalternative/imagetextalternativeui.js +173 -173
  91. package/src/imagetextalternative/ui/textalternativeformview.d.ts +72 -72
  92. package/src/imagetextalternative/ui/textalternativeformview.js +121 -121
  93. package/src/imagetextalternative.d.ts +29 -29
  94. package/src/imagetextalternative.js +33 -33
  95. package/src/imagetoolbar.d.ts +35 -35
  96. package/src/imagetoolbar.js +57 -57
  97. package/src/imageupload/imageuploadediting.d.ts +111 -111
  98. package/src/imageupload/imageuploadediting.js +337 -335
  99. package/src/imageupload/imageuploadprogress.d.ts +42 -42
  100. package/src/imageupload/imageuploadprogress.js +211 -211
  101. package/src/imageupload/imageuploadui.d.ts +23 -23
  102. package/src/imageupload/imageuploadui.js +57 -57
  103. package/src/imageupload/uploadimagecommand.d.ts +60 -60
  104. package/src/imageupload/uploadimagecommand.js +100 -100
  105. package/src/imageupload/utils.d.ts +33 -33
  106. package/src/imageupload/utils.js +112 -112
  107. package/src/imageupload.d.ts +32 -32
  108. package/src/imageupload.js +36 -36
  109. package/src/imageutils.d.ts +125 -102
  110. package/src/imageutils.js +306 -248
  111. package/src/index.d.ts +48 -47
  112. package/src/index.js +39 -38
  113. package/src/pictureediting.d.ts +88 -88
  114. package/src/pictureediting.js +130 -130
  115. package/theme/image.css +38 -11
  116. package/theme/imageresize.css +5 -0
@@ -1,79 +1,79 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- import { first } from 'ckeditor5/src/utils';
6
- /**
7
- * @module image/imagestyle/converters
8
- */
9
- /**
10
- * Returns a converter for the `imageStyle` attribute. It can be used for adding, changing and removing the attribute.
11
- *
12
- * @param styles An array containing available image style options.
13
- * @returns A model-to-view attribute converter.
14
- */
15
- export function modelToViewStyleAttribute(styles) {
16
- return (evt, data, conversionApi) => {
17
- if (!conversionApi.consumable.consume(data.item, evt.name)) {
18
- return;
19
- }
20
- // Check if there is class name associated with given value.
21
- const newStyle = getStyleDefinitionByName(data.attributeNewValue, styles);
22
- const oldStyle = getStyleDefinitionByName(data.attributeOldValue, styles);
23
- const viewElement = conversionApi.mapper.toViewElement(data.item);
24
- const viewWriter = conversionApi.writer;
25
- if (oldStyle) {
26
- viewWriter.removeClass(oldStyle.className, viewElement);
27
- }
28
- if (newStyle) {
29
- viewWriter.addClass(newStyle.className, viewElement);
30
- }
31
- };
32
- }
33
- /**
34
- * Returns a view-to-model converter converting image CSS classes to a proper value in the model.
35
- *
36
- * @param styles Image style options for which the converter is created.
37
- * @returns A view-to-model converter.
38
- */
39
- export function viewToModelStyleAttribute(styles) {
40
- // Convert only non–default styles.
41
- const nonDefaultStyles = {
42
- imageInline: styles.filter(style => !style.isDefault && style.modelElements.includes('imageInline')),
43
- imageBlock: styles.filter(style => !style.isDefault && style.modelElements.includes('imageBlock'))
44
- };
45
- return (evt, data, conversionApi) => {
46
- if (!data.modelRange) {
47
- return;
48
- }
49
- const viewElement = data.viewItem;
50
- const modelImageElement = first(data.modelRange.getItems());
51
- // Run this converter only if an image has been found in the model.
52
- // In some cases it may not be found (for example if we run this on a figure with different type than image).
53
- if (!modelImageElement) {
54
- return;
55
- }
56
- // ...and the `imageStyle` attribute is allowed for that element, otherwise stop conversion early.
57
- if (!conversionApi.schema.checkAttribute(modelImageElement, 'imageStyle')) {
58
- return;
59
- }
60
- // Convert styles one by one.
61
- for (const style of nonDefaultStyles[modelImageElement.name]) {
62
- // Try to consume class corresponding with the style.
63
- if (conversionApi.consumable.consume(viewElement, { classes: style.className })) {
64
- // And convert this style to model attribute.
65
- conversionApi.writer.setAttribute('imageStyle', style.name, modelImageElement);
66
- }
67
- }
68
- };
69
- }
70
- /**
71
- * Returns the style with a given `name` from an array of styles.
72
- */
73
- function getStyleDefinitionByName(name, styles) {
74
- for (const style of styles) {
75
- if (style.name === name) {
76
- return style;
77
- }
78
- }
79
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ import { first } from 'ckeditor5/src/utils';
6
+ /**
7
+ * @module image/imagestyle/converters
8
+ */
9
+ /**
10
+ * Returns a converter for the `imageStyle` attribute. It can be used for adding, changing and removing the attribute.
11
+ *
12
+ * @param styles An array containing available image style options.
13
+ * @returns A model-to-view attribute converter.
14
+ */
15
+ export function modelToViewStyleAttribute(styles) {
16
+ return (evt, data, conversionApi) => {
17
+ if (!conversionApi.consumable.consume(data.item, evt.name)) {
18
+ return;
19
+ }
20
+ // Check if there is class name associated with given value.
21
+ const newStyle = getStyleDefinitionByName(data.attributeNewValue, styles);
22
+ const oldStyle = getStyleDefinitionByName(data.attributeOldValue, styles);
23
+ const viewElement = conversionApi.mapper.toViewElement(data.item);
24
+ const viewWriter = conversionApi.writer;
25
+ if (oldStyle) {
26
+ viewWriter.removeClass(oldStyle.className, viewElement);
27
+ }
28
+ if (newStyle) {
29
+ viewWriter.addClass(newStyle.className, viewElement);
30
+ }
31
+ };
32
+ }
33
+ /**
34
+ * Returns a view-to-model converter converting image CSS classes to a proper value in the model.
35
+ *
36
+ * @param styles Image style options for which the converter is created.
37
+ * @returns A view-to-model converter.
38
+ */
39
+ export function viewToModelStyleAttribute(styles) {
40
+ // Convert only non–default styles.
41
+ const nonDefaultStyles = {
42
+ imageInline: styles.filter(style => !style.isDefault && style.modelElements.includes('imageInline')),
43
+ imageBlock: styles.filter(style => !style.isDefault && style.modelElements.includes('imageBlock'))
44
+ };
45
+ return (evt, data, conversionApi) => {
46
+ if (!data.modelRange) {
47
+ return;
48
+ }
49
+ const viewElement = data.viewItem;
50
+ const modelImageElement = first(data.modelRange.getItems());
51
+ // Run this converter only if an image has been found in the model.
52
+ // In some cases it may not be found (for example if we run this on a figure with different type than image).
53
+ if (!modelImageElement) {
54
+ return;
55
+ }
56
+ // ...and the `imageStyle` attribute is allowed for that element, otherwise stop conversion early.
57
+ if (!conversionApi.schema.checkAttribute(modelImageElement, 'imageStyle')) {
58
+ return;
59
+ }
60
+ // Convert styles one by one.
61
+ for (const style of nonDefaultStyles[modelImageElement.name]) {
62
+ // Try to consume class corresponding with the style.
63
+ if (conversionApi.consumable.consume(viewElement, { classes: style.className })) {
64
+ // And convert this style to model attribute.
65
+ conversionApi.writer.setAttribute('imageStyle', style.name, modelImageElement);
66
+ }
67
+ }
68
+ };
69
+ }
70
+ /**
71
+ * Returns the style with a given `name` from an array of styles.
72
+ */
73
+ function getStyleDefinitionByName(name, styles) {
74
+ for (const style of styles) {
75
+ if (style.name === name) {
76
+ return style;
77
+ }
78
+ }
79
+ }
@@ -1,65 +1,68 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module image/imagestyle/imagestylecommand
7
- */
8
- import type { Element } from 'ckeditor5/src/engine';
9
- import { Command, type Editor } from 'ckeditor5/src/core';
10
- import type { ImageStyleOptionDefinition } from '../imageconfig';
11
- /**
12
- * The image style command. It is used to apply {@link module:image/imageconfig~ImageStyleConfig#options image style option}
13
- * to a selected image.
14
- *
15
- * **Note**: Executing this command may change the image model element if the desired style requires an image of a different
16
- * type. See {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand#execute} to learn more.
17
- */
18
- export default class ImageStyleCommand extends Command {
19
- /**
20
- * An object containing names of default style options for the inline and block images.
21
- * If there is no default style option for the given image type in the configuration,
22
- * the name will be `false`.
23
- */
24
- private _defaultStyles;
25
- /**
26
- * The styles handled by this command.
27
- */
28
- private _styles;
29
- /**
30
- * Creates an instance of the image style command. When executed, the command applies one of
31
- * {@link module:image/imageconfig~ImageStyleConfig#options style options} to the currently selected image.
32
- *
33
- * @param editor The editor instance.
34
- * @param styles The style options that this command supports.
35
- */
36
- constructor(editor: Editor, styles: Array<ImageStyleOptionDefinition>);
37
- /**
38
- * @inheritDoc
39
- */
40
- refresh(): void;
41
- /**
42
- * Executes the command and applies the style to the currently selected image:
43
- *
44
- * ```ts
45
- * editor.execute( 'imageStyle', { value: 'side' } );
46
- * ```
47
- *
48
- * **Note**: Executing this command may change the image model element if the desired style requires an image
49
- * of a different type. Learn more about {@link module:image/imageconfig~ImageStyleOptionDefinition#modelElements model element}
50
- * configuration for the style option.
51
- *
52
- * @param options.value The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
53
- * @fires execute
54
- */
55
- execute(options?: {
56
- value?: string;
57
- }): void;
58
- /**
59
- * Returns `true` if requested style change would trigger the image type change.
60
- *
61
- * @param requestedStyle The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
62
- * @param imageElement The image model element.
63
- */
64
- shouldConvertImageType(requestedStyle: string, imageElement: Element): boolean;
65
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module image/imagestyle/imagestylecommand
7
+ */
8
+ import type { Element } from 'ckeditor5/src/engine';
9
+ import { Command, type Editor } from 'ckeditor5/src/core';
10
+ import type { ImageStyleOptionDefinition } from '../imageconfig';
11
+ /**
12
+ * The image style command. It is used to apply {@link module:image/imageconfig~ImageStyleConfig#options image style option}
13
+ * to a selected image.
14
+ *
15
+ * **Note**: Executing this command may change the image model element if the desired style requires an image of a different
16
+ * type. See {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand#execute} to learn more.
17
+ */
18
+ export default class ImageStyleCommand extends Command {
19
+ /**
20
+ * An object containing names of default style options for the inline and block images.
21
+ * If there is no default style option for the given image type in the configuration,
22
+ * the name will be `false`.
23
+ */
24
+ private _defaultStyles;
25
+ /**
26
+ * The styles handled by this command.
27
+ */
28
+ private _styles;
29
+ /**
30
+ * Creates an instance of the image style command. When executed, the command applies one of
31
+ * {@link module:image/imageconfig~ImageStyleConfig#options style options} to the currently selected image.
32
+ *
33
+ * @param editor The editor instance.
34
+ * @param styles The style options that this command supports.
35
+ */
36
+ constructor(editor: Editor, styles: Array<ImageStyleOptionDefinition>);
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ refresh(): void;
41
+ /**
42
+ * Executes the command and applies the style to the currently selected image:
43
+ *
44
+ * ```ts
45
+ * editor.execute( 'imageStyle', { value: 'side' } );
46
+ * ```
47
+ *
48
+ * **Note**: Executing this command may change the image model element if the desired style requires an image
49
+ * of a different type. Learn more about {@link module:image/imageconfig~ImageStyleOptionDefinition#modelElements model element}
50
+ * configuration for the style option.
51
+ *
52
+ * @param options.value The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
53
+ * @param options.setImageSizes Specifies whether the image `width` and `height` attributes should be set automatically.
54
+ * The default is `true`.
55
+ * @fires execute
56
+ */
57
+ execute(options?: {
58
+ value?: string;
59
+ setImageSizes?: boolean;
60
+ }): void;
61
+ /**
62
+ * Returns `true` if requested style change would trigger the image type change.
63
+ *
64
+ * @param requestedStyle The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
65
+ * @param imageElement The image model element.
66
+ */
67
+ shouldConvertImageType(requestedStyle: string, imageElement: Element): boolean;
68
+ }
@@ -1,101 +1,107 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- import { Command } from 'ckeditor5/src/core';
6
- /**
7
- * The image style command. It is used to apply {@link module:image/imageconfig~ImageStyleConfig#options image style option}
8
- * to a selected image.
9
- *
10
- * **Note**: Executing this command may change the image model element if the desired style requires an image of a different
11
- * type. See {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand#execute} to learn more.
12
- */
13
- export default class ImageStyleCommand extends Command {
14
- /**
15
- * Creates an instance of the image style command. When executed, the command applies one of
16
- * {@link module:image/imageconfig~ImageStyleConfig#options style options} to the currently selected image.
17
- *
18
- * @param editor The editor instance.
19
- * @param styles The style options that this command supports.
20
- */
21
- constructor(editor, styles) {
22
- super(editor);
23
- this._defaultStyles = {
24
- imageBlock: false,
25
- imageInline: false
26
- };
27
- this._styles = new Map(styles.map(style => {
28
- if (style.isDefault) {
29
- for (const modelElementName of style.modelElements) {
30
- this._defaultStyles[modelElementName] = style.name;
31
- }
32
- }
33
- return [style.name, style];
34
- }));
35
- }
36
- /**
37
- * @inheritDoc
38
- */
39
- refresh() {
40
- const editor = this.editor;
41
- const imageUtils = editor.plugins.get('ImageUtils');
42
- const element = imageUtils.getClosestSelectedImageElement(this.editor.model.document.selection);
43
- this.isEnabled = !!element;
44
- if (!this.isEnabled) {
45
- this.value = false;
46
- }
47
- else if (element.hasAttribute('imageStyle')) {
48
- this.value = element.getAttribute('imageStyle');
49
- }
50
- else {
51
- this.value = this._defaultStyles[element.name];
52
- }
53
- }
54
- /**
55
- * Executes the command and applies the style to the currently selected image:
56
- *
57
- * ```ts
58
- * editor.execute( 'imageStyle', { value: 'side' } );
59
- * ```
60
- *
61
- * **Note**: Executing this command may change the image model element if the desired style requires an image
62
- * of a different type. Learn more about {@link module:image/imageconfig~ImageStyleOptionDefinition#modelElements model element}
63
- * configuration for the style option.
64
- *
65
- * @param options.value The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
66
- * @fires execute
67
- */
68
- execute(options = {}) {
69
- const editor = this.editor;
70
- const model = editor.model;
71
- const imageUtils = editor.plugins.get('ImageUtils');
72
- model.change(writer => {
73
- const requestedStyle = options.value;
74
- let imageElement = imageUtils.getClosestSelectedImageElement(model.document.selection);
75
- // Change the image type if a style requires it.
76
- if (requestedStyle && this.shouldConvertImageType(requestedStyle, imageElement)) {
77
- this.editor.execute(imageUtils.isBlockImage(imageElement) ? 'imageTypeInline' : 'imageTypeBlock');
78
- // Update the imageElement to the newly created image.
79
- imageElement = imageUtils.getClosestSelectedImageElement(model.document.selection);
80
- }
81
- // Default style means that there is no `imageStyle` attribute in the model.
82
- // https://github.com/ckeditor/ckeditor5-image/issues/147
83
- if (!requestedStyle || this._styles.get(requestedStyle).isDefault) {
84
- writer.removeAttribute('imageStyle', imageElement);
85
- }
86
- else {
87
- writer.setAttribute('imageStyle', requestedStyle, imageElement);
88
- }
89
- });
90
- }
91
- /**
92
- * Returns `true` if requested style change would trigger the image type change.
93
- *
94
- * @param requestedStyle The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
95
- * @param imageElement The image model element.
96
- */
97
- shouldConvertImageType(requestedStyle, imageElement) {
98
- const supportedTypes = this._styles.get(requestedStyle).modelElements;
99
- return !supportedTypes.includes(imageElement.name);
100
- }
101
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ import { Command } from 'ckeditor5/src/core';
6
+ /**
7
+ * The image style command. It is used to apply {@link module:image/imageconfig~ImageStyleConfig#options image style option}
8
+ * to a selected image.
9
+ *
10
+ * **Note**: Executing this command may change the image model element if the desired style requires an image of a different
11
+ * type. See {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand#execute} to learn more.
12
+ */
13
+ export default class ImageStyleCommand extends Command {
14
+ /**
15
+ * Creates an instance of the image style command. When executed, the command applies one of
16
+ * {@link module:image/imageconfig~ImageStyleConfig#options style options} to the currently selected image.
17
+ *
18
+ * @param editor The editor instance.
19
+ * @param styles The style options that this command supports.
20
+ */
21
+ constructor(editor, styles) {
22
+ super(editor);
23
+ this._defaultStyles = {
24
+ imageBlock: false,
25
+ imageInline: false
26
+ };
27
+ this._styles = new Map(styles.map(style => {
28
+ if (style.isDefault) {
29
+ for (const modelElementName of style.modelElements) {
30
+ this._defaultStyles[modelElementName] = style.name;
31
+ }
32
+ }
33
+ return [style.name, style];
34
+ }));
35
+ }
36
+ /**
37
+ * @inheritDoc
38
+ */
39
+ refresh() {
40
+ const editor = this.editor;
41
+ const imageUtils = editor.plugins.get('ImageUtils');
42
+ const element = imageUtils.getClosestSelectedImageElement(this.editor.model.document.selection);
43
+ this.isEnabled = !!element;
44
+ if (!this.isEnabled) {
45
+ this.value = false;
46
+ }
47
+ else if (element.hasAttribute('imageStyle')) {
48
+ this.value = element.getAttribute('imageStyle');
49
+ }
50
+ else {
51
+ this.value = this._defaultStyles[element.name];
52
+ }
53
+ }
54
+ /**
55
+ * Executes the command and applies the style to the currently selected image:
56
+ *
57
+ * ```ts
58
+ * editor.execute( 'imageStyle', { value: 'side' } );
59
+ * ```
60
+ *
61
+ * **Note**: Executing this command may change the image model element if the desired style requires an image
62
+ * of a different type. Learn more about {@link module:image/imageconfig~ImageStyleOptionDefinition#modelElements model element}
63
+ * configuration for the style option.
64
+ *
65
+ * @param options.value The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
66
+ * @param options.setImageSizes Specifies whether the image `width` and `height` attributes should be set automatically.
67
+ * The default is `true`.
68
+ * @fires execute
69
+ */
70
+ execute(options = {}) {
71
+ const editor = this.editor;
72
+ const model = editor.model;
73
+ const imageUtils = editor.plugins.get('ImageUtils');
74
+ model.change(writer => {
75
+ const requestedStyle = options.value;
76
+ const { setImageSizes = true } = options;
77
+ let imageElement = imageUtils.getClosestSelectedImageElement(model.document.selection);
78
+ // Change the image type if a style requires it.
79
+ if (requestedStyle && this.shouldConvertImageType(requestedStyle, imageElement)) {
80
+ this.editor.execute(imageUtils.isBlockImage(imageElement) ? 'imageTypeInline' : 'imageTypeBlock', { setImageSizes });
81
+ // Update the imageElement to the newly created image.
82
+ imageElement = imageUtils.getClosestSelectedImageElement(model.document.selection);
83
+ }
84
+ // Default style means that there is no `imageStyle` attribute in the model.
85
+ // https://github.com/ckeditor/ckeditor5-image/issues/147
86
+ if (!requestedStyle || this._styles.get(requestedStyle).isDefault) {
87
+ writer.removeAttribute('imageStyle', imageElement);
88
+ }
89
+ else {
90
+ writer.setAttribute('imageStyle', requestedStyle, imageElement);
91
+ }
92
+ if (setImageSizes) {
93
+ imageUtils.setImageNaturalSizeAttributes(imageElement);
94
+ }
95
+ });
96
+ }
97
+ /**
98
+ * Returns `true` if requested style change would trigger the image type change.
99
+ *
100
+ * @param requestedStyle The name of the style (as configured in {@link module:image/imageconfig~ImageStyleConfig#options}).
101
+ * @param imageElement The image model element.
102
+ */
103
+ shouldConvertImageType(requestedStyle, imageElement) {
104
+ const supportedTypes = this._styles.get(requestedStyle).modelElements;
105
+ return !supportedTypes.includes(imageElement.name);
106
+ }
107
+ }
@@ -1,50 +1,50 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module image/imagestyle/imagestyleediting
7
- */
8
- import { Plugin } from 'ckeditor5/src/core';
9
- import ImageUtils from '../imageutils';
10
- import type { ImageStyleOptionDefinition } from '../imageconfig';
11
- /**
12
- * The image style engine plugin. It sets the default configuration, creates converters and registers
13
- * {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand ImageStyleCommand}.
14
- */
15
- export default class ImageStyleEditing extends Plugin {
16
- /**
17
- * @inheritDoc
18
- */
19
- static get pluginName(): "ImageStyleEditing";
20
- /**
21
- * @inheritDoc
22
- */
23
- static get requires(): readonly [typeof ImageUtils];
24
- /**
25
- * It contains a list of the normalized and validated style options.
26
- *
27
- * * Each option contains a complete icon markup.
28
- * * The style options not supported by any of the loaded image editing plugins (
29
- * {@link module:image/image/imageinlineediting~ImageInlineEditing `ImageInlineEditing`} or
30
- * {@link module:image/image/imageblockediting~ImageBlockEditing `ImageBlockEditing`}) are filtered out.
31
- *
32
- * @internal
33
- * @readonly
34
- */
35
- normalizedStyles?: Array<ImageStyleOptionDefinition>;
36
- /**
37
- * @inheritDoc
38
- */
39
- init(): void;
40
- /**
41
- * Sets the editor conversion taking the presence of
42
- * {@link module:image/image/imageinlineediting~ImageInlineEditing `ImageInlineEditing`}
43
- * and {@link module:image/image/imageblockediting~ImageBlockEditing `ImageBlockEditing`} plugins into consideration.
44
- */
45
- private _setupConversion;
46
- /**
47
- * Registers a post-fixer that will make sure that the style attribute value is correct for a specific image type (block vs inline).
48
- */
49
- private _setupPostFixer;
50
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module image/imagestyle/imagestyleediting
7
+ */
8
+ import { Plugin } from 'ckeditor5/src/core';
9
+ import ImageUtils from '../imageutils';
10
+ import type { ImageStyleOptionDefinition } from '../imageconfig';
11
+ /**
12
+ * The image style engine plugin. It sets the default configuration, creates converters and registers
13
+ * {@link module:image/imagestyle/imagestylecommand~ImageStyleCommand ImageStyleCommand}.
14
+ */
15
+ export default class ImageStyleEditing extends Plugin {
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get pluginName(): "ImageStyleEditing";
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ static get requires(): readonly [typeof ImageUtils];
24
+ /**
25
+ * It contains a list of the normalized and validated style options.
26
+ *
27
+ * * Each option contains a complete icon markup.
28
+ * * The style options not supported by any of the loaded image editing plugins (
29
+ * {@link module:image/image/imageinlineediting~ImageInlineEditing `ImageInlineEditing`} or
30
+ * {@link module:image/image/imageblockediting~ImageBlockEditing `ImageBlockEditing`}) are filtered out.
31
+ *
32
+ * @internal
33
+ * @readonly
34
+ */
35
+ normalizedStyles?: Array<ImageStyleOptionDefinition>;
36
+ /**
37
+ * @inheritDoc
38
+ */
39
+ init(): void;
40
+ /**
41
+ * Sets the editor conversion taking the presence of
42
+ * {@link module:image/image/imageinlineediting~ImageInlineEditing `ImageInlineEditing`}
43
+ * and {@link module:image/image/imageblockediting~ImageBlockEditing `ImageBlockEditing`} plugins into consideration.
44
+ */
45
+ private _setupConversion;
46
+ /**
47
+ * Registers a post-fixer that will make sure that the style attribute value is correct for a specific image type (block vs inline).
48
+ */
49
+ private _setupPostFixer;
50
+ }