An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Stabilize table cell selection across display scales https://github.com/siyuan-note/siyuan/issues/18489
Daniel0b7117388d30
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.tsindex 0e4daf0e0b..c8be48d763 100644--- a/app/src/protyle/util/tableControl.ts+++ b/app/src/protyle/util/tableControl.ts@@ -23,6 +23,7 @@ import {isTableResizeControlVisible,} from "./tableResize";import {applyTableCellStyleHotkey, getTableCellTextStyleMenus} from "../toolbar/tableCell";+import {getTableCellsInRectangle} from "./tableSelection";type TableSelectionMode = "row" | "column" | "cell";type TableAddControlType = "add-row" | "add-column" | "add-both";@@ -660,7 +661,7 @@ export class TableControl {if (mode === "cell") {if (extend && this.selection.anchor instanceof HTMLTableCellElement) {const anchorInfo = grid.cellInfos.find(item => item.cell === this.selection.anchor);- const selected = this.getCellsInRectangle(grid.cellInfos, anchorInfo, info);+ const selected = getTableCellsInRectangle(grid.cellInfos, anchorInfo, info).map(item => item.cell);if (!toggle) {this.selection.cells.clear();}@@ -750,37 +751,6 @@ export class TableControl {return indexes;}- private getCellsInRectangle(cellInfos: ITableCellInfo[], start: ITableCellInfo, end: ITableCellInfo) {- if (!start || !end) {- return [];- }- let rowStart = Math.min(start.row, end.row);- let rowEnd = Math.max(start.row + start.rowspan - 1, end.row + end.rowspan - 1);- let colStart = Math.min(start.col, end.col);- let colEnd = Math.max(start.col + start.colspan - 1, end.col + end.colspan - 1);- let changed = true;- while (changed) {- changed = false;- cellInfos.forEach(info => {- if (info.row <= rowEnd && info.row + info.rowspan - 1 >= rowStart &&- info.col <= colEnd && info.col + info.colspan - 1 >= colStart) {- const nextRowStart = Math.min(rowStart, info.row);- const nextRowEnd = Math.max(rowEnd, info.row + info.rowspan - 1);- const nextColStart = Math.min(colStart, info.col);- const nextColEnd = Math.max(colEnd, info.col + info.colspan - 1);- changed = nextRowStart !== rowStart || nextRowEnd !== rowEnd ||- nextColStart !== colStart || nextColEnd !== colEnd;- rowStart = nextRowStart;- rowEnd = nextRowEnd;- colStart = nextColStart;- colEnd = nextColEnd;- }- });- }- return cellInfos.filter(info => info.row <= rowEnd && info.row + info.rowspan - 1 >= rowStart &&- info.col <= colEnd && info.col + info.colspan - 1 >= colStart).map(info => info.cell);- }-private isCellInSelection(cell: HTMLTableCellElement) {if (!this.selection) {return false;diff --git a/app/src/protyle/util/tableSelection.test.ts b/app/src/protyle/util/tableSelection.test.tsnew file mode 100644index 0000000000..d562adeaf9--- /dev/null+++ b/app/src/protyle/util/tableSelection.test.ts@@ -0,0 +1,55 @@+import * as assert from "node:assert/strict";+import {describe, it} from "node:test";+import {getTableCellsInRectangle} from "./tableSelection";++interface ITestCellInfo {+ id: string;+ row: number;+ col: number;+ rowspan: number;+ colspan: number;+}++const createCell = (id: string, row: number, col: number, rowspan = 1, colspan = 1): ITestCellInfo => ({+ id,+ row,+ col,+ rowspan,+ colspan,+});++describe("getTableCellsInRectangle", () => {+ it("keeps horizontal and vertical ranges within their logical rows and columns", () => {+ const cells = [+ createCell("a", 0, 0), createCell("b", 0, 1), createCell("c", 0, 2),+ createCell("d", 1, 0), createCell("e", 1, 1), createCell("f", 1, 2),+ createCell("g", 2, 0), createCell("h", 2, 1), createCell("i", 2, 2),+ ];++ assert.deepEqual(getTableCellsInRectangle(cells, cells[0], cells[2]).map(item => item.id), ["a", "b", "c"]);+ assert.deepEqual(getTableCellsInRectangle(cells, cells[2], cells[0]).map(item => item.id), ["a", "b", "c"]);+ assert.deepEqual(getTableCellsInRectangle(cells, cells[0], cells[3]).map(item => item.id), ["a", "d"]);+ assert.deepEqual(getTableCellsInRectangle(cells, cells[3], cells[0]).map(item => item.id), ["a", "d"]);+ });++ it("expands repeatedly across intersecting merged cells", () => {+ const cells = [+ createCell("a", 0, 0, 2),+ createCell("b", 0, 1),+ createCell("c", 0, 2),+ createCell("d", 1, 1, 1, 2),+ createCell("e", 2, 0),+ createCell("f", 2, 1),+ createCell("g", 2, 2),+ ];++ assert.deepEqual(getTableCellsInRectangle(cells, cells[0], cells[1]).map(item => item.id),+ ["a", "b", "c", "d"]);+ });++ it("returns no cells without both endpoints", () => {+ const cells = [createCell("a", 0, 0)];++ assert.deepEqual(getTableCellsInRectangle(cells, cells[0]), []);+ });+});diff --git a/app/src/protyle/util/tableSelection.ts b/app/src/protyle/util/tableSelection.tsnew file mode 100644index 0000000000..fa98b4f41d--- /dev/null+++ b/app/src/protyle/util/tableSelection.ts@@ -0,0 +1,37 @@+interface ITableSelectionCellInfo {+ row: number;+ col: number;+ rowspan: number;+ colspan: number;+}++export const getTableCellsInRectangle = <T extends ITableSelectionCellInfo>(cellInfos: T[], start?: T, end?: T) => {+ if (!start || !end) {+ return [];+ }+ let rowStart = Math.min(start.row, end.row);+ let rowEnd = Math.max(start.row + start.rowspan - 1, end.row + end.rowspan - 1);+ let colStart = Math.min(start.col, end.col);+ let colEnd = Math.max(start.col + start.colspan - 1, end.col + end.colspan - 1);+ let changed = true;+ while (changed) {+ changed = false;+ cellInfos.forEach(info => {+ if (info.row <= rowEnd && info.row + info.rowspan - 1 >= rowStart &&+ info.col <= colEnd && info.col + info.colspan - 1 >= colStart) {+ const nextRowStart = Math.min(rowStart, info.row);+ const nextRowEnd = Math.max(rowEnd, info.row + info.rowspan - 1);+ const nextColStart = Math.min(colStart, info.col);+ const nextColEnd = Math.max(colEnd, info.col + info.colspan - 1);+ changed = nextRowStart !== rowStart || nextRowEnd !== rowEnd ||+ nextColStart !== colStart || nextColEnd !== colEnd;+ rowStart = nextRowStart;+ rowEnd = nextRowEnd;+ colStart = nextColStart;+ colEnd = nextColEnd;+ }+ });+ }+ return cellInfos.filter(info => info.row <= rowEnd && info.row + info.rowspan - 1 >= rowStart &&+ info.col <= colEnd && info.col + info.colspan - 1 >= colStart);+};diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex b417c49b9c..a182103893 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -92,6 +92,7 @@ import {copyPlainText, encodeBase64, isInIOS, isMac, isOnlyMeta, readClipboard}import {MenuItem} from "../../menus/Menu";import {fetchPost, fetchSyncPost} from "../../util/fetch";import {+ buildTableGrid,clearTableCell,deleteTableColumns,deleteTableRows,@@ -100,6 +101,7 @@ import {isIncludeCell,updateTableTitle,} from "../util/table";+import {getTableCellsInRectangle} from "../util/tableSelection";import {getTableCellAlignmentMenus,getTableCellBackgroundMenus,@@ -1683,6 +1685,10 @@ export class WYSIWYG {hideElements(["toolbar"], protyle);if (target.classList.contains("table__select")) {target = document.elementFromPoint(event.clientX, event.clientY) as HTMLElement;+ const cellElement = hasClosestByTag(target, "TH") || hasClosestByTag(target, "TD");+ if (cellElement) {+ target = cellElement;+ }nodeElement = hasClosestBlock(target) as HTMLElement;}event.stopPropagation();@@ -1960,51 +1966,70 @@ export class WYSIWYG {bottom: rect.bottom - tableRect.top,};};- const targetRect = getCellRect(target);- const moveTargetRect = getCellRect(moveTarget);- let left = Math.min(targetRect.left, moveTargetRect.left);- let top = Math.min(targetRect.top, moveTargetRect.top);- let right = Math.max(targetRect.right, moveTargetRect.right);- let bottom = Math.max(targetRect.bottom, moveTargetRect.bottom);- if (targetRect.left === moveTargetRect.left) {- right = left + Math.max(targetRect.right - targetRect.left,- moveTargetRect.right - moveTargetRect.left);- }- if (targetRect.top === moveTargetRect.top) {- bottom = top + Math.max(targetRect.bottom - targetRect.top,- moveTargetRect.bottom - moveTargetRect.top);+ let selectionRects: ReturnType<typeof getCellRect>[] = [];+ let logicalSelection = false;+ if (target.tagName === "TH" || target.tagName === "TD") {+ const grid = buildTableGrid(tableElement);+ const targetInfo = grid.cellInfos.find(item => item.cell === target);+ const moveTargetInfo = grid.cellInfos.find(item => item.cell === moveTarget);+ const selectedInfos = getTableCellsInRectangle(grid.cellInfos, targetInfo, moveTargetInfo);+ if (selectedInfos.length > 0) {+ selectionRects = selectedInfos.map(item => getCellRect(item.cell));+ logicalSelection = true;+ }}- // https://github.com/siyuan-note/insider/issues/1015- Array.from(tableBlockElement.querySelectorAll("th, td")).find((item: HTMLElement) => {- const itemRect = getCellRect(item);- const updateRight = itemRect.left < right && itemRect.right > right;- const updateLeft = itemRect.left < left && itemRect.right > left;- if (itemRect.top < top && itemRect.bottom > top) {- if ((itemRect.left + 6 > left && itemRect.right - 6 < right) || updateRight || updateLeft) {- top = itemRect.top;- }- if (updateRight) {- right = itemRect.right;- }- if (updateLeft) {+ if (!logicalSelection) {+ const targetRect = getCellRect(target);+ const moveTargetRect = getCellRect(moveTarget);+ let left = Math.min(targetRect.left, moveTargetRect.left);+ let top = Math.min(targetRect.top, moveTargetRect.top);+ let right = Math.max(targetRect.right, moveTargetRect.right);+ let bottom = Math.max(targetRect.bottom, moveTargetRect.bottom);+ if (targetRect.left === moveTargetRect.left) {+ right = left + Math.max(targetRect.right - targetRect.left,+ moveTargetRect.right - moveTargetRect.left);+ }+ if (targetRect.top === moveTargetRect.top) {+ bottom = top + Math.max(targetRect.bottom - targetRect.top,+ moveTargetRect.bottom - moveTargetRect.top);+ }+ // https://github.com/siyuan-note/insider/issues/1015+ Array.from(tableBlockElement.querySelectorAll("th, td")).find((item: HTMLElement) => {+ const itemRect = getCellRect(item);+ const updateRight = itemRect.left < right && itemRect.right > right;+ const updateLeft = itemRect.left < left && itemRect.right > left;+ if (itemRect.top < top && itemRect.bottom > top) {+ if ((itemRect.left + 6 > left && itemRect.right - 6 < right) || updateRight || updateLeft) {+ top = itemRect.top;+ }+ if (updateRight) {+ right = itemRect.right;+ }+ if (updateLeft) {+ left = itemRect.left;+ }+ } else if (itemRect.top < bottom && itemRect.bottom > bottom) {+ if ((itemRect.left + 6 > left && itemRect.right - 6 < right) || updateRight || updateLeft) {+ bottom = itemRect.bottom;+ }+ if (updateRight) {+ right = itemRect.right;+ }+ if (updateLeft) {+ left = itemRect.left;+ }+ } else if (updateLeft && itemRect.top + 6 > top && itemRect.bottom - 6 < bottom) {left = itemRect.left;- }- } else if (itemRect.top < bottom && itemRect.bottom > bottom) {- if ((itemRect.left + 6 > left && itemRect.right - 6 < right) || updateRight || updateLeft) {- bottom = itemRect.bottom;- }- if (updateRight) {+ } else if (updateRight && itemRect.top + 6 > top && itemRect.bottom - 6 < bottom) {right = itemRect.right;}- if (updateLeft) {- left = itemRect.left;- }- } else if (updateLeft && itemRect.top + 6 > top && itemRect.bottom - 6 < bottom) {- left = itemRect.left;- } else if (updateRight && itemRect.top + 6 > top && itemRect.bottom - 6 < bottom) {- right = itemRect.right;- }- });+ });+ selectionRects = [{left, top, right, bottom}];+ }+ const left = Math.min(...selectionRects.map(rect => rect.left));+ const top = Math.min(...selectionRects.map(rect => rect.top));+ const right = Math.max(...selectionRects.map(rect => rect.right));+ const bottom = Math.max(...selectionRects.map(rect => rect.bottom));protyle.wysiwyg.element.classList.add("protyle-wysiwyg--hiderange");const radius = "var(--b3-border-radius-s)";const touches = (edge: number, gridEdge: number) => Math.abs(edge - gridEdge) < 1;