Files
Trilium/src/public/app/widgets/mobile_widgets/mobile_detail_menu.ts

62 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-04-26 09:40:02 +02:00
import BasicWidget from "../basic_widget.js";
2022-12-01 13:07:23 +01:00
import appContext from "../../components/app_context.js";
2022-08-05 16:44:26 +02:00
import contextMenu from "../../menus/context_menu.js";
2020-04-26 09:40:02 +02:00
import noteCreateService from "../../services/note_create.js";
import branchService from "../../services/branches.js";
import treeService from "../../services/tree.js";
import { t } from "../../services/i18n.js";
2020-03-01 12:50:02 +01:00
const TPL = `<button type="button" class="action-button bx" style="padding-top: 10px;"></button>`;
2020-03-01 12:50:02 +01:00
class MobileDetailMenuWidget extends BasicWidget {
private isHorizontalLayout: boolean;
constructor(isHorizontalLayout: boolean) {
super();
this.isHorizontalLayout = isHorizontalLayout;
}
2020-03-01 12:50:02 +01:00
doRender() {
this.$widget = $(TPL);
this.$widget.addClass(this.isHorizontalLayout ? "bx-dots-vertical-rounded" : "bx-menu");
2025-01-09 18:07:02 +02:00
this.$widget.on("click", async (e) => {
2021-05-22 12:35:41 +02:00
const note = appContext.tabManager.getActiveContextNote();
2020-03-01 12:50:02 +01:00
contextMenu.show({
x: e.pageX,
y: e.pageY,
items: [
2025-01-09 18:07:02 +02:00
{ title: t("mobile_detail_menu.insert_child_note"), command: "insertChildNote", uiIcon: "bx bx-plus", enabled: note.type !== "search" },
{ title: t("mobile_detail_menu.delete_this_note"), command: "delete", uiIcon: "bx bx-trash", enabled: note.noteId !== "root" }
2020-03-01 12:50:02 +01:00
],
2025-01-09 18:07:02 +02:00
selectMenuItemHandler: async ({ command }) => {
2020-03-01 12:50:02 +01:00
if (command === "insertChildNote") {
noteCreateService.createNote(appContext.tabManager.getActiveContextNotePath() ?? undefined);
2025-01-09 18:07:02 +02:00
} else if (command === "delete") {
2021-05-22 12:35:41 +02:00
const notePath = appContext.tabManager.getActiveContextNotePath();
if (!notePath) {
throw new Error("Cannot get note path to delete.");
}
const branchId = await treeService.getBranchIdFromUrl(notePath);
if (!branchId) {
throw new Error(t("mobile_detail_menu.error_cannot_get_branch_id", { notePath }));
}
if (await branchService.deleteNotes([branchId])) {
2025-01-09 18:07:02 +02:00
this.triggerCommand("setActiveScreen", { screen: "tree" });
2020-03-01 12:50:02 +01:00
}
2025-01-09 18:07:02 +02:00
} else {
throw new Error(t("mobile_detail_menu.error_unrecognized_command", { command }));
2020-03-01 12:50:02 +01:00
}
}
});
});
}
}
2020-07-03 22:27:45 +02:00
export default MobileDetailMenuWidget;