mirror of
https://github.com/zadam/trilium.git
synced 2025-10-28 16:56:34 +01:00
feat(book/table): allow show/hide columns
This commit is contained in:
@@ -2,7 +2,7 @@ import keyboardActionService from "../services/keyboard_actions.js";
|
|||||||
import note_tooltip from "../services/note_tooltip.js";
|
import note_tooltip from "../services/note_tooltip.js";
|
||||||
import utils from "../services/utils.js";
|
import utils from "../services/utils.js";
|
||||||
|
|
||||||
interface ContextMenuOptions<T> {
|
export interface ContextMenuOptions<T> {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
orientation?: "left";
|
orientation?: "left";
|
||||||
@@ -28,6 +28,7 @@ export interface MenuCommandItem<T> {
|
|||||||
items?: MenuItem<T>[] | null;
|
items?: MenuItem<T>[] | null;
|
||||||
shortcut?: string;
|
shortcut?: string;
|
||||||
spellingSuggestion?: string;
|
spellingSuggestion?: string;
|
||||||
|
checked?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MenuItem<T> = MenuCommandItem<T> | MenuSeparatorItem;
|
export type MenuItem<T> = MenuCommandItem<T> | MenuSeparatorItem;
|
||||||
@@ -146,11 +147,14 @@ class ContextMenu {
|
|||||||
} else {
|
} else {
|
||||||
const $icon = $("<span>");
|
const $icon = $("<span>");
|
||||||
|
|
||||||
if ("uiIcon" in item && item.uiIcon) {
|
if ("uiIcon" in item || "checked" in item) {
|
||||||
$icon.addClass(item.uiIcon);
|
const icon = (item.checked ? "bx bx-check" : item.uiIcon);
|
||||||
|
if (icon) {
|
||||||
|
$icon.addClass(icon);
|
||||||
} else {
|
} else {
|
||||||
$icon.append(" ");
|
$icon.append(" ");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const $link = $("<span>")
|
const $link = $("<span>")
|
||||||
.append($icon)
|
.append($icon)
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
import { GridApi } from "ag-grid-community";
|
||||||
|
import contextMenu, { MenuItem } from "../../../menus/context_menu.js";
|
||||||
|
import { TableData } from "./data.js";
|
||||||
|
|
||||||
|
export default function applyHeaderCustomization(baseEl: HTMLElement, api: GridApi<TableData>) {
|
||||||
|
const header = baseEl.querySelector(".ag-header");
|
||||||
|
if (!header) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
header.addEventListener("contextmenu", (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
|
||||||
|
contextMenu.show({
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
title: "Columns",
|
||||||
|
items: buildColumnChooser(api)
|
||||||
|
}
|
||||||
|
],
|
||||||
|
x: e.pageX,
|
||||||
|
y: e.pageY,
|
||||||
|
selectMenuItemHandler: () => {}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildColumnChooser(api: GridApi<TableData>) {
|
||||||
|
const items: MenuItem<unknown>[] = [];
|
||||||
|
|
||||||
|
for (const column of api.getColumns() ?? []) {
|
||||||
|
const colDef = column.getColDef();
|
||||||
|
if (!colDef) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const visible = column.isVisible();
|
||||||
|
items.push({
|
||||||
|
title: colDef.headerName ?? api.getDisplayNameForColumn(column, "header") ?? "",
|
||||||
|
checked: visible,
|
||||||
|
handler() {
|
||||||
|
api.setColumnsVisible([ column ], !visible);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import { buildData, type TableData } from "./data.js";
|
|||||||
import FNote from "../../../entities/fnote.js";
|
import FNote from "../../../entities/fnote.js";
|
||||||
import getPromotedAttributeInformation, { PromotedAttributeInformation } from "./parser.js";
|
import getPromotedAttributeInformation, { PromotedAttributeInformation } from "./parser.js";
|
||||||
import { setLabel } from "../../../services/attributes.js";
|
import { setLabel } from "../../../services/attributes.js";
|
||||||
|
import applyHeaderCustomization from "./header-customization.js";
|
||||||
|
|
||||||
ModuleRegistry.registerModules([ AllCommunityModule ]);
|
ModuleRegistry.registerModules([ AllCommunityModule ]);
|
||||||
|
|
||||||
@@ -11,7 +12,10 @@ export default function renderTable(el: HTMLElement, parentNote: FNote, notes: F
|
|||||||
|
|
||||||
createGrid(el, {
|
createGrid(el, {
|
||||||
...buildData(info, notes),
|
...buildData(info, notes),
|
||||||
...setupEditing(info)
|
...setupEditing(info),
|
||||||
|
onGridReady(event) {
|
||||||
|
applyHeaderCustomization(el, event.api);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user