code optimization

This commit is contained in:
Kerwin Bryant 2024-11-20 08:12:55 +00:00
parent c71520088d
commit bfdb076952
4 changed files with 57 additions and 40 deletions

View File

@ -763,6 +763,7 @@ uploaded_avatar_not_a_image = The uploaded file is not an image.
uploaded_avatar_is_too_big = The uploaded file size (%d KiB) exceeds the maximum size (%d KiB). uploaded_avatar_is_too_big = The uploaded file size (%d KiB) exceeds the maximum size (%d KiB).
update_avatar_success = Your avatar has been updated. update_avatar_success = Your avatar has been updated.
update_user_avatar_success = The user's avatar has been updated. update_user_avatar_success = The user's avatar has been updated.
cropper_prompt = Note: The saved image format after cropping is unified as JPEG.
change_password = Update Password change_password = Update Password
old_password = Current Password old_password = Current Password

View File

@ -127,17 +127,20 @@
<input id="new-avatar" name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp"> <input id="new-avatar" name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
</div> </div>
<div class="inline field cropper hidden" id="cropper"> <div class="inline field cropper-panel tw-hidden" id="cropper-panel">
<div class="preview"> <div class="cropper-preview">
<h3>{{ctx.Locale.Tr "preview"}}</h3> <h3>{{ctx.Locale.Tr "preview"}}</h3>
<div> <div>
<img id="result"> <img id="cropper-result">
</div> </div>
</div> </div>
<div class="editor"> <div class="cropper-editor">
<h3>{{ctx.Locale.Tr "edit"}}</h3>
<div> <div>
<img id="image"> <h3>{{ctx.Locale.Tr "edit"}}</h3>
<span>{{ctx.Locale.Tr "settings.cropper_prompt"}}</span>
</div>
<div class="cropper-wrapper">
<img class="tw-hidden" id="cropper-source">
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,22 +1,28 @@
@import "cropperjs/dist/cropper.css"; @import "cropperjs/dist/cropper.css";
.cropper { .cropper-panel {
display: flex; display: flex;
column-gap: 10px; column-gap: 10px;
}
.hidden { .cropper-editor {
display: none;
}
.editor {
flex: 1; flex: 1;
} max-width: 100%;
overflow: hidden;
.cropper-wrapper {
height: 600px;
max-height: 600px;
}
>div {
display: flex;
column-gap: 10px;
}
}
#result { #cropper-result {
overflow: hidden; overflow: hidden;
width: 256px; width: 256px;
height: 256px; height: 256px;
max-width: 256px; max-width: 256px;
max-height: 256px; max-height: 256px;
}
} }

View File

@ -1,19 +1,25 @@
import Cropper from 'cropperjs'; import Cropper from 'cropperjs';
import {showElem} from '../../utils/dom.ts';
export function initCompCropper() { export function initCompCropper() {
if (!document.querySelector('#cropper')) { const cropperContainer = document.querySelector('#cropper-panel');
if (!cropperContainer) {
return; return;
} }
let filename; let filename;
const image = document.querySelector('#image'); let cropper;
const result = document.querySelector('#result'); const source = document.querySelector('#cropper-source');
const result = document.querySelector('#cropper-result');
const input = document.querySelector('#new-avatar'); const input = document.querySelector('#new-avatar');
const done = function (url) { const done = function (url) {
image.src = url; source.src = url;
const cropper = new Cropper(image, { if (cropper) {
cropper.replace(url);
} else {
cropper = new Cropper(source, {
aspectRatio: 1, aspectRatio: 1,
viewMode: 1, viewMode: 1,
crop() { crop() {
@ -27,7 +33,8 @@ export function initCompCropper() {
}); });
}, },
}); });
document.querySelector('#cropper').classList.remove('hidden'); }
showElem(cropperContainer);
}; };
input.addEventListener('change', (e) => { input.addEventListener('change', (e) => {