forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow cropping an avatar before setting it (go-gitea#32565)
Provide a cropping tool on the avatar editing page, allowing users to select the cropping area themselves. This way, users can decide the displayed area of the image, rather than us deciding for them. --------- Co-authored-by: silverwind <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: Giteabot <[email protected]>
- Loading branch information
1 parent
f1bea3c
commit 68d9f36
Showing
12 changed files
with
80 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@import "cropperjs/dist/cropper.css"; | ||
|
||
.page-content.user.profile .cropper-panel .cropper-wrapper { | ||
max-width: 400px; | ||
max-height: 400px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {showElem} from '../../utils/dom.ts'; | ||
|
||
type CropperOpts = { | ||
container: HTMLElement, | ||
imageSource: HTMLImageElement, | ||
fileInput: HTMLInputElement, | ||
} | ||
|
||
export async function initCompCropper({container, fileInput, imageSource}: CropperOpts) { | ||
const {default: Cropper} = await import(/* webpackChunkName: "cropperjs" */'cropperjs'); | ||
let currentFileName = ''; | ||
let currentFileLastModified = 0; | ||
const cropper = new Cropper(imageSource, { | ||
aspectRatio: 1, | ||
viewMode: 2, | ||
autoCrop: false, | ||
crop() { | ||
const canvas = cropper.getCroppedCanvas(); | ||
canvas.toBlob((blob) => { | ||
const croppedFileName = currentFileName.replace(/\.[^.]{3,4}$/, '.png'); | ||
const croppedFile = new File([blob], croppedFileName, {type: 'image/png', lastModified: currentFileLastModified}); | ||
const dataTransfer = new DataTransfer(); | ||
dataTransfer.items.add(croppedFile); | ||
fileInput.files = dataTransfer.files; | ||
}); | ||
}, | ||
}); | ||
|
||
fileInput.addEventListener('input', (e: Event & {target: HTMLInputElement}) => { | ||
const files = e.target.files; | ||
if (files?.length > 0) { | ||
currentFileName = files[0].name; | ||
currentFileLastModified = files[0].lastModified; | ||
const fileURL = URL.createObjectURL(files[0]); | ||
imageSource.src = fileURL; | ||
cropper.replace(fileURL); | ||
showElem(container); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters