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

84 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-11-08 17:11:00 +08:00
import utils from "./utils.js";
import noteDetailService from "./note_detail.js";
2018-11-09 01:35:23 +08:00
import infoService from "./info.js";
import server from "./server.js";
2018-11-08 17:11:00 +08:00
2019-05-03 04:24:43 +08:00
class NoteDetailImage {
/**
* @param {NoteContext} ctx
*/
constructor(ctx) {
this.ctx = ctx;
2019-05-03 04:24:43 +08:00
this.$component = ctx.$noteTabContent.find('.note-detail-image');
this.$imageWrapper = ctx.$noteTabContent.find('.note-detail-image-wrapper');
this.$imageView = ctx.$noteTabContent.find('.note-detail-image-view');
this.$copyToClipboardButton = ctx.$noteTabContent.find(".image-copy-to-clipboard");
this.$fileName = ctx.$noteTabContent.find(".image-filename");
this.$fileType = ctx.$noteTabContent.find(".image-filetype");
this.$fileSize = ctx.$noteTabContent.find(".image-filesize");
2018-11-08 17:11:00 +08:00
2019-05-03 04:24:43 +08:00
this.$imageDownloadButton = ctx.$noteTabContent.find(".image-download");
this.$imageDownloadButton.click(() => utils.download(this.getFileUrl()));
2018-11-08 17:11:00 +08:00
2019-05-03 04:24:43 +08:00
this.$copyToClipboardButton.click(() => {
this.$imageWrapper.attr('contenteditable','true');
2018-11-08 17:11:00 +08:00
2019-05-03 04:24:43 +08:00
try {
this.selectImage(this.$imageWrapper.get(0));
2019-05-03 04:24:43 +08:00
const success = document.execCommand('copy');
2018-11-08 17:11:00 +08:00
2019-05-03 04:24:43 +08:00
if (success) {
infoService.showMessage("Image copied to the clipboard");
}
else {
infoService.showAndLogError("Could not copy the image to clipboard.");
}
}
finally {
window.getSelection().removeAllRanges();
this.$imageWrapper.removeAttr('contenteditable');
}
});
}
2018-11-08 17:11:00 +08:00
2019-05-03 04:24:43 +08:00
async show() {
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]);
2018-11-09 01:35:23 +08:00
2019-05-03 04:24:43 +08:00
this.$component.show();
2018-11-09 01:35:23 +08:00
2019-05-03 04:24:43 +08:00
this.$fileName.text(attributeMap.originalFileName || "?");
this.$fileSize.text((attributeMap.fileSize || "?") + " bytes");
this.$fileType.text(this.ctx.note.mime);
2018-11-09 01:35:23 +08:00
this.$imageView.prop("src", `api/images/${this.ctx.note.noteId}/${this.ctx.note.title}`);
2019-05-03 04:24:43 +08:00
}
2018-11-09 01:35:23 +08:00
2019-05-03 04:24:43 +08:00
selectImage(element) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
2018-11-09 01:35:23 +08:00
}
2019-05-03 04:24:43 +08:00
getFileUrl() {
// electron needs absolute URL so we extract current host, port, protocol
return utils.getHost() + "/api/notes/" + noteDetailService.getActiveNoteId() + "/download";
2018-11-09 01:35:23 +08:00
}
2019-05-03 04:24:43 +08:00
getContent() {}
focus() {}
onNoteChange() {}
cleanup() {}
scrollToTop() {
this.$component.scrollTop(0);
}
2018-11-08 17:11:00 +08:00
}
2019-05-03 04:24:43 +08:00
export default NoteDetailImage