From e3dbe21c5a0df687509820dbc23a1a3f685ffdb0 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 31 Jan 2025 21:08:09 +0200 Subject: [PATCH] chore(client/ts): port note_actions --- src/public/app/components/app_context.ts | 4 ++ .../{note_actions.js => note_actions.ts} | 49 +++++++++++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) rename src/public/app/widgets/buttons/{note_actions.js => note_actions.ts} (80%) diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts index 072977695..dbfe78cd0 100644 --- a/src/public/app/components/app_context.ts +++ b/src/public/app/components/app_context.ts @@ -81,6 +81,10 @@ export type CommandMappings = { showOptions: CommandData & { section: string; }; + showExportDialog: CommandData & { + notePath: string; + defaultType: "single"; + }; showDeleteNotesDialog: CommandData & { branchIdsToDelete: string[]; callback: (value: ResolveOptions) => void; diff --git a/src/public/app/widgets/buttons/note_actions.js b/src/public/app/widgets/buttons/note_actions.ts similarity index 80% rename from src/public/app/widgets/buttons/note_actions.js rename to src/public/app/widgets/buttons/note_actions.ts index c5c1587ad..047a950bb 100644 --- a/src/public/app/widgets/buttons/note_actions.js +++ b/src/public/app/widgets/buttons/note_actions.ts @@ -5,8 +5,15 @@ import dialogService from "../../services/dialog.js"; import server from "../../services/server.js"; import toastService from "../../services/toast.js"; import ws from "../../services/ws.js"; -import appContext from "../../components/app_context.js"; +import appContext, { type EventData } from "../../components/app_context.js"; import { t } from "../../services/i18n.js"; +import type FNote from "../../entities/fnote.js"; +import type { FAttachmentRow } from "../../entities/fattachment.js"; + +// TODO: Deduplicate with server +interface ConvertToAttachmentResponse { + attachment: FAttachmentRow; +} const TPL = ` `; export default class NoteActionsWidget extends NoteContextAwareWidget { + + private $convertNoteIntoAttachmentButton!: JQuery; + private $findInTextButton!: JQuery; + private $printActiveNoteButton!: JQuery; + private $showSourceButton!: JQuery; + private $showAttachmentsButton!: JQuery; + private $renderNoteButton!: JQuery; + private $saveRevisionButton!: JQuery; + private $exportNoteButton!: JQuery; + private $importNoteButton!: JQuery; + private $openNoteExternallyButton!: JQuery; + private $openNoteCustomButton!: JQuery; + private $deleteNoteButton!: JQuery; + isEnabled() { return this.note?.type !== "launcher"; } doRender() { this.$widget = $(TPL); - this.$widget.on("show.bs.dropdown", () => this.refreshVisibility(this.note)); + this.$widget.on("show.bs.dropdown", () => { + if (this.note) { + this.refreshVisibility(this.note); + } + }); this.$convertNoteIntoAttachmentButton = this.$widget.find("[data-trigger-command='convertNoteIntoAttachment']"); this.$findInTextButton = this.$widget.find(".find-in-text-button"); @@ -118,7 +143,7 @@ export default class NoteActionsWidget extends NoteContextAwareWidget { this.$exportNoteButton = this.$widget.find(".export-note-button"); this.$exportNoteButton.on("click", () => { - if (this.$exportNoteButton.hasClass("disabled")) { + if (this.$exportNoteButton.hasClass("disabled") || !this.noteContext?.notePath) { return; } @@ -129,7 +154,11 @@ export default class NoteActionsWidget extends NoteContextAwareWidget { }); this.$importNoteButton = this.$widget.find(".import-files-button"); - this.$importNoteButton.on("click", () => this.triggerCommand("showImportDialog", { noteId: this.noteId })); + this.$importNoteButton.on("click", () => { + if (this.noteId) { + this.triggerCommand("showImportDialog", { noteId: this.noteId }); + } + }); this.$widget.on("click", ".dropdown-item", () => this.$widget.find("[data-bs-toggle='dropdown']").dropdown("toggle")); @@ -138,7 +167,7 @@ export default class NoteActionsWidget extends NoteContextAwareWidget { this.$deleteNoteButton = this.$widget.find(".delete-note-button"); this.$deleteNoteButton.on("click", () => { - if (this.note.noteId === "root") { + if (!this.note || this.note.noteId === "root") { return; } @@ -146,7 +175,7 @@ export default class NoteActionsWidget extends NoteContextAwareWidget { }); } - async refreshVisibility(note) { + async refreshVisibility(note: FNote) { const isInOptions = note.noteId.startsWith("_options"); this.$convertNoteIntoAttachmentButton.toggle(note.isEligibleForConversionToAttachment()); @@ -177,11 +206,11 @@ export default class NoteActionsWidget extends NoteContextAwareWidget { } async convertNoteIntoAttachmentCommand() { - if (!(await dialogService.confirm(t("note_actions.convert_into_attachment_prompt", { title: this.note.title })))) { + if (!this.note || !(await dialogService.confirm(t("note_actions.convert_into_attachment_prompt", { title: this.note.title })))) { return; } - const { attachment: newAttachment } = await server.post(`notes/${this.noteId}/convert-to-attachment`); + const { attachment: newAttachment } = await server.post(`notes/${this.noteId}/convert-to-attachment`); if (!newAttachment) { toastService.showMessage(t("note_actions.convert_into_attachment_failed", { title: this.note.title })); @@ -198,7 +227,7 @@ export default class NoteActionsWidget extends NoteContextAwareWidget { }); } - toggleDisabled($el, enable) { + toggleDisabled($el: JQuery, enable: boolean) { if (enable) { $el.removeAttr("disabled"); } else { @@ -206,7 +235,7 @@ export default class NoteActionsWidget extends NoteContextAwareWidget { } } - entitiesReloadedEvent({ loadResults }) { + entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { if (loadResults.isNoteReloaded(this.noteId)) { this.refresh(); }