|
1 | 1 | import {showElem} from '../../utils/dom.ts'; |
2 | 2 |
|
3 | | -export async function initCompCropper() { |
4 | | - const cropperContainer = document.querySelector('#cropper-panel'); |
5 | | - if (!cropperContainer) { |
6 | | - return; |
7 | | - } |
| 3 | +type CropperOpts = { |
| 4 | + container: HTMLElement, |
| 5 | + imageSource: HTMLImageElement, |
| 6 | + fileInput: HTMLInputElement, |
| 7 | +} |
8 | 8 |
|
| 9 | +export async function initCompCropper({container, fileInput, imageSource}:CropperOpts) { |
9 | 10 | const {default: Cropper} = await import(/* webpackChunkName: "cropperjs" */'cropperjs'); |
10 | | - |
11 | | - const source = document.querySelector('#cropper-source'); |
12 | | - const result = document.querySelector('#cropper-result'); |
13 | | - const input = document.querySelector('#new-avatar'); |
14 | | - |
15 | | - const done = function (url: string, filename: string): void { |
16 | | - source.src = url; |
17 | | - result.src = url; |
18 | | - |
19 | | - if (input._cropper) { |
20 | | - input._cropper.replace(url); |
21 | | - } else { |
22 | | - input._cropper = new Cropper(source, { |
23 | | - aspectRatio: 1, |
24 | | - viewMode: 1, |
25 | | - autoCrop: false, |
26 | | - crop() { |
27 | | - const canvas = input._cropper.getCroppedCanvas(); |
28 | | - result.src = canvas.toDataURL(); |
29 | | - canvas.toBlob((blob) => { |
30 | | - const file = new File([blob], filename, {type: 'image/png', lastModified: Date.now()}); |
31 | | - const container = new DataTransfer(); |
32 | | - container.items.add(file); |
33 | | - input.files = container.files; |
34 | | - }); |
35 | | - }, |
| 11 | + let currentFileName = '', currentFileLastModified = 0; |
| 12 | + const cropper = new Cropper(imageSource, { |
| 13 | + aspectRatio: 1, |
| 14 | + viewMode: 2, |
| 15 | + autoCrop: false, |
| 16 | + crop() { |
| 17 | + const canvas = cropper.getCroppedCanvas(); |
| 18 | + canvas.toBlob((blob) => { |
| 19 | + const croppedFileName = currentFileName.replace(/\.[^.]{3,4}$/, '.png'); |
| 20 | + const croppedFile = new File([blob], croppedFileName, {type: 'image/png', lastModified: currentFileLastModified}); |
| 21 | + const dataTransfer = new DataTransfer(); |
| 22 | + dataTransfer.items.add(croppedFile); |
| 23 | + fileInput.files = dataTransfer.files; |
36 | 24 | }); |
37 | | - } |
38 | | - showElem(cropperContainer); |
39 | | - }; |
| 25 | + }, |
| 26 | + }); |
40 | 27 |
|
41 | | - input.addEventListener('change', (e: Event & {target: HTMLInputElement}) => { |
| 28 | + fileInput.addEventListener('input', (e: Event & {target: HTMLInputElement}) => { |
42 | 29 | const files = e.target.files; |
43 | | - |
44 | 30 | if (files?.length > 0) { |
45 | | - done(URL.createObjectURL(files[0]), files[0].name); |
| 31 | + currentFileName = files[0].name; |
| 32 | + currentFileLastModified = files[0].lastModified; |
| 33 | + const fileURL = URL.createObjectURL(files[0]); |
| 34 | + imageSource.src = fileURL; |
| 35 | + cropper.replace(fileURL); |
| 36 | + showElem(container); |
46 | 37 | } |
47 | 38 | }); |
48 | 39 | } |
0 commit comments