Notes/src/public/app/widgets/buttons/attachments_actions.js

97 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-03-24 10:57:32 +01:00
import BasicWidget from "../basic_widget.js";
2023-03-30 23:48:26 +02:00
import server from "../../services/server.js";
2023-04-01 13:58:53 +02:00
import dialogService from "../../services/dialog.js";
2023-04-01 23:55:04 +02:00
import toastService from "../../services/toast.js";
import ws from "../../services/ws.js";
import appContext from "../../components/app_context.js";
2023-05-03 10:23:20 +02:00
import openService from "../../services/open.js";
2023-03-24 10:57:32 +01:00
const TPL = `
<div class="dropdown attachment-actions">
<style>
.attachment-actions {
width: 35px;
height: 35px;
}
.attachment-actions .dropdown-menu {
2023-04-01 23:55:04 +02:00
width: 20em;
2023-03-24 10:57:32 +01:00
}
.attachment-actions .dropdown-item[disabled], .attachment-actions .dropdown-item[disabled]:hover {
color: var(--muted-text-color) !important;
background-color: transparent !important;
pointer-events: none; /* makes it unclickable */
}
</style>
<button type="button" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false" class="icon-action icon-action-always-border bx bx-dots-vertical-rounded"></button>
<div class="dropdown-menu dropdown-menu-right">
2023-05-03 10:23:20 +02:00
<a data-trigger-command="openAttachment" class="dropdown-item">Open</a>
<a data-trigger-command="openAttachmentExternally" class="dropdown-item"
title="File will be open in an external application and watched for changes. You'll then be able to upload the modified version back to Trilium.">
Open externally</a>
<a data-trigger-command="downloadAttachment" class="dropdown-item">Download</a>
<a data-trigger-command="uploadNewAttachmentRevision" class="dropdown-item">Upload new revision</a>
<a data-trigger-command="copyAttachmentReferenceToClipboard" class="dropdown-item">Copy reference to clipboard</a>
2023-04-01 23:55:04 +02:00
<a data-trigger-command="convertAttachmentIntoNote" class="dropdown-item">Convert attachment into note</a>
2023-05-03 10:23:20 +02:00
<a data-trigger-command="deleteAttachment" class="dropdown-item">Delete attachment</a>
2023-03-24 10:57:32 +01:00
</div>
</div>`;
export default class AttachmentActionsWidget extends BasicWidget {
constructor(attachment) {
super();
this.attachment = attachment;
}
2023-05-03 10:23:20 +02:00
get attachmentId() {
return this.attachment.attachmentId;
}
2023-03-24 10:57:32 +01:00
doRender() {
this.$widget = $(TPL);
2023-04-01 13:58:53 +02:00
this.$widget.on('click', '.dropdown-item', () => this.$widget.find("[data-toggle='dropdown']").dropdown('toggle'));
2023-05-03 10:23:20 +02:00
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);
2023-03-24 10:57:32 +01:00
}
2023-03-30 23:48:26 +02:00
async deleteAttachmentCommand() {
if (!await dialogService.confirm(`Are you sure you want to delete attachment '${this.attachment.title}'?`)) {
return;
2023-04-01 23:55:04 +02:00
}
2023-05-03 10:23:20 +02:00
await server.remove(`attachments/${this.attachmentId}`);
toastService.showMessage(`Attachment '${this.attachment.title}' has been deleted.`);
2023-04-01 23:55:04 +02:00
}
async convertAttachmentIntoNoteCommand() {
if (!await dialogService.confirm(`Are you sure you want to convert attachment '${this.attachment.title}' into a separate note?`)) {
return;
2023-04-01 13:58:53 +02:00
}
2023-05-03 10:23:20 +02:00
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);
2023-03-24 10:57:32 +01:00
}
}