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 CodeMirror from "@triliumnext/codemirror";
import { StartupChecks } from "./startup_checks.js";
import type { CreateNoteOpts } from "../services/note_create.js";
interface Layout {
getRootWidget: (appContext: AppContext) => RootWidget;
@@ -276,6 +277,12 @@ export type CommandMappings = {
geoMapCreateChildNote: CommandData;
// Table view
addNewRow: CommandData & {
customOpts: CreateNoteOpts;
parentNotePath?: string;
};
buildTouchBar: CommandData & {
TouchBar: typeof TouchBar;
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 type FNote from "../../../entities/fnote.js";
import froca from "../../../services/froca.js";
import type Component from "../../../components/component.js";
export function setupContextMenu(tabulator: Tabulator, parentNote: FNote) {
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"),
uiIcon: "bx bx-list-plus",
handler: () => {
const target = e.target;
if (!target) {
return;
handler: () => getParentComponent(e)?.triggerCommand("addNewRow", {
customOpts: {
target: "before",
targetBranchId: rowData.branchId,
}
const component = $(target).closest(".component").prop("component");
component.triggerCommand("addNewRow", {
customOpts: {
target: "before",
targetBranchId: rowData.branchId,
}
});
}
})
},
{
title: t("table_view.row-insert-child"),
uiIcon: "bx bx-empty",
handler: async () => {
const target = e.target;
if (!target) {
return;
}
const branchId = row.getData().branchId;
const note = await froca.getBranch(branchId)?.getNote();
const component = $(target).closest(".component").prop("component");
component.triggerCommand("addNewRow", {
getParentComponent(e)?.triggerCommand("addNewRow", {
parentNotePath: note?.noteId,
customOpts: {
target: "after",
@@ -142,19 +131,12 @@ export function showRowContextMenu(_e: UIEvent, row: RowComponent, parentNote: F
{
title: t("table_view.row-insert-below"),
uiIcon: "bx bx-empty",
handler: () => {
const target = e.target;
if (!target) {
return;
handler: () => getParentComponent(e)?.triggerCommand("addNewRow", {
customOpts: {
target: "after",
targetBranchId: rowData.branchId,
}
const component = $(target).closest(".component").prop("component");
component.triggerCommand("addNewRow", {
customOpts: {
target: "after",
targetBranchId: rowData.branchId,
}
});
}
})
},
{ title: "----" },
{
@@ -169,3 +151,13 @@ export function showRowContextMenu(_e: UIEvent, row: RowComponent, parentNote: F
});
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);
}
addNewRowCommand({ customOpts, parentNotePath: customNotePath }: { customOpts: CreateNoteOpts, parentNotePath?: string }) {
addNewRowCommand({ customOpts, parentNotePath: customNotePath }: CommandListenerData<"addNewRow">) {
const parentNotePath = customNotePath ?? this.args.parentNotePath;
if (parentNotePath) {
const opts: CreateNoteOpts = {
activate: false,
...customOpts
}
console.log("Create with ", opts);
note_create.createNote(parentNotePath, opts).then(({ branch }) => {
if (branch) {
setTimeout(() => {