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-06-20 04:24:08 +00:00
|
|
|
import { AnySvelteComponent, IconSize, showPopup } from '@anticrm/ui'
|
2022-01-12 09:52:22 +00:00
|
|
|
|
|
|
|
import Avatar from './Avatar.svelte'
|
|
|
|
import EditAvatarPopup from './EditAvatarPopup.svelte'
|
2022-03-28 15:25:39 +00:00
|
|
|
import { getFileUrl } from '../utils'
|
2022-06-20 04:24:08 +00:00
|
|
|
import { Asset } from '@anticrm/platform'
|
2022-01-12 09:52:22 +00:00
|
|
|
|
2022-03-28 15:25:39 +00:00
|
|
|
export let avatar: string | null | 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-01-12 09:52:22 +00:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher()
|
|
|
|
|
|
|
|
let inputRef: HTMLInputElement
|
|
|
|
const targetMimes = ['image/png', 'image/jpg', 'image/jpeg']
|
2022-03-28 15:25:39 +00:00
|
|
|
async function onClick () {
|
|
|
|
let file: Blob
|
|
|
|
if (direct !== undefined) {
|
|
|
|
file = direct
|
|
|
|
} else if (avatar != null) {
|
|
|
|
const url = getFileUrl(avatar)
|
|
|
|
file = await (await fetch(url)).blob()
|
|
|
|
} else {
|
|
|
|
return inputRef.click()
|
|
|
|
}
|
2022-05-26 09:12:02 +00:00
|
|
|
showPopup(EditAvatarPopup, { file }, undefined, (blob) => {
|
2022-03-28 15:25:39 +00:00
|
|
|
if (blob === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (blob === null) {
|
|
|
|
direct = undefined
|
|
|
|
dispatch('remove')
|
|
|
|
} else {
|
|
|
|
direct = blob
|
|
|
|
dispatch('done', { file: new File([blob], 'avatar') })
|
|
|
|
}
|
|
|
|
})
|
2022-01-12 09:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSelect (e: any) {
|
|
|
|
const file = e.target?.files[0] as File | undefined
|
|
|
|
if (file === undefined || !targetMimes.includes(file.type)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
showPopup(EditAvatarPopup, { file }, undefined, (blob) => {
|
|
|
|
if (blob === undefined) {
|
|
|
|
return
|
|
|
|
}
|
2022-03-28 15:25:39 +00:00
|
|
|
if (blob === null) {
|
|
|
|
direct = undefined
|
|
|
|
dispatch('remove')
|
|
|
|
} else {
|
|
|
|
direct = blob
|
|
|
|
dispatch('done', { file: new File([blob], file.name) })
|
|
|
|
}
|
2022-01-12 09:52:22 +00:00
|
|
|
})
|
2022-02-02 09:03:29 +00:00
|
|
|
e.target.value = null
|
2022-01-12 09:52:22 +00:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="cursor-pointer" on:click={onClick}>
|
2022-06-20 04:24:08 +00:00
|
|
|
<Avatar {avatar} {direct} {size} {icon} />
|
2022-04-29 05:27:17 +00:00
|
|
|
<input style="display: none;" type="file" bind:this={inputRef} on:change={onSelect} accept={targetMimes.join(',')} />
|
2022-01-12 09:52:22 +00:00
|
|
|
</div>
|