PEE/src/EyeButton.svelte

51 lines
1.2 KiB
Svelte
Raw Normal View History

2022-01-05 20:50:44 +00:00
<script lang="ts">
2022-01-05 22:20:20 +00:00
import { fileTypeFromBuffer } from 'file-type';
import type { EmbeddedFile } from './main';
2022-01-05 20:50:44 +00:00
import { settings } from './stores'
export let id = ''
2022-01-05 22:20:20 +00:00
export let file: EmbeddedFile;
2022-01-05 20:50:44 +00:00
let visible = false
function reveal() {
visible = !visible
document.dispatchEvent(new CustomEvent('reveal', { detail: { id } }))
}
2022-01-05 22:20:20 +00:00
async function downloadFile() {
const a = document.createElement("a") as HTMLAnchorElement;
document.body.appendChild(a);
a.style.display = 'none';
const thumb = Buffer.isBuffer(file.data) ? file.data : await file.data();
const type = await fileTypeFromBuffer(thumb);
const url = URL.createObjectURL(new Blob([thumb], { type: type?.mime }))
a.href = url;
a.download = file.filename;
a.click();
window.URL.revokeObjectURL(url);
}
2022-01-05 20:50:44 +00:00
</script>
{#if $settings.eye}
<span
on:click={reveal}
class:fa-eye={!visible}
class:fa-eye-slash={visible}
class="fa clickable"
/>
{/if}
2022-01-05 22:20:20 +00:00
<span title={file.filename} on:click={downloadFile} class="fa fa-download clickable" />
2022-01-05 20:50:44 +00:00
<style scoped>
.clickable {
cursor: pointer;
2022-01-05 21:14:45 +00:00
margin-left: 5px;
2022-01-05 20:50:44 +00:00
}
.clickable:hover {
text-shadow: 0 0 4px palevioletred;
}
</style>