2022-01-16 22:58:05 +01:00
|
|
|
<script context="module" lang="ts">
|
2022-01-30 15:39:19 +01:00
|
|
|
import mitt from "mitt";
|
|
|
|
|
2022-01-16 22:58:05 +01:00
|
|
|
export type AddEvents = {
|
|
|
|
files: File[];
|
|
|
|
urls: string[];
|
|
|
|
};
|
|
|
|
export const addEmitter = mitt<AddEvents>();
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2022-01-18 23:57:03 +01:00
|
|
|
import { useNavigate } from "svelte-navigator";
|
2022-02-20 13:06:01 +01:00
|
|
|
import { uploadFile } from "../lib/api";
|
2022-01-18 17:05:45 +01:00
|
|
|
import Icon from "./utils/Icon.svelte";
|
|
|
|
import IconButton from "./utils/IconButton.svelte";
|
2022-01-18 23:57:03 +01:00
|
|
|
const navigate = useNavigate();
|
2022-01-16 22:58:05 +01:00
|
|
|
|
|
|
|
let files: File[] = [];
|
|
|
|
let URLs: string[] = [];
|
2022-01-18 17:05:45 +01:00
|
|
|
let uploading = false;
|
|
|
|
|
2022-01-16 22:58:05 +01:00
|
|
|
$: visible = files.length + URLs.length > 0;
|
|
|
|
|
|
|
|
addEmitter.on("files", (ev) => {
|
2022-04-09 21:47:27 +02:00
|
|
|
ev.forEach((file) => {
|
|
|
|
if (
|
|
|
|
!files
|
|
|
|
.map((f) => `${f.name}${f.size}`)
|
|
|
|
.includes(`${file.name}${file.size}`)
|
|
|
|
) {
|
|
|
|
files.push(file);
|
|
|
|
}
|
|
|
|
files = files;
|
|
|
|
});
|
2022-01-16 22:58:05 +01:00
|
|
|
});
|
2022-01-18 17:05:45 +01:00
|
|
|
|
|
|
|
async function upload() {
|
|
|
|
uploading = true;
|
|
|
|
|
|
|
|
try {
|
2023-04-24 19:25:02 +02:00
|
|
|
const addresses = await Promise.all(
|
2022-02-20 13:06:01 +01:00
|
|
|
files.map(async (file) => uploadFile(file))
|
2022-01-18 17:05:45 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
navigate(`/browse/${addresses.join(",")}`);
|
|
|
|
} catch (error) {
|
|
|
|
alert(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
uploading = false;
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
if (!uploading) {
|
|
|
|
files = [];
|
|
|
|
URLs = [];
|
|
|
|
}
|
|
|
|
}
|
2022-01-16 22:58:05 +01:00
|
|
|
</script>
|
|
|
|
|
2022-01-18 17:05:45 +01:00
|
|
|
<div class="addmodal-container" class:visible class:uploading on:click={reset}>
|
|
|
|
<div class="addmodal" on:click|stopPropagation>
|
|
|
|
<div class="files">
|
|
|
|
{#each files as file}
|
|
|
|
<div class="file">
|
2022-02-23 15:55:57 +01:00
|
|
|
{#if file.type.startsWith("image")}
|
|
|
|
<img src={URL.createObjectURL(file)} alt="To be uploaded." />
|
|
|
|
{:else}
|
|
|
|
<div class="icon">
|
|
|
|
<Icon name="file" />
|
|
|
|
</div>
|
|
|
|
{/if}
|
2022-01-18 17:05:45 +01:00
|
|
|
<div class="label">{file.name}</div>
|
|
|
|
</div>
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
<div class="controls">
|
|
|
|
<IconButton name="upload" on:click={upload} />
|
|
|
|
</div>
|
2022-01-16 22:58:05 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.addmodal-container {
|
|
|
|
position: absolute;
|
|
|
|
left: 0;
|
|
|
|
top: 0;
|
|
|
|
width: 100vw;
|
|
|
|
height: 100vh;
|
|
|
|
background: rgba(0, 0, 0, 0.5);
|
|
|
|
color: var(--foreground);
|
|
|
|
|
|
|
|
display: none;
|
|
|
|
&.visible {
|
|
|
|
display: unset;
|
|
|
|
}
|
2022-01-18 17:05:45 +01:00
|
|
|
|
|
|
|
&.uploading {
|
|
|
|
cursor: progress;
|
|
|
|
|
|
|
|
.addmodal {
|
|
|
|
filter: brightness(0.5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.addmodal {
|
|
|
|
position: absolute;
|
|
|
|
left: 50%;
|
|
|
|
top: 50%;
|
|
|
|
transform: translate(-50%, -50%);
|
|
|
|
|
|
|
|
background: var(--background);
|
|
|
|
|
|
|
|
color: var(--foreground);
|
|
|
|
border: solid 2px var(--foreground);
|
|
|
|
border-radius: 8px;
|
|
|
|
padding: 1rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
.files {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
gap: 1em;
|
|
|
|
|
|
|
|
padding: 0.5em;
|
|
|
|
|
|
|
|
overflow-y: auto;
|
|
|
|
max-height: 66vh;
|
|
|
|
}
|
|
|
|
|
|
|
|
.file {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2022-02-23 15:55:57 +01:00
|
|
|
flex-direction: column;
|
2022-01-18 17:05:45 +01:00
|
|
|
|
|
|
|
border: 1px solid var(--foreground);
|
|
|
|
border-radius: 4px;
|
|
|
|
background: var(--background-lighter);
|
|
|
|
padding: 0.5em;
|
|
|
|
|
2022-02-23 15:55:57 +01:00
|
|
|
img {
|
|
|
|
max-height: 12em;
|
|
|
|
max-width: 12em;
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:05:45 +01:00
|
|
|
.icon {
|
|
|
|
font-size: 24px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.label {
|
|
|
|
flex-grow: 1;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.controls {
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
font-size: 48px;
|
|
|
|
margin-top: 0.5rem;
|
2022-01-16 22:58:05 +01:00
|
|
|
}
|
|
|
|
</style>
|