Files
Trilium/apps/client/src/widgets/view_widgets/table_view/renderer.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-06-25 16:18:34 +03:00
import { createGrid, AllCommunityModule, ModuleRegistry, GridOptions } from "ag-grid-community";
import { buildData, type TableData } from "./data.js";
2025-06-25 11:03:43 +03:00
import FNote from "../../../entities/fnote.js";
2025-06-25 16:18:34 +03:00
import getPromotedAttributeInformation from "./parser.js";
import { setLabel } from "../../../services/attributes.js";
import applyHeaderCustomization from "./header-customization.js";
2025-06-25 16:18:34 +03:00
import ViewModeStorage from "../view_mode_storage.js";
import { type StateInfo } from "./storage.js";
2025-06-25 10:49:33 +03:00
ModuleRegistry.registerModules([ AllCommunityModule ]);
2025-06-25 16:18:34 +03:00
export default async function renderTable(el: HTMLElement, parentNote: FNote, notes: FNote[], storage: ViewModeStorage<StateInfo>) {
const info = getPromotedAttributeInformation(parentNote);
2025-06-25 16:18:34 +03:00
const viewStorage = await storage.restore();
const initialState = viewStorage?.gridState;
2025-06-25 10:49:33 +03:00
createGrid(el, {
...buildData(info, notes),
2025-06-25 16:18:34 +03:00
...setupEditing(),
initialState,
async onGridReady(event) {
applyHeaderCustomization(el, event.api);
},
2025-06-25 16:18:34 +03:00
onStateUpdated(event) {
storage.store({
gridState: event.api.getState()
});
}
2025-06-25 10:49:33 +03:00
});
}
2025-06-25 16:18:34 +03:00
function setupEditing(): GridOptions<TableData> {
return {
onCellValueChanged(event) {
if (event.type !== "cellValueChanged") {
return;
}
const noteId = event.data.noteId;
const name = event.colDef.field;
if (!name) {
return;
}
const { newValue } = event;
setLabel(noteId, name, newValue);
}
}
}
2025-06-25 16:18:34 +03:00