mirror of
https://github.com/go-gitea/gitea
synced 2024-11-21 14:30:58 +01:00
code optimization
This commit is contained in:
parent
692fcff98a
commit
cd598bfd3d
1 changed files with 9 additions and 22 deletions
|
@ -1,31 +1,30 @@
|
||||||
import Cropper from 'cropperjs';
|
|
||||||
import {showElem} from '../../utils/dom.ts';
|
import {showElem} from '../../utils/dom.ts';
|
||||||
|
|
||||||
export function initCompCropper() {
|
export async function initCompCropper() {
|
||||||
const cropperContainer = document.querySelector('#cropper-panel');
|
const cropperContainer = document.querySelector('#cropper-panel');
|
||||||
if (!cropperContainer) {
|
if (!cropperContainer) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename;
|
const {default: Cropper} = await import(/* webpackChunkName: "cropperjs" */'cropperjs');
|
||||||
let cropper;
|
|
||||||
const source = document.querySelector('#cropper-source');
|
const source = document.querySelector('#cropper-source');
|
||||||
const result = document.querySelector('#cropper-result');
|
const result = document.querySelector('#cropper-result');
|
||||||
const input = document.querySelector('#new-avatar');
|
const input = document.querySelector('#new-avatar');
|
||||||
|
|
||||||
const done = function (url: string): void {
|
const done = function (url: string, filename: string): void {
|
||||||
source.src = url;
|
source.src = url;
|
||||||
result.src = url;
|
result.src = url;
|
||||||
|
|
||||||
if (cropper) {
|
if (input._cropper) {
|
||||||
cropper.replace(url);
|
input._cropper.replace(url);
|
||||||
} else {
|
} else {
|
||||||
cropper = new Cropper(source, {
|
input._cropper = new Cropper(source, {
|
||||||
aspectRatio: 1,
|
aspectRatio: 1,
|
||||||
viewMode: 1,
|
viewMode: 1,
|
||||||
autoCrop: false,
|
autoCrop: false,
|
||||||
crop() {
|
crop() {
|
||||||
const canvas = cropper.getCroppedCanvas();
|
const canvas = input._cropper.getCroppedCanvas();
|
||||||
result.src = canvas.toDataURL();
|
result.src = canvas.toDataURL();
|
||||||
canvas.toBlob((blob) => {
|
canvas.toBlob((blob) => {
|
||||||
const file = new File([blob], filename, {type: 'image/png', lastModified: Date.now()});
|
const file = new File([blob], filename, {type: 'image/png', lastModified: Date.now()});
|
||||||
|
@ -42,20 +41,8 @@ export function initCompCropper() {
|
||||||
input.addEventListener('change', (e: Event & {target: HTMLInputElement}) => {
|
input.addEventListener('change', (e: Event & {target: HTMLInputElement}) => {
|
||||||
const files = e.target.files;
|
const files = e.target.files;
|
||||||
|
|
||||||
let reader;
|
|
||||||
let file;
|
|
||||||
if (files?.length > 0) {
|
if (files?.length > 0) {
|
||||||
file = files[0];
|
done(URL.createObjectURL(files[0]), files[0].name);
|
||||||
filename = file.name;
|
|
||||||
if (URL) {
|
|
||||||
done(URL.createObjectURL(file));
|
|
||||||
} else if (FileReader) {
|
|
||||||
reader = new FileReader();
|
|
||||||
reader.addEventListener('load', () => {
|
|
||||||
done(reader.result);
|
|
||||||
});
|
|
||||||
reader.readAsDataURL(file);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue