@ckeditor/ckeditor5-image 36.0.0 → 37.0.0-alpha.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 (106) hide show
  1. package/build/image.js +1 -1
  2. package/package.json +40 -35
  3. package/src/autoimage.d.ts +53 -0
  4. package/src/autoimage.js +111 -160
  5. package/src/image/converters.d.ts +66 -0
  6. package/src/image/converters.js +200 -261
  7. package/src/image/imageblockediting.d.ts +57 -0
  8. package/src/image/imageblockediting.js +111 -155
  9. package/src/image/imageediting.d.ts +34 -0
  10. package/src/image/imageediting.js +53 -67
  11. package/src/image/imageinlineediting.d.ts +53 -0
  12. package/src/image/imageinlineediting.js +135 -180
  13. package/src/image/imageloadobserver.d.ts +44 -0
  14. package/src/image/imageloadobserver.js +28 -47
  15. package/src/image/imagetypecommand.d.ts +46 -0
  16. package/src/image/imagetypecommand.js +67 -95
  17. package/src/image/insertimagecommand.d.ts +72 -0
  18. package/src/image/insertimagecommand.js +95 -101
  19. package/src/image/replaceimagesourcecommand.d.ts +39 -0
  20. package/src/image/replaceimagesourcecommand.js +28 -33
  21. package/src/image/ui/utils.d.ts +25 -0
  22. package/src/image/ui/utils.js +25 -35
  23. package/src/image/utils.d.ts +52 -0
  24. package/src/image/utils.js +63 -90
  25. package/src/image.d.ts +38 -0
  26. package/src/image.js +13 -43
  27. package/src/imageblock.d.ts +35 -0
  28. package/src/imageblock.js +12 -21
  29. package/src/imagecaption/imagecaptionediting.d.ts +92 -0
  30. package/src/imagecaption/imagecaptionediting.js +203 -262
  31. package/src/imagecaption/imagecaptionui.d.ts +30 -0
  32. package/src/imagecaption/imagecaptionui.js +46 -63
  33. package/src/imagecaption/imagecaptionutils.d.ts +42 -0
  34. package/src/imagecaption/imagecaptionutils.js +51 -78
  35. package/src/imagecaption/toggleimagecaptioncommand.d.ts +71 -0
  36. package/src/imagecaption/toggleimagecaptioncommand.js +112 -138
  37. package/src/imagecaption.d.ts +29 -0
  38. package/src/imagecaption.js +12 -19
  39. package/src/imageconfig.d.ts +723 -0
  40. package/src/imageconfig.js +5 -0
  41. package/src/imageinline.d.ts +35 -0
  42. package/src/imageinline.js +12 -21
  43. package/src/imageinsert/imageinsertui.d.ts +49 -0
  44. package/src/imageinsert/imageinsertui.js +120 -158
  45. package/src/imageinsert/ui/imageinsertformrowview.d.ts +61 -0
  46. package/src/imageinsert/ui/imageinsertformrowview.js +37 -86
  47. package/src/imageinsert/ui/imageinsertpanelview.d.ts +106 -0
  48. package/src/imageinsert/ui/imageinsertpanelview.js +148 -258
  49. package/src/imageinsert/utils.d.ts +26 -0
  50. package/src/imageinsert/utils.js +42 -58
  51. package/src/imageinsert.d.ts +35 -0
  52. package/src/imageinsert.js +13 -84
  53. package/src/imageinsertviaurl.d.ts +34 -0
  54. package/src/imageinsertviaurl.js +12 -18
  55. package/src/imageresize/imageresizebuttons.d.ts +66 -0
  56. package/src/imageresize/imageresizebuttons.js +190 -255
  57. package/src/imageresize/imageresizeediting.d.ts +42 -0
  58. package/src/imageresize/imageresizeediting.js +100 -125
  59. package/src/imageresize/imageresizehandles.d.ts +30 -0
  60. package/src/imageresize/imageresizehandles.js +91 -123
  61. package/src/imageresize/resizeimagecommand.d.ts +47 -0
  62. package/src/imageresize/resizeimagecommand.js +48 -55
  63. package/src/imageresize.d.ts +29 -0
  64. package/src/imageresize.js +12 -209
  65. package/src/imagestyle/converters.d.ts +24 -0
  66. package/src/imagestyle/converters.js +60 -78
  67. package/src/imagestyle/imagestylecommand.d.ts +70 -0
  68. package/src/imagestyle/imagestylecommand.js +88 -124
  69. package/src/imagestyle/imagestyleediting.d.ts +54 -0
  70. package/src/imagestyle/imagestyleediting.js +90 -137
  71. package/src/imagestyle/imagestyleui.d.ts +60 -0
  72. package/src/imagestyle/imagestyleui.js +169 -277
  73. package/src/imagestyle/utils.d.ts +61 -0
  74. package/src/imagestyle/utils.js +253 -306
  75. package/src/imagestyle.d.ts +35 -0
  76. package/src/imagestyle.js +13 -261
  77. package/src/imagetextalternative/imagetextalternativecommand.d.ts +39 -0
  78. package/src/imagetextalternative/imagetextalternativecommand.js +31 -47
  79. package/src/imagetextalternative/imagetextalternativeediting.d.ts +32 -0
  80. package/src/imagetextalternative/imagetextalternativeediting.js +18 -25
  81. package/src/imagetextalternative/imagetextalternativeui.d.ts +72 -0
  82. package/src/imagetextalternative/imagetextalternativeui.js +156 -219
  83. package/src/imagetextalternative/ui/textalternativeformview.d.ts +72 -0
  84. package/src/imagetextalternative/ui/textalternativeformview.js +103 -192
  85. package/src/imagetextalternative.d.ts +32 -0
  86. package/src/imagetextalternative.js +12 -18
  87. package/src/imagetoolbar.d.ts +38 -0
  88. package/src/imagetoolbar.js +31 -77
  89. package/src/imageupload/imageuploadediting.d.ts +114 -0
  90. package/src/imageupload/imageuploadediting.js +308 -427
  91. package/src/imageupload/imageuploadprogress.d.ts +47 -0
  92. package/src/imageupload/imageuploadprogress.js +180 -261
  93. package/src/imageupload/imageuploadui.d.ts +29 -0
  94. package/src/imageupload/imageuploadui.js +41 -57
  95. package/src/imageupload/uploadimagecommand.d.ts +65 -0
  96. package/src/imageupload/uploadimagecommand.js +73 -87
  97. package/src/imageupload/utils.d.ts +33 -0
  98. package/src/imageupload/utils.js +87 -112
  99. package/src/imageupload.d.ts +34 -0
  100. package/src/imageupload.js +12 -61
  101. package/src/imageutils.d.ts +108 -0
  102. package/src/imageutils.js +233 -329
  103. package/src/index.d.ts +31 -0
  104. package/src/index.js +0 -2
  105. package/src/pictureediting.d.ts +86 -0
  106. package/src/pictureediting.js +101 -120
