Use plain text viewer for plain text files

Fixes #29
This commit is contained in:
2021-02-15 11:27:04 +01:00
parent 3a3cdaa59d
commit 443f8c1af5
6 changed files with 152 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
function TextViewer(viewer, file) {
this.viewer = viewer
this.file = file
this.pre = null
this.viewer = viewer
this.file = file
this.pre = null
this.prettyprint = null
this.container = document.createElement("div")
@@ -9,18 +9,20 @@ function TextViewer(viewer, file) {
if (this.file.name.endsWith(".md") || this.file.name.endsWith(".markdown") || file.mime_type === "text/demo") {
this.getMarkdown()
} else {
} else if (this.file.name.endsWith(".txt")) {
this.getText()
} else {
this.getCode()
}
}
TextViewer.prototype.getText = function() {
TextViewer.prototype.getCode = function () {
this.pre = document.createElement("pre")
this.pre.classList = "pre-container prettyprint linenums"
this.pre.innerText = "Loading..."
this.container.appendChild(this.pre)
if (this.file.size > 1<<20) { // File larger than 1 MiB
if (this.file.size > 1 << 20) { // File larger than 1 MiB
this.pre.innerText = "File is too large to view online.\nPlease download and view it locally."
return
}
@@ -36,23 +38,43 @@ TextViewer.prototype.getText = function() {
this.prettyprint.src = "https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=desert"
this.container.appendChild(this.prettyprint)
}).catch(err => {
this.pre.innerText = "Error loading file: "+err
this.pre.innerText = "Error loading file: " + err
})
}
TextViewer.prototype.getMarkdown = function() {
TextViewer.prototype.getText = function () {
this.pre = document.createElement("pre")
this.pre.innerText = "Loading..."
this.container.appendChild(this.pre)
if (this.file.size > 1 << 20) { // File larger than 1 MiB
this.pre.innerText = "File is too large to view online.\nPlease download and view it locally."
return
}
fetch(this.file.get_href).then(resp => {
if (!resp.ok) { return Promise.reject(resp.status) }
return resp.text()
}).then(resp => {
this.pre.innerText = resp
}).catch(err => {
this.pre.innerText = "Error loading file: " + err
})
}
TextViewer.prototype.getMarkdown = function () {
fetch(
domainURL()+window.location.pathname+"/preview"
domainURL() + window.location.pathname + "/preview"
).then(resp => {
if (!resp.ok) { return Promise.reject(resp.status) }
return resp.text()
}).then(resp => {
this.container.innerHTML = resp
}).catch(err => {
this.container.innerText = "Error loading file: "+err
this.container.innerText = "Error loading file: " + err
})
}
TextViewer.prototype.render = function(parent) {
TextViewer.prototype.render = function (parent) {
parent.appendChild(this.container)
}