Skip to content

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

:bug: Allow moving table columns to edge positions https://github.com/siyuan-note/siyuan/issues/18537

Danield5c36bcea34b
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index d281b46ebe..a970be7537 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -23,7 +23,7 @@ import {
isTableResizeControlVisible,
} from "./tableResize";
import {applyTableCellStyleHotkey, getTableCellTextStyleMenus} from "../toolbar/tableCell";
-import {getTableCellsInRectangle} from "./tableSelection";
+import {getTableCellsInRectangle, getTableDragEdge} from "./tableSelection";
type TableSelectionMode = "row" | "column" | "cell";
type TableAddControlType = "add-row" | "add-column" | "add-both";
@@ -2252,8 +2252,35 @@ export class TableControl {
if (!preview) {
return;
}
- const {position, viewportRect} = preview;
+ const {position, viewportRect, edge} = preview;
const grid = this.selectionGrid || buildTableGrid(this.selection.table);
+ if (edge) {
+ const target = edge === "start" ? 0 :
+ (this.dragState.mode === "row" ? grid.rowCount : grid.columnCount);
+ if (!this.getMoveTarget(target)) {
+ this.dragState.target = -1;
+ this.dropIndicator.classList.add("fn__none");
+ return;
+ }
+ this.dragState.target = target;
+ this.dropIndicator.classList.remove("fn__none");
+ if (this.dragState.mode === "row") {
+ this.dropIndicator.classList.add("protyle-table-control__drop--row");
+ this.dropIndicator.classList.remove("protyle-table-control__drop--column");
+ this.dropIndicator.style.left = `${viewportRect.left}px`;
+ this.dropIndicator.style.top = `${edge === "start" ? viewportRect.top : viewportRect.bottom}px`;
+ this.dropIndicator.style.width = `${viewportRect.width}px`;
+ this.dropIndicator.style.height = "2px";
+ } else {
+ this.dropIndicator.classList.add("protyle-table-control__drop--column");
+ this.dropIndicator.classList.remove("protyle-table-control__drop--row");
+ this.dropIndicator.style.left = `${edge === "start" ? viewportRect.left : viewportRect.right}px`;
+ this.dropIndicator.style.top = `${viewportRect.top}px`;
+ this.dropIndicator.style.width = "2px";
+ this.dropIndicator.style.height = `${viewportRect.height}px`;
+ }
+ return;
+ }
let cell: HTMLTableCellElement;
if (this.dragState.mode === "row") {
const rowIndex = Array.from(this.selection.table.rows).findIndex(row => {
@@ -2329,6 +2356,7 @@ export class TableControl {
const targetCenter = state.handleCenter + pointerOffset;
const center = minCenter <= maxCenter ? Math.min(Math.max(targetCenter, minCenter), maxCenter) :
(start + end) / 2;
+ const edge = getTableDragEdge(targetCenter, minCenter, maxCenter);
const offset = Math.round(center - state.handleCenter);
const offsetX = state.mode === "column" ? offset : 0;
const offsetY = state.mode === "row" ? offset : 0;
@@ -2339,6 +2367,7 @@ export class TableControl {
return {
position: state.handleCenter + offset,
viewportRect,
+ edge,
};
}
diff --git a/app/src/protyle/util/tableSelection.test.ts b/app/src/protyle/util/tableSelection.test.ts
index d562adeaf9..042979b326 100644
--- a/app/src/protyle/util/tableSelection.test.ts
+++ b/app/src/protyle/util/tableSelection.test.ts
@@ -1,6 +1,6 @@
import * as assert from "node:assert/strict";
import {describe, it} from "node:test";
-import {getTableCellsInRectangle} from "./tableSelection";
+import {getTableCellsInRectangle, getTableDragEdge} from "./tableSelection";
interface ITestCellInfo {
id: string;
@@ -53,3 +53,16 @@ describe("getTableCellsInRectangle", () => {
assert.deepEqual(getTableCellsInRectangle(cells, cells[0]), []);
});
});
+
+describe("getTableDragEdge", () => {
+ it("allows dropping at both clamped table edges", () => {
+ assert.equal(getTableDragEdge(50, 50, 250), "start");
+ assert.equal(getTableDragEdge(250, 50, 250), "end");
+ });
+
+ it("keeps positions inside the table on cell-based targeting", () => {
+ assert.equal(getTableDragEdge(51, 50, 250), undefined);
+ assert.equal(getTableDragEdge(249, 50, 250), undefined);
+ assert.equal(getTableDragEdge(100, 150, 50), undefined);
+ });
+});
diff --git a/app/src/protyle/util/tableSelection.ts b/app/src/protyle/util/tableSelection.ts
index fa98b4f41d..16659f7182 100644
--- a/app/src/protyle/util/tableSelection.ts
+++ b/app/src/protyle/util/tableSelection.ts
@@ -5,6 +5,18 @@ interface ITableSelectionCellInfo {
colspan: number;
}
+export const getTableDragEdge = (targetCenter: number, minCenter: number, maxCenter: number) => {
+ if (minCenter > maxCenter) {
+ return;
+ }
+ if (targetCenter <= minCenter) {
+ return "start" as const;
+ }
+ if (targetCenter >= maxCenter) {
+ return "end" as const;
+ }
+};
+
export const getTableCellsInRectangle = <T extends ITableSelectionCellInfo>(cellInfos: T[], start?: T, end?: T) => {
if (!start || !end) {
return [];