@@ -0,0 +1,42 @@
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/imagecaptionutils/utils
7
+ */
8
+ import type { DocumentSelection, Element, Selection, ViewElement, Match } from 'ckeditor5/src/engine';
9
+ import { Plugin, type PluginDependencies } from 'ckeditor5/src/core';
10
+ /**
11
+ * The image caption utilities plugin.
12
+ */
13
+ export default class ImageCaptionUtils extends Plugin {
14
+ /**
15
+ * @inheritDoc
16
+ */
17
+ static get pluginName(): 'ImageCaptionUtils';
18
+ /**
19
+ * @inheritDoc
20
+ */
21
+ static get requires(): PluginDependencies;
22
+ /**
23
+ * Returns the caption model element from a given image element. Returns `null` if no caption is found.
24
+ */
25
+ getCaptionFromImageModelElement(imageModelElement: Element): Element | null;
26
+ /**
27
+ * Returns the caption model element for a model selection. Returns `null` if the selection has no caption element ancestor.
28
+ */
29
+ getCaptionFromModelSelection(selection: Selection | DocumentSelection): Element | null;
30
+ /**
31
+ * {@link module:engine/view/matcher~Matcher} pattern. Checks if a given element is a `<figcaption>` element that is placed
32
+ * inside the image `<figure>` element.
33
+ * @returns Returns the object accepted by {@link module:engine/view/matcher~Matcher} or `null` if the element
34
+ * cannot be matched.
35
+ */
36
+ matchImageCaptionViewElement(element: ViewElement): Match | null;
37
+ }
38
+ declare module '@ckeditor/ckeditor5-core' {
39
+ interface PluginsMap {
40
+ [ImageCaptionUtils.pluginName]: ImageCaptionUtils;
41
+ }
42
+ }
@@ -2,88 +2,61 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * @module image/imagecaptionutils/utils
8
- */
9
-
10
5
  import { Plugin } from 'ckeditor5/src/core';
