| 1 |
<!-- |
| 2 |
@component |
| 3 |
A component to show PDFs, text, images, and HTML files. |
| 4 |
--> |
| 5 |
<script lang="ts" module> |
| 6 |
/** For these file extensions we show a plain-text read-only editor. */ |
| 7 |
const plainTextExtensions = ["csv", "json", "qfx", "txt", "xml"]; |
| 8 |
/** For these file extensions we try to show the file as an `<img>` */ |
| 9 |
const imageExtensions = [ |
| 10 |
"gif", |
| 11 |
"jpg", |
| 12 |
"jpeg", |
| 13 |
"png", |
| 14 |
"svg", |
| 15 |
"webp", |
| 16 |
"bmp", |
| 17 |
"ico", |
| 18 |
]; |
| 19 |
</script> |
| 20 |
|
| 21 |
<script lang="ts"> |
| 22 |
import DocumentPreviewEditor from "../../editor/DocumentPreviewEditor.svelte"; |
| 23 |
import { urlForRaw } from "../../helpers"; |
| 24 |
import { ext } from "../../lib/paths"; |
| 25 |
|
| 26 |
interface Props { |
| 27 |
filename: string; |
| 28 |
} |
| 29 |
|
| 30 |
let { filename }: Props = $props(); |
| 31 |
|
| 32 |
let extension = $derived(ext(filename).toLowerCase()); |
| 33 |
let url = $derived($urlForRaw("document/", { filename })); |
| 34 |
</script> |
| 35 |
|
| 36 |
{#if extension === "pdf"} |
| 37 |
<object title={filename} data={url}></object> |
| 38 |
{:else if plainTextExtensions.includes(extension)} |
| 39 |
<DocumentPreviewEditor {url} /> |
| 40 |
{:else if imageExtensions.includes(extension)} |
| 41 |
<img src={url} alt={filename} /> |
| 42 |
{:else if ["html", "htm"].includes(extension)} |
| 43 |
<iframe src={url} title={filename} sandbox=""></iframe> |
| 44 |
{:else} |
| 45 |
Preview for file `{filename}` with file type `{extension}` is not implemented |
| 46 |
{/if} |
| 47 |
|
| 48 |
<style> |
| 49 |
object, |
| 50 |
img, |
| 51 |
iframe { |
| 52 |
width: 100%; |
| 53 |
height: 100%; |
| 54 |
} |
| 55 |
|
| 56 |
img { |
| 57 |
object-fit: contain; |
| 58 |
} |
| 59 |
</style> |