refactor(views/table): simplify context menu handling

This commit is contained in:
Elian Doran
2025-07-14 15:46:22 +03:00
parent 84479a2c2a
commit e703ce92a8
3 changed files with 30 additions and 32 deletions

View File

@@ -28,6 +28,7 @@ import TouchBarComponent from "./touch_bar.js";
import type { CKTextEditor } from "@triliumnext/ckeditor5"; import type { CKTextEditor } from "@triliumnext/ckeditor5";
import type CodeMirror from "@triliumnext/codemirror"; import type CodeMirror from "@triliumnext/codemirror";
import { StartupChecks } from "./startup_checks.js"; import { StartupChecks } from "./startup_checks.js";
import type { CreateNoteOpts } from "../services/note_create.js";
interface Layout { interface Layout {
getRootWidget: (appContext: AppContext) => RootWidget; getRootWidget: (appContext: AppContext) => RootWidget;
@@ -276,6 +277,12 @@ export type CommandMappings = {
geoMapCreateChildNote: CommandData; geoMapCreateChildNote: CommandData;
// Table view
addNewRow: CommandData & {
customOpts: CreateNoteOpts;
parentNotePath?: string;
};
buildTouchBar: CommandData & { buildTouchBar: CommandData & {
TouchBar: typeof TouchBar; TouchBar: typeof TouchBar;
buildIcon(name: string): NativeImage; buildIcon(name: string): NativeImage;

View File

@@ -6,6 +6,7 @@ import { t } from "../../../services/i18n.js";
import link_context_menu from "../../../menus/link_context_menu.js"; import link_context_menu from "../../../menus/link_context_menu.js";
import type FNote from "../../../entities/fnote.js"; import type FNote from "../../../entities/fnote.js";
import froca from "../../../services/froca.js"; import froca from "../../../services/froca.js";
import type Component from "../../../components/component.js";
export function setupContextMenu(tabulator: Tabulator, parentNote: FNote) { export function setupContextMenu(tabulator: Tabulator, parentNote: FNote) {
tabulator.on("rowContext", (e, row) => showRowContextMenu(e, row, parentNote)); tabulator.on("rowContext", (e, row) => showRowContextMenu(e, row, parentNote));
@@ -105,32 +106,20 @@ export function showRowContextMenu(_e: UIEvent, row: RowComponent, parentNote: F
{ {
title: t("table_view.row-insert-above"), title: t("table_view.row-insert-above"),
uiIcon: "bx bx-list-plus", uiIcon: "bx bx-list-plus",
handler: () => { handler: () => getParentComponent(e)?.triggerCommand("addNewRow", {
const target = e.target;
if (!target) {
return;
}
const component = $(target).closest(".component").prop("component");
component.triggerCommand("addNewRow", {
customOpts: { customOpts: {
target: "before", target: "before",
targetBranchId: rowData.branchId, targetBranchId: rowData.branchId,
} }
}); })
}
}, },
{ {
title: t("table_view.row-insert-child"), title: t("table_view.row-insert-child"),
uiIcon: "bx bx-empty", uiIcon: "bx bx-empty",
handler: async () => { handler: async () => {
const target = e.target;
if (!target) {
return;
}
const branchId = row.getData().branchId; const branchId = row.getData().branchId;
const note = await froca.getBranch(branchId)?.getNote(); const note = await froca.getBranch(branchId)?.getNote();
const component = $(target).closest(".component").prop("component"); getParentComponent(e)?.triggerCommand("addNewRow", {
component.triggerCommand("addNewRow", {
parentNotePath: note?.noteId, parentNotePath: note?.noteId,
customOpts: { customOpts: {
target: "after", target: "after",
@@ -142,19 +131,12 @@ export function showRowContextMenu(_e: UIEvent, row: RowComponent, parentNote: F
{ {
title: t("table_view.row-insert-below"), title: t("table_view.row-insert-below"),
uiIcon: "bx bx-empty", uiIcon: "bx bx-empty",
handler: () => { handler: () => getParentComponent(e)?.triggerCommand("addNewRow", {
const target = e.target;
if (!target) {
return;
}
const component = $(target).closest(".component").prop("component");
component.triggerCommand("addNewRow", {
customOpts: { customOpts: {
target: "after", target: "after",
targetBranchId: rowData.branchId, targetBranchId: rowData.branchId,
} }
}); })
}
}, },
{ title: "----" }, { title: "----" },
{ {
@@ -169,3 +151,13 @@ export function showRowContextMenu(_e: UIEvent, row: RowComponent, parentNote: F
}); });
e.preventDefault(); e.preventDefault();
} }
function getParentComponent(e: MouseEvent) {
if (!e.target) {
return;
}
return $(e.target)
.closest(".component")
.prop("component") as Component;
}

View File

@@ -229,14 +229,13 @@ export default class TableView extends ViewMode<StateInfo> {
console.log("Save attributes", this.newAttribute); console.log("Save attributes", this.newAttribute);
} }
addNewRowCommand({ customOpts, parentNotePath: customNotePath }: { customOpts: CreateNoteOpts, parentNotePath?: string }) { addNewRowCommand({ customOpts, parentNotePath: customNotePath }: CommandListenerData<"addNewRow">) {
const parentNotePath = customNotePath ?? this.args.parentNotePath; const parentNotePath = customNotePath ?? this.args.parentNotePath;
if (parentNotePath) { if (parentNotePath) {
const opts: CreateNoteOpts = { const opts: CreateNoteOpts = {
activate: false, activate: false,
...customOpts ...customOpts
} }
console.log("Create with ", opts);
note_create.createNote(parentNotePath, opts).then(({ branch }) => { note_create.createNote(parentNotePath, opts).then(({ branch }) => {
if (branch) { if (branch) {
setTimeout(() => { setTimeout(() => {