11
-
12
6
  import ImageUtils from '../imageutils';
13
-
14
7
  /**
15
8
  * The image caption utilities plugin.
16
- *
17
- * @extends module:core/plugin~Plugin
18
9
  */
19
10
  export default class ImageCaptionUtils extends Plugin {
20
- /**
21
- * @inheritDoc
22
- */
23
- static get pluginName() {
24
- return 'ImageCaptionUtils';
25
- }
26
-
27
- /**
28
- * @inheritDoc
29
- */
30
- static get requires() {
31
- return [ ImageUtils ];
32
- }
33
-
34
- /**
35
- * Returns the caption model element from a given image element. Returns `null` if no caption is found.
36
- *
37
- * @param {module:engine/model/element~Element} imageModelElement
38
- * @returns {module:engine/model/element~Element|null}
39
- */
40
- getCaptionFromImageModelElement( imageModelElement ) {
41
- for ( const node of imageModelElement.getChildren() ) {
42
- if ( !!node && node.is( 'element', 'caption' ) ) {
43
- return node;
44
- }
45
- }
46
-
47
- return null;
48
- }
49
-
50
- /**
51
- * Returns the caption model element for a model selection. Returns `null` if the selection has no caption element ancestor.
52
- *
53
- * @param {module:engine/model/selection~Selection} selection
54
- * @returns {module:engine/model/element~Element|null}
55
- */
56
- getCaptionFromModelSelection( selection ) {
57
- const imageUtils = this.editor.plugins.get( 'ImageUtils' );
58
- const captionElement = selection.getFirstPosition().findAncestor( 'caption' );
59
-
60
- if ( !captionElement ) {
61
- return null;
62
- }
63
-
64
- if ( imageUtils.isBlockImage( captionElement.parent ) ) {
65
- return captionElement;
66
- }
67
-
68
- return null;
69
- }
70
-
71
- /**
72
- * {@link module:engine/view/matcher~Matcher} pattern. Checks if a given element is a `<figcaption>` element that is placed
73
- * inside the image `<figure>` element.
74
- *
75
- * @param {module:engine/view/element~Element} element
76
- * @returns {Object|null} Returns the object accepted by {@link module:engine/view/matcher~Matcher} or `null` if the element
77
- * cannot be matched.
78
- */
79
- matchImageCaptionViewElement( element ) {
80
- const imageUtils = this.editor.plugins.get( 'ImageUtils' );
81
-
82
- // Convert only captions for images.
83
- if ( element.name == 'figcaption' && imageUtils.isBlockImageView( element.parent ) ) {
84
- return { name: true };
85
- }
86
-
87
- return null;
88
- }
11
+ /**
12
+ * @inheritDoc
13
+ */
14
+ static get pluginName() {
15
+ return 'ImageCaptionUtils';
16
+ }
17
+ /**
18
+ * @inheritDoc
19
+ */
20
+ static get requires() {
21
+ return [ImageUtils];
22
+ }
23
+ /**
24
+ * Returns the caption model element from a given image element. Returns `null` if no caption is found.
25
+ */
26
+ getCaptionFromImageModelElement(imageModelElement) {
27
+ for (const node of imageModelElement.getChildren()) {
28
+ if (!!node && node.is('element', 'caption')) {
29
+ return node;
30
+ }
31
+ }
32
+ return null;
33
+ }
34
+ /**
35
+ * Returns the caption model element for a model selection. Returns `null` if the selection has no caption element ancestor.
36
+ */
37
+ getCaptionFromModelSelection(selection) {
38
+ const imageUtils = this.editor.plugins.get('ImageUtils');
39
+ const captionElement = selection.getFirstPosition().findAncestor('caption');
40
+ if (!captionElement) {
41
+ return null;
42
+ }
43
+ if (imageUtils.isBlockImage(captionElement.parent)) {
44
+ return captionElement;
45
+ }
46
+ return null;
47
+ }
48
+ /**
49
+ * {@link module:engine/view/matcher~Matcher} pattern. Checks if a given element is a `<figcaption>` element that is placed
50
+ * inside the image `<figure>` element.
51
+ * @returns Returns the object accepted by {@link module:engine/view/matcher~Matcher} or `null` if the element
52
+ * cannot be matched.
53
+ */
54
+ matchImageCaptionViewElement(element) {
55
+ const imageUtils = this.editor.plugins.get('ImageUtils');
56
+ // Convert only captions for images.
57
+ if (element.name == 'figcaption' && imageUtils.isBlockImageView(element.parent)) {
58
+ return { name: true };
59
+ }
60
+ return null;
61
+ }
89
62
  }
