An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve database column width resizing https://github.com/siyuan-note/siyuan/issues/12952
Daniel07836b92a18a
diff --git a/app/src/assets/scss/business/_av.scss b/app/src/assets/scss/business/_av.scssindex 9db7923020..2a204a6cc5 100644--- a/app/src/assets/scss/business/_av.scss+++ b/app/src/assets/scss/business/_av.scss@@ -1188,6 +1188,35 @@}}+ &__width-guide {+ background-color: var(--b3-theme-primary);+ opacity: .45;+ pointer-events: none;+ position: fixed;+ width: 1px;+ z-index: 1000;++ &--snapped {+ opacity: 1;+ width: 2px;+ }+ }++ &__width-tip {+ background-color: var(--b3-tooltips-background);+ border-radius: var(--b3-border-radius);+ color: var(--b3-tooltips-color);+ font-size: 12px;+ line-height: 20px;+ padding: 0 6px;+ pointer-events: none;+ position: fixed;+ transform: translate(-50%, calc(-100% - 6px));+ user-select: none;+ white-space: nowrap;+ z-index: 1001;+ }+&__panel {position: relative;diff --git a/app/src/protyle/render/av/col.ts b/app/src/protyle/render/av/col.tsindex 4d46ad7f7c..cac937f72b 100644--- a/app/src/protyle/render/av/col.ts+++ b/app/src/protyle/render/av/col.ts@@ -771,6 +771,47 @@ export const setFreezeColumn = (protyle: IProtyle, blockElement: Element, freezetransaction(protyle, [operation], [undoOperation]);};+export const autoFitAVColumns = (protyle: IProtyle, blockElement: HTMLElement, columnIDs?: string[]) => {+ const data = getAVData(blockElement);+ if (!data || data.viewType !== "table") {+ return;+ }+ const widths = getAVTableFitWidths(+ data.view as IAVTable,+ getCellValueText,+ getAVColumnTextMeasurer(blockElement),+ columnIDs,+ );+ const oldWidths: Record<string, string> = {};+ const newWidths: Record<string, string> = {};+ Object.entries(widths).forEach(([columnID, width]) => {+ const headerElement = blockElement.querySelector<HTMLElement>(+ `.av__row--header .av__cell[data-col-id="${columnID}"]`,+ );+ if (!headerElement || headerElement.style.width === width) {+ return;+ }+ oldWidths[columnID] = headerElement.style.width || "200px";+ newWidths[columnID] = width;+ });+ if (Object.keys(newWidths).length === 0) {+ return;+ }+ const operation = {+ action: "setAttrViewColsWidth" as TOperation,+ avID: blockElement.dataset.avId,+ blockID: blockElement.dataset.nodeId,+ viewID: blockElement.getAttribute(Constants.CUSTOM_SY_AV_VIEW),+ };+ transaction(protyle, [{+ ...operation,+ data: newWidths,+ }], [{+ ...operation,+ data: oldWidths,+ }]);+};+export const showColMenu = (protyle: IProtyle, blockElement: Element, cellElement: HTMLElement) => {const type = cellElement.getAttribute("data-dtype") as TAVCol;const colId = cellElement.getAttribute("data-col-id");@@ -1125,43 +1166,7 @@ export const showColMenu = (protyle: IProtyle, blockElement: Element, cellElemenicon: "iconWidth",label: window.siyuan.languages.autoFitAllColWidths,click() {- const data = getAVData(blockElement as HTMLElement);- if (!data || data.viewType !== "table") {- return;- }- const widths = getAVTableFitWidths(- data.view as IAVTable,- getCellValueText,- getAVColumnTextMeasurer(blockElement as HTMLElement),- );- const oldWidths: Record<string, string> = {};- const newWidths: Record<string, string> = {};- Object.entries(widths).forEach(([columnID, width]) => {- const headerElement = blockElement.querySelector<HTMLElement>(- `.av__row--header .av__cell[data-col-id="${columnID}"]`,- );- if (!headerElement || headerElement.style.width === width) {- return;- }- oldWidths[columnID] = headerElement.style.width || "200px";- newWidths[columnID] = width;- });- if (Object.keys(newWidths).length === 0) {- return;- }- const operation = {- action: "setAttrViewColsWidth" as TOperation,- avID,- blockID,- viewID,- };- transaction(protyle, [{- ...operation,- data: newWidths,- }], [{- ...operation,- data: oldWidths,- }]);+ autoFitAVColumns(protyle, blockElement as HTMLElement);}});menu.addItem({diff --git a/app/src/protyle/render/av/columnWidth.test.ts b/app/src/protyle/render/av/columnWidth.test.tsindex 3084ddbe22..5f19c77a57 100644--- a/app/src/protyle/render/av/columnWidth.test.ts+++ b/app/src/protyle/render/av/columnWidth.test.ts@@ -1,6 +1,6 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {getAVColumnFitWidth, getAVTableFitWidths} from "./columnWidth";+import {getAVColumnFitWidth, getAVColumnResizeWidth, getAVTableFitWidths} from "./columnWidth";const measureText = (value: string) => value.length * 10;@@ -67,5 +67,26 @@ describe("getAVTableFitWidths", () => {priority: "72px",number: "70px",});++ assert.deepEqual(getAVTableFitWidths(+ view,+ value => value.text?.content || "",+ measureText,+ ["priority"],+ ), {+ priority: "72px",+ });+ });+});++describe("getAVColumnResizeWidth", () => {+ it("snaps to the previous visible column within the threshold", () => {+ assert.deepEqual(getAVColumnResizeWidth(194, 200), {width: 200, snapped: true});+ assert.deepEqual(getAVColumnResizeWidth(206, 200), {width: 200, snapped: true});+ assert.deepEqual(getAVColumnResizeWidth(207, 200), {width: 207, snapped: false});+ });++ it("keeps manual resizing above the minimum width", () => {+ assert.deepEqual(getAVColumnResizeWidth(10), {width: 25, snapped: false});});});diff --git a/app/src/protyle/render/av/columnWidth.ts b/app/src/protyle/render/av/columnWidth.tsindex efda808b52..94e67b3417 100644--- a/app/src/protyle/render/av/columnWidth.ts+++ b/app/src/protyle/render/av/columnWidth.ts@@ -33,12 +33,26 @@ export const getAVColumnFitWidth = (name: string, type: TAVCol, values: string[]return `${Math.ceil(Math.min(480, Math.max(64, headerWidth, contentWidth)))}px`;};+export const getAVColumnResizeWidth = (width: number, previousWidth?: number, snapThreshold = 6) => {+ const limitedWidth = Math.max(Math.round(width), 25);+ const normalizedPreviousWidth = typeof previousWidth === "number" ? Math.round(previousWidth) : undefined;+ const snapped = typeof normalizedPreviousWidth === "number" &&+ Math.abs(limitedWidth - normalizedPreviousWidth) <= snapThreshold;+ return {+ width: snapped ? normalizedPreviousWidth : limitedWidth,+ snapped,+ };+};+export const getAVTableFitWidths = (view: IAVTable,getValueText: (value: IAVCellValue, column: IAVColumn, rowIndex: number) => string,measureText = getEstimatedTextWidth,+ columnIDs?: string[],) => {- const visibleColumns = view.columns.filter(column => !column.hidden);+ const targetColumnIDs = columnIDs ? new Set(columnIDs) : undefined;+ const visibleColumns = view.columns.filter(column =>+ !column.hidden && (!targetColumnIDs || targetColumnIDs.has(column.id)));const values = new Map(visibleColumns.map(column => [column.id, [] as string[]]));const collect = (table: IAVTable) => {if (table.groups?.length > 0) {diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex b700a63774..6cb437492f 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -108,7 +108,7 @@ import {commonClick} from "./commonClick";import {avClick, avContextmenu, updateAVName} from "../render/av/action";import {selectRow, stickyRow, updateHeader} from "../render/av/row";import {getAVSelectedItemIDs, getAVSelectedTableCells, updateAVRowSelect} from "../render/av/virtualScroll";-import {setFreezeColumn, showColMenu} from "../render/av/col";+import {autoFitAVColumns, setFreezeColumn, showColMenu} from "../render/av/col";import {openViewMenu} from "../render/av/view";import {getAVCurrentViewID} from "../render/av/viewVisibility";import {checkFold} from "../../util/noRelyPCFunction";@@ -158,6 +158,7 @@ import {setAVDragItemAnchor,setAVItemAnchor,} from "../render/av/rangeSelect";+import {getAVColumnResizeWidth} from "../render/av/columnWidth";interface IShiftClickBlockPoint {blockElement: HTMLElement;@@ -1377,20 +1378,64 @@ export class WYSIWYG {}const avId = nodeElement.getAttribute("data-av-id");const blockID = nodeElement.dataset.nodeId;- const dragElement = target.parentElement;+ const dragElement = target.parentElement as HTMLElement;const oldWidth = dragElement.clientWidth;const dragColId = dragElement.getAttribute("data-col-id");- let newWidth: number;+ const bodyElement = hasClosestByClassName(target, "av__body") as HTMLElement;+ const headerElement = hasClosestByClassName(target, "av__row--header") as HTMLElement;const scrollElement = nodeElement.querySelector(".av__scroll");+ if (!dragColId || !bodyElement || !headerElement || !scrollElement) {+ return;+ }+ const headerCells = Array.from(headerElement.querySelectorAll<HTMLElement>(".av__cell--header"));+ const dragIndex = headerCells.indexOf(dragElement);+ const previousWidth = dragIndex > 0 ? headerCells[dragIndex - 1].clientWidth : undefined;+ let newWidth: number;+ let resizeGuide: HTMLElement;+ let resizeTip: HTMLElement;+ const clearResizePreview = () => {+ resizeGuide?.remove();+ resizeTip?.remove();+ };+ const updateResizePreview = (snapped: boolean) => {+ if (!resizeGuide) {+ resizeGuide = document.createElement("div");+ resizeGuide.className = "av__width-guide";+ document.body.appendChild(resizeGuide);+ resizeTip = document.createElement("div");+ resizeTip.className = "av__width-tip";+ document.body.appendChild(resizeTip);+ }+ const dragRect = dragElement.getBoundingClientRect();+ const headerRect = headerElement.getBoundingClientRect();+ const bodyRect = bodyElement.getBoundingClientRect();+ const guideBottom = Math.min(bodyRect.bottom, contentRect.bottom, window.innerHeight);+ resizeGuide.classList.toggle("av__width-guide--snapped", snapped);+ resizeGuide.style.left = `${Math.round(dragRect.right)}px`;+ resizeGuide.style.top = `${Math.round(headerRect.top)}px`;+ resizeGuide.style.height = `${Math.max(0, Math.round(guideBottom - headerRect.top))}px`;+ resizeTip.style.left = `${Math.round(dragRect.right)}px`;+ resizeTip.style.top = `${Math.round(headerRect.top)}px`;+ resizeTip.textContent = `${newWidth}px`;+ };documentSelf.onmousemove = (moveEvent: MouseEvent) => {- newWidth = Math.max(oldWidth + (moveEvent.clientX - event.clientX), 25);+ const resizeWidth = getAVColumnResizeWidth(+ oldWidth + (moveEvent.clientX - event.clientX),+ previousWidth,+ );+ newWidth = resizeWidth.width;scrollElement.querySelectorAll(".av__row, .av__row--footer").forEach(item => {- (item.querySelector(`[data-col-id="${dragColId}"]`) as HTMLElement).style.width = newWidth + "px";+ const columnElement = item.querySelector<HTMLElement>(`[data-col-id="${dragColId}"]`);+ if (columnElement) {+ columnElement.style.width = newWidth + "px";+ }});+ updateResizePreview(resizeWidth.snapped);stickyRow(nodeElement, protyle.contentElement, "bottom");};documentSelf.onmouseup = () => {+ clearResizePreview();documentSelf.onmousemove = null;documentSelf.onmouseup = null;documentSelf.ondragstart = null;@@ -1416,6 +1461,9 @@ export class WYSIWYG {viewID}]);};+ documentSelf.ondragstart = () => false;+ documentSelf.onselectstart = () => false;+ documentSelf.onselect = () => false;this.preventClick = true;event.preventDefault();return;@@ -3718,6 +3766,16 @@ export class WYSIWYG {return;}const target = event.target as HTMLElement;+ if (!protyle.disabled && target.classList.contains("av__widthdrag")) {+ const blockElement = hasClosestBlock(target) as HTMLElement;+ const columnID = target.parentElement?.getAttribute("data-col-id");+ if (blockElement && columnID) {+ autoFitAVColumns(protyle, blockElement, [columnID]);+ }+ event.stopPropagation();+ event.preventDefault();+ return;+ }// 双击超级块拖拽手柄,均分所有列宽if (target.classList.contains("sb__resize")) {const doOperations: IOperation[] = [];