Files
fnx_web/svelte/src/file_viewer/viewers/Text.svelte

131 lines
2.8 KiB
Svelte
Raw Normal View History

<script>
import { tick } from "svelte";
let container
let text_type = ""
export const set_file = file => {
console.log("loading text file", file.id)
if (file.name.endsWith(".md") || file.name.endsWith(".markdown")) {
markdown(file)
} 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
text(file)
} else {
code(file)
}
}
let md_container
const markdown = async file => {
text_type = "markdown"
await tick()
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
const text = async file => {
text_type = "text"
await tick()
2021-12-09 22:16:52 +01:00
if (file.size > 1 << 22) { // File larger than 4 MiB
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
const code = async file => {
text_type = "code"
await tick()
2021-12-09 22:16:52 +01:00
if (file.size > 1 << 22) { // File larger than 4 MiB
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"}
<section bind:this={md_container} class="md">
Loading...
</section>
{: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);
text-align: left;
height: 100%;
width: 100%;
line-height: 1.5em;
2022-02-26 15:41:53 +01:00
overflow-y: auto;
overflow-x: hidden;
}
.container > pre {
margin: 0;
padding: 10px;
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;
}
.container > .md {
display: block;
padding: 10px;
margin: auto;
2022-06-21 14:00:03 +02:00
text-align: justify;
}
</style>