Files
Trilium/apps/client/src/widgets/buttons/note_actions.ts

125 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-05-24 22:29:49 +02:00
import NoteContextAwareWidget from "../note_context_aware_widget.js";
import utils from "../../services/utils.js";
import branchService from "../../services/branches.js";
import dialogService from "../../services/dialog.js";
import server from "../../services/server.js";
import toastService from "../../services/toast.js";
import ws from "../../services/ws.js";
2025-01-31 21:08:09 +02:00
import appContext, { type EventData } from "../../components/app_context.js";
import { t } from "../../services/i18n.js";
2025-01-31 21:08:09 +02:00
import type FNote from "../../entities/fnote.js";
import type { FAttachmentRow } from "../../entities/fattachment.js";
const TPL = /*html*/`
2020-01-19 13:19:40 +01:00
<div class="dropdown note-actions">
2024-11-19 14:03:30 +08:00
<style>
.note-actions {
width: 35px;
height: 35px;
}
2024-11-19 11:08:20 +08:00
2024-11-19 14:03:30 +08:00
.note-actions .dropdown-menu {
min-width: 15em;
}
2024-11-19 11:08:20 +08:00
2024-11-19 14:03:30 +08:00
.note-actions .dropdown-item .bx {
position: relative;
top: 3px;
font-size: 120%;
margin-right: 5px;
}
2024-11-19 11:08:20 +08:00
2024-11-19 14:03:30 +08:00
.note-actions .dropdown-item[disabled], .note-actions .dropdown-item[disabled]:hover {
color: var(--muted-text-color) !important;
background-color: transparent !important;
pointer-events: none; /* makes it unclickable */
}
2020-02-27 00:58:10 +01:00
2024-11-19 14:03:30 +08:00
</style>
2020-01-19 13:19:40 +01:00
</div>`;
2021-05-22 12:35:41 +02:00
export default class NoteActionsWidget extends NoteContextAwareWidget {
2025-01-31 21:08:09 +02:00
private $convertNoteIntoAttachmentButton!: JQuery<HTMLElement>;
private $findInTextButton!: JQuery<HTMLElement>;
private $printActiveNoteButton!: JQuery<HTMLElement>;
private $exportAsPdfButton!: JQuery<HTMLElement>;
2025-01-31 21:08:09 +02:00
private $showSourceButton!: JQuery<HTMLElement>;
private $showAttachmentsButton!: JQuery<HTMLElement>;
private $renderNoteButton!: JQuery<HTMLElement>;
private $saveRevisionButton!: JQuery<HTMLElement>;
private $exportNoteButton!: JQuery<HTMLElement>;
private $importNoteButton!: JQuery<HTMLElement>;
private $openNoteExternallyButton!: JQuery<HTMLElement>;
private $openNoteCustomButton!: JQuery<HTMLElement>;
private $deleteNoteButton!: JQuery<HTMLElement>;
2021-09-30 13:02:07 +02:00
isEnabled() {
2025-01-09 18:07:02 +02:00
return this.note?.type !== "launcher";
2021-09-30 13:02:07 +02:00
}
2020-01-19 13:19:40 +01:00
doRender() {
this.$widget = $(TPL);
2025-01-31 21:08:09 +02:00
this.$widget.on("show.bs.dropdown", () => {
if (this.note) {
this.refreshVisibility(this.note);
}
});
2020-01-19 13:19:40 +01:00
this.$convertNoteIntoAttachmentButton = this.$widget.find("[data-trigger-command='convertNoteIntoAttachment']");
2025-01-09 18:07:02 +02:00
this.$findInTextButton = this.$widget.find(".find-in-text-button");
this.$printActiveNoteButton = this.$widget.find(".print-active-note-button");
this.$exportAsPdfButton = this.$widget.find(".export-as-pdf-button");
2025-01-09 18:07:02 +02:00
this.$showSourceButton = this.$widget.find(".show-source-button");
this.$showAttachmentsButton = this.$widget.find(".show-attachments-button");
this.$renderNoteButton = this.$widget.find(".render-note-button");
2024-08-17 01:57:52 +03:00
this.$saveRevisionButton = this.$widget.find(".save-revision-button");
2020-01-21 21:43:23 +01:00
2025-01-09 18:07:02 +02:00
this.$widget.on("click", ".dropdown-item", () => this.$widget.find("[data-bs-toggle='dropdown']").dropdown("toggle"));
this.$openNoteExternallyButton = this.$widget.find(".open-note-externally-button");
2023-05-16 11:57:28 +00:00
this.$openNoteCustomButton = this.$widget.find(".open-note-custom-button");
2020-01-19 13:19:40 +01:00
}
2020-01-20 22:35:52 +01:00
2025-01-31 21:08:09 +02:00
async refreshVisibility(note: FNote) {
this.$convertNoteIntoAttachmentButton.toggle(note.isEligibleForConversionToAttachment());
this.toggleDisabled(this.$showAttachmentsButton, !isInOptions);
this.toggleDisabled(this.$showSourceButton, ["text", "code", "relationMap", "mermaid", "canvas", "mindMap"].includes(note.type));
this.toggleDisabled(this.$printActiveNoteButton, canPrint);
this.toggleDisabled(this.$exportAsPdfButton, canPrint);
this.$exportAsPdfButton.toggleClass("hidden-ext", !utils.isElectron());
2025-01-09 18:07:02 +02:00
this.toggleDisabled(this.$openNoteExternallyButton, utils.isElectron() && !["search", "book"].includes(note.type));
this.toggleDisabled(
this.$openNoteCustomButton,
utils.isElectron() &&
!utils.isMac() && // no implementation for Mac yet
!["search", "book"].includes(note.type)
);
// I don't want to handle all special notes like this, but intuitively user might want to export content of backend log
2025-01-09 18:07:02 +02:00
this.toggleDisabled(this.$exportNoteButton, !["_backendLog"].includes(note.noteId) && !isInOptions);
2025-01-09 18:07:02 +02:00
this.toggleDisabled(this.$importNoteButton, !["search"].includes(note.type) && !isInOptions);
2024-08-17 01:57:52 +03:00
this.toggleDisabled(this.$deleteNoteButton, !isInOptions);
this.toggleDisabled(this.$saveRevisionButton, !isInOptions);
}
2025-01-31 21:08:09 +02:00
toggleDisabled($el: JQuery<HTMLElement>, enable: boolean) {
if (enable) {
2025-01-09 18:07:02 +02:00
$el.removeAttr("disabled");
} else {
2025-01-09 18:07:02 +02:00
$el.attr("disabled", "disabled");
}
}
2025-01-31 21:08:09 +02:00
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (loadResults.isNoteReloaded(this.noteId)) {
this.refresh();
}
2020-01-20 22:35:52 +01:00
}
2020-07-03 22:27:45 +02:00
}