2021-12-21 00:15:04 +01:00
|
|
|
<script lang="ts">
|
|
|
|
import type { FileDTO } from '$lib/api'
|
|
|
|
import { Files } from '$lib/files'
|
|
|
|
import { createEventDispatcher } from 'svelte'
|
2022-01-16 14:02:53 +01:00
|
|
|
import { t } from 'svelte-intl-precompile'
|
2021-12-22 13:10:08 +01:00
|
|
|
import MaxSize from './MaxSize.svelte'
|
2021-12-21 00:15:04 +01:00
|
|
|
|
|
|
|
export let label: string = ''
|
|
|
|
let files: File[] = []
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher<{ file: string }>()
|
|
|
|
|
|
|
|
async function onInput(e: Event) {
|
|
|
|
const input = e.target as HTMLInputElement
|
|
|
|
if (input.files.length) {
|
|
|
|
files = Array.from(input.files)
|
|
|
|
const data: FileDTO[] = await Promise.all(
|
|
|
|
files.map(async (file) => ({
|
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
size: file.size,
|
|
|
|
contents: await Files.toString(file),
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
console.debug(
|
|
|
|
'files',
|
|
|
|
data.map((d) => d.contents.length)
|
|
|
|
)
|
|
|
|
dispatch('file', JSON.stringify(data))
|
|
|
|
} else {
|
|
|
|
dispatch('file', '')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<label>
|
|
|
|
<small>
|
|
|
|
{label}
|
|
|
|
</small>
|
|
|
|
<input type="file" on:change={onInput} multiple />
|
|
|
|
<div class="box">
|
|
|
|
{#if files.length}
|
|
|
|
<div>
|
2022-01-16 14:02:53 +01:00
|
|
|
<b>{$t('file_upload.selected_files')}</b>
|
2021-12-21 00:15:04 +01:00
|
|
|
{#each files as file}
|
|
|
|
<div class="file">
|
|
|
|
{file.name}
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{:else}
|
|
|
|
<div>
|
2022-01-16 14:02:53 +01:00
|
|
|
<b>{$t('file_upload.no_files_selected')}</b>
|
2021-12-22 13:10:08 +01:00
|
|
|
<br />
|
2022-01-16 14:02:53 +01:00
|
|
|
<small>{$t('common.max')}: <MaxSize /></small>
|
2021-12-21 00:15:04 +01:00
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
input {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.box {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
</style>
|