refactor(views/table): merge renderer into table view

This commit is contained in:
Elian Doran 2025-06-27 17:39:57 +03:00
parent 88b4fc73de
commit 19eff5e6d6
No known key found for this signature in database
2 changed files with 58 additions and 65 deletions

View File

@ -1,7 +1,12 @@
import froca from "../../services/froca.js";
import renderTable from "./table_view/renderer.js";
import type { StateInfo } from "./table_view/storage.js";
import ViewMode, { ViewModeArgs } from "./view_mode";
import ViewMode, { type ViewModeArgs } from "./view_mode.js";
import { createGrid, AllCommunityModule, ModuleRegistry, GridOptions } from "ag-grid-community";
import { setLabel } from "../../services/attributes.js";
import getPromotedAttributeInformation from "./table_view/parser.js";
import { buildData, TableData } from "./table_view/data.js";
import applyHeaderCustomization from "./table_view/header-customization.js";
import server from "../../services/server.js";
const TPL = /*html*/`
<div class="table-view">
@ -44,6 +49,8 @@ export default class TableView extends ViewMode<StateInfo> {
this.$container = this.$root.find(".table-view-container");
this.args = args;
args.$parent.append(this.$root);
ModuleRegistry.registerModules([ AllCommunityModule ]);
}
get isFullHeight(): boolean {
@ -51,12 +58,57 @@ export default class TableView extends ViewMode<StateInfo> {
}
async renderList() {
const { noteIds, parentNote } = this.args;
const notes = await froca.getNotes(noteIds);
this.$container.empty();
renderTable(this.$container[0], parentNote, notes, this.viewStorage);
this.renderTable(this.$container[0]);
return this.$root;
}
private async renderTable(el: HTMLElement) {
const { noteIds, parentNote } = this.args;
const notes = await froca.getNotes(noteIds);
const info = getPromotedAttributeInformation(parentNote);
const viewStorage = await this.viewStorage.restore();
const initialState = viewStorage?.gridState;
createGrid(el, {
...buildData(info, notes),
...setupEditing(),
initialState,
async onGridReady(event) {
applyHeaderCustomization(el, event.api);
},
onStateUpdated: (event) => this.viewStorage.store({
gridState: event.api.getState()
})
});
}
}
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;
if (name === "title") {
// TODO: Deduplicate with note_title.
server.put(`notes/${noteId}/title`, { title: newValue });
}
if (name.startsWith("labels.")) {
const labelName = name.split(".", 2)[1];
setLabel(noteId, labelName, newValue);
}
}
}
}

View File

@ -1,59 +0,0 @@
import { createGrid, AllCommunityModule, ModuleRegistry, GridOptions } from "ag-grid-community";
import { buildData, type TableData } from "./data.js";
import FNote from "../../../entities/fnote.js";
import getPromotedAttributeInformation from "./parser.js";
import { setLabel } from "../../../services/attributes.js";
import applyHeaderCustomization from "./header-customization.js";
import ViewModeStorage from "../view_mode_storage.js";
import { type StateInfo } from "./storage.js";
import server from "../../../services/server.js";
ModuleRegistry.registerModules([ AllCommunityModule ]);
export default async function renderTable(el: HTMLElement, parentNote: FNote, notes: FNote[], storage: ViewModeStorage<StateInfo>) {
const info = getPromotedAttributeInformation(parentNote);
const viewStorage = await storage.restore();
const initialState = viewStorage?.gridState;
createGrid(el, {
...buildData(info, notes),
...setupEditing(),
initialState,
async onGridReady(event) {
applyHeaderCustomization(el, event.api);
},
onStateUpdated(event) {
storage.store({
gridState: event.api.getState()
});
}
});
}
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;
if (name === "title") {
// TODO: Deduplicate with note_title.
server.put(`notes/${noteId}/title`, { title: newValue });
}
if (name.startsWith("labels.")) {
const labelName = name.split(".", 2)[1];
setLabel(noteId, labelName, newValue);
}
}
}
}