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

67 lines
1.9 KiB
JavaScript
Raw Normal View History

import utils from "./utils.js";
import server from "./server.js";
import protectedSessionHolder from "./protected_session_holder.js";
import noteDetailService from "./note_detail.js";
2018-11-08 17:30:35 +08:00
const $component = $('#note-detail-file');
2019-01-28 06:10:37 +08:00
const $fileNoteId = $("#file-note-id");
const $fileName = $("#file-filename");
const $fileType = $("#file-filetype");
const $fileSize = $("#file-filesize");
2018-11-17 01:40:58 +08:00
const $previewRow = $("#file-preview-row");
const $previewContent = $("#file-preview-content");
const $downloadButton = $("#file-download");
const $openButton = $("#file-open");
async function show() {
const activeNote = noteDetailService.getActiveNote();
const attributes = await server.get('notes/' + activeNote.noteId + '/attributes');
2018-08-10 02:55:16 +08:00
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);
2018-11-08 17:30:35 +08:00
$component.show();
$fileNoteId.text(activeNote.noteId);
$fileName.text(attributeMap.originalFileName || "?");
$fileSize.text((attributeMap.fileSize || "?") + " bytes");
$fileType.text(activeNote.mime);
2018-11-17 01:40:58 +08:00
if (activeNote.content) {
$previewRow.show();
$previewContent.text(activeNote.content);
}
else {
$previewRow.hide();
}
2019-04-22 21:01:05 +08:00
// open doesn't work for protected notes since it works through browser which isn't in protected session
2019-04-22 23:05:04 +08:00
$openButton.toggle(!activeNote.isProtected);
}
$downloadButton.click(() => utils.download(getFileUrl()));
$openButton.click(() => {
if (utils.isElectron()) {
const open = require("open");
open(getFileUrl());
}
else {
window.location.href = getFileUrl();
}
});
function getFileUrl() {
// electron needs absolute URL so we extract current host, port, protocol
return utils.getHost() + "/api/notes/" + noteDetailService.getActiveNoteId() + "/download";
}
export default {
show,
getContent: () => null,
focus: () => null,
onNoteChange: () => null,
cleanup: () => null,
scrollToTop: () => null
}