cryptgeon/frontend/src/lib/ui/ShowNote.svelte

88 lines
2.0 KiB
Svelte
Raw Normal View History

2021-12-21 00:15:04 +01:00
<script lang="ts">
import type { FileDTO, NotePublic } from '$lib/api'
import { Files } from '$lib/files'
import copy from 'copy-to-clipboard'
import { saveAs } from 'file-saver'
import prettyBytes from 'pretty-bytes'
2022-05-14 16:57:28 +02:00
import sanitize from 'sanitize-html'
2022-01-16 14:02:53 +01:00
import { t } from 'svelte-intl-precompile'
2021-12-21 00:15:04 +01:00
import Button from './Button.svelte'
export let note: NotePublic
2022-05-14 16:57:28 +02:00
const RE_URL = /[A-Za-z]+:\/\/([A-Z a-z0-9\-._~:\/?#\[\]@!$&'()*+,;%=])+/g
2021-12-21 00:15:04 +01:00
let files: FileDTO[] = []
$: if (note.meta.type === 'file') {
files = JSON.parse(note.contents) as FileDTO[]
}
$: download = () => {
for (const file of files) {
downloadFile(file)
}
}
async function downloadFile(file: FileDTO) {
const f = new File([await Files.fromString(file.contents)], file.name, {
type: file.type,
})
saveAs(f)
}
2022-05-14 16:57:28 +02:00
function contentWithLinks(content: string): string {
const replaced = note.contents.replace(
RE_URL,
(url) => `<a href="${url}" rel="noreferrer">${url}</a>`
)
return sanitize(replaced, { allowedTags: ['a'], allowedAttributes: { a: ['href', 'rel'] } })
}
2021-12-21 00:15:04 +01:00
</script>
2022-01-16 14:02:53 +01:00
<p class="error-text">{@html $t('show.warning_will_not_see_again')}</p>
2021-12-21 00:15:04 +01:00
{#if note.meta.type === 'text'}
2022-05-14 16:57:28 +02:00
<div class="note">
{@html contentWithLinks(note.contents)}
</div>
2022-01-16 14:02:53 +01:00
<Button on:click={() => copy(note.contents)}>{$t('common.copy_clipboard')}</Button>
2021-12-21 00:15:04 +01:00
{:else}
{#each files as file}
2022-01-16 14:02:53 +01:00
<div class="note file">
2021-12-21 00:15:04 +01:00
<b on:click={() => downloadFile(file)}> {file.name}</b>
<small> {file.type} {prettyBytes(file.size)}</small>
</div>
{/each}
2022-01-16 14:02:53 +01:00
<Button on:click={download}>{$t('show.download_all')}</Button>
2021-12-21 00:15:04 +01:00
{/if}
<style>
.note {
width: 100%;
margin: 0;
padding: 0;
border: 2px solid var(--ui-bg-1);
outline: none;
padding: 0.5rem;
white-space: pre;
overflow: auto;
margin-bottom: 0.5rem;
}
.note b {
cursor: pointer;
2022-03-01 01:52:04 +01:00
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
2021-12-21 00:15:04 +01:00
}
.note.file {
display: flex;
justify-content: space-between;
align-items: center;
}
2022-03-01 01:52:04 +01:00
.note.file small {
padding-left: 1rem;
}
2021-12-21 00:15:04 +01:00
</style>