Skip to content

Commit bfdb076

Browse files
committed
code optimization
1 parent c715200 commit bfdb076

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ uploaded_avatar_not_a_image = The uploaded file is not an image.
763763
uploaded_avatar_is_too_big = The uploaded file size (%d KiB) exceeds the maximum size (%d KiB).
764764
update_avatar_success = Your avatar has been updated.
765765
update_user_avatar_success = The user's avatar has been updated.
766+
cropper_prompt = Note: The saved image format after cropping is unified as JPEG.
766767

767768
change_password = Update Password
768769
old_password = Current Password

templates/user/settings/profile.tmpl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,20 @@
127127
<input id="new-avatar" name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
128128
</div>
129129

130-
<div class="inline field cropper hidden" id="cropper">
131-
<div class="preview">
130+
<div class="inline field cropper-panel tw-hidden" id="cropper-panel">
131+
<div class="cropper-preview">
132132
<h3>{{ctx.Locale.Tr "preview"}}</h3>
133133
<div>
134-
<img id="result">
134+
<img id="cropper-result">
135135
</div>
136136
</div>
137-
<div class="editor">
138-
<h3>{{ctx.Locale.Tr "edit"}}</h3>
137+
<div class="cropper-editor">
139138
<div>
140-
<img id="image">
139+
<h3>{{ctx.Locale.Tr "edit"}}</h3>
140+
<span>{{ctx.Locale.Tr "settings.cropper_prompt"}}</span>
141+
</div>
142+
<div class="cropper-wrapper">
143+
<img class="tw-hidden" id="cropper-source">
141144
</div>
142145
</div>
143146
</div>

web_src/css/features/cropper.css

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
@import "cropperjs/dist/cropper.css";
22

3-
.cropper {
3+
.cropper-panel {
44
display: flex;
55
column-gap: 10px;
6-
}
7-
8-
.hidden {
9-
display: none;
10-
}
116

12-
.editor {
13-
flex: 1;
14-
}
7+
.cropper-editor {
8+
flex: 1;
9+
max-width: 100%;
10+
overflow: hidden;
11+
.cropper-wrapper {
12+
height: 600px;
13+
max-height: 600px;
14+
}
15+
>div {
16+
display: flex;
17+
column-gap: 10px;
18+
}
19+
}
1520

16-
#result {
17-
overflow: hidden;
18-
width: 256px;
19-
height: 256px;
20-
max-width: 256px;
21-
max-height: 256px;
21+
#cropper-result {
22+
overflow: hidden;
23+
width: 256px;
24+
height: 256px;
25+
max-width: 256px;
26+
max-height: 256px;
27+
}
2228
}

web_src/js/features/comp/Cropper.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
import Cropper from 'cropperjs';
2+
import {showElem} from '../../utils/dom.ts';
23

34
export function initCompCropper() {
4-
if (!document.querySelector('#cropper')) {
5+
const cropperContainer = document.querySelector('#cropper-panel');
6+
if (!cropperContainer) {
57
return;
68
}
79

810
let filename;
9-
const image = document.querySelector('#image');
10-
const result = document.querySelector('#result');
11+
let cropper;
12+
const source = document.querySelector('#cropper-source');
13+
const result = document.querySelector('#cropper-result');
1114
const input = document.querySelector('#new-avatar');
1215

1316
const done = function (url) {
14-
image.src = url;
17+
source.src = url;
1518

16-
const cropper = new Cropper(image, {
17-
aspectRatio: 1,
18-
viewMode: 1,
19-
crop() {
20-
const canvas = cropper.getCroppedCanvas();
21-
result.src = canvas.toDataURL();
22-
canvas.toBlob((blob) => {
23-
const file = new File([blob], filename, {type: 'image/jpeg', lastModified: Date.now()});
24-
const container = new DataTransfer();
25-
container.items.add(file);
26-
input.files = container.files;
27-
});
28-
},
29-
});
30-
document.querySelector('#cropper').classList.remove('hidden');
19+
if (cropper) {
20+
cropper.replace(url);
21+
} else {
22+
cropper = new Cropper(source, {
23+
aspectRatio: 1,
24+
viewMode: 1,
25+
crop() {
26+
const canvas = cropper.getCroppedCanvas();
27+
result.src = canvas.toDataURL();
28+
canvas.toBlob((blob) => {
29+
const file = new File([blob], filename, {type: 'image/jpeg', lastModified: Date.now()});
30+
const container = new DataTransfer();
31+
container.items.add(file);
32+
input.files = container.files;
33+
});
34+
},
35+
});
36+
}
37+
showElem(cropperContainer);
3138
};
3239

3340
input.addEventListener('change', (e) => {

0 commit comments

Comments
 (0)