Skip to content

Upgrade cropperjs to v2, add avatar id and link to it #33827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 129 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"chartjs-adapter-dayjs-4": "1.0.4",
"chartjs-plugin-zoom": "2.2.0",
"clippie": "4.1.5",
"cropperjs": "1.6.2",
"cropperjs": "2.0.0",
"css-loader": "7.1.2",
"dayjs": "1.11.13",
"dropzone": "6.0.0-beta.2",
Expand Down
8 changes: 5 additions & 3 deletions web_src/css/features/cropper.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@import "cropperjs/dist/cropper.css";

.avatar-file-with-cropper + .cropper-panel .cropper-wrapper {
max-width: 400px;
width: 400px;
max-height: 400px;
}

cropper-canvas {
height: 400px;
}
56 changes: 39 additions & 17 deletions web_src/js/features/comp/Cropper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {showElem, type DOMEvent} from '../../utils/dom.ts';
import {createElementFromHTML, showElem, type DOMEvent} from '../../utils/dom.ts';
import type {CropperCanvas, CropperImage} from 'cropperjs';

type CropperOpts = {
container: HTMLElement,
Expand All @@ -7,33 +8,54 @@
}

async function initCompCropper({container, fileInput, imageSource}: CropperOpts) {
const {default: Cropper} = await import(/* webpackChunkName: "cropperjs" */'cropperjs');
await import(/* webpackChunkName: "cropperjs" */'cropperjs');
let currentFileName = '';
let currentFileLastModified = 0;
const cropper = new Cropper(imageSource, {
aspectRatio: 1,
viewMode: 2,
autoCrop: false,
crop() {
const canvas = cropper.getCroppedCanvas();
canvas.toBlob((blob) => {
const croppedFileName = currentFileName.replace(/\.[^.]{3,4}$/, '.png');
const croppedFile = new File([blob], croppedFileName, {type: 'image/png', lastModified: currentFileLastModified});
const dataTransfer = new DataTransfer();
dataTransfer.items.add(croppedFile);
fileInput.files = dataTransfer.files;
});
},

const canvasEl = createElementFromHTML<CropperCanvas>(`
<cropper-canvas background>
<cropper-image src="${imageSource.src}" scalable skewable translatable></cropper-image>
<cropper-shade hidden></cropper-shade>
<cropper-handle action="select" plain></cropper-handle>
<cropper-selection initial-coverage="0.5" initial-aspect-ratio="1" movable resizable>
<cropper-grid role="grid" covered></cropper-grid>
<cropper-crosshair centered></cropper-crosshair>
<cropper-handle action="move" theme-color="#ffffff23"></cropper-handle>
<cropper-handle action="n-resize"></cropper-handle>
<cropper-handle action="e-resize"></cropper-handle>
<cropper-handle action="s-resize"></cropper-handle>
<cropper-handle action="w-resize"></cropper-handle>
<cropper-handle action="ne-resize"></cropper-handle>
<cropper-handle action="nw-resize"></cropper-handle>
<cropper-handle action="se-resize"></cropper-handle>
<cropper-handle action="sw-resize"></cropper-handle>
</cropper-selection>
</cropper-canvas>
`);

const imgEl = canvasEl.querySelector<CropperImage>('cropper-image');

canvasEl.addEventListener('action', async (e) => {
const canvas = await (e.target as CropperCanvas).$toCanvas();
canvas.toBlob((blob) => {
const croppedFileName = currentFileName.replace(/\.[^.]{3,4}$/, '.png');
const croppedFile = new File([blob], croppedFileName, {type: 'image/png', lastModified: currentFileLastModified});
const dataTransfer = new DataTransfer();
dataTransfer.items.add(croppedFile);
fileInput.files = dataTransfer.files;
});
});

imageSource.replaceWith(canvasEl);

fileInput.addEventListener('input', (e: DOMEvent<Event, HTMLInputElement>) => {
const files = e.target.files;
if (files?.length > 0) {
currentFileName = files[0].name;
currentFileLastModified = files[0].lastModified;
const fileURL = URL.createObjectURL(files[0]);
imageSource.src = fileURL;
cropper.replace(fileURL);
imgEl.src = fileURL;

Check failure on line 58 in web_src/js/features/comp/Cropper.ts

View workflow job for this annotation

GitHub Actions / frontend

Property 'src' does not exist on type 'CropperImage'.
showElem(container);
}
});
Expand Down
Loading