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

57 lines
1.7 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;
this.prettyprint = null;
2020-01-27 16:56:16 +01:00
2020-01-28 12:51:21 +01:00
this.container = document.createElement("div");
this.container.classList = "text-container";
2020-01-27 16:56:16 +01:00
if (file.name.endsWith(".md") || file.name.endsWith(".markdown") || file.id === "demo") {
2020-01-28 12:51:21 +01:00
this.getMarkdown();
2020-01-27 16:56:16 +01:00
} else {
2020-01-28 12:51:21 +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() {
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
2020-01-28 12:51:21 +01:00
if (this.file.size > 1<<22) { // File larger than 4 MiB
this.pre.innerText = "File is too large to view online.\nPlease download and view it locally.";
2020-01-27 16:56:16 +01:00
return;
2020-01-21 15:33:09 +01:00
}
2020-01-28 12:51:21 +01:00
fetch(apiEndpoint+"/file/"+this.file.id).then(resp => {
2020-01-27 16:56:16 +01:00
if (!resp.ok) { return Promise.reject(resp.status); }
return resp.text();
}).then(resp => {
2020-01-28 12:51:21 +01:00
this.pre.innerText = resp;
2020-01-27 16:56:16 +01:00
// Load prettyprint script
2020-01-28 12:51:21 +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-01-28 12:51:21 +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() {
fetch("/u/"+this.file.id+"/preview").then(resp => {
2020-01-27 16:56:16 +01:00
if (!resp.ok) { return Promise.reject(resp.status); }
return resp.text();
}).then(resp => {
2020-01-28 12:51:21 +01:00
this.container.innerHTML = resp;
2020-01-27 16:56:16 +01:00
}).catch(err => {
2020-01-28 12:51:21 +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) {
parent.appendChild(this.container);
2020-01-21 15:33:09 +01:00
}