Skip to content

Commit 40426ad

Browse files
Add cropping support when modifying the user/org/repo avatar (#33498)
Fixed #33321 --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 943cc4f commit 40426ad

File tree

11 files changed

+35
-28
lines changed

11 files changed

+35
-28
lines changed

templates/admin/user/edit.tmpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@
195195
</div>
196196

197197
<div class="inline field tw-pl-4">
198-
<label for="avatar">{{ctx.Locale.Tr "settings.choose_new_avatar"}}</label>
199-
<input name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
198+
{{template "shared/avatar_upload_crop" dict "LabelText" (ctx.Locale.Tr "settings.choose_new_avatar")}}
200199
</div>
201200

202201
<div class="field">

templates/org/settings/options.tmpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@
8989
<form class="ui form" action="{{.Link}}/avatar" method="post" enctype="multipart/form-data">
9090
{{.CsrfTokenHtml}}
9191
<div class="inline field">
92-
<label for="avatar">{{ctx.Locale.Tr "settings.choose_new_avatar"}}</label>
93-
<input name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
92+
{{template "shared/avatar_upload_crop" dict "LabelText" (ctx.Locale.Tr "settings.choose_new_avatar")}}
9493
</div>
95-
9694
<div class="field">
9795
<button class="ui primary button">{{ctx.Locale.Tr "settings.update_avatar"}}</button>
9896
<button class="ui red button link-action" data-url="{{.Link}}/avatar/delete">{{ctx.Locale.Tr "settings.delete_current_avatar"}}</button>

templates/repo/settings/options.tmpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
<form class="ui form" action="{{.Link}}/avatar" method="post" enctype="multipart/form-data">
4141
{{.CsrfTokenHtml}}
4242
<div class="inline field">
43-
<label for="avatar">{{ctx.Locale.Tr "settings.choose_new_avatar"}}</label>
44-
<input name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
43+
{{template "shared/avatar_upload_crop" dict "LabelText" (ctx.Locale.Tr "settings.choose_new_avatar")}}
4544
</div>
4645
<div class="field">
4746
<button class="ui primary button">{{ctx.Locale.Tr "settings.update_avatar"}}</button>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{- /* we do not need to set for/id here, global aria init code will add them automatically */ -}}
2+
<label>{{.LabelText}}</label>
3+
<input class="avatar-file-with-cropper" name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
4+
{{- /* the cropper-panel must be next sibling of the input "avatar" */ -}}
5+
<div class="cropper-panel tw-hidden">
6+
<div class="tw-my-2">{{ctx.Locale.Tr "settings.cropper_prompt"}}</div>
7+
<div class="cropper-wrapper"><img class="cropper-source" src alt></div>
8+
</div>

templates/user/settings/profile.tmpl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,7 @@
124124
</div>
125125

126126
<div class="inline field tw-pl-4">
127-
<label for="new-avatar">{{ctx.Locale.Tr "settings.choose_new_avatar"}}</label>
128-
<input id="new-avatar" name="avatar" type="file" accept="image/png,image/jpeg,image/gif,image/webp">
129-
</div>
130-
131-
<div class="field tw-pl-4 cropper-panel tw-hidden">
132-
<div>{{ctx.Locale.Tr "settings.cropper_prompt"}}</div>
133-
<div class="cropper-wrapper"><img class="cropper-source" src alt></div>
127+
{{template "shared/avatar_upload_crop" dict "LabelText" (ctx.Locale.Tr "settings.choose_new_avatar")}}
134128
</div>
135129

136130
<div class="field">

web_src/css/features/cropper.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@import "cropperjs/dist/cropper.css";
22

