From 1aa6c17b56fae16f2b54b50360801808fec0ce6a Mon Sep 17 00:00:00 2001 From: Jin <22962980+JYC333@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:22:40 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20Port=20bulk=20action?= =?UTF-8?q?s=20to=20ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/public/app/components/app_context.ts | 9 ++++-- .../app/widgets/dialogs/branch_prefix.ts | 2 +- .../{bulk_actions.js => bulk_actions.ts} | 30 ++++++++++++++----- 3 files changed, 30 insertions(+), 11 deletions(-) rename src/public/app/widgets/dialogs/{bulk_actions.js => bulk_actions.ts} (87%) diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts index 4eb8bdb6a..8b50a8101 100644 --- a/src/public/app/components/app_context.ts +++ b/src/public/app/components/app_context.ts @@ -174,9 +174,9 @@ export type CommandMappings = { callback: (value: NoteDetailWidget | PromiseLike) => void; }; executeWithTextEditor: CommandData & - ExecuteCommandData & { - callback?: GetTextEditorCallback; - }; + ExecuteCommandData & { + callback?: GetTextEditorCallback; + }; executeWithCodeEditor: CommandData & ExecuteCommandData; /** * Called upon when attempting to retrieve the content element of a {@link NoteContext}. @@ -368,6 +368,9 @@ type EventMappings = { textTypeWidget: EditableTextTypeWidget; text: string; }; + openBulkActionsDialog: { + selectedOrActiveNoteIds: string[]; + }; }; diff --git a/src/public/app/widgets/dialogs/branch_prefix.ts b/src/public/app/widgets/dialogs/branch_prefix.ts index c2c8bd788..46a014e6c 100644 --- a/src/public/app/widgets/dialogs/branch_prefix.ts +++ b/src/public/app/widgets/dialogs/branch_prefix.ts @@ -42,7 +42,7 @@ export default class BranchPrefixDialog extends BasicWidget { private $noteTitle!: JQuery; private branchId: string | null = null; - doRender(): void { + doRender() { this.$widget = $(TPL); this.modal = Modal.getOrCreateInstance(this.$widget[0]); this.$form = this.$widget.find(".branch-prefix-form"); diff --git a/src/public/app/widgets/dialogs/bulk_actions.js b/src/public/app/widgets/dialogs/bulk_actions.ts similarity index 87% rename from src/public/app/widgets/dialogs/bulk_actions.js rename to src/public/app/widgets/dialogs/bulk_actions.ts index 41ff5e3b0..7bff5a0c8 100644 --- a/src/public/app/widgets/dialogs/bulk_actions.js +++ b/src/public/app/widgets/dialogs/bulk_actions.ts @@ -5,6 +5,8 @@ import utils from "../../services/utils.js"; import server from "../../services/server.js"; import toastService from "../../services/toast.js"; import { t } from "../../services/i18n.js"; +import type { EventData } from "../../components/app_context.js"; + const TPL = ` `; export default class BulkActionsDialog extends BasicWidget { + private $includeDescendants!: JQuery; + private $affectedNoteCount!: JQuery; + private $availableActionList!: JQuery; + private $existingActionList!: JQuery; + private $executeButton!: JQuery; + private selectedOrActiveNoteIds: string[] | null = null; + doRender() { this.$widget = $(TPL); this.$includeDescendants = this.$widget.find(".include-descendants"); @@ -79,9 +88,11 @@ export default class BulkActionsDialog extends BasicWidget { this.$widget.on("click", "[data-action-add]", async (event) => { const actionName = $(event.target).attr("data-action-add"); + if (!actionName) { + return; + } await bulkActionService.addAction("_bulkAction", actionName); - await this.refresh(); }); @@ -93,7 +104,6 @@ export default class BulkActionsDialog extends BasicWidget { }); toastService.showMessage(t("bulk_actions.bulk_actions_executed"), 3000); - utils.closeActiveDialog(); }); } @@ -101,21 +111,28 @@ export default class BulkActionsDialog extends BasicWidget { async refresh() { this.renderAvailableActions(); + if (!this.selectedOrActiveNoteIds) { + return; + } + const { affectedNoteCount } = await server.post("bulk-action/affected-notes", { noteIds: this.selectedOrActiveNoteIds, includeDescendants: this.$includeDescendants.is(":checked") - }); + }) as { affectedNoteCount: number }; this.$affectedNoteCount.text(affectedNoteCount); const bulkActionNote = await froca.getNote("_bulkAction"); + if (!bulkActionNote) { + return; + } const actions = bulkActionService.parseActions(bulkActionNote); this.$existingActionList.empty(); if (actions.length > 0) { - this.$existingActionList.append(...actions.map((action) => action.render())); + this.$existingActionList.append(...actions.map((action) => action.render()).filter((action) => action !== null)); } else { this.$existingActionList.append($("

").text(t("bulk_actions.none_yet"))); } @@ -138,7 +155,7 @@ export default class BulkActionsDialog extends BasicWidget { } } - entitiesReloadedEvent({ loadResults }) { + entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) { // only refreshing deleted attrs, otherwise components update themselves if (loadResults.getAttributeRows().find((row) => row.type === "label" && row.name === "action" && row.noteId === "_bulkAction" && row.isDeleted)) { // this may be triggered from e.g., sync without open widget, then no need to refresh the widget @@ -148,12 +165,11 @@ export default class BulkActionsDialog extends BasicWidget { } } - async openBulkActionsDialogEvent({ selectedOrActiveNoteIds }) { + async openBulkActionsDialogEvent({ selectedOrActiveNoteIds }: EventData<"openBulkActionsDialog">) { this.selectedOrActiveNoteIds = selectedOrActiveNoteIds; this.$includeDescendants.prop("checked", false); await this.refresh(); - utils.openDialog(this.$widget); } }