Skip to content

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

:bug: Keep table resize controls inside editor https://github.com/siyuan-note/siyuan/issues/18502

Daniel4d934824b28f
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index 4052ca0b7c..f00a70f5f0 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -17,6 +17,7 @@ import {
} from "./table";
import {
constrainTableResizeCount,
+ getTableResizeControlCenter,
getTableResizeCount,
isTableCellContentEmpty,
} from "./tableResize";
@@ -1031,13 +1032,17 @@ export class TableControl {
const tableRect = state.table.getBoundingClientRect();
const viewportRect = this.getTableViewportRect(state.table);
const addRowEdge = this.getTableAddRowEdge(state.table);
+ const contentRect = (this.protyle.contentElement || this.protyle.element).getBoundingClientRect();
+ const rowControlCenter = getTableResizeControlCenter(addRowEdge, contentRect.top, contentRect.bottom,
+ TABLE_ADD_CONTROL_THICKNESS);
+ const columnControlCenter = getTableResizeControlCenter(tableRect.right, contentRect.left,
+ contentRect.right, TABLE_ADD_CONTROL_THICKNESS);
if (state.mode === "row" || state.mode === "both") {
this.addRowButton.classList.remove("fn__none");
this.addRowButton.classList.add("protyle-table-control__add--active");
this.addRowButton.style.width = `${Math.max(0, viewportRect.width)}px`;
this.addRowButton.style.height = `${TABLE_ADD_CONTROL_THICKNESS}px`;
- this.setPosition(this.addRowButton, viewportRect.left + viewportRect.width / 2,
- addRowEdge + TABLE_ADD_CONTROL_THICKNESS / 2);
+ this.setPosition(this.addRowButton, viewportRect.left + viewportRect.width / 2, rowControlCenter);
state.table.classList.add("protyle-table-control__table--add-row");
}
if (state.mode === "column" || state.mode === "both") {
@@ -1045,7 +1050,7 @@ export class TableControl {
this.addColumnButton.classList.add("protyle-table-control__add--active");
this.addColumnButton.style.width = `${TABLE_ADD_CONTROL_THICKNESS}px`;
this.addColumnButton.style.height = `${Math.max(0, viewportRect.height)}px`;
- this.setPosition(this.addColumnButton, tableRect.right + TABLE_ADD_CONTROL_THICKNESS / 2,
+ this.setPosition(this.addColumnButton, columnControlCenter,
viewportRect.top + viewportRect.height / 2);
state.table.classList.add("protyle-table-control__table--add-column");
}
@@ -1054,8 +1059,7 @@ export class TableControl {
this.addBothButton.classList.add("protyle-table-control__add--active");
this.addBothButton.style.width = `${TABLE_ADD_CONTROL_THICKNESS}px`;
this.addBothButton.style.height = `${TABLE_ADD_CONTROL_THICKNESS}px`;
- this.setPosition(this.addBothButton, tableRect.right + TABLE_ADD_CONTROL_THICKNESS / 2,
- addRowEdge + TABLE_ADD_CONTROL_THICKNESS / 2);
+ this.setPosition(this.addBothButton, columnControlCenter, rowControlCenter);
}
this.joinedControlTable = state.table;
this.resizeLabel.textContent = `${state.targetRows} × ${state.targetColumns}`;
diff --git a/app/src/protyle/util/tableResize.test.ts b/app/src/protyle/util/tableResize.test.ts
index 8eb4d1a93b..bcb34d81a7 100644
--- a/app/src/protyle/util/tableResize.test.ts
+++ b/app/src/protyle/util/tableResize.test.ts
@@ -2,6 +2,7 @@ import * as assert from "node:assert/strict";
import {describe, it} from "node:test";
import {
constrainTableResizeCount,
+ getTableResizeControlCenter,
getTableResizeCount,
isTableCellContentEmpty,
} from "./tableResize";
@@ -45,3 +46,14 @@ describe("constrainTableResizeCount", () => {
assert.equal(constrainTableResizeCount(8, 5, 4, new Set([4])), 8);
});
});
+
+describe("getTableResizeControlCenter", () => {
+ it("keeps the control after a visible table edge", () => {
+ assert.equal(getTableResizeControlCenter(240, 0, 400, 16), 248);
+ });
+
+ it("keeps the whole control inside the editor viewport", () => {
+ assert.equal(getTableResizeControlCenter(395, 0, 400, 16), 392);
+ assert.equal(getTableResizeControlCenter(450, 0, 400, 16), 392);
+ });
+});
diff --git a/app/src/protyle/util/tableResize.ts b/app/src/protyle/util/tableResize.ts
index 977e1ab0c9..edb1b013a9 100644
--- a/app/src/protyle/util/tableResize.ts
+++ b/app/src/protyle/util/tableResize.ts
@@ -34,3 +34,14 @@ export const constrainTableResizeCount = (requestedCount: number, startCount: nu
}
return startCount;
};
+
+export const getTableResizeControlCenter = (edge: number, viewportStart: number, viewportEnd: number,
+ controlSize: number) => {
+ const halfSize = controlSize / 2;
+ const min = viewportStart + halfSize;
+ const max = viewportEnd - halfSize;
+ if (min > max) {
+ return (viewportStart + viewportEnd) / 2;
+ }
+ return Math.min(Math.max(edge + halfSize, min), max);
+};