@@ -0,0 +1,71 @@
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 toggle image caption command.
8
+ *
9
+ * This command is registered by {@link module:image/imagecaption/imagecaptionediting~ImageCaptionEditing} as the
10
+ * `'toggleImageCaption'` editor command.
11
+ *
12
+ * Executing this command:
13
+ *
14
+ * * either adds or removes the image caption of a selected image (depending on whether the caption is present or not),
15
+ * * removes the image caption if the selection is anchored in one.
16
+ *
17
+ * ```ts
18
+ * // Toggle the presence of the caption.
19
+ * editor.execute( 'toggleImageCaption' );
20
+ * ```
21
+ *
22
+ * **Note**: Upon executing this command, the selection will be set on the image if previously anchored in the caption element.
23
+ *
24
+ * **Note**: You can move the selection to the caption right away as it shows up upon executing this command by using
25
+ * the `focusCaptionOnShow` option:
26
+ *
27
+ * ```ts
28
+ * editor.execute( 'toggleImageCaption', { focusCaptionOnShow: true } );
29
+ * ```
30
+ */
31
+ export default class ToggleImageCaptionCommand extends Command {
32
+ value: boolean;
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ refresh(): void;
37
+ /**
38
+ * Executes the command.
39
+ *
40
+ * ```ts
41
+ * editor.execute( 'toggleImageCaption' );
42
+ * ```
43
+ *
44
+ * @param options Options for the executed command.
45
+ * @param options.focusCaptionOnShow When true and the caption shows up, the selection will be moved into it straight away.
46
+ * @fires execute
47
+ */
48
+ execute(options?: {
49
+ focusCaptionOnShow?: boolean;
50
+ }): void;
51
+ /**
52
+ * Shows the caption of the `<imageBlock>` or `<imageInline>`. Also:
53
+ *
54
+ * * it converts `<imageInline>` to `<imageBlock>` to show the caption,
55
+ * * it attempts to restore the caption content from the `ImageCaptionEditing` caption registry,
56
+ * * it moves the selection to the caption right away, it the `focusCaptionOnShow` option was set.
57
+ */
58
+ private _showImageCaption;
59
+ /**
60
+ * Hides the caption of a selected image (or an image caption the selection is anchored to).
61
+ *
62
+ * The content of the caption is stored in the `ImageCaptionEditing` caption registry to make this
63
+ * a reversible action.
64
+ */
65
+ private _hideImageCaption;
66
+ }
67
+ declare module '@ckeditor/ckeditor5-core' {
68
+ interface CommandsMap {
69
+ toggleImageCaption: ToggleImageCaptionCommand;
70
+ }
71
+ }
@@ -2,15 +2,8 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
- /**
7
- * @module image/imagecaption/toggleimagecaptioncommand
8
- */
9
-
10
5
  import { Command } from 'ckeditor5/src/core';
11
-
12
6
  import ImageBlockEditing from '../image/imageblockediting';
13
-
14
7
  /**
15
8
  * The toggle image caption command.
16
9
  *
@@ -22,143 +15,124 @@ import ImageBlockEditing from '../image/imageblockediting';
22
15
  * * either adds or removes the image caption of a selected image (depending on whether the caption is present or not),
23
16
  * * removes the image caption if the selection is anchored in one.
24
17
  *
25
- * // Toggle the presence of the caption.
26
- * editor.execute( 'toggleImageCaption' );
18
+ * ```ts
19
+ * // Toggle the presence of the caption.
20
+ * editor.execute( 'toggleImageCaption' );
21
+ * ```
27
22
  *
28
23
  * **Note**: Upon executing this command, the selection will be set on the image if previously anchored in the caption element.
29
24
  *
30
25
  * **Note**: You can move the selection to the caption right away as it shows up upon executing this command by using
31
26
  * the `focusCaptionOnShow` option:
32
27
  *
33
- * editor.execute( 'toggleImageCaption', { focusCaptionOnShow: true } );
34
- *
35
- * @extends module:core/command~Command
28
+ * ```ts
29
+ * editor.execute( 'toggleImageCaption', { focusCaptionOnShow: true } );
30
+ * ```
36
31
  */
37
32
  export default class ToggleImageCaptionCommand extends Command {
38
- /**
39
- * @inheritDoc
40
- */
41
- refresh() {
42
- const editor = this.editor;
43
- const imageCaptionUtils = editor.plugins.get( 'ImageCaptionUtils' );
44
-
45
- // Only block images can get captions.
46
- if ( !editor.plugins.has( ImageBlockEditing ) ) {
47
- this.isEnabled = false;
48
- this.value = false;
49
-
50
- return;
51
- }
52
-
53
- const selection = editor.model.document.selection;
54
- const selectedElement = selection.getSelectedElement();
55
-
56
- if ( !selectedElement ) {
57
- const ancestorCaptionElement = imageCaptionUtils.getCaptionFromModelSelection( selection );
58
-
59
- this.isEnabled = !!ancestorCaptionElement;
60
- this.value = !!ancestorCaptionElement;
61
-
62
- return;
63
- }
64
-
65
- // Block images support captions by default but the command should also be enabled for inline
66
- // images because toggling the caption when one is selected should convert it into a block image.
67
- this.isEnabled = this.editor.plugins.get( 'ImageUtils' ).isImage( selectedElement );
68
-
69
- if ( !this.isEnabled ) {
70
- this.value = false;
71
- } else {
72
- this.value = !!imageCaptionUtils.getCaptionFromImageModelElement( selectedElement );
73
- }
74
- }
75
-
76
- /**
77
- * Executes the command.
78
- *
79
- * editor.execute( 'toggleImageCaption' );
80
- *
81
- * @param {Object} [options] Options for the executed command.
82
- * @param {String} [options.focusCaptionOnShow] When true and the caption shows up, the selection will be moved into it straight away.
83
- * @fires execute
84
- */
85
- execute( options = {} ) {
86
- const { focusCaptionOnShow } = options;
87
-
88
- this.editor.model.change( writer => {
89
- if ( this.value ) {
90
- this._hideImageCaption( writer );
91
- } else {
92
- this._showImageCaption( writer, focusCaptionOnShow );
93
- }
94
- } );
95
- }
96
-
97
- /**
98
- * Shows the caption of the `<imageBlock>` or `<imageInline>`. Also:
99
- *
100
- * * it converts `<imageInline>` to `<imageBlock>` to show the caption,
101
- * * it attempts to restore the caption content from the `ImageCaptionEditing` caption registry,
102
- * * it moves the selection to the caption right away, it the `focusCaptionOnShow` option was set.
103
- *
104
- * @private
105
- * @param {module:engine/model/writer~Writer} writer
106
- */
107
- _showImageCaption( writer, focusCaptionOnShow ) {
108
- const model = this.editor.model;
109
- const selection = model.document.selection;
110
- const imageCaptionEditing = this.editor.plugins.get( 'ImageCaptionEditing' );
111
-
112
- let selectedImage = selection.getSelectedElement();
113
-
114
- const savedCaption = imageCaptionEditing._getSavedCaption( selectedImage );
115
-
116
- // Convert imageInline -> image first.
117
- if ( this.editor.plugins.get( 'ImageUtils' ).isInlineImage( selectedImage ) ) {
118
- this.editor.execute( 'imageTypeBlock' );
119
-
120
- // Executing the command created a new model element. Let's pick it again.
121
- selectedImage = selection.getSelectedElement();
122
- }
123
-
124
- // Try restoring the caption from the ImageCaptionEditing plugin storage.
125
- const newCaptionElement = savedCaption || writer.createElement( 'caption' );
126
-
127
- writer.append( newCaptionElement, selectedImage );
128
-
129
- if ( focusCaptionOnShow ) {
130
- writer.setSelection( newCaptionElement, 'in' );
131
- }
132
- }
133
-
134
- /**
135
- * Hides the caption of a selected image (or an image caption the selection is anchored to).
136
- *
137
- * The content of the caption is stored in the `ImageCaptionEditing` caption registry to make this
138
- * a reversible action.
139
- *
140
- * @private
141
- * @param {module:engine/model/writer~Writer} writer
142
- */
143
- _hideImageCaption( writer ) {
144
- const editor = this.editor;
145
- const selection = editor.model.document.selection;
146
- const imageCaptionEditing = editor.plugins.get( 'ImageCaptionEditing' );
147
- const imageCaptionUtils = editor.plugins.get( 'ImageCaptionUtils' );
148
- let selectedImage = selection.getSelectedElement();
149
- let captionElement;
150
-
151
- if ( selectedImage ) {
152
- captionElement = imageCaptionUtils.getCaptionFromImageModelElement( selectedImage );
153
- } else {
154
- captionElement = imageCaptionUtils.getCaptionFromModelSelection( selection );
155
- selectedImage = captionElement.parent;
156
- }
157
-
158
- // Store the caption content so it can be restored quickly if the user changes their mind even if they toggle image<->imageInline.
159
- imageCaptionEditing._saveCaption( selectedImage, captionElement );
160
-
161
- writer.setSelection( selectedImage, 'on' );
162
- writer.remove( captionElement );
163
- }
33
+ /**
34
+ * @inheritDoc
35
+ */
36
+ refresh() {
37
+ const editor = this.editor;
38
+ const imageCaptionUtils = editor.plugins.get('ImageCaptionUtils');
39
+ const imageUtils = editor.plugins.get('ImageUtils');
40
+ // Only block images can get captions.
41
+ if (!editor.plugins.has(ImageBlockEditing)) {
42
+ this.isEnabled = false;
43
+ this.value = false;
44
+ return;
45
+ }
46
+ const selection = editor.model.document.selection;
47
+ const selectedElement = selection.getSelectedElement();
48
+ if (!selectedElement) {
49
+ const ancestorCaptionElement = imageCaptionUtils.getCaptionFromModelSelection(selection);
50
+ this.isEnabled = !!ancestorCaptionElement;
51
+ this.value = !!ancestorCaptionElement;
52
+ return;
53
+ }
54
+ // Block images support captions by default but the command should also be enabled for inline
55
+ // images because toggling the caption when one is selected should convert it into a block image.
56
+ this.isEnabled = imageUtils.isImage(selectedElement);
57
+ if (!this.isEnabled) {
58
+ this.value = false;
59
+ }
60
+ else {
61
+ this.value = !!imageCaptionUtils.getCaptionFromImageModelElement(selectedElement);
62
+ }
63
+ }
64
+ /**
65
+ * Executes the command.
66
+ *
67
+ * ```ts
68
+ * editor.execute( 'toggleImageCaption' );
69
+ * ```
70
+ *
71
+ * @param options Options for the executed command.
72
+ * @param options.focusCaptionOnShow When true and the caption shows up, the selection will be moved into it straight away.
73
+ * @fires execute
74
+ */
75
+ execute(options = {}) {
76
+ const { focusCaptionOnShow } = options;
77
+ this.editor.model.change(writer => {
78
+ if (this.value) {
79
+ this._hideImageCaption(writer);
80
+ }
81
+ else {
82
+ this._showImageCaption(writer, focusCaptionOnShow);
83
+ }
84
+ });
85
+ }
86
+ /**
87
+ * Shows the caption of the `<imageBlock>` or `<imageInline>`. Also:
88
+ *
89
+ * * it converts `<imageInline>` to `<imageBlock>` to show the caption,
90
+ * * it attempts to restore the caption content from the `ImageCaptionEditing` caption registry,
91
+ * * it moves the selection to the caption right away, it the `focusCaptionOnShow` option was set.
92
+ */
93
+ _showImageCaption(writer, focusCaptionOnShow) {
94
+ const model = this.editor.model;
95
+ const selection = model.document.selection;
96
+ const imageCaptionEditing = this.editor.plugins.get('ImageCaptionEditing');
97
+ const imageUtils = this.editor.plugins.get('ImageUtils');
98
+ let selectedImage = selection.getSelectedElement();
99
+ const savedCaption = imageCaptionEditing._getSavedCaption(selectedImage);
100
+ // Convert imageInline -> image first.
101
+ if (imageUtils.isInlineImage(selectedImage)) {
102
+ this.editor.execute('imageTypeBlock');
103
+ // Executing the command created a new model element. Let's pick it again.
104
+ selectedImage = selection.getSelectedElement();
105
+ }
106
+ // Try restoring the caption from the ImageCaptionEditing plugin storage.
107
+ const newCaptionElement = savedCaption || writer.createElement('caption');
108
+ writer.append(newCaptionElement, selectedImage);
109
+ if (focusCaptionOnShow) {
110
+ writer.setSelection(newCaptionElement, 'in');
111
+ }
112
+ }
113
+ /**
114
+ * Hides the caption of a selected image (or an image caption the selection is anchored to).
115
+ *
116
+ * The content of the caption is stored in the `ImageCaptionEditing` caption registry to make this
117
+ * a reversible action.
118
+ */
119
+ _hideImageCaption(writer) {
120
+ const editor = this.editor;
121
+ const selection = editor.model.document.selection;
122
+ const imageCaptionEditing = editor.plugins.get('ImageCaptionEditing');
123
+ const imageCaptionUtils = editor.plugins.get('ImageCaptionUtils');
124
+ let selectedImage = selection.getSelectedElement();
125
+ let captionElement;
126
+ if (selectedImage) {
127
+ captionElement = imageCaptionUtils.getCaptionFromImageModelElement(selectedImage);
128
+ }
129
+ else {
130
+ captionElement = imageCaptionUtils.getCaptionFromModelSelection(selection);
131
+ selectedImage = captionElement.parent;
132
+ }
133
+ // Store the caption content so it can be restored quickly if the user changes their mind even if they toggle image<->imageInline.
134
+ imageCaptionEditing._saveCaption(selectedImage, captionElement);
135
+ writer.setSelection(selectedImage, 'on');
136
+ writer.remove(captionElement);
137
+ }
164
138
  }
@@ -0,0 +1,29 @@
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/imagecaption
7
+ */
8
+ import { Plugin, type PluginDependencies } from 'ckeditor5/src/core';
9
+ import '../theme/imagecaption.css';
10
+ /**
11
+ * The image caption plugin.
12
+ *
13
+ * For a detailed overview, check the {@glink features/images/images-captions image caption} documentation.
14
+ */
15
+ export default class ImageCaption extends Plugin {
16
+ /**
17
+ * @inheritDoc
18
+ */
19
+ static get requires(): PluginDependencies;
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ static get pluginName(): 'ImageCaption';
24
+ }
25
+ declare module '@ckeditor/ckeditor5-core' {
26
+ interface PluginsMap {
27
+ [ImageCaption.pluginName]: ImageCaption;
28
+ }
29
+ }
@@ -2,36 +2,29 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module image/imagecaption
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import ImageCaptionEditing from './imagecaption/imagecaptionediting';
12
10
  import ImageCaptionUI from './imagecaption/imagecaptionui';
13
-
14
11
  import '../theme/imagecaption.css';
15
-
16
12
  /**
17
13
  * The image caption plugin.
18
14
  *
19
15
  * For a detailed overview, check the {@glink features/images/images-captions image caption} documentation.
20
- *
21
- * @extends module:core/plugin~Plugin
22
16
  */
23
17
  export default class ImageCaption extends Plugin {
24
- /**
25
- * @inheritDoc
26
- */
27
- static get requires() {
28
- return [ ImageCaptionEditing, ImageCaptionUI ];
29
- }
30
-
31
- /**
32
- * @inheritDoc
33
- */
34
- static get pluginName() {
35
- return 'ImageCaption';
36
- }
18
+ /**
19
+ * @inheritDoc
20
+ */
21
+ static get requires() {
22
+ return [ImageCaptionEditing, ImageCaptionUI];
23
+ }
24
+ /**
25
+ * @inheritDoc
26
+ */
27
+ static get pluginName() {
28
+ return 'ImageCaption';
29
+ }
37
30
  }