2024-12-22 17:56:53 +02:00
|
|
|
import treeService, { Node } from '../services/tree.js';
|
2022-08-05 16:44:26 +02:00
|
|
|
import froca from "../services/froca.js";
|
2024-12-22 17:56:53 +02:00
|
|
|
import contextMenu, { MenuCommandItem, MenuItem } from "./context_menu.js";
|
2022-11-25 15:29:57 +01:00
|
|
|
import dialogService from "../services/dialog.js";
|
|
|
|
import server from "../services/server.js";
|
2024-10-20 10:49:50 +03:00
|
|
|
import { t } from '../services/i18n.js';
|
2024-12-22 17:56:53 +02:00
|
|
|
import type { SelectMenuItemEventListener } from '../components/events.js';
|
|
|
|
import NoteTreeWidget from '../widgets/note_tree.js';
|
2022-08-05 16:44:26 +02:00
|
|
|
|
2024-12-22 17:56:53 +02:00
|
|
|
export default class LauncherContextMenu implements SelectMenuItemEventListener {
|
|
|
|
|
|
|
|
private treeWidget: NoteTreeWidget;
|
|
|
|
private node: Node;
|
|
|
|
|
|
|
|
constructor(treeWidget: NoteTreeWidget, node: Node) {
|
2022-08-05 16:44:26 +02:00
|
|
|
this.treeWidget = treeWidget;
|
|
|
|
this.node = node;
|
|
|
|
}
|
|
|
|
|
2024-12-22 18:03:03 +02:00
|
|
|
async show(e: PointerEvent) {
|
2022-08-05 16:44:26 +02:00
|
|
|
contextMenu.show({
|
|
|
|
x: e.pageX,
|
|
|
|
y: e.pageY,
|
|
|
|
items: await this.getMenuItems(),
|
2023-01-15 21:04:17 +01:00
|
|
|
selectMenuItemHandler: (item, e) => this.selectMenuItemHandler(item)
|
2022-08-05 16:44:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-22 17:56:53 +02:00
|
|
|
async getMenuItems(): Promise<MenuItem[]> {
|
|
|
|
const note = this.node.data.noteId ? await froca.getNote(this.node.data.noteId) : null;
|
2022-08-07 13:23:03 +02:00
|
|
|
const parentNoteId = this.node.getParent().data.noteId;
|
|
|
|
|
2024-12-22 17:56:53 +02:00
|
|
|
const isVisibleRoot = note?.noteId === '_lbVisibleLaunchers';
|
|
|
|
const isAvailableRoot = note?.noteId === '_lbAvailableLaunchers';
|
2022-12-21 16:11:00 +01:00
|
|
|
const isVisibleItem = parentNoteId === '_lbVisibleLaunchers';
|
|
|
|
const isAvailableItem = parentNoteId === '_lbAvailableLaunchers';
|
2022-08-05 16:44:26 +02:00
|
|
|
const isItem = isVisibleItem || isAvailableItem;
|
2024-12-22 17:56:53 +02:00
|
|
|
const canBeDeleted = !note?.noteId.startsWith("_"); // fixed notes can't be deleted
|
|
|
|
const canBeReset = !canBeDeleted && note?.isLaunchBarConfig();
|
2022-08-05 16:44:26 +02:00
|
|
|
|
2024-12-22 18:33:57 +02:00
|
|
|
const items: (MenuItem | null)[] = [
|
2024-11-20 09:48:20 +02:00
|
|
|
(isVisibleRoot || isAvailableRoot) ? { title: t("launcher_context_menu.add-note-launcher"), command: 'addNoteLauncher', uiIcon: "bx bx-note" } : null,
|
|
|
|
(isVisibleRoot || isAvailableRoot) ? { title: t("launcher_context_menu.add-script-launcher"), command: 'addScriptLauncher', uiIcon: "bx bx-code-curly" } : null,
|
|
|
|
(isVisibleRoot || isAvailableRoot) ? { title: t("launcher_context_menu.add-custom-widget"), command: 'addWidgetLauncher', uiIcon: "bx bx-customize" } : null,
|
|
|
|
(isVisibleRoot || isAvailableRoot) ? { title: t("launcher_context_menu.add-spacer"), command: 'addSpacerLauncher', uiIcon: "bx bx-dots-horizontal" } : null,
|
2022-08-06 13:47:27 +02:00
|
|
|
(isVisibleRoot || isAvailableRoot) ? { title: "----" } : null,
|
2024-11-20 09:38:20 +02:00
|
|
|
|
2024-10-20 10:56:56 +03:00
|
|
|
isAvailableItem ? { title: t("launcher_context_menu.move-to-visible-launchers"), command: "moveLauncherToVisible", uiIcon: "bx bx-show", enabled: true } : null,
|
|
|
|
isVisibleItem ? { title: t("launcher_context_menu.move-to-available-launchers"), command: "moveLauncherToAvailable", uiIcon: "bx bx-hide", enabled: true } : null,
|
2024-11-20 09:38:20 +02:00
|
|
|
(isVisibleItem || isAvailableItem) ? { title: "----" } : null,
|
|
|
|
|
2024-11-20 09:48:20 +02:00
|
|
|
{ title: `${t("launcher_context_menu.duplicate-launcher")} <kbd data-command="duplicateSubtree">`, command: "duplicateSubtree", uiIcon: "bx bx-outline", enabled: isItem },
|
|
|
|
{ title: `${t("launcher_context_menu.delete")} <kbd data-command="deleteNotes"></kbd>`, command: "deleteNotes", uiIcon: "bx bx-trash destructive-action-icon", enabled: canBeDeleted },
|
2024-12-22 17:56:53 +02:00
|
|
|
|
2024-11-20 09:38:20 +02:00
|
|
|
{ title: "----" },
|
2024-12-22 17:56:53 +02:00
|
|
|
|
2024-11-20 09:48:20 +02:00
|
|
|
{ title: t("launcher_context_menu.reset"), command: "resetLauncher", uiIcon: "bx bx-reset destructive-action-icon", enabled: canBeReset}
|
2024-12-22 18:33:57 +02:00
|
|
|
];
|
|
|
|
return items.filter(row => row !== null);
|
2022-08-05 16:44:26 +02:00
|
|
|
}
|
|
|
|
|
2024-12-22 17:56:53 +02:00
|
|
|
async selectMenuItemHandler({command}: MenuCommandItem) {
|
|
|
|
if (!command) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:16:57 +01:00
|
|
|
if (command === 'resetLauncher') {
|
2024-10-20 10:49:50 +03:00
|
|
|
const confirmed = await dialogService.confirm(t("launcher_context_menu.reset_launcher_confirm", { title: this.node.title }));
|
2022-11-25 15:29:57 +01:00
|
|
|
|
|
|
|
if (confirmed) {
|
2022-12-01 10:16:57 +01:00
|
|
|
await server.post(`special-notes/launchers/${this.node.data.noteId}/reset`);
|
2022-11-25 15:29:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:00:56 +02:00
|
|
|
this.treeWidget.triggerCommand(command, {
|
|
|
|
node: this.node,
|
|
|
|
notePath: treeService.getNotePath(this.node),
|
|
|
|
selectedOrActiveBranchIds: this.treeWidget.getSelectedOrActiveBranchIds(this.node),
|
|
|
|
selectedOrActiveNoteIds: this.treeWidget.getSelectedOrActiveNoteIds(this.node)
|
|
|
|
});
|
2022-08-05 16:44:26 +02:00
|
|
|
}
|
|
|
|
}
|