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

81 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-01-28 12:51:21 +01:00
function TextViewer(viewer, file) {
this.viewer = viewer
this.file = file
this.pre = null
2020-02-04 19:37:46 +01:00
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()
} else if (this.file.name.endsWith(".txt")) {
2020-02-04 19:37:46 +01:00
this.getText()
} else {
this.getCode()
2020-01-21 15:33:09 +01:00
}
2020-01-27 16:56:16 +01:00
}
2020-01-21 15:33:09 +01:00
TextViewer.prototype.getCode = 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 = `{{noescape "https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=desert"}}`
2020-02-04 19:37:46 +01:00
this.container.appendChild(this.prettyprint)
2020-01-27 16:56:16 +01:00
}).catch(err => {
this.pre.innerText = "Error loading file: " + err
})
}
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
2020-02-04 19:37:46 +01:00
})
2020-01-27 16:56:16 +01:00
}
TextViewer.prototype.getMarkdown = function () {
2020-02-04 19:37:46 +01:00
fetch(
domainURL() + window.location.pathname + "/preview"
2020-02-04 19:37:46 +01:00
).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 => {
this.container.innerText = "Error loading file: " + err
2020-02-04 19:37:46 +01:00
})
2020-01-27 16:56:16 +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
}