import BasicWidget from "../basic_widget.js"; import server from "../../services/server.js"; import dialogService from "../../services/dialog.js"; import toastService from "../../services/toast.js"; import ws from "../../services/ws.js"; import appContext from "../../components/app_context.js"; import openService from "../../services/open.js"; const TPL = ` `; export default class AttachmentActionsWidget extends BasicWidget { constructor(attachment) { super(); this.attachment = attachment; } get attachmentId() { return this.attachment.attachmentId; } doRender() { this.$widget = $(TPL); this.$widget.on('click', '.dropdown-item', () => this.$widget.find("[data-toggle='dropdown']").dropdown('toggle')); this.$widget.find("[data-trigger-command='copyAttachmentReferenceToClipboard']").toggle(this.attachment.role === 'image'); } async openAttachmentCommand() { await openService.openAttachmentExternally(this.attachmentId, this.attachment.mime); } async downloadAttachmentCommand() { await openService.downloadAttachment(this.attachmentId); } async copyAttachmentReferenceToClipboardCommand() { this.parent.copyAttachmentReferenceToClipboard(); } async openAttachmentExternallyCommand() { await openService.openAttachmentExternally(this.attachmentId, this.attachment.mime); } async deleteAttachmentCommand() { if (!await dialogService.confirm(`Are you sure you want to delete attachment '${this.attachment.title}'?`)) { return; } await server.remove(`attachments/${this.attachmentId}`); toastService.showMessage(`Attachment '${this.attachment.title}' has been deleted.`); } async convertAttachmentIntoNoteCommand() { if (!await dialogService.confirm(`Are you sure you want to convert attachment '${this.attachment.title}' into a separate note?`)) { return; } const {note: newNote} = await server.post(`attachments/${this.attachmentId}/convert-to-note`) toastService.showMessage(`Attachment '${this.attachment.title}' has been converted to note.`); await ws.waitForMaxKnownEntityChangeId(); await appContext.tabManager.getActiveContext().setNote(newNote.noteId); } }