Skip to content

Commit

Permalink
Merge branch 'file-upload-image-crop' into 6.2-user-coverphoto
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyperghost committed Dec 9, 2024
2 parents 663c9a6 + 243135e commit 89143be
Show file tree
Hide file tree
Showing 3 changed files with 51 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.

10 changes: 10 additions & 0 deletions wcfsetup/install/files/style/ui/dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,13 @@ html[data-color-scheme="dark"] .dialog::backdrop {
min-width: 0;
}
}

.dialog cropper-canvas {
margin-left: auto;
margin-right: auto;
}

/* If the height of the image is many times greater than the width, a white area would be displayed at the bottom and/or top. */
.dialog cropper-shade {
outline-width: max(100vh, 100vw) !important;
}

0 comments on commit 89143be

Please sign in to comment.