Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Synchronize column widths in folded database groups https://github.com/siyuan-note/siyuan/issues/20157

Daniel1009346f9ddc
diff --git a/app/src/protyle/render/av/columnWidth.ts b/app/src/protyle/render/av/columnWidth.ts
index b04075ce53..b7744b000a 100644
--- a/app/src/protyle/render/av/columnWidth.ts
+++ b/app/src/protyle/render/av/columnWidth.ts
@@ -1,5 +1,14 @@
import {hasAVRenderTemplateResult} from "./cellValue";
+export const updateAVTableColumnWidths = (view: IAVTable, widths: Record<string, string>) => {
+ view.columns.forEach(column => {
+ if (Object.prototype.hasOwnProperty.call(widths, column.id)) {
+ column.width = widths[column.id];
+ }
+ });
+ (view.groups as IAVTable[] || []).forEach(group => updateAVTableColumnWidths(group, widths));
+};
+
const getEstimatedTextWidth = (value: string) => {
return Array.from(value.trim().replace(/[\r\n]+/g, " ")).reduce((width, character) => {
if (/[\u2E80-\u9FFF\uAC00-\uD7AF]/u.test(character)) {
diff --git a/app/src/protyle/render/av/columnWidthRefresh.test.ts b/app/src/protyle/render/av/columnWidthRefresh.test.ts
new file mode 100644
index 0000000000..fbf566dd82
--- /dev/null
+++ b/app/src/protyle/render/av/columnWidthRefresh.test.ts
@@ -0,0 +1,75 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import {updateAVTableColumnWidths} from "./columnWidth";
+
+const code = transpileModule(readFileSync("src/protyle/render/av/render.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+}).outputText;
+
+const setup = (rendered: boolean) => {
+ const createTable = (id: string, folded = false): IAVTable => ({
+ id, columns: [{id: "title", width: "200px"}, {id: "status", width: "120px"}] as IAVColumn[],
+ rows: [], rowCount: 0, groupFolded: folded,
+ });
+ const view = createTable("view");
+ view.groups = [createTable("visible"), createTable("folded", true)];
+ const otherView = createTable("other");
+ const cells = [{style: {width: "64px"}}, {style: {width: "200px"}}];
+ const blocks = [view, otherView].map((table, index) => ({
+ closest: (): null => null,
+ getAttribute: () => table.id,
+ querySelector: () => rendered ? cells[index] : null,
+ querySelectorAll: () => rendered ? [{querySelector: (selector: string) =>
+ selector.includes("title") ? cells[index] : null}] : [],
+ }));
+ let frozenUpdates = 0;
+ const exports: {refreshAV?: (protyle: unknown, operation: unknown) => void} = {};
+ const dependencies: Record<string, unknown> = {
+ "./virtualScroll": {getAVData: (block: unknown) => ({
+ viewType: "table", view: block === blocks[0] ? view : otherView,
+ })},
+ "./viewType": {isTableLikeView: (type: string) => ["table", "list"].includes(type)},
+ "./columnWidth": {updateAVTableColumnWidths},
+ "./frozenColumns": {updateFrozenColumns: () => frozenUpdates++},
+ "../../../constants": {Constants: {}},
+ };
+ runInNewContext(code, {exports, require: (name: string) => dependencies[name] || {}});
+ const protyle = {wysiwyg: {element: {querySelectorAll: () => blocks}}};
+ return {view, otherView, cells, frozenUpdates: () => frozenUpdates,
+ refresh: (operation: object) => exports.refreshAV(protyle, {avID: "database", viewID: "view", ...operation})};
+};
+
+for (const rendered of [true, false]) {
+ test(`single-column resizing updates folded group and virtual row data (${rendered ? "rendered" : "unrendered"})`, () => {
+ const scenario = setup(rendered);
+ scenario.refresh({action: "setAttrViewColWidth", id: "title", data: "64px"});
+ for (const table of [scenario.view, ...scenario.view.groups as IAVTable[]]) {
+ assert.equal(table.columns[0].width, "64px");
+ assert.equal(table.columns[1].width, "120px");
+ }
+ assert.equal(scenario.view.groups[1].groupFolded, true);
+ assert.equal(scenario.otherView.columns[0].width, "200px");
+ assert.equal(scenario.frozenUpdates(), 1);
+ if (rendered) {
+ assert.equal(scenario.cells[0].style.width, "64px");
+ assert.equal(scenario.cells[1].style.width, "200px");
+ }
+ scenario.refresh({action: "setAttrViewColWidth", id: "title", data: "200px"});
+ assert.equal((scenario.view.groups[1] as IAVTable).columns[0].width, "200px");
+ });
+}
+
+test("batch column resizing, undo and redo preserve widths in folded group data", () => {
+ const scenario = setup(false);
+ for (const widths of [{title: "80px", status: "80px"}, {title: "200px", status: "120px"},
+ {title: "80px", status: "80px"}]) {
+ scenario.refresh({action: "setAttrViewColsWidth", data: widths});
+ for (const table of [scenario.view, ...scenario.view.groups as IAVTable[]]) {
+ assert.deepEqual(table.columns.map(column => column.width), [widths.title, widths.status]);
+ }
+ assert.deepEqual(scenario.otherView.columns.map(column => column.width), ["200px", "120px"]);
+ }
+});
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index d923c302cb..fad4090355 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -67,6 +67,7 @@ import {getContextFilterKeyID} from "./contextFilterState";
import {isAVCellPanelForBlock} from "./panelTarget";
import {replaceAVContainer} from "./container";
import {updateFrozenColumns} from "./frozenColumns";
+import {updateAVTableColumnWidths} from "./columnWidth";
interface IIds {
groupId: string,
@@ -930,26 +931,17 @@ export const refreshAV = (protyle: IProtyle, operation: IOperation) => {
});
return;
}
- if (operation.action === "setAttrViewColWidth") {
+ if (operation.action === "setAttrViewColWidth" || operation.action === "setAttrViewColsWidth") {
+ const widths = operation.action === "setAttrViewColWidth" ?
+ {[operation.id]: operation.data} : operation.data as Record<string, string>;
getAVElements(protyle, operation.avID, operation.viewID).forEach((item) => {
- const cellElement = item.querySelector(`.av__cell[data-col-id="${operation.id}"]`) as HTMLElement;
- if (!cellElement || cellElement.style.width === operation.data) {
- return;
+ const data = getAVData(item);
+ if (data && isTableLikeView(data.viewType)) {
+ // 折叠分组和虚拟滚动从缓存创建单元格,列宽需同步更新到全部分组。
+ updateAVTableColumnWidths(data.view as IAVTable, widths);
}
item.querySelectorAll(".av__row, .av__row--footer").forEach(rowItem => {
- const columnElement = rowItem.querySelector(`[data-col-id="${operation.id}"]`) as HTMLElement;
- if (columnElement) {
- columnElement.style.width = operation.data;
- }
- });
- updateFrozenColumns(item);
- });
- return;
- }
- if (operation.action === "setAttrViewColsWidth") {
- getAVElements(protyle, operation.avID, operation.viewID).forEach((item) => {
- item.querySelectorAll(".av__row, .av__row--footer").forEach(rowItem => {
- Object.entries(operation.data as Record<string, string>).forEach(([columnID, width]) => {
+ Object.entries(widths).forEach(([columnID, width]) => {
const columnElement = rowItem.querySelector(`[data-col-id="${columnID}"]`) as HTMLElement;
if (columnElement) {
columnElement.style.width = width;