2021-10-26 22:15:01 +02:00
|
|
|
<script>
|
2021-12-14 20:04:05 +01:00
|
|
|
import { tick } from "svelte";
|
|
|
|
|
2021-10-26 22:15:01 +02:00
|
|
|
let container
|
|
|
|
let text_type = ""
|
|
|
|
|
2021-12-13 16:33:23 +01:00
|
|
|
export const set_file = file => {
|
|
|
|
console.log("loading text file", file.id)
|
|
|
|
|
2023-05-10 15:08:29 +02:00
|
|
|
if (file.name.endsWith(".md") || file.name.endsWith(".markdown")) {
|
2021-12-13 16:33:23 +01:00
|
|
|
markdown(file)
|
2023-01-11 20:28:08 +01:00
|
|
|
} else if (file.name.endsWith(".txt") || file.size > 524288) {
|
|
|
|
// If the file is larger than 512KiB we do not enable code highlighting
|
|
|
|
// because it's too slow
|
2021-12-13 16:33:23 +01:00
|
|
|
text(file)
|
2021-10-26 22:15:01 +02:00
|
|
|
} else {
|
2021-12-13 16:33:23 +01:00
|
|
|
code(file)
|
2021-10-26 22:15:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let md_container
|
2021-12-14 20:04:05 +01:00
|
|
|
const markdown = async file => {
|
2021-10-26 22:15:01 +02:00
|
|
|
text_type = "markdown"
|
2021-12-14 20:04:05 +01:00
|
|
|
await tick()
|
2021-10-26 22:15:01 +02:00
|
|
|
|
|
|
|
fetch("/u/" + file.id + "/preview").then(resp => {
|
|
|
|
if (!resp.ok) { return Promise.reject(resp.status) }
|
|
|
|
return resp.text()
|
|
|
|
}).then(resp => {
|
|
|
|
md_container.innerHTML = resp
|
|
|
|
}).catch(err => {
|
|
|
|
md_container.innerText = "Error loading file: " + err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let text_pre
|
2021-12-14 20:04:05 +01:00
|
|
|
const text = async file => {
|
2021-10-26 22:15:01 +02:00
|
|
|
text_type = "text"
|
2021-12-14 20:04:05 +01:00
|
|
|
await tick()
|
2021-10-26 22:15:01 +02:00
|
|
|
|
2021-12-09 22:16:52 +01:00
|
|
|
if (file.size > 1 << 22) { // File larger than 4 MiB
|
2021-10-26 22:15:01 +02:00
|
|
|
text_pre.innerText = "File is too large to view online.\nPlease download and view it locally."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch(file.get_href).then(resp => {
|
|
|
|
if (!resp.ok) { return Promise.reject(resp.status) }
|
|
|
|
return resp.text()
|
|
|
|
}).then(resp => {
|
|
|
|
text_pre.innerText = resp
|
|
|
|
}).catch(err => {
|
|
|
|
text_pre.innerText = "Error loading file: " + err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let code_pre
|
|
|
|
let prettyprint = false
|
2021-12-14 20:04:05 +01:00
|
|
|
const code = async file => {
|
2021-10-26 22:15:01 +02:00
|
|
|
text_type = "code"
|
2021-12-14 20:04:05 +01:00
|
|
|
await tick()
|
2021-10-26 22:15:01 +02:00
|
|
|
|
2021-12-09 22:16:52 +01:00
|
|
|
if (file.size > 1 << 22) { // File larger than 4 MiB
|
2021-10-26 22:15:01 +02:00
|
|
|
code_pre.innerText = "File is too large to view online.\nPlease download and view it locally."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch(file.get_href).then(resp => {
|
|
|
|
if (!resp.ok) { return Promise.reject(resp.status) }
|
|
|
|
return resp.text()
|
|
|
|
}).then(resp => {
|
|
|
|
code_pre.innerText = resp
|
|
|
|
|
|
|
|
// Load prettyprint script
|
|
|
|
if (!prettyprint) {
|
|
|
|
let prettyprint = document.createElement("script")
|
|
|
|
prettyprint.src = "https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?skin=desert"
|
|
|
|
container.appendChild(prettyprint)
|
|
|
|
prettyprint = true
|
|
|
|
} else {
|
|
|
|
PR.prettyPrint()
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
code_pre.innerText = "Error loading file: " + err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<div bind:this={container} class="container">
|
|
|
|
{#if text_type === "markdown"}
|
2022-02-28 16:35:51 +01:00
|
|
|
<section bind:this={md_container} class="md">
|
2021-10-26 22:15:01 +02:00
|
|
|
Loading...
|
2022-02-28 16:35:51 +01:00
|
|
|
</section>
|
2021-10-26 22:15:01 +02:00
|
|
|
{:else if text_type === "text"}
|
|
|
|
<pre bind:this={text_pre}>
|
|
|
|
Loading...
|
|
|
|
</pre>
|
|
|
|
{:else if text_type === "code"}
|
|
|
|
<pre bind:this={code_pre} class="pre-container prettyprint linenums">
|
|
|
|
Loading...
|
|
|
|
</pre>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.container {
|
2022-03-29 21:41:46 +02:00
|
|
|
background: var(--body_color);
|
2021-10-26 22:15:01 +02:00
|
|
|
text-align: left;
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
line-height: 1.5em;
|
2022-02-26 15:41:53 +01:00
|
|
|
overflow-y: auto;
|
2021-10-26 22:15:01 +02:00
|
|
|
overflow-x: hidden;
|
|
|
|
}
|
|
|
|
.container > pre {
|
2022-02-28 16:35:51 +01:00
|
|
|
margin: 0;
|
|
|
|
padding: 10px;
|
2021-10-26 22:15:01 +02:00
|
|
|
white-space: pre-wrap;
|
|
|
|
overflow: hidden;
|
2021-11-01 22:32:46 +01:00
|
|
|
border: none;
|
2022-06-21 14:00:03 +02:00
|
|
|
font-size: 0.9em;
|
2024-05-29 17:13:56 +02:00
|
|
|
word-break: break-word;
|
2021-10-26 22:15:01 +02:00
|
|
|
}
|
2022-02-28 16:35:51 +01:00
|
|
|
.container > .md {
|
|
|
|
display: block;
|
|
|
|
padding: 10px;
|
|
|
|
margin: auto;
|
2022-06-21 14:00:03 +02:00
|
|
|
text-align: justify;
|
2022-02-28 16:35:51 +01:00
|
|
|
}
|
2021-10-26 22:15:01 +02:00
|
|
|
</style>
|