Skip to content

Commit

Permalink
Recalculate cropper height on window resize.
Browse files Browse the repository at this point in the history
Set height on container(parent) height
  • Loading branch information
Cyperghost committed Dec 9, 2024
1 parent a9d93bf commit 58dce4d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
26 changes: 24 additions & 2 deletions ts/WoltLabSuite/Core/Component/Image/Cropper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getPhrase } from "WoltLabSuite/Core/Language";
import WoltlabCoreDialogElement from "WoltLabSuite/Core/Element/woltlab-core-dialog";
import * as ExifUtil from "WoltLabSuite/Core/Image/ExifUtil";
import ExifReader from "exifreader";
import DomUtil from "WoltLabSuite/Core/Dom/Util";

export interface CropperConfiguration {
aspectRatio: number;
Expand All @@ -27,8 +28,8 @@ export interface CropperConfiguration {

function inSelection(selection: Selection, maxSelection: Selection): boolean {
return (
selection.x >= maxSelection.x &&
selection.y >= maxSelection.y &&
Math.ceil(selection.x) >= maxSelection.x &&
Math.ceil(selection.y) >= maxSelection.y &&
Math.ceil(selection.x + selection.width) <= Math.ceil(maxSelection.x + maxSelection.width) &&
Math.ceil(selection.y + selection.height) <= Math.ceil(maxSelection.y + maxSelection.height)
);
Expand Down Expand Up @@ -83,6 +84,21 @@ abstract class ImageCropper {

this.createCropper();

const resize = () => {
this.centerSelection();
};

window.addEventListener("resize", resize, { passive: true });
this.dialog.addEventListener(
"afterClose",
() => {
window.removeEventListener("resize", resize);
},
{
once: true,
},
);

return new Promise<File>((resolve, reject) => {
this.dialog!.addEventListener("primary", () => {
void this.getCanvas()
Expand Down Expand Up @@ -371,6 +387,12 @@ class MinMaxImageCropper extends ImageCropper {
}

protected centerSelection(): void {
// Reset to get the maximum available height
this.cropperCanvas!.style.height = "";

const dimensions = DomUtil.outerDimensions(this.cropperCanvas!.parentElement!);
this.cropperCanvas!.style.height = `${dimensions.height}px`;

this.cropperImage!.$center("contain");
this.#cropperCanvasRect = this.cropperImage!.getBoundingClientRect();

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58dce4d

Please sign in to comment.