-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea2a23c
commit 5f893c3
Showing
5 changed files
with
45 additions
and
89 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
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,36 +1,6 @@ | ||
@import "cropperjs/dist/cropper.css"; | ||
|
||
.cropper-panel { | ||
display: flex; | ||
flex-wrap: wrap; | ||
column-gap: 10px; | ||
} | ||
|
||
.cropper-panel h3 { | ||
word-break: keep-all; | ||
} | ||
|
||
.cropper-panel #cropper-result { | ||
overflow: hidden; | ||
width: 256px; | ||
height: 256px; | ||
max-width: 256px; | ||
max-height: 256px; | ||
} | ||
|
||
.cropper-panel .cropper-editor { | ||
flex: 1; | ||
min-width: 300px; | ||
max-width: 100%; | ||
overflow: hidden; | ||
} | ||
|
||
.cropper-panel .cropper-editor >div { | ||
display: flex; | ||
column-gap: 10px; | ||
} | ||
|
||
.cropper-panel .cropper-editor .cropper-wrapper { | ||
height: 600px; | ||
max-height: 600px; | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,39 @@ | ||
import {showElem} from '../../utils/dom.ts'; | ||
|
||
export async function initCompCropper() { | ||
const cropperContainer = document.querySelector('#cropper-panel'); | ||
if (!cropperContainer) { | ||
return; | ||
} | ||
type CropperOpts = { | ||
container: HTMLElement, | ||
imageSource: HTMLImageElement, | ||
fileInput: HTMLInputElement, | ||
} | ||
|
||
export async function initCompCropper({container, fileInput, imageSource}:CropperOpts) { | ||
const {default: Cropper} = await import(/* webpackChunkName: "cropperjs" */'cropperjs'); | ||
|
||
const source = document.querySelector('#cropper-source'); | ||
const result = document.querySelector('#cropper-result'); | ||
const input = document.querySelector('#new-avatar'); | ||
|
||
const done = function (url: string, filename: string): void { | ||
source.src = url; | ||
result.src = url; | ||
|
||
if (input._cropper) { | ||
input._cropper.replace(url); | ||
} else { | ||
input._cropper = new Cropper(source, { | ||
aspectRatio: 1, | ||
viewMode: 1, | ||
autoCrop: false, | ||
crop() { | ||
const canvas = input._cropper.getCroppedCanvas(); | ||
result.src = canvas.toDataURL(); | ||
canvas.toBlob((blob) => { | ||
const file = new File([blob], filename, {type: 'image/png', lastModified: Date.now()}); | ||
const container = new DataTransfer(); | ||
container.items.add(file); | ||
input.files = container.files; | ||
}); | ||
}, | ||
let currentFileName = '', 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; | ||
}); | ||
} | ||
showElem(cropperContainer); | ||
}; | ||
}, | ||
}); | ||
|
||
input.addEventListener('change', (e: Event & {target: HTMLInputElement}) => { | ||
fileInput.addEventListener('input', (e: Event & {target: HTMLInputElement}) => { | ||
const files = e.target.files; | ||
|
||
if (files?.length > 0) { | ||
done(URL.createObjectURL(files[0]), files[0].name); | ||
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