Skip to content

Commit

Permalink
Convert resizedHeight to resizedWidth if there are attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmotyczynska committed Jun 23, 2023
1 parent fc13ab7 commit e29e786
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/ckeditor5-image/src/imageresize/imageresizeediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
} )
);
Expand Down Expand Up @@ -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;
}
}
} );
}
}

0 comments on commit e29e786

Please sign in to comment.