From e29e786844be92ba89980ddbe9848af5a8e9125d Mon Sep 17 00:00:00 2001 From: Marta Motyczynska Date: Fri, 23 Jun 2023 15:24:07 +0200 Subject: [PATCH] Convert resizedHeight to resizedWidth if there are attributes. --- .../src/imageresize/imageresizeediting.ts | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/ckeditor5-image/src/imageresize/imageresizeediting.ts b/packages/ckeditor5-image/src/imageresize/imageresizeediting.ts index 018a0063f58..e53d0a6207b 100644 --- a/packages/ckeditor5-image/src/imageresize/imageresizeediting.ts +++ b/packages/ckeditor5-image/src/imageresize/imageresizeediting.ts @@ -133,7 +133,9 @@ export default class ImageResizeEditing extends Plugin { viewWriter.addClass( 'image_resized', figure ); } else { viewWriter.removeStyle( 'height', figure ); - viewWriter.removeClass( 'image_resized', figure ); + if ( !figure.getStyle( 'width' ) ) { + viewWriter.removeClass( 'image_resized', figure ); + } } } ) ); @@ -186,6 +188,38 @@ export default class ImageResizeEditing extends Plugin { return viewElement.getStyle( 'height' ); } } + } ) + .attributeToAttribute( { + view: { + name: imageType === 'imageBlock' ? 'figure' : 'img', + styles: { + height: /.+/ + } + }, + model: { + key: 'resizedWidth', + value: ( viewElement: ViewElement ) => { + const widthStyle = imageUtils.getSizeInPx( viewElement.getStyle( 'width' ) ); + const heightStyle = imageUtils.getSizeInPx( viewElement.getStyle( 'height' ) ); + + if ( !widthStyle && !heightStyle ) { + return null; + } + + const img = imageUtils.findViewImgElement( viewElement )!; + const widthAttr = img.getAttribute( 'width' ); + const heightAttr = img.getAttribute( 'height' ); + const imgHasAttributes = widthAttr || heightAttr; + + if ( heightStyle && !viewElement.getStyle( 'width' ) ) { + if ( imgHasAttributes ) { + return Math.round( parseInt( widthAttr! ) * heightStyle / parseInt( heightAttr! ) ) + 'px'; + } + } + + return null; + } + } } ); } }