trilium/src/public/javascripts/services/note_detail_file.js

75 lines
2.3 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import server from "./server.js";
2019-05-03 04:24:43 +08:00
class NoteDetailFile {
/**
2019-05-09 01:55:24 +08:00
* @param {TabContext} ctx
2019-05-03 04:24:43 +08:00
*/
constructor(ctx) {
2019-05-04 03:50:14 +08:00
this.ctx = ctx;
2019-05-09 01:55:24 +08:00
this.$component = ctx.$tabContent.find('.note-detail-file');
this.$fileNoteId = ctx.$tabContent.find(".file-note-id");
this.$fileName = ctx.$tabContent.find(".file-filename");
this.$fileType = ctx.$tabContent.find(".file-filetype");
this.$fileSize = ctx.$tabContent.find(".file-filesize");
this.$previewRow = ctx.$tabContent.find(".file-preview-row");
this.$previewContent = ctx.$tabContent.find(".file-preview-content");
this.$downloadButton = ctx.$tabContent.find(".file-download");
this.$openButton = ctx.$tabContent.find(".file-open");
2019-05-03 04:24:43 +08:00
this.$downloadButton.click(() => utils.download(this.getFileUrl()));
2019-05-03 04:24:43 +08:00
this.$openButton.click(() => {
if (utils.isElectron()) {
const open = require("open");
2019-10-17 01:42:42 +08:00
open(this.getFileUrl(), {url: true});
2019-05-03 04:24:43 +08:00
}
else {
window.location.href = this.getFileUrl();
}
});
}
2019-05-12 18:58:55 +08:00
async render() {
2019-05-04 03:50:14 +08:00
const attributes = await server.get('notes/' + this.ctx.note.noteId + '/attributes');
2019-05-03 04:24:43 +08:00
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);
this.$component.show();
2019-05-04 03:50:14 +08:00
this.$fileNoteId.text(this.ctx.note.noteId);
2019-05-03 04:24:43 +08:00
this.$fileName.text(attributeMap.originalFileName || "?");
2019-11-09 16:36:08 +08:00
this.$fileSize.text(this.ctx.note.contentLength + " bytes");
2019-05-04 03:50:14 +08:00
this.$fileType.text(this.ctx.note.mime);
2019-05-04 03:50:14 +08:00
if (this.ctx.note.content) {
2019-05-03 04:24:43 +08:00
this.$previewRow.show();
2019-05-04 03:50:14 +08:00
this.$previewContent.text(this.ctx.note.content);
2019-05-03 04:24:43 +08:00
}
else {
this.$previewRow.hide();
}
2018-11-17 01:40:58 +08:00
2019-05-03 04:24:43 +08:00
// open doesn't work for protected notes since it works through browser which isn't in protected session
2019-05-04 03:50:14 +08:00
this.$openButton.toggle(!this.ctx.note.isProtected);
}
2019-05-03 04:24:43 +08:00
getFileUrl() {
// electron needs absolute URL so we extract current host, port, protocol
2019-05-04 03:50:14 +08:00
return utils.getHost() + "/api/notes/" + this.ctx.note.noteId + "/download";
}
2019-04-22 21:01:05 +08:00
show() {}
2019-05-03 04:24:43 +08:00
getContent() {}
2019-05-03 04:24:43 +08:00
focus() {}
2019-05-03 04:24:43 +08:00
onNoteChange() {}
2019-05-03 04:24:43 +08:00
cleanup() {}
2019-05-03 04:24:43 +08:00
scrollToTop() {}
}
2019-05-03 04:24:43 +08:00
export default NoteDetailFile;