Skip to content

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

:bug: Update table resize handles while captions scroll https://github.com/siyuan-note/siyuan/issues/18537

Daniel0c1c9db405c7
diff --git a/app/src/boot/globalEvent/event.ts b/app/src/boot/globalEvent/event.ts
index ba05814696..04aba61547 100644
--- a/app/src/boot/globalEvent/event.ts
+++ b/app/src/boot/globalEvent/event.ts
@@ -1,5 +1,5 @@
import type {App} from "../../index";
-import {windowMouseMove} from "./mousemove";
+import {getTableResizeBounds, windowMouseMove} from "./mousemove";
import {windowKeyUp} from "./keyup";
import {windowKeyDown} from "./keydown";
import {globalClick} from "./click";
@@ -61,14 +61,19 @@ export const initWindowEvent = (app: App) => {
}
}, {passive: true});
- // 横向滚动表格时重新定位表格列宽调整手柄 https://github.com/siyuan-note/siyuan/issues/13828
+ // 滚动表格时重新定位表格列宽调整手柄 https://github.com/siyuan-note/siyuan/issues/13828
window.addEventListener("scroll", (event: Event) => {
const scrollElement = event.target as HTMLElement;
- // 仅处理表格内容容器(.table 块的 firstElementChild)的滚动
- if (!scrollElement.parentElement || !scrollElement.parentElement.classList.contains("table")) {
+ const tableBlockElement = hasClosestByClassName(scrollElement, "table");
+ if (!tableBlockElement) {
return;
}
- const resizeElement = scrollElement.parentElement.querySelector(".table__resize") as HTMLElement;
+ const tableElement = tableBlockElement.querySelector("table") as HTMLTableElement;
+ if (!tableElement ||
+ (scrollElement !== tableBlockElement.firstElementChild && scrollElement !== tableElement)) {
+ return;
+ }
+ const resizeElement = tableBlockElement.querySelector(".table__resize") as HTMLElement;
if (!resizeElement) {
return;
}
@@ -77,8 +82,12 @@ export const initWindowEvent = (app: App) => {
if (baseLeft === null || !style || style.indexOf("display:block") === -1) {
return;
}
- const left = parseInt(baseLeft) - scrollElement.scrollLeft;
- resizeElement.setAttribute("style", style.replace(/left: ?-?\d+px;/, `left: ${Math.round(left)}px;`));
+ const left = parseInt(baseLeft) - (tableBlockElement.firstElementChild as HTMLElement).scrollLeft;
+ const resizeBounds = getTableResizeBounds(tableElement);
+ resizeElement.setAttribute("style", style
+ .replace(/top:-?\d+(?:\.\d+)?px;/, `top:${resizeBounds.top}px;`)
+ .replace(/height:-?\d+(?:\.\d+)?px;/, `height:${resizeBounds.height}px;`)
+ .replace(/left: ?-?\d+px;/, `left: ${Math.round(left)}px;`));
}, true);
let scrollTarget: HTMLElement | false;
diff --git a/app/src/boot/globalEvent/mousemove.ts b/app/src/boot/globalEvent/mousemove.ts
index a358b3ddd7..b9cf1838f6 100644
--- a/app/src/boot/globalEvent/mousemove.ts
+++ b/app/src/boot/globalEvent/mousemove.ts
@@ -3,6 +3,21 @@ import {isWindow} from "../../util/functions";
import {hasClosestBlock, hasClosestByClassName, hasClosestByTag} from "../../protyle/util/hasClosest";
import {getColIndex} from "../../protyle/util/table";
+export const getTableResizeBounds = (tableElement: HTMLTableElement) => {
+ const captionElement = tableElement.querySelector("caption");
+ let top = 0;
+ if (captionElement && captionElement.style.captionSide !== "bottom") {
+ const tableRect = tableElement.getBoundingClientRect();
+ top = Math.max(0, Math.min(tableRect.height,
+ captionElement.getBoundingClientRect().bottom - tableRect.top));
+ }
+ return {
+ top,
+ height: Math.max(0, Math.min(tableElement.querySelector("colgroup").clientHeight,
+ tableElement.clientHeight - top)),
+ };
+};
+
const getRightBlock = (element: HTMLElement, x: number, y: number) => {
let left = x + 34;
let nodeElement = element;
@@ -260,21 +275,17 @@ export const windowMouseMove = (event: MouseEvent) => {
}
if (tableElement.getAttribute("contenteditable") === "true") {
- const captionElement = blockElement.querySelector("caption");
- const captionHeight = (captionElement && captionElement.style.captionSide !== "bottom") ?
- captionElement.getBoundingClientRect().height : 0;
- const tableHeight = Math.min(blockElement.querySelector("colgroup").clientHeight,
- tableElement.clientHeight - captionHeight);
+ const resizeBounds = getTableResizeBounds(tableElement);
const rect = cellElement.getBoundingClientRect();
if (rect.right - event.clientX < 3 && rect.right - event.clientX > 0) {
resizeElement.setAttribute("data-col-index", (getColIndex(cellElement) + cellElement.colSpan - 1).toString());
// 记录基础 left(不含 scrollLeft),以便横向滚动后重新定位 https://github.com/siyuan-note/siyuan/issues/13828
resizeElement.setAttribute("data-left", (cellElement.offsetWidth + cellElement.offsetLeft - 3).toString());
- resizeElement.setAttribute("style", `top:${captionHeight}px;height:${tableHeight}px;left: ${Math.round(cellElement.offsetWidth + cellElement.offsetLeft - blockElement.firstElementChild.scrollLeft - 3)}px;display:block`);
+ resizeElement.setAttribute("style", `top:${resizeBounds.top}px;height:${resizeBounds.height}px;left: ${Math.round(cellElement.offsetWidth + cellElement.offsetLeft - blockElement.firstElementChild.scrollLeft - 3)}px;display:block`);
} else if (event.clientX - rect.left < 3 && event.clientX - rect.left > 0 && cellElement.previousElementSibling) {
resizeElement.setAttribute("data-col-index", (getColIndex(cellElement) - 1).toString());
resizeElement.setAttribute("data-left", (cellElement.offsetLeft - 3).toString());
- resizeElement.setAttribute("style", `top:${captionHeight}px;height:${tableHeight}px;left: ${Math.round(cellElement.offsetLeft - blockElement.firstElementChild.scrollLeft - 3)}px;display:block`);
+ resizeElement.setAttribute("style", `top:${resizeBounds.top}px;height:${resizeBounds.height}px;left: ${Math.round(cellElement.offsetLeft - blockElement.firstElementChild.scrollLeft - 3)}px;display:block`);
}
}
}