3-
.page-content.user.profile .cropper-panel .cropper-wrapper {
3+
.avatar-file-with-cropper + .cropper-panel .cropper-wrapper {
44
max-width: 400px;
55
max-height: 400px;
66
}

web_src/js/features/admin/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import $ from 'jquery';
22
import {checkAppUrl} from '../common-page.ts';
3-
import {hideElem, showElem, toggleElem} from '../../utils/dom.ts';
3+
import {hideElem, queryElems, showElem, toggleElem} from '../../utils/dom.ts';
44
import {POST} from '../../modules/fetch.ts';
5+
import {initAvatarUploaderWithCropper} from '../comp/Cropper.ts';
56

67
const {appSubUrl} = window.config;
78

@@ -258,4 +259,6 @@ export function initAdminCommon(): void {
258259
window.location.href = this.getAttribute('data-redirect');
259260
});
260261
}
262+
263+
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
261264
}

web_src/js/features/common-organization.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {initCompLabelEdit} from './comp/LabelEdit.ts';
2-
import {toggleElem} from '../utils/dom.ts';
2+
import {queryElems, toggleElem} from '../utils/dom.ts';
3+
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
34

45
export function initCommonOrganization() {
56
if (!document.querySelectorAll('.organization').length) {
@@ -13,4 +14,6 @@ export function initCommonOrganization() {
1314

1415
// Labels
1516
initCompLabelEdit('.page-content.organization.settings.labels');
17+
18+
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
1619
}

web_src/js/features/comp/Cropper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type CropperOpts = {
66
fileInput: HTMLInputElement,
77
}
88

9-
export async function initCompCropper({container, fileInput, imageSource}: CropperOpts) {
9+
async function initCompCropper({container, fileInput, imageSource}: CropperOpts) {
1010
const {default: Cropper} = await import(/* webpackChunkName: "cropperjs" */'cropperjs');
1111
let currentFileName = '';
1212
let currentFileLastModified = 0;
@@ -38,3 +38,10 @@ export async function initCompCropper({container, fileInput, imageSource}: Cropp
3838
}
3939
});
4040
}
41+
42+
export async function initAvatarUploaderWithCropper(fileInput: HTMLInputElement) {
43+
const panel = fileInput.nextElementSibling as HTMLElement;
44+
if (!panel?.matches('.cropper-panel')) throw new Error('Missing cropper panel for avatar uploader');
45+
const imageSource = panel.querySelector<HTMLImageElement>('.cropper-source');
46+
await initCompCropper({container: panel, fileInput, imageSource});
47+
}

web_src/js/features/repo-settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {minimatch} from 'minimatch';
33
import {createMonaco} from './codeeditor.ts';
44
import {onInputDebounce, queryElems, toggleElem} from '../utils/dom.ts';
55
import {POST} from '../modules/fetch.ts';
6+
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
67
import {initRepoSettingsBranchesDrag} from './repo-settings-branches.ts';
78

89
const {appSubUrl, csrfToken} = window.config;
@@ -156,4 +157,6 @@ export function initRepoSettings() {
156157
initRepoSettingsSearchTeamBox();
157158
initRepoSettingsGitHook();
158159
initRepoSettingsBranchesDrag();
160+
161+
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
159162
}

web_src/js/features/user-settings.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import {hideElem, showElem} from '../utils/dom.ts';
2-
import {initCompCropper} from './comp/Cropper.ts';
3-
4-
function initUserSettingsAvatarCropper() {
5-
const fileInput = document.querySelector<HTMLInputElement>('#new-avatar');
6-
const container = document.querySelector<HTMLElement>('.user.settings.profile .cropper-panel');
7-
const imageSource = container.querySelector<HTMLImageElement>('.cropper-source');
8-
initCompCropper({container, fileInput, imageSource});
9-
}
1+
import {hideElem, queryElems, showElem} from '../utils/dom.ts';
2+
import {initAvatarUploaderWithCropper} from './comp/Cropper.ts';
103

114
export function initUserSettings() {
125
if (!document.querySelector('.user.settings.profile')) return;
136

14-
initUserSettingsAvatarCropper();
7+
queryElems(document, '.avatar-file-with-cropper', initAvatarUploaderWithCropper);
158

169
const usernameInput = document.querySelector<HTMLInputElement>('#username');
1710
if (!usernameInput) return;

0 commit comments

Comments
 (0)