@ckeditor/ckeditor5-image 36.0.1 → 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
package/src/autoimage.js CHANGED
@@ -2,180 +2,131 @@
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/autoimage
8
7
  */
9
-
10
8
  import { Plugin } from 'ckeditor5/src/core';
11
9
  import { Clipboard } from 'ckeditor5/src/clipboard';
12
10
  import { LivePosition, LiveRange } from 'ckeditor5/src/engine';
13
11
  import { Undo } from 'ckeditor5/src/undo';
14
12
  import { Delete } from 'ckeditor5/src/typing';
15
13
  import { global } from 'ckeditor5/src/utils';
16
-
17
14
  import ImageUtils from './imageutils';
18
-
19
15
  // Implements the pattern: http(s)://(www.)example.com/path/to/resource.ext?query=params&maybe=too.
20
- const IMAGE_URL_REGEXP = new RegExp( String( /^(http(s)?:\/\/)?[\w-]+\.[\w.~:/[\]@!$&'()*+,;=%-]+/.source +
21
- /\.(jpg|jpeg|png|gif|ico|webp|JPG|JPEG|PNG|GIF|ICO|WEBP)/.source +
22
- /(\?[\w.~:/[\]@!$&'()*+,;=%-]*)?/.source +
23
- /(#[\w.~:/[\]@!$&'()*+,;=%-]*)?$/.source ) );
24
-
16
+ const IMAGE_URL_REGEXP = new RegExp(String(/^(http(s)?:\/\/)?[\w-]+\.[\w.~:/[\]@!$&'()*+,;=%-]+/.source +
17
+ /\.(jpg|jpeg|png|gif|ico|webp|JPG|JPEG|PNG|GIF|ICO|WEBP)/.source +
18
+ /(\?[\w.~:/[\]@!$&'()*+,;=%-]*)?/.source +
19
+ /(#[\w.~:/[\]@!$&'()*+,;=%-]*)?$/.source));
25
20
  /**
26
21
  * The auto-image plugin. It recognizes image links in the pasted content and embeds
27
22
  * them shortly after they are injected into the document.
28
- *
29
- * @extends module:core/plugin~Plugin
30
23
  */
31
24
  export default class AutoImage extends Plugin {
32
- /**
33
- * @inheritDoc
34
- */
35
- static get requires() {
36
- return [ Clipboard, ImageUtils, Undo, Delete ];
37
- }
38
-
39
- /**
40
- * @inheritDoc
41
- */
42
- static get pluginName() {
43
- return 'AutoImage';
44
- }
45
-
46
- /**
47
- * @inheritDoc
48
- */
49
- constructor( editor ) {
50
- super( editor );
51
-
52
- /**
53
- * The paste–to–embed `setTimeout` ID. Stored as a property to allow
54
- * cleaning of the timeout.
55
- *
56
- * @private
57
- * @member {Number} #_timeoutId
58
- */
59
- this._timeoutId = null;
60
-
61
- /**
62
- * The position where the `<imageBlock>` element will be inserted after the timeout,
63
- * determined each time a new content is pasted into the document.
64
- *
65
- * @private
66
- * @member {module:engine/model/liveposition~LivePosition} #_positionToInsert
67
- */
68
- this._positionToInsert = null;
69
- }
70
-
71
- /**
72
- * @inheritDoc
73
- */
74
- init() {
75
- const editor = this.editor;
76
- const modelDocument = editor.model.document;
77
-
78
- // We need to listen on `Clipboard#inputTransformation` because we need to save positions of selection.
79
- // After pasting, the content between those positions will be checked for a URL that could be transformed
80
- // into an image.
81
- this.listenTo( editor.plugins.get( 'ClipboardPipeline' ), 'inputTransformation', () => {
82
- const firstRange = modelDocument.selection.getFirstRange();
83
-
84
- const leftLivePosition = LivePosition.fromPosition( firstRange.start );
85
- leftLivePosition.stickiness = 'toPrevious';
86
-
87
- const rightLivePosition = LivePosition.fromPosition( firstRange.end );
88
- rightLivePosition.stickiness = 'toNext';
89
-
90
- modelDocument.once( 'change:data', () => {
91
- this._embedImageBetweenPositions( leftLivePosition, rightLivePosition );
92
-
93
- leftLivePosition.detach();
94
- rightLivePosition.detach();
95
- }, { priority: 'high' } );
96
- } );
97
-
98
- editor.commands.get( 'undo' ).on( 'execute', () => {
99
- if ( this._timeoutId ) {
100
- global.window.clearTimeout( this._timeoutId );
101
- this._positionToInsert.detach();
102
-
103
- this._timeoutId = null;
104
- this._positionToInsert = null;
105
- }
106
- }, { priority: 'high' } );
107
- }
108
-
109
- /**
110
- * Analyzes the part of the document between provided positions in search for a URL representing an image.
111
- * When the URL is found, it is automatically converted into an image.
112
- *
113
- * @protected
114
- * @param {module:engine/model/liveposition~LivePosition} leftPosition Left position of the selection.
115
- * @param {module:engine/model/liveposition~LivePosition} rightPosition Right position of the selection.
116
- */
117
- _embedImageBetweenPositions( leftPosition, rightPosition ) {
118
- const editor = this.editor;
119
- // TODO: Use a marker instead of LiveRange & LivePositions.
120
- const urlRange = new LiveRange( leftPosition, rightPosition );
121
- const walker = urlRange.getWalker( { ignoreElementEnd: true } );
122
- const selectionAttributes = Object.fromEntries( editor.model.document.selection.getAttributes() );
123
- const imageUtils = this.editor.plugins.get( 'ImageUtils' );
124
-
125
- let src = '';
126
-
127
- for ( const node of walker ) {
128
- if ( node.item.is( '$textProxy' ) ) {
129
- src += node.item.data;
130
- }
131
- }
132
-
133
- src = src.trim();
134
-
135
- // If the URL does not match the image URL regexp, let's skip that.
136
- if ( !src.match( IMAGE_URL_REGEXP ) ) {
137
- urlRange.detach();
138
-
139
- return;
140
- }
141
-
142
- // Position will not be available in the `setTimeout` function so let's clone it.
143
- this._positionToInsert = LivePosition.fromPosition( leftPosition );
144
-
145
- // This action mustn't be executed if undo was called between pasting and auto-embedding.
146
- this._timeoutId = global.window.setTimeout( () => {
147
- // Do nothing if image element cannot be inserted at the current position.
148
- // See https://github.com/ckeditor/ckeditor5/issues/2763.
149
- // Condition must be checked after timeout - pasting may take place on an element, replacing it. The final position matters.
150
- const imageCommand = editor.commands.get( 'insertImage' );
151
-
152
- if ( !imageCommand.isEnabled ) {
153
- urlRange.detach();
154
-
155
- return;
156
- }
157
-
158
- editor.model.change( writer => {
159
- this._timeoutId = null;
160
-
161
- writer.remove( urlRange );
162
- urlRange.detach();
163
-
164
- let insertionPosition;
165
-
166
- // Check if the position where the element should be inserted is still valid.
167
- // Otherwise leave it as undefined to use the logic of insertImage().
168
- if ( this._positionToInsert.root.rootName !== '$graveyard' ) {
169
- insertionPosition = this._positionToInsert.toPosition();
170
- }
171
-
172
- imageUtils.insertImage( { ...selectionAttributes, src }, insertionPosition );
173
-
174
- this._positionToInsert.detach();
175
- this._positionToInsert = null;
176
- } );
177
-
178
- editor.plugins.get( 'Delete' ).requestUndoOnBackspace();
179
- }, 100 );
180
- }
25
+ /**
26
+ * @inheritDoc
27
+ */
28
+ static get requires() {
29
+ return [Clipboard, ImageUtils, Undo, Delete];
30
+ }
31
+ /**
32
+ * @inheritDoc
33
+ */
34
+ static get pluginName() {
35
+ return 'AutoImage';
36
+ }
37
+ /**
38
+ * @inheritDoc
39
+ */
40
+ constructor(editor) {
41
+ super(editor);
42
+ this._timeoutId = null;
43
+ this._positionToInsert = null;
44
+ }
45
+ /**
46
+ * @inheritDoc
47
+ */
48
+ init() {
49
+ const editor = this.editor;
50
+ const modelDocument = editor.model.document;
51
+ const clipboardPipeline = editor.plugins.get('ClipboardPipeline');
52
+ // We need to listen on `Clipboard#inputTransformation` because we need to save positions of selection.
53
+ // After pasting, the content between those positions will be checked for a URL that could be transformed
54
+ // into an image.
55
+ this.listenTo(clipboardPipeline, 'inputTransformation', () => {
56
+ const firstRange = modelDocument.selection.getFirstRange();
57
+ const leftLivePosition = LivePosition.fromPosition(firstRange.start);
58
+ leftLivePosition.stickiness = 'toPrevious';
59
+ const rightLivePosition = LivePosition.fromPosition(firstRange.end);
60
+ rightLivePosition.stickiness = 'toNext';
61
+ modelDocument.once('change:data', () => {
62
+ this._embedImageBetweenPositions(leftLivePosition, rightLivePosition);
63
+ leftLivePosition.detach();
64
+ rightLivePosition.detach();
65
+ }, { priority: 'high' });
66
+ });
67
+ editor.commands.get('undo').on('execute', () => {
68
+ if (this._timeoutId) {
69
+ global.window.clearTimeout(this._timeoutId);
70
+ this._positionToInsert.detach();
71
+ this._timeoutId = null;
72
+ this._positionToInsert = null;
73
+ }
74
+ }, { priority: 'high' });
75
+ }
76
+ /**
77
+ * Analyzes the part of the document between provided positions in search for a URL representing an image.
78
+ * When the URL is found, it is automatically converted into an image.
79
+ *
80
+ * @param leftPosition Left position of the selection.
81
+ * @param rightPosition Right position of the selection.
82
+ */
83
+ _embedImageBetweenPositions(leftPosition, rightPosition) {
84
+ const editor = this.editor;
85
+ // TODO: Use a marker instead of LiveRange & LivePositions.
86
+ const urlRange = new LiveRange(leftPosition, rightPosition);
87
+ const walker = urlRange.getWalker({ ignoreElementEnd: true });
88
+ const selectionAttributes = Object.fromEntries(editor.model.document.selection.getAttributes());
89
+ const imageUtils = this.editor.plugins.get('ImageUtils');
90
+ let src = '';
91
+ for (const node of walker) {
92
+ if (node.item.is('$textProxy')) {
93
+ src += node.item.data;
94
+ }
95
+ }
96
+ src = src.trim();
97
+ // If the URL does not match the image URL regexp, let's skip that.
98
+ if (!src.match(IMAGE_URL_REGEXP)) {
99
+ urlRange.detach();
100
+ return;
101
+ }
102
+ // Position will not be available in the `setTimeout` function so let's clone it.
103
+ this._positionToInsert = LivePosition.fromPosition(leftPosition);
104
+ // This action mustn't be executed if undo was called between pasting and auto-embedding.
105
+ this._timeoutId = setTimeout(() => {
106
+ // Do nothing if image element cannot be inserted at the current position.
107
+ // See https://github.com/ckeditor/ckeditor5/issues/2763.
108
+ // Condition must be checked after timeout - pasting may take place on an element, replacing it. The final position matters.
109
+ const imageCommand = editor.commands.get('insertImage');
110
+ if (!imageCommand.isEnabled) {
111
+ urlRange.detach();
112
+ return;
113
+ }
114
+ editor.model.change(writer => {
115
+ this._timeoutId = null;
116
+ writer.remove(urlRange);
117
+ urlRange.detach();
118
+ let insertionPosition;
119
+ // Check if the position where the element should be inserted is still valid.
120
+ // Otherwise leave it as undefined to use the logic of insertImage().
121
+ if (this._positionToInsert.root.rootName !== '$graveyard') {
122
+ insertionPosition = this._positionToInsert.toPosition();
123
+ }
124
+ imageUtils.insertImage({ ...selectionAttributes, src }, insertionPosition);
125
+ this._positionToInsert.detach();
126
+ this._positionToInsert = null;
127
+ });
128
+ const deletePlugin = editor.plugins.get('Delete');
129
+ deletePlugin.requestUndoOnBackspace();
130
+ }, 100);
131
+ }
181
132
  }
@@ -0,0 +1,66 @@
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/image/converters
7
+ */
8
+ import type { DowncastDispatcher, UpcastDispatcher } from 'ckeditor5/src/engine';
9
+ import type ImageUtils from '../imageutils';
10
+ /**
11
+ * Returns a function that converts the image view representation:
12
+ *
13
+ * ```html
14
+ * <figure class="image"><img src="..." alt="..."></img></figure>
15
+ * ```
16
+ *
17
+ * to the model representation:
18
+ *
19
+ * ```html
20
+ * <imageBlock src="..." alt="..."></imageBlock>
21
+ * ```
22
+ *
23
+ * The entire content of the `<figure>` element except the first `<img>` is being converted as children
24
+ * of the `<imageBlock>` model element.
25
+ *
26
+ * @internal
27
+ */
28
+ export declare function upcastImageFigure(imageUtils: ImageUtils): (dispatcher: UpcastDispatcher) => void;
29
+ /**
30
+ * Returns a function that converts the image view representation:
31
+ *
32
+ * ```html
33
+ * <picture><source ... /><source ... />...<img ... /></picture>
34
+ * ```
35
+ *
36
+ * to the model representation as the `sources` attribute:
37
+ *
38
+ * ```html
39
+ * <image[Block|Inline] ... sources="..."></image[Block|Inline]>
40
+ * ```
41
+ *
42
+ * @internal
43
+ */
44
+ export declare function upcastPicture(imageUtils: ImageUtils): (dispatcher: UpcastDispatcher) => void;
45
+ /**
46
+ * Converter used to convert the `srcset` model image attribute to the `srcset`, `sizes` and `width` attributes in the view.
47
+ *
48
+ * @internal
49
+ * @param imageType The type of the image.
50
+ */
51
+ export declare function downcastSrcsetAttribute(imageUtils: ImageUtils, imageType: 'imageBlock' | 'imageInline'): (dispatcher: DowncastDispatcher) => void;
52
+ /**
53
+ * Converts the `source` model attribute to the `<picture><source /><source />...<img /></picture>`
54
+ * view structure.
55
+ *
56
+ * @internal
57
+ */
58
+ export declare function downcastSourcesAttribute(imageUtils: ImageUtils): (dispatcher: DowncastDispatcher) => void;
59
+ /**
60
+ * Converter used to convert a given image attribute from the model to the view.
61
+ *
62
+ * @internal
63
+ * @param imageType The type of the image.
64
+ * @param attributeKey The name of the attribute to convert.
65
+ */
66
+ export declare function downcastImageAttribute(imageUtils: ImageUtils, imageType: 'imageBlock' | 'imageInline', attributeKey: string): (dispatcher: DowncastDispatcher) => void;