refactor: 💡 Port bulk actions to ts

This commit is contained in:
Jin 2025-03-19 14:22:40 +01:00
parent 91a963fafd
commit 1aa6c17b56
3 changed files with 30 additions and 11 deletions

View File

@ -174,9 +174,9 @@ export type CommandMappings = {
callback: (value: NoteDetailWidget | PromiseLike<NoteDetailWidget>) => void; callback: (value: NoteDetailWidget | PromiseLike<NoteDetailWidget>) => void;
}; };
executeWithTextEditor: CommandData & executeWithTextEditor: CommandData &
ExecuteCommandData<TextEditor> & { ExecuteCommandData<TextEditor> & {
callback?: GetTextEditorCallback; callback?: GetTextEditorCallback;
}; };
executeWithCodeEditor: CommandData & ExecuteCommandData<CodeMirrorInstance>; executeWithCodeEditor: CommandData & ExecuteCommandData<CodeMirrorInstance>;
/** /**
* Called upon when attempting to retrieve the content element of a {@link NoteContext}. * Called upon when attempting to retrieve the content element of a {@link NoteContext}.
@ -368,6 +368,9 @@ type EventMappings = {
textTypeWidget: EditableTextTypeWidget; textTypeWidget: EditableTextTypeWidget;
text: string; text: string;
}; };
openBulkActionsDialog: {
selectedOrActiveNoteIds: string[];
};
}; };

View File

@ -42,7 +42,7 @@ export default class BranchPrefixDialog extends BasicWidget {
private $noteTitle!: JQuery<HTMLElement>; private $noteTitle!: JQuery<HTMLElement>;
private branchId: string | null = null; private branchId: string | null = null;
doRender(): void { doRender() {
this.$widget = $(TPL); this.$widget = $(TPL);
this.modal = Modal.getOrCreateInstance(this.$widget[0]); this.modal = Modal.getOrCreateInstance(this.$widget[0]);
this.$form = this.$widget.find(".branch-prefix-form"); this.$form = this.$widget.find(".branch-prefix-form");

View File

@ -5,6 +5,8 @@ import utils from "../../services/utils.js";
import server from "../../services/server.js"; import server from "../../services/server.js";
import toastService from "../../services/toast.js"; import toastService from "../../services/toast.js";
import { t } from "../../services/i18n.js"; import { t } from "../../services/i18n.js";
import type { EventData } from "../../components/app_context.js";
const TPL = ` const TPL = `
<div class="bulk-actions-dialog modal mx-auto" tabindex="-1" role="dialog"> <div class="bulk-actions-dialog modal mx-auto" tabindex="-1" role="dialog">
@ -67,6 +69,13 @@ const TPL = `
</div>`; </div>`;
export default class BulkActionsDialog extends BasicWidget { export default class BulkActionsDialog extends BasicWidget {
private $includeDescendants!: JQuery<HTMLElement>;
private $affectedNoteCount!: JQuery<HTMLElement>;
private $availableActionList!: JQuery<HTMLElement>;
private $existingActionList!: JQuery<HTMLElement>;
private $executeButton!: JQuery<HTMLElement>;
private selectedOrActiveNoteIds: string[] | null = null;
doRender() { doRender() {
this.$widget = $(TPL); this.$widget = $(TPL);
this.$includeDescendants = this.$widget.find(".include-descendants"); 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) => { this.$widget.on("click", "[data-action-add]", async (event) => {
const actionName = $(event.target).attr("data-action-add"); const actionName = $(event.target).attr("data-action-add");
if (!actionName) {
return;
}
await bulkActionService.addAction("_bulkAction", actionName); await bulkActionService.addAction("_bulkAction", actionName);
await this.refresh(); await this.refresh();
}); });
@ -93,7 +104,6 @@ export default class BulkActionsDialog extends BasicWidget {
}); });
toastService.showMessage(t("bulk_actions.bulk_actions_executed"), 3000); toastService.showMessage(t("bulk_actions.bulk_actions_executed"), 3000);
utils.closeActiveDialog(); utils.closeActiveDialog();
}); });
} }
@ -101,21 +111,28 @@ export default class BulkActionsDialog extends BasicWidget {
async refresh() { async refresh() {
this.renderAvailableActions(); this.renderAvailableActions();
if (!this.selectedOrActiveNoteIds) {
return;
}
const { affectedNoteCount } = await server.post("bulk-action/affected-notes", { const { affectedNoteCount } = await server.post("bulk-action/affected-notes", {
noteIds: this.selectedOrActiveNoteIds, noteIds: this.selectedOrActiveNoteIds,
includeDescendants: this.$includeDescendants.is(":checked") includeDescendants: this.$includeDescendants.is(":checked")
}); }) as { affectedNoteCount: number };
this.$affectedNoteCount.text(affectedNoteCount); this.$affectedNoteCount.text(affectedNoteCount);
const bulkActionNote = await froca.getNote("_bulkAction"); const bulkActionNote = await froca.getNote("_bulkAction");
if (!bulkActionNote) {
return;
}
const actions = bulkActionService.parseActions(bulkActionNote); const actions = bulkActionService.parseActions(bulkActionNote);
this.$existingActionList.empty(); this.$existingActionList.empty();
if (actions.length > 0) { 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 { } else {
this.$existingActionList.append($("<p>").text(t("bulk_actions.none_yet"))); this.$existingActionList.append($("<p>").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 // only refreshing deleted attrs, otherwise components update themselves
if (loadResults.getAttributeRows().find((row) => row.type === "label" && row.name === "action" && row.noteId === "_bulkAction" && row.isDeleted)) { 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 // 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.selectedOrActiveNoteIds = selectedOrActiveNoteIds;
this.$includeDescendants.prop("checked", false); this.$includeDescendants.prop("checked", false);
await this.refresh(); await this.refresh();
utils.openDialog(this.$widget); utils.openDialog(this.$widget);
} }
} }