Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(collections/table): keep flags read-only after an editable table
Elian Doran7c50acab9d6a
diff --git a/apps/client/src/widgets/collections/table/TablePrintView.tsx b/apps/client/src/widgets/collections/table/TablePrintView.tsxindex 236d741592..e4ee9ec2d1 100644--- a/apps/client/src/widgets/collections/table/TablePrintView.tsx+++ b/apps/client/src/widgets/collections/table/TablePrintView.tsx@@ -10,7 +10,7 @@ import Tabulator from "./tabulator";export default function TablePrintView({ note, noteIds, viewConfig, onReady }: ViewModeProps<TableConfig>) {const tabulatorRef = useRef<VanillaTabulator>(null);- const { columnDefs, rowData, hasChildren } = useData(note, noteIds, viewConfig, undefined, () => {});+ const { columnDefs, rowData, hasChildren } = useData(note, noteIds, viewConfig, undefined, () => {}, true);const [ html, setHtml ] = useState<string>();useEffect(() => {diff --git a/apps/client/src/widgets/collections/table/columns.spec.ts b/apps/client/src/widgets/collections/table/columns.spec.tsindex c2ce23ada4..04a149f6fb 100644--- a/apps/client/src/widgets/collections/table/columns.spec.ts+++ b/apps/client/src/widgets/collections/table/columns.spec.ts@@ -269,10 +269,14 @@ describe("buildColumnDefinitions — typed columns", () => {const checkboxOf = (value: unknown, { editable = true } = {}) => {const setValue = vi.fn();+ const definition = { ...column, title: "Done", ...(!editable && { editable: false }) };const cell = {getValue: () => value,setValue,- getTable: () => ({ modExists: (name: string) => editable && name === "edit" })+ getColumn: () => ({ getDefinition: () => definition }),+ // Tabulator registers modules on its shared class, so a read-only table built after+ // an editable one has the edit module as well.+ getTable: () => ({ modExists: () => true })} as unknown as CellComponent;const element = formatter(cell, {}, () => {}) as HTMLElement;// A checkbox fires `change` only while connected, as it is inside a rendered cell.@@ -302,7 +306,7 @@ describe("buildColumnDefinitions — typed columns", () => {act(() => ticked.checkbox.click());expect(ticked.setValue).toHaveBeenCalledExactlyOnceWith(false);- // A read-only table, built without the edit module, keeps the box as it is.+ // A read-only table marks its columns `editable: false`, which keeps the box as it is.const readOnly = checkboxOf("true", { editable: false });expect(readOnly.checkbox.tabIndex).toBe(-1);act(() => readOnly.checkbox.click());diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsxindex 4a9b3abfe1..83fe23c9d9 100644--- a/apps/client/src/widgets/collections/table/columns.tsx+++ b/apps/client/src/widgets/collections/table/columns.tsx@@ -636,19 +636,14 @@ function ColorFormatter({ cell }: FormatterOpts) {return <LabelValueChips values={[ color ]} labelType="color" />;}-// `modExists` is provided by Tabulator but missing from the current type definitions.-type TabulatorWithModExists = ReturnType<CellComponent["getTable"]> & {- modExists(name: string): boolean;-};-/** A flag cell as a checkbox, unticked for a note without the label. */function FlagFormatter({ cell }: FormatterOpts) {const value = cell.getValue();// A stored label arrives as the text "true"/"false", but one just set from the box is a real// boolean. A note without the label reads as false, as the promoted checkbox shows it unticked.const isSet = value === true || value === "true";- // A read-only table is built without the edit module.- const isEditable = (cell.getTable() as TabulatorWithModExists).modExists("edit");+ const definition = cell.getColumn().getDefinition();+ const isEditable = definition.editable !== false;// `cell.setValue()` fires `cellEdited`, which saves the label as an edit from an editor would.return (diff --git a/apps/client/src/widgets/collections/table/data.spec.tsx b/apps/client/src/widgets/collections/table/data.spec.tsxindex 7b7325306b..c3396c8b32 100644--- a/apps/client/src/widgets/collections/table/data.spec.tsx+++ b/apps/client/src/widgets/collections/table/data.spec.tsx@@ -129,6 +129,7 @@ describe("useData", () => {await mount(note, noteIds, true);expect(captured?.columnDefs?.map((column) => column.field)).toContain("labels.status");expect(captured?.columnDefs?.some((column) => column.editor || column.editorParams)).toBe(false);+ expect(captured?.columnDefs?.every((column) => column.editable === false)).toBe(true);expect(captured?.movableRows).toBe(false);});diff --git a/apps/client/src/widgets/collections/table/data.tsx b/apps/client/src/widgets/collections/table/data.tsxindex 0c610b58df..77bb6e1e5b 100644--- a/apps/client/src/widgets/collections/table/data.tsx+++ b/apps/client/src/widgets/collections/table/data.tsx@@ -124,7 +124,7 @@ export default function useData(note: FNote, noteIds: string[], viewConfig: Tabl}function withoutEditor({ editor, editorParams, ...column }: ColumnDefinition): ColumnDefinition {- return column;+ return { ...column, editable: false };}/**