-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Showing
18 changed files
with
129 additions
and
31 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
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
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
File renamed without changes.
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
73 changes: 73 additions & 0 deletions
73
src/panels/RolePanel/RoleEdit/Info/AvatarWithUpload/index.tsx
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,73 @@ | ||
import { Upload } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
import NextImage from 'next/image'; | ||
import { CSSProperties, memo, useCallback } from 'react'; | ||
|
||
import { DEFAULT_AGENT_AVATAR_URL } from '@/constants/common'; | ||
import { agentSelectors, useAgentStore } from '@/store/agent'; | ||
import { createUploadImageHandler } from '@/utils/common'; | ||
import { imageToBase64 } from '@/utils/imageToBase64'; | ||
|
||
const useStyle = createStyles( | ||
({ css, token }) => css` | ||
cursor: pointer; | ||
overflow: hidden; | ||
border-radius: 50%; | ||
transition: | ||
scale 400ms ${token.motionEaseOut}, | ||
box-shadow 100ms ${token.motionEaseOut}; | ||
&:hover { | ||
box-shadow: 0 0 0 3px ${token.colorText}; | ||
} | ||
&:active { | ||
scale: 0.8; | ||
} | ||
`, | ||
); | ||
|
||
interface AvatarWithUploadProps { | ||
compressSize?: number; | ||
id?: string; | ||
size?: number; | ||
style?: CSSProperties; | ||
} | ||
|
||
const AvatarWithUpload = memo<AvatarWithUploadProps>( | ||
({ size = 40, compressSize = 256, style, id }) => { | ||
const { styles } = useStyle(); | ||
const [meta, updateAgentMeta] = useAgentStore((s) => [ | ||
agentSelectors.currentAgentMeta(s), | ||
s.updateAgentMeta, | ||
]); | ||
|
||
const handleUploadAvatar = useCallback( | ||
createUploadImageHandler((avatar) => { | ||
const img = new Image(); | ||
img.src = avatar; | ||
img.addEventListener('load', () => { | ||
const webpBase64 = imageToBase64({ img, size: compressSize }); | ||
updateAgentMeta({ avatar: webpBase64 }); | ||
}); | ||
}), | ||
[], | ||
); | ||
|
||
return ( | ||
<div className={styles} id={id} style={{ maxHeight: size, maxWidth: size, ...style }}> | ||
<Upload beforeUpload={handleUploadAvatar} itemRender={() => void 0} maxCount={1}> | ||
<NextImage | ||
alt={meta?.avatar ? 'userAvatar' : 'LobeVidol'} | ||
height={size} | ||
src={!!meta?.avatar ? meta?.avatar : DEFAULT_AGENT_AVATAR_URL} | ||
unoptimized | ||
width={size} | ||
/> | ||
</Upload> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
export default AvatarWithUpload; |
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
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