mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 02:45:54 +01:00
fix(views/table): position not restored after new columns (closes #6285)
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { restoreExistingData } from "./columns";
|
||||||
|
import type { ColumnDefinition } from "tabulator-tables";
|
||||||
|
|
||||||
|
describe("restoreExistingData", () => {
|
||||||
|
it("should restore existing column data", () => {
|
||||||
|
const newDefs: ColumnDefinition[] = [
|
||||||
|
{ field: "title", title: "Title", editor: "input" },
|
||||||
|
{ field: "noteId", title: "Note ID", visible: false }
|
||||||
|
];
|
||||||
|
const oldDefs: ColumnDefinition[] = [
|
||||||
|
{ field: "title", title: "Title", width: 300, visible: true },
|
||||||
|
{ field: "noteId", title: "Note ID", width: 200, visible: true }
|
||||||
|
];
|
||||||
|
const restored = restoreExistingData(newDefs, oldDefs);
|
||||||
|
expect(restored[0].width).toBe(300);
|
||||||
|
expect(restored[1].width).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("restores order of columns", () => {
|
||||||
|
const newDefs: ColumnDefinition[] = [
|
||||||
|
{ field: "title", title: "Title", editor: "input" },
|
||||||
|
{ field: "noteId", title: "Note ID", visible: false }
|
||||||
|
];
|
||||||
|
const oldDefs: ColumnDefinition[] = [
|
||||||
|
{ field: "noteId", title: "Note ID", width: 200, visible: true },
|
||||||
|
{ field: "title", title: "Title", width: 300, visible: true }
|
||||||
|
];
|
||||||
|
const restored = restoreExistingData(newDefs, oldDefs);
|
||||||
|
expect(restored[0].field).toBe("noteId");
|
||||||
|
expect(restored[1].field).toBe("title");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("inserts new columns at given position", () => {
|
||||||
|
const newDefs: ColumnDefinition[] = [
|
||||||
|
{ field: "title", title: "Title", editor: "input" },
|
||||||
|
{ field: "noteId", title: "Note ID", visible: false },
|
||||||
|
{ field: "newColumn", title: "New Column", editor: "input" }
|
||||||
|
];
|
||||||
|
const oldDefs: ColumnDefinition[] = [
|
||||||
|
{ field: "title", title: "Title", width: 300, visible: true },
|
||||||
|
{ field: "noteId", title: "Note ID", width: 200, visible: true }
|
||||||
|
];
|
||||||
|
const restored = restoreExistingData(newDefs, oldDefs, 0);
|
||||||
|
expect(restored[0].field).toBe("newColumn");
|
||||||
|
expect(restored[1].field).toBe("title");
|
||||||
|
expect(restored[2].field).toBe("noteId");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -92,14 +92,15 @@ export function buildColumnDefinitions(info: AttributeDefinitionInformation[], m
|
|||||||
return columnDefs;
|
return columnDefs;
|
||||||
}
|
}
|
||||||
|
|
||||||
function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinition[], position?: number) {
|
export function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinition[], position?: number) {
|
||||||
|
const existingColumns: ColumnDefinition[] = []
|
||||||
const byField = new Map<string, ColumnDefinition>;
|
const byField = new Map<string, ColumnDefinition>;
|
||||||
for (const def of oldDefs) {
|
for (const def of oldDefs) {
|
||||||
byField.set(def.field ?? "", def);
|
byField.set(def.field ?? "", def);
|
||||||
|
existingColumns.push(def);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newColumns: ColumnDefinition[] = [];
|
const newColumns: ColumnDefinition[] = [];
|
||||||
const existingColumns: ColumnDefinition[] = []
|
|
||||||
for (const newDef of newDefs) {
|
for (const newDef of newDefs) {
|
||||||
const oldDef = byField.get(newDef.field ?? "");
|
const oldDef = byField.get(newDef.field ?? "");
|
||||||
if (!oldDef) {
|
if (!oldDef) {
|
||||||
@@ -107,7 +108,6 @@ function restoreExistingData(newDefs: ColumnDefinition[], oldDefs: ColumnDefinit
|
|||||||
} else {
|
} else {
|
||||||
newDef.width = oldDef.width;
|
newDef.width = oldDef.width;
|
||||||
newDef.visible = oldDef.visible;
|
newDef.visible = oldDef.visible;
|
||||||
existingColumns.push(newDef);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user