-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade cropperjs to v2, add avatar id and link to it #33827
Draft
silverwind
wants to merge
10
commits into
go-gitea:main
Choose a base branch
from
silverwind:cropper2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ccc498
Upgrade cropperjs to v2
silverwind cced481
wip
silverwind ad16758
Merge branch 'main' into cropper2
silverwind 356bf32
ignore ts error
silverwind af941ab
add theme color, link to avatar settings section from user profile
silverwind f5d870c
move id to header
silverwind 11feb73
finish
silverwind b8b54d8
rename variable
silverwind 5210919
Merge branch 'main' into cropper2
wxiaoguang 8f17dbc
use two words for id to avoid conflicts
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
@import "cropperjs/dist/cropper.css"; | ||
|
||
.avatar-file-with-cropper + .cropper-panel .cropper-wrapper { | ||
max-width: 400px; | ||
width: 400px; | ||
max-height: 400px; | ||
} | ||
|
||
cropper-canvas { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,67 @@ | ||
import {showElem, type DOMEvent} from '../../utils/dom.ts'; | ||
import {createElementFromHTML, hideElem, showElem, type DOMEvent} from '../../utils/dom.ts'; | ||
import {debounce} from 'perfect-debounce'; | ||
import type {CropperCanvas, CropperSelection} from 'cropperjs'; | ||
|
||
type CropperOpts = { | ||
container: HTMLElement, | ||
imageSource: HTMLImageElement, | ||
wrapper: HTMLDivElement, | ||
fileInput: HTMLInputElement, | ||
} | ||
|
||
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(); | ||
async function initCompCropper({container, fileInput, wrapper}: CropperOpts) { | ||
await import(/* webpackChunkName: "cropperjs" */'cropperjs'); | ||
|
||
fileInput.addEventListener('input', (e: DOMEvent<Event, HTMLInputElement>) => { | ||
if (!e.target.files?.length) { | ||
wrapper.replaceChildren(); | ||
hideElem(container); | ||
return; | ||
} | ||
|
||
const [file] = e.target.files; | ||
const objectUrl = URL.createObjectURL(file); | ||
const cropperCanvas = createElementFromHTML<CropperCanvas>(` | ||
<cropper-canvas theme-color="var(--color-primary)"> | ||
<cropper-image src="${objectUrl}" scalable skewable translatable></cropper-image> | ||
<cropper-shade hidden></cropper-shade> | ||
<cropper-handle action="select" plain></cropper-handle> | ||
<cropper-selection aspect-ratio="1" movable resizable> | ||
<cropper-handle action="move" theme-color="transparent"></cropper-handle> | ||
<cropper-handle action="n-resize"></cropper-handle> | ||
<cropper-handle action="e-resize"></cropper-handle> | ||
<cropper-handle action="s-resize"></cropper-handle> | ||
<cropper-handle action="w-resize"></cropper-handle> | ||
<cropper-handle action="ne-resize"></cropper-handle> | ||
<cropper-handle action="nw-resize"></cropper-handle> | ||
<cropper-handle action="se-resize"></cropper-handle> | ||
<cropper-handle action="sw-resize"></cropper-handle> | ||
</cropper-selection> | ||
</cropper-canvas> | ||
`); | ||
cropperCanvas.querySelector<CropperSelection>('cropper-selection').addEventListener('change', debounce(async (e) => { | ||
const selection = e.target as CropperSelection; | ||
if (!selection.width || !selection.height) return; | ||
const canvas = await selection.$toCanvas(); | ||
|
||
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); | ||
dataTransfer.items.add(new File( | ||
[blob], | ||
file.name.replace(/\.[^.]{3,4}$/, '.png'), | ||
{type: 'image/png', lastModified: file.lastModified}, | ||
)); | ||
fileInput.files = dataTransfer.files; | ||
}); | ||
}, | ||
}); | ||
}, 200)); | ||
|
||
fileInput.addEventListener('input', (e: DOMEvent<Event, 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); | ||
} | ||
wrapper.replaceChildren(cropperCanvas); | ||
showElem(container); | ||
}); | ||
} | ||
|
||
export async function initAvatarUploaderWithCropper(fileInput: HTMLInputElement) { | ||
const panel = fileInput.nextElementSibling as HTMLElement; | ||
if (!panel?.matches('.cropper-panel')) throw new Error('Missing cropper panel for avatar uploader'); | ||
const imageSource = panel.querySelector<HTMLImageElement>('.cropper-source'); | ||
await initCompCropper({container: panel, fileInput, imageSource}); | ||
const wrapper = panel.querySelector<HTMLImageElement>('.cropper-wrapper'); | ||
await initCompCropper({container: panel, fileInput, wrapper}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This event is not right. For example: "zoom" won't trigger "change" event and users will get wrong image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally I would like to disable zooming, but found no way to do so.