Files
fnx_web/res/include/script/file_viewer/viewer_scripts/TextViewer.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-01-28 12:51:21 +01:00
function TextViewer(viewer, file) {
2020-02-04 19:37:46 +01:00
this.viewer = viewer
this.file = file
this.pre = null
this.prettyprint = null
2020-01-27 16:56:16 +01:00
2020-02-04 19:37:46 +01:00
this.container = document.createElement("div")
this.container.classList = "text-container"
2020-01-27 16:56:16 +01:00
2020-02-04 19:37:46 +01:00
if (this.file.name.endsWith(".md") || this.file.name.endsWith(".markdown") || file.mime_type === "text/demo") {
this.getMarkdown()
2020-01-27 16:56:16 +01:00
} else {
2020-02-04 19:37:46 +01:00
this.getText()
2020-01-21 15:33:09 +01:00
}
2020-01-27 16:56:16 +01:00
}
2020-01-21 15:33:09 +01:00
2020-01-28 12:51:21 +01:00
TextViewer.prototype.getText = function() {
2020-02-04 19:37:46 +01:00
this.pre = document.createElement("pre")
this.pre.classList = "pre-container prettyprint linenums"
this.pre.innerText = "Loading..."
this.container.appendChild(this.pre)
2020-01-21 15:33:09 +01:00
if (this.file.size > 1<<20) { // File larger than 1 MiB
2020-02-04 19:37:46 +01:00
this.pre.innerText = "File is too large to view online.\nPlease download and view it locally."
return
2020-01-21 15:33:09 +01:00
}
2020-02-04 19:37:46 +01:00
fetch(this.file.get_href).then(resp => {
if (!resp.ok) { return Promise.reject(resp.status) }
return resp.text()
2020-01-27 16:56:16 +01:00
}).then(resp => {
2020-02-04 19:37:46 +01:00
this.pre.innerText = resp
2020-01-27 16:56:16 +01:00
// Load prettyprint script
2020-02-04 19:37:46 +01:00
this.prettyprint = document.createElement("script")
this.prettyprint.src = "https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=desert"
this.container.appendChild(this.prettyprint)
2020-01-27 16:56:16 +01:00
}).catch(err => {
2020-02-04 19:37:46 +01:00
this.pre.innerText = "Error loading file: "+err
})
2020-01-27 16:56:16 +01:00
}
2020-01-28 12:51:21 +01:00
TextViewer.prototype.getMarkdown = function() {
2020-02-04 19:37:46 +01:00
fetch(
domainURL()+window.location.pathname+"/preview"
).then(resp => {
if (!resp.ok) { return Promise.reject(resp.status) }
return resp.text()
2020-01-27 16:56:16 +01:00
}).then(resp => {
2020-02-04 19:37:46 +01:00
this.container.innerHTML = resp
2020-01-27 16:56:16 +01:00
}).catch(err => {
2020-02-04 19:37:46 +01:00
this.container.innerText = "Error loading file: "+err
})
2020-01-27 16:56:16 +01:00
}
2020-01-28 12:51:21 +01:00
TextViewer.prototype.render = function(parent) {
2020-02-04 19:37:46 +01:00
parent.appendChild(this.container)
2020-01-21 15:33:09 +01:00
}