2022-01-12 09:52:22 +00:00
|
|
|
<!--
|
|
|
|
// Copyright © 2021, 2022 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
-->
|
|
|
|
<script lang="ts">
|
|
|
|
import { createEventDispatcher } from 'svelte'
|
2022-10-31 05:34:42 +00:00
|
|
|
import attachment from '@hcengineering/attachment'
|
2023-09-21 04:07:53 +00:00
|
|
|
import {
|
|
|
|
AnySvelteComponent,
|
|
|
|
IconSize,
|
|
|
|
showPopup,
|
|
|
|
getPlatformAvatarColorForTextDef,
|
|
|
|
themeStore
|
|
|
|
} from '@hcengineering/ui'
|
|
|
|
import { AvatarType } from '@hcengineering/contact'
|
2022-10-31 05:34:42 +00:00
|
|
|
import { Asset, getResource } from '@hcengineering/platform'
|
2022-01-12 09:52:22 +00:00
|
|
|
|
2022-10-31 05:34:42 +00:00
|
|
|
import AvatarComponent from './Avatar.svelte'
|
|
|
|
import SelectAvatarPopup from './SelectAvatarPopup.svelte'
|
2022-01-12 09:52:22 +00:00
|
|
|
|
2022-10-31 05:34:42 +00:00
|
|
|
export let avatar: string | null | undefined
|
2023-09-20 17:01:09 +00:00
|
|
|
export let name: string | null | undefined = undefined
|
2022-10-31 05:34:42 +00:00
|
|
|
export let email: string | undefined = undefined
|
2022-05-16 11:41:22 +00:00
|
|
|
export let size: IconSize
|
2022-02-02 09:03:29 +00:00
|
|
|
export let direct: Blob | undefined = undefined
|
2022-06-20 04:24:08 +00:00
|
|
|
export let icon: Asset | AnySvelteComponent | undefined = undefined
|
2022-11-03 08:13:25 +00:00
|
|
|
export let disabled: boolean = false
|
2022-01-12 09:52:22 +00:00
|
|
|
|
2023-09-20 17:01:09 +00:00
|
|
|
$: [schema, uri] = avatar?.split('://') || []
|
2022-10-31 05:34:42 +00:00
|
|
|
|
2023-09-20 17:01:09 +00:00
|
|
|
let selectedAvatarType: AvatarType | undefined
|
|
|
|
let selectedAvatar: string | null | undefined
|
|
|
|
$: selectedAvatarType = avatar?.includes('://')
|
|
|
|
? (schema as AvatarType)
|
|
|
|
: avatar === undefined
|
|
|
|
? AvatarType.COLOR
|
|
|
|
: AvatarType.IMAGE
|
|
|
|
$: selectedAvatar = selectedAvatarType === AvatarType.IMAGE ? avatar : uri
|
|
|
|
$: if (selectedAvatar === undefined && selectedAvatarType === AvatarType.COLOR) {
|
2023-09-21 04:07:53 +00:00
|
|
|
selectedAvatar = getPlatformAvatarColorForTextDef(name ?? '', $themeStore.dark).name
|
2023-09-20 17:01:09 +00:00
|
|
|
}
|
2022-01-12 09:52:22 +00:00
|
|
|
|
2022-10-31 05:34:42 +00:00
|
|
|
export async function createAvatar (): Promise<string | undefined> {
|
|
|
|
if (selectedAvatarType === AvatarType.IMAGE && direct !== undefined) {
|
|
|
|
const uploadFile = await getResource(attachment.helper.UploadFile)
|
|
|
|
const file = new File([direct], 'avatar')
|
|
|
|
|
|
|
|
return await uploadFile(file)
|
|
|
|
}
|
|
|
|
if (selectedAvatarType && selectedAvatar) {
|
|
|
|
return `${selectedAvatarType}://${selectedAvatar}`
|
2022-03-28 15:25:39 +00:00
|
|
|
}
|
2022-01-12 09:52:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 05:34:42 +00:00
|
|
|
export async function removeAvatar (avatar: string) {
|
|
|
|
if (!avatar.includes('://')) {
|
|
|
|
const deleteFile = await getResource(attachment.helper.DeleteFile)
|
|
|
|
await deleteFile(avatar)
|
2022-01-12 09:52:22 +00:00
|
|
|
}
|
2022-10-31 05:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handlePopupSubmit (submittedAvatarType?: AvatarType, submittedAvatar?: string, submittedDirect?: Blob) {
|
|
|
|
selectedAvatarType = submittedAvatarType
|
|
|
|
selectedAvatar = submittedAvatar
|
|
|
|
direct = submittedDirect
|
2023-09-20 17:01:09 +00:00
|
|
|
avatar = selectedAvatarType === AvatarType.IMAGE ? selectedAvatar : `${selectedAvatarType}://${selectedAvatar}`
|
2022-10-31 05:34:42 +00:00
|
|
|
dispatch('done')
|
|
|
|
}
|
|
|
|
const dispatch = createEventDispatcher()
|
2022-01-12 09:52:22 +00:00
|
|
|
|
2022-10-31 05:34:42 +00:00
|
|
|
async function showSelectionPopup (e: MouseEvent) {
|
2022-11-03 08:13:25 +00:00
|
|
|
if (!disabled) {
|
2023-09-20 17:01:09 +00:00
|
|
|
showPopup(SelectAvatarPopup, {
|
|
|
|
avatar:
|
|
|
|
selectedAvatarType === AvatarType.IMAGE
|
|
|
|
? selectedAvatar
|
|
|
|
: selectedAvatarType === AvatarType.COLOR && avatar == null
|
|
|
|
? undefined
|
|
|
|
: `${selectedAvatarType}://${selectedAvatar}`,
|
|
|
|
email,
|
|
|
|
name,
|
|
|
|
file: direct,
|
|
|
|
icon,
|
|
|
|
onSubmit: handlePopupSubmit
|
|
|
|
})
|
2022-11-03 08:13:25 +00:00
|
|
|
}
|
2022-01-12 09:52:22 +00:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2023-03-22 02:48:57 +00:00
|
|
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
2022-10-31 05:34:42 +00:00
|
|
|
<div class="cursor-pointer" on:click|self={showSelectionPopup}>
|
|
|
|
<AvatarComponent
|
|
|
|
{direct}
|
|
|
|
{size}
|
|
|
|
{icon}
|
2023-09-20 17:01:09 +00:00
|
|
|
avatar={selectedAvatarType === AvatarType.IMAGE
|
|
|
|
? selectedAvatar
|
|
|
|
: selectedAvatarType === AvatarType.COLOR && avatar == null
|
|
|
|
? undefined
|
|
|
|
: `${selectedAvatarType}://${selectedAvatar}`}
|
|
|
|
{name}
|
2022-10-31 05:34:42 +00:00
|
|
|
/>
|
2022-01-12 09:52:22 +00:00
|
|
|
</div>
|