Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for image height attribute #14763

Merged
merged 143 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
7a114f1
Introduce the ImageSizeAttributes plugin.
mmotyczynska May 17, 2023
55d3c9b
Image size attributes manual test.
mmotyczynska May 17, 2023
2f301e6
Decoupled srcset and width. Made ImageSizeAttributes a mandatory depe…
mmotyczynska May 17, 2023
79ab7c3
Tests: Clean up after srcset and width decoupling.
mmotyczynska May 17, 2023
e8b9a68
Streamlined image srcset attribute (object -> string).
mmotyczynska May 17, 2023
868c5f4
Add height to img CSS.
mmotyczynska May 22, 2023
0458e84
Change image resize model attribute: width -> resizedWidth.
mmotyczynska May 23, 2023
f33dc43
Tests (PFW, GHS) update after introducing image width & height.
mmotyczynska May 24, 2023
4da8be5
Tests: cc for ImageSizeAttributes.
mmotyczynska May 25, 2023
df3894f
Description for added height style in image.
mmotyczynska May 25, 2023
fb9b061
Missing api docs.
mmotyczynska May 25, 2023
3c7f6e2
Missing manual test description.
mmotyczynska May 25, 2023
d1f1679
Remove unnecessary type.
mmotyczynska May 25, 2023
597cb0e
Update api docs.
mmotyczynska May 25, 2023
dd229db
Change image attributes: widthAttribute -> width, heightAttribute -> …
mmotyczynska May 25, 2023
c264818
Merge remote-tracking branch 'origin/master' into ck/14201-allow-and-…
mmotyczynska May 25, 2023
b389967
Tests: update the srcset attribute to the correct format.
mmotyczynska May 26, 2023
0bfa0e2
Merge remote-tracking branch 'origin/master' into ck/14201-allow-and-…
mmotyczynska May 30, 2023
30e9796
Set image width and height on upload.
mmotyczynska May 30, 2023
cb64f2c
Test: set image width and height after image insert.
mmotyczynska May 31, 2023
0b0af82
Test: set image width and height after upload.
mmotyczynska May 31, 2023
a3c1565
Merge remote-tracking branch 'origin/master' into ck/14202-set-image-…
mmotyczynska May 31, 2023
0e99f8a
Remove error handler.
mmotyczynska Jun 1, 2023
3188336
Improve listener for loading image and setting width and height.
mmotyczynska Jun 1, 2023
3eacd50
Do not override width and height if they were provided.
mmotyczynska Jun 1, 2023
7f84ec2
Merge pull request #14233 from ckeditor/ck/14201-allow-and-preserve-i…
mmotyczynska Jun 7, 2023
dae043f
Merge branch 'ck/epic/14146-support-image-height-attribute' into ck/1…
mmotyczynska Jun 7, 2023
961bbfe
Merge branch 'ck/epic/14146-support-image-height-attribute' into ck/1…
mmotyczynska Jun 7, 2023
82975fe
Add converter for image resizedHeight.
mmotyczynska Jun 7, 2023
65065ab
Remove resizedHeight on image resize.
mmotyczynska Jun 7, 2023
c80b762
Set inline aspect-ratio on image in editing downcast.
mmotyczynska Jun 12, 2023
c2b8cff
Update resizedHeight on image resize (when resize unit is px).
mmotyczynska Jun 13, 2023
8345334
Add PFO to manual test with image size attributes.
mmotyczynska Jun 13, 2023
d3a2752
Add manual test for image size attributes with ghs.
mmotyczynska Jun 13, 2023
226d1cd
Revert "Update resizedHeight on image resize (when resize unit is px)."
mmotyczynska Jun 16, 2023
6ba54a6
Reverse changes in image.css.
mmotyczynska Jun 21, 2023
603e961
Add height: auto for resized images (downcast).
mmotyczynska Jun 21, 2023
007ddec
Change upcast and downcast of image attributes.
mmotyczynska Jun 21, 2023
7d429e3
Set aspect-ratio for image view when resizing begins.
mmotyczynska Jun 21, 2023
1f1be62
Manual test with many image resize attributes test cases.
mmotyczynska Jun 21, 2023
fc13ab7
Upcast height style to height attribute if no other styles/attributes…
mmotyczynska Jun 23, 2023
e29e786
Convert resizedHeight to resizedWidth if there are attributes.
mmotyczynska Jun 23, 2023
1f602a7
Merge pull request #14305 from ckeditor/ck/14202-set-image-width-and-…
mmotyczynska Jun 26, 2023
02578a8
Correct manual test for image resize in px style only.
mmotyczynska Jun 26, 2023
470be3f
Upcast image aspect-ratio.
mmotyczynska Jun 26, 2023
02b1fb0
Revert "Upcast image aspect-ratio."
mmotyczynska Jun 26, 2023
941e9d1
Revert "Convert resizedHeight to resizedWidth if there are attributes."
mmotyczynska Jun 26, 2023
9ccaf9e
Revert "Upcast height style to height attribute if no other styles/at…
mmotyczynska Jun 26, 2023
bbb9f4a
Do not remove image_resized class when removing style height but styl…
mmotyczynska Jun 26, 2023
58ec57d
Remove unused styles.
mmotyczynska Jun 26, 2023
aafb59e
For image inline in editing set height style on img instead of span.
mmotyczynska Jun 26, 2023
e4faaa8
Remove style height from img after starting resizing.
mmotyczynska Jun 28, 2023
5a851e5
Remove style height after starting resizing - fix for block images.
mmotyczynska Jun 28, 2023
d8cea9a
Tests: image resize schema allowed attributes.
mmotyczynska Jun 30, 2023
7b3e614
Refactor editing downcast for resizedHeight.
mmotyczynska Jun 30, 2023
ecbd6e9
Tests: image block resizedHeight downcast.
mmotyczynska Jun 30, 2023
4c71086
Tests: image inline resizedHeight downcast.
mmotyczynska Jun 30, 2023
eb3bf54
Tests: image resizedWidth & resizedHeight upcast.
mmotyczynska Jun 30, 2023
93d0efe
Tests: image upcast from styles to attributes.
mmotyczynska Jun 30, 2023
452bcf3
Tests: image downcast aspect-ration inline style.
mmotyczynska Jul 1, 2023
5280b85
Tests: style height and aspect-ratio after starting resizing.
mmotyczynska Jul 3, 2023
86789e1
Add description for new ck-content resized image css rules.
mmotyczynska Jul 3, 2023
c301b21
Tests: correction for image size attributes.
mmotyczynska Jul 3, 2023
a49fee6
Tests: image resize command should remove resizedHeight.
mmotyczynska Jul 3, 2023
d7ea66a
Tests: improve manual test for image attributes & styles with full gh…
mmotyczynska Jul 3, 2023
412de18
Tests: add description to manual test for image attributes.
mmotyczynska Jul 3, 2023
fc0300d
Tests: manual test for image attributes - typo.
mmotyczynska Jul 3, 2023
47a97c6
Tests: manual test image sizes: remove easy image, add picture.
mmotyczynska Jul 4, 2023
a86f6ad
Tests: add picture cases to manual test for image sizes.
mmotyczynska Jul 4, 2023
3210440
Enforce importing ImageUtils.
mmotyczynska Jul 5, 2023
1985558
Refactor.
mmotyczynska Jul 5, 2023
4decf19
Comment upcasting image styles to width/height instead of resizedWidt…
mmotyczynska Jul 5, 2023
9d7fda9
Tests: update comment.
mmotyczynska Jul 5, 2023
662472b
Enforce importing ImageUtils.
mmotyczynska Jul 5, 2023
eea68dc
Picture (inline): make sure the image does not exceed the width of th…
mmotyczynska Jul 4, 2023
c7d79e3
Merge branch 'ck/14357-add-image-resizedHeight-attribute-convert-from…
mmotyczynska Jul 5, 2023
fb117a5
Do not set width after image upload if the image has width or height …
mmotyczynska Jul 5, 2023
dfe8da2
Merge pull request #14520 from ckeditor/ck/14357-add-image-resizedHei…
arkflpc Jul 6, 2023
ebe8d35
Revert "Picture (inline): make sure the image does not exceed the wid…
mmotyczynska Jul 7, 2023
7853261
Merge remote-tracking branch 'origin/ck/epic/14146-support-image-heig…
mmotyczynska Jul 7, 2023
c249d94
Merge pull request #14534 from ckeditor/ck/14532-image-sizes-and-picture
mmotyczynska Jul 7, 2023
461ef55
Fix appearance of inline images in editing, which are wider than edit…
mmotyczynska Jul 7, 2023
f22ff3c
Fix appearance of block images (editing & data), which are wider than…
mmotyczynska Jul 7, 2023
e18fa95
Add inline aspect-ratio for inline (editing) and block (editing & dat…
mmotyczynska Jul 7, 2023
d74868b
Add aspect ratio for inline images which are resized (data).
mmotyczynska Jul 7, 2023
a1b88a5
Remove css rules which are already covered in image.css file.
mmotyczynska Jul 7, 2023
174443f
Tests: update after setting aspect-ratio for more use cases.
mmotyczynska Jul 7, 2023
afc03de
Remove code for setting aspect-ratio on starting resizng, as now we s…
mmotyczynska Jul 7, 2023
08ecfb8
Merge remote-tracking branch 'origin/ck/epic/14146-support-image-heig…
mmotyczynska Jul 7, 2023
8cab8c9
Tests: do not set image width after image upload if width or height w…
mmotyczynska Jul 10, 2023
ed8a0a1
Tests: Refactor.
mmotyczynska Jul 11, 2023
aec7511
Merge pull request #14544 from ckeditor/ck/14542-images-wider-than-ed…
mmotyczynska Jul 11, 2023
c2be094
Merge remote-tracking branch 'origin/ck/epic/14146-support-image-heig…
mmotyczynska Jul 11, 2023
5f60808
Merge pull request #14556 from ckeditor/ck/14531-keep-image-sizes-aft…
arkflpc Jul 12, 2023
4c2508c
Set image width & height on image attribute change.
mmotyczynska Jul 12, 2023
ba9693d
Refactor: extract getImageWidgetFromImageView function to ImageUtils.
mmotyczynska Jul 12, 2023
635a031
Tests: set image width and height on image change.
mmotyczynska Jul 13, 2023
6e5a1c6
Make sure image widget view exists.
mmotyczynska Jul 13, 2023
a6a1c34
Update return type.
mmotyczynska Jul 14, 2023
96be2e4
Merge pull request #14577 from ckeditor/ck/14557-set-image-width-and-…
mmotyczynska Jul 14, 2023
2963fd9
Do not set aspect-ratio for pictures.
mmotyczynska Jul 14, 2023
564d1fd
Modify not setting aspect-ratio for pictures.
mmotyczynska Jul 25, 2023
1fbf2b7
Tests: do not set aspect-ratio for pictures.
mmotyczynska Jul 25, 2023
ce5de89
Merge pull request #14656 from ckeditor/ck/14579-handle-pictures-with…
arkflpc Jul 26, 2023
f41dd76
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Aug 2, 2023
3b647b9
Use local images in imagesizeattributesallcases manual test.
mmotyczynska Aug 2, 2023
d0a3b1b
Tests: small refactor for imagesizeattributesallcases manual test.
mmotyczynska Aug 2, 2023
9049912
Tests: update description for imagesizeattributesallcases manual test.
mmotyczynska Aug 2, 2023
9909482
Fix package.json package versions.
mmotyczynska Aug 7, 2023
bc8ece6
Correct version for ckeditor5-dev-utils.
mmotyczynska Aug 7, 2023
5df5ecb
Add missing as const.
mmotyczynska Aug 8, 2023
ef2583b
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Aug 8, 2023
3f9bf29
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Aug 10, 2023
15da281
Change setting image width and height on attribute change (will not b…
mmotyczynska Aug 22, 2023
12ceba1
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Aug 31, 2023
256445b
Change setting image width and height on attribute change (continued)…
mmotyczynska Aug 31, 2023
5f7bfa9
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Sep 1, 2023
864dca9
Refactor.
mmotyczynska Sep 5, 2023
00b2915
Improve regexp for image widget classes.
mmotyczynska Sep 5, 2023
9519309
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Sep 5, 2023
5ba2193
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Sep 6, 2023
00df4c9
Update plugins versions.
mmotyczynska Sep 6, 2023
96f52b7
Merge remote-tracking branch 'origin/master' into ck/epic/14146-suppo…
mmotyczynska Sep 6, 2023
7077b7c
Manual test init refactor.
niegowski Sep 6, 2023
47e4d2b
Give a variable a better name.
mmotyczynska Sep 7, 2023
b7fe537
Simplify downcast converter for resizedHeight attribute.
mmotyczynska Sep 7, 2023
a00c608
Update image view after removing `resizedHeight` attribute from model.
mmotyczynska Sep 7, 2023
041bb7d
Extract common logic to a helper function.
mmotyczynska Sep 7, 2023
de853ed
Correct plugin description.
mmotyczynska Sep 7, 2023
c8b7e38
Simplify upcast conversion for image for width attribute.
mmotyczynska Sep 7, 2023
0e2c084
Simplify upcast conversion for image for height attribute.
mmotyczynska Sep 7, 2023
aee4b43
Simplify upcast conversion for image srcset attribute.
mmotyczynska Sep 7, 2023
021073f
Move helper for checking width & height styles to utils. Use it in ad…
mmotyczynska Sep 7, 2023
b1ba144
Make function parameters more readable.
mmotyczynska Sep 7, 2023
d14fe1a
Give a function a better name.
mmotyczynska Sep 7, 2023
0787734
Remove unsused function params.
mmotyczynska Sep 7, 2023
1427f17
Explain using writer.batch while setting natural image width and heig…
mmotyczynska Sep 7, 2023
d7c05a2
Move internal function to utils file.
mmotyczynska Sep 7, 2023
44250e2
Change editor initialization in manual tests.
mmotyczynska Sep 7, 2023
8be30ee
Simplified imagesizeattributesallcases manual test.
mmotyczynska Sep 7, 2023
be765a0
Give a variable a more precise name.
mmotyczynska Sep 7, 2023
9cf1ed5
Simplify helper function.
mmotyczynska Sep 7, 2023
84d77c6
Update description for uploadComplete handler.
mmotyczynska Sep 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ckeditor5-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type { Marker } from './model/markercollection';
export type { default as Batch } from './model/batch';
export type { default as Differ, DiffItem, DiffItemAttribute, DiffItemInsert, DiffItemRemove } from './model/differ';
export type { default as Item } from './model/item';
export type { default as Node } from './model/node';
export type { default as Node, NodeAttributes } from './model/node';
export type { default as RootElement } from './model/rootelement';
export type {
default as Schema,
Expand Down
4 changes: 4 additions & 0 deletions packages/ckeditor5-html-support/tests/integrations/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,8 @@ describe( 'ImageElementSupport', () => {
'src',
'srcset',
'linkHref',
'width',
'height',
'htmlImgAttributes',
'htmlFigureAttributes',
'htmlLinkAttributes'
Expand Down Expand Up @@ -2509,6 +2511,8 @@ describe( 'ImageElementSupport', () => {
'alt',
'src',
'srcset',
'width',
'height',
'htmlA',
'htmlImgAttributes'
] );
Expand Down
2 changes: 2 additions & 0 deletions packages/ckeditor5-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"@ckeditor/ckeditor5-cloud-services": "39.0.2",
"@ckeditor/ckeditor5-core": "39.0.2",
"@ckeditor/ckeditor5-dev-utils": "^38.0.0",
"@ckeditor/ckeditor5-html-support": "39.0.2",
"@ckeditor/ckeditor5-paste-from-office": "39.0.2",
"@ckeditor/ckeditor5-easy-image": "39.0.2",
"@ckeditor/ckeditor5-editor-classic": "39.0.2",
"@ckeditor/ckeditor5-engine": "39.0.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/ckeditor5-image/src/augmentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
ImageCaptionUtils,
ImageInsertUI,
ImageResizeEditing,
ImageSizeAttributes,
ImageStyleEditing,
ImageStyleUI,
ImageTextAlternativeEditing,
Expand Down Expand Up @@ -76,6 +77,7 @@ declare module '@ckeditor/ckeditor5-core' {
[ ImageCaptionUtils.pluginName ]: ImageCaptionUtils;
[ ImageInsertUI.pluginName ]: ImageInsertUI;
[ ImageResizeEditing.pluginName ]: ImageResizeEditing;
[ ImageSizeAttributes.pluginName ]: ImageSizeAttributes;
[ ImageStyleEditing.pluginName ]: ImageStyleEditing;
[ ImageStyleUI.pluginName ]: ImageStyleUI;
[ ImageTextAlternativeEditing.pluginName ]: ImageTextAlternativeEditing;
Expand Down
26 changes: 5 additions & 21 deletions packages/ckeditor5-image/src/image/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import type {
import { first, type GetCallback } from 'ckeditor5/src/utils';
import type ImageUtils from '../imageutils';

type SrcsetAttributeType = null | { data: unknown; width: unknown };

/**
* Returns a function that converts the image view representation:
*
Expand Down Expand Up @@ -178,7 +176,7 @@ export function upcastPicture( imageUtils: ImageUtils ): ( dispatcher: UpcastDis
}

/**
* Converter used to convert the `srcset` model image attribute to the `srcset`, `sizes` and `width` attributes in the view.
* Converter used to convert the `srcset` model image attribute to the `srcset` and `sizes` attributes in the view.
*
* @internal
* @param imageType The type of the image.
Expand All @@ -197,27 +195,13 @@ export function downcastSrcsetAttribute(
const img = imageUtils.findViewImgElement( element )!;

if ( data.attributeNewValue === null ) {
const srcset = data.attributeOldValue as SrcsetAttributeType;

if ( srcset && srcset.data ) {
writer.removeAttribute( 'srcset', img );
writer.removeAttribute( 'sizes', img );

if ( srcset.width ) {
writer.removeAttribute( 'width', img );
}
}
writer.removeAttribute( 'srcset', img );
writer.removeAttribute( 'sizes', img );
} else {
const srcset = data.attributeNewValue as SrcsetAttributeType;

if ( srcset && srcset.data ) {
writer.setAttribute( 'srcset', srcset.data, img );
if ( data.attributeNewValue ) {
writer.setAttribute( 'srcset', data.attributeNewValue, img );
// Always outputting `100vw`. See https://github.com/ckeditor/ckeditor5-image/issues/2.
writer.setAttribute( 'sizes', '100vw', img );

if ( srcset.width ) {
writer.setAttribute( 'width', srcset.width, img );
}
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion packages/ckeditor5-image/src/image/imageblockediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from './converters';

import ImageEditing from './imageediting';
import ImageSizeAttributes from '../imagesizeattributes';
import ImageTypeCommand from './imagetypecommand';
import ImageUtils from '../imageutils';
import {
Expand All @@ -41,7 +42,7 @@ export default class ImageBlockEditing extends Plugin {
* @inheritDoc
*/
public static get requires() {
return [ ImageEditing, ImageUtils, ClipboardPipeline ] as const;
return [ ImageEditing, ImageSizeAttributes, ImageUtils, ClipboardPipeline ] as const;
}

/**
Expand Down
15 changes: 1 addition & 14 deletions packages/ckeditor5-image/src/image/imageediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,7 @@ export default class ImageEditing extends Plugin {
name: 'img',
key: 'srcset'
},
model: {
key: 'srcset',
value: ( viewImage: ViewElement ) => {
const value: Record<string, string> = {
data: viewImage.getAttribute( 'srcset' )!
};

if ( viewImage.hasAttribute( 'width' ) ) {
value.width = viewImage.getAttribute( 'width' )!;
}

return value;
}
}
model: 'srcset'
} );

const insertImageCommand = new InsertImageCommand( editor );
Expand Down
3 changes: 2 additions & 1 deletion packages/ckeditor5-image/src/image/imageinlineediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from './converters';

import ImageEditing from './imageediting';
import ImageSizeAttributes from '../imagesizeattributes';
import ImageTypeCommand from './imagetypecommand';
import ImageUtils from '../imageutils';
import {
Expand All @@ -40,7 +41,7 @@ export default class ImageInlineEditing extends Plugin {
* @inheritDoc
*/
public static get requires() {
return [ ImageEditing, ImageUtils, ClipboardPipeline ] as const;
return [ ImageEditing, ImageSizeAttributes, ImageUtils, ClipboardPipeline ] as const;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions packages/ckeditor5-image/src/image/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,28 @@ export function determineImageTypeForInsertionAtSelection(
// Otherwise insert an inline image.
return 'imageInline';
}

/**
* Returns parsed value of the size, but only if it contains unit: px.
*/
export function getSizeValueIfInPx( size: string | undefined ): number | null {
if ( size && size.endsWith( 'px' ) ) {
return parseInt( size );
}

return null;
}

/**
* Returns true if both styles (width and height) are set.
*
* If both image styles: width & height are set, they will override the image width & height attributes in the
* browser. In this case, the image looks the same as if these styles were applied to attributes instead of styles.
* That's why we can upcast these styles to width & height attributes instead of resizedWidth and resizedHeight.
*/
export function widthAndHeightStylesAreBothSet( viewElement: ViewElement ): boolean {
const widthStyle = getSizeValueIfInPx( viewElement.getStyle( 'width' ) );
const heightStyle = getSizeValueIfInPx( viewElement.getStyle( 'height' ) );

return !!( widthStyle && heightStyle );
}
79 changes: 69 additions & 10 deletions packages/ckeditor5-image/src/imageresize/imageresizeediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ViewElement } from 'ckeditor5/src/engine';
import { type Editor, Plugin } from 'ckeditor5/src/core';
import ImageUtils from '../imageutils';
import ResizeImageCommand from './resizeimagecommand';
import { widthAndHeightStylesAreBothSet } from '../image/utils';

/**
* The image resize editing feature.
Expand Down Expand Up @@ -82,11 +83,11 @@ export default class ImageResizeEditing extends Plugin {

private _registerSchema(): void {
if ( this.editor.plugins.has( 'ImageBlockEditing' ) ) {
this.editor.model.schema.extend( 'imageBlock', { allowAttributes: 'width' } );
this.editor.model.schema.extend( 'imageBlock', { allowAttributes: [ 'resizedWidth', 'resizedHeight' ] } );
}

if ( this.editor.plugins.has( 'ImageInlineEditing' ) ) {
this.editor.model.schema.extend( 'imageInline', { allowAttributes: 'width' } );
this.editor.model.schema.extend( 'imageInline', { allowAttributes: [ 'resizedWidth', 'resizedHeight' ] } );
}
}

Expand All @@ -97,23 +98,55 @@ export default class ImageResizeEditing extends Plugin {
*/
private _registerConverters( imageType: 'imageBlock' | 'imageInline' ) {
const editor = this.editor;
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );

// Dedicated converter to propagate image's attribute to the img tag.
editor.conversion.for( 'downcast' ).add( dispatcher =>
dispatcher.on( `attribute:width:${ imageType }`, ( evt, data, conversionApi ) => {
dispatcher.on( `attribute:resizedWidth:${ imageType }`, ( evt, data, conversionApi ) => {
if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
return;
}

const viewWriter = conversionApi.writer;
const figure = conversionApi.mapper.toViewElement( data.item );
const viewImg = conversionApi.mapper.toViewElement( data.item );

if ( data.attributeNewValue !== null ) {
viewWriter.setStyle( 'width', data.attributeNewValue, figure );
viewWriter.addClass( 'image_resized', figure );
viewWriter.setStyle( 'width', data.attributeNewValue, viewImg );
viewWriter.addClass( 'image_resized', viewImg );
} else {
viewWriter.removeStyle( 'width', figure );
viewWriter.removeClass( 'image_resized', figure );
viewWriter.removeStyle( 'width', viewImg );
viewWriter.removeClass( 'image_resized', viewImg );
}
} )
);

editor.conversion.for( 'dataDowncast' ).attributeToAttribute( {
model: {
name: imageType,
key: 'resizedHeight'
},
view: modelAttributeValue => ( {
key: 'style',
value: {
'height': modelAttributeValue
}
} )
} );

editor.conversion.for( 'editingDowncast' ).add( dispatcher =>
dispatcher.on( `attribute:resizedHeight:${ imageType }`, ( evt, data, conversionApi ) => {
if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
return;
}

const viewWriter = conversionApi.writer;
const viewImg = conversionApi.mapper.toViewElement( data.item );
const target = imageType === 'imageInline' ? imageUtils.findViewImgElement( viewImg ) : viewImg;

if ( data.attributeNewValue !== null ) {
viewWriter.setStyle( 'height', data.attributeNewValue, target );
} else {
viewWriter.removeStyle( 'height', target );
}
} )
);
Expand All @@ -127,8 +160,34 @@ export default class ImageResizeEditing extends Plugin {
}
},
model: {
key: 'width',
value: ( viewElement: ViewElement ) => viewElement.getStyle( 'width' )
key: 'resizedWidth',
value: ( viewElement: ViewElement ) => {
if ( widthAndHeightStylesAreBothSet( viewElement ) ) {
return null;
}

return viewElement.getStyle( 'width' );
}
}
} );

editor.conversion.for( 'upcast' )
.attributeToAttribute( {
view: {
name: imageType === 'imageBlock' ? 'figure' : 'img',
styles: {
height: /.+/
}
},
model: {
key: 'resizedHeight',
value: ( viewElement: ViewElement ) => {
if ( widthAndHeightStylesAreBothSet( viewElement ) ) {
return null;
}

return viewElement.getStyle( 'height' );
}
}
} );
}
Expand Down
18 changes: 13 additions & 5 deletions packages/ckeditor5-image/src/imageresize/imageresizehandles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* @module image/imageresize/imageresizehandles
*/

import type { Element, ViewContainerElement, ViewElement } from 'ckeditor5/src/engine';
import type { Element, ViewElement } from 'ckeditor5/src/engine';
import { Plugin } from 'ckeditor5/src/core';
import { WidgetResize } from 'ckeditor5/src/widget';
import ImageUtils from '../imageutils';

import ImageLoadObserver, { type ImageLoadedEvent } from '../image/imageloadobserver';
import type ResizeImageCommand from './resizeimagecommand';
Expand All @@ -22,8 +23,6 @@ const RESIZABLE_IMAGES_CSS_SELECTOR =
'span.image-inline.ck-widget > img,' +
'span.image-inline.ck-widget > picture > img';

const IMAGE_WIDGETS_CLASSES_MATCH_REGEXP = /(image|image-inline)/;

const RESIZED_IMAGE_CLASS = 'image_resized';

/**
Expand All @@ -37,7 +36,7 @@ export default class ImageResizeHandles extends Plugin {
* @inheritDoc
*/
public static get requires() {
return [ WidgetResize ] as const;
return [ WidgetResize, ImageUtils ] as const;
}

/**
Expand All @@ -63,6 +62,7 @@ export default class ImageResizeHandles extends Plugin {
private _setupResizerCreator(): void {
const editor = this.editor;
const editingView = editor.editing.view;
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );

editingView.addObserver( ImageLoadObserver );

Expand All @@ -74,7 +74,7 @@ export default class ImageResizeHandles extends Plugin {

const domConverter = editor.editing.view.domConverter;
const imageView = domConverter.domToView( domEvent.target as HTMLElement ) as ViewElement;
const widgetView = imageView.findAncestor( { classes: IMAGE_WIDGETS_CLASSES_MATCH_REGEXP } ) as ViewContainerElement;
const widgetView = imageUtils.getImageWidgetFromImageView( imageView )!;
let resizer = this.editor.plugins.get( WidgetResize ).getResizerByViewElement( widgetView );

if ( resizer ) {
Expand Down Expand Up @@ -130,6 +130,14 @@ export default class ImageResizeHandles extends Plugin {
writer.addClass( RESIZED_IMAGE_CLASS, widgetView );
} );
}

const target = imageModel.name === 'imageInline' ? imageView : widgetView;

if ( target.getStyle( 'height' ) ) {
editingView.change( writer => {
writer.removeStyle( 'height', target );
} );
}
} );

resizer.bind( 'isEnabled' ).to( this );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default class ResizeImageCommand extends Command {

this.isEnabled = !!element;

if ( !element || !element.hasAttribute( 'width' ) ) {
if ( !element || !element.hasAttribute( 'resizedWidth' ) ) {
this.value = null;
} else {
this.value = {
width: element.getAttribute( 'width' ) as string,
width: element.getAttribute( 'resizedWidth' ) as string,
height: null
};
}
Expand Down Expand Up @@ -71,7 +71,9 @@ export default class ResizeImageCommand extends Command {

if ( imageElement ) {
model.change( writer => {
writer.setAttribute( 'width', options.width, imageElement );
writer.setAttribute( 'resizedWidth', options.width, imageElement );
writer.removeAttribute( 'resizedHeight', imageElement );
imageUtils.setImageNaturalSizeAttributes( imageElement );
} );
}
}
Expand Down
Loading