2018-11-08 17:11:00 +08:00
|
|
|
import utils from "./utils.js";
|
|
|
|
import protectedSessionHolder from "./protected_session_holder.js";
|
|
|
|
import noteDetailService from "./note_detail.js";
|
2018-11-09 01:35:23 +08:00
|
|
|
import infoService from "./info.js";
|
2018-11-15 19:13:32 +08:00
|
|
|
import server from "./server.js";
|
2018-11-08 17:11:00 +08:00
|
|
|
|
2018-11-08 17:30:35 +08:00
|
|
|
const $component = $('#note-detail-image');
|
2018-11-19 19:12:58 +08:00
|
|
|
const $imageWrapper = $('#note-detail-image-wrapper');
|
2018-11-08 17:30:35 +08:00
|
|
|
const $imageView = $('#note-detail-image-view');
|
2018-11-08 17:11:00 +08:00
|
|
|
|
2018-11-15 19:13:32 +08:00
|
|
|
const $imageDownloadButton = $("#image-download");
|
|
|
|
const $copyToClipboardButton = $("#image-copy-to-clipboard");
|
|
|
|
const $fileName = $("#image-filename");
|
|
|
|
const $fileType = $("#image-filetype");
|
|
|
|
const $fileSize = $("#image-filesize");
|
2018-11-08 17:11:00 +08:00
|
|
|
|
|
|
|
async function show() {
|
|
|
|
const currentNote = noteDetailService.getCurrentNote();
|
|
|
|
|
2018-11-15 19:13:32 +08:00
|
|
|
const attributes = await server.get('notes/' + currentNote.noteId + '/attributes');
|
|
|
|
const attributeMap = utils.toObject(attributes, l => [l.name, l.value]);
|
|
|
|
|
2018-11-08 17:30:35 +08:00
|
|
|
$component.show();
|
2018-11-08 17:11:00 +08:00
|
|
|
|
2018-11-15 21:33:02 +08:00
|
|
|
$fileName.text(attributeMap.originalFileName || "?");
|
|
|
|
$fileSize.text((attributeMap.fileSize || "?") + " bytes");
|
2018-11-15 19:13:32 +08:00
|
|
|
$fileType.text(currentNote.mime);
|
|
|
|
|
2018-11-08 17:30:35 +08:00
|
|
|
$imageView.prop("src", `/api/images/${currentNote.noteId}/${currentNote.title}`);
|
2018-11-08 17:11:00 +08:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:32 +08:00
|
|
|
$imageDownloadButton.click(() => utils.download(getFileUrl()));
|
2018-11-08 17:11:00 +08:00
|
|
|
|
2018-11-09 01:35:23 +08:00
|
|
|
function selectImage(element) {
|
|
|
|
const selection = window.getSelection();
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(element);
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:32 +08:00
|
|
|
$copyToClipboardButton.click(() => {
|
2018-11-19 19:12:58 +08:00
|
|
|
$imageWrapper.attr('contenteditable','true');
|
2018-11-09 01:35:23 +08:00
|
|
|
|
|
|
|
try {
|
2018-11-19 19:12:58 +08:00
|
|
|
selectImage($imageWrapper.get(0));
|
2018-11-09 01:35:23 +08:00
|
|
|
|
|
|
|
const success = document.execCommand('copy');
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
infoService.showMessage("Image copied to the clipboard");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
infoService.showAndLogError("Could not copy the image to clipboard.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
window.getSelection().removeAllRanges();
|
2018-11-19 19:12:58 +08:00
|
|
|
$imageWrapper.removeAttr('contenteditable');
|
2018-11-09 01:35:23 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-08 17:11:00 +08:00
|
|
|
function getFileUrl() {
|
|
|
|
// electron needs absolute URL so we extract current host, port, protocol
|
|
|
|
return utils.getHost() + "/api/notes/" + noteDetailService.getCurrentNoteId()
|
|
|
|
+ "/download?protectedSessionId=" + encodeURIComponent(protectedSessionHolder.getProtectedSessionId());
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
show,
|
|
|
|
getContent: () => null,
|
|
|
|
focus: () => null,
|
|
|
|
onNoteChange: () => null,
|
2018-12-24 16:47:00 +08:00
|
|
|
cleanup: () => null,
|
|
|
|
scrollToTop: () => $component.scrollTop(0)
|
2018-11-08 17:11:00 +08:00
|
|
|
}
|