Skip to content

Commit

Permalink
Update resizedHeight on image resize (when resize unit is px).
Browse files Browse the repository at this point in the history
  • Loading branch information
mmotyczynska committed Jun 13, 2023
1 parent c80b762 commit c2b8cff
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default class ImageResizeHandles extends Plugin {
return !imageStyle || imageStyle == 'block' || imageStyle == 'alignCenter';
},

onCommit( newValue ) {
onCommit( newWidth, newHeight ) {
// Get rid of the CSS class in case the command execution that follows is unsuccessful
// (e.g. Track Changes can override it and the new dimensions will not apply). Otherwise,
// the presence of the class and the absence of the width style will cause it to take 100%
Expand All @@ -120,7 +120,7 @@ export default class ImageResizeHandles extends Plugin {
writer.removeClass( RESIZED_IMAGE_CLASS, widgetView );
} );

editor.execute( 'resizeImage', { width: newValue } );
editor.execute( 'resizeImage', { width: newWidth, height: newHeight } );
}
} );

Expand Down
11 changes: 8 additions & 3 deletions packages/ckeditor5-image/src/imageresize/resizeimagecommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,26 @@ export default class ResizeImageCommand extends Command {
* @param options.width The new width of the image.
* @fires execute
*/
public override execute( options: { width: string | null } ): void {
public override execute( options: { width: string | null; height: string | null } ): void {
const editor = this.editor;
const model = editor.model;
const imageUtils: ImageUtils = editor.plugins.get( 'ImageUtils' );
const imageElement = imageUtils.getClosestSelectedImageElement( model.document.selection );

this.value = {
width: options.width,
height: null
height: options.height
};

if ( imageElement ) {
model.change( writer => {
writer.setAttribute( 'resizedWidth', options.width, imageElement );
writer.removeAttribute( 'resizedHeight', imageElement );

if ( options.height ) {
writer.setAttribute( 'resizedHeight', options.height, imageElement );
} else {
writer.removeAttribute( 'resizedHeight', imageElement );
}
} );
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-widget/src/widgetresize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export interface ResizerOptions {
* };
* ```
*/
onCommit: ( newValue: string ) => void;
onCommit: ( newWidth: string, newHeight: string | null ) => void;

getResizeHost: ( widgetWrapper: HTMLElement ) => HTMLElement;

Expand Down
5 changes: 3 additions & 2 deletions packages/ckeditor5-widget/src/widgetresize/resizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,13 @@ export default class Resizer extends ObservableMixin() {
*/
public commit(): void {
const unit = this._options.unit || '%';
const newValue = ( unit === '%' ? this.state.proposedWidthPercents : this.state.proposedWidth ) + unit;
const newWidth = ( unit === '%' ? this.state.proposedWidthPercents : this.state.proposedWidth ) + unit;
const newHeight = ( unit === '%' ) ? null : this.state.proposedHeight + unit;

// Both cleanup and onCommit callback are very likely to make view changes. Ensure that it is made in a single step.
this._options.editor.editing.view.change( () => {
this._cleanup();
this._options.onCommit( newValue );
this._options.onCommit( newWidth, newHeight );
} );
}

Expand Down

0 comments on commit c2b8cff

Please sign in to comment.