Skip to content

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

:bug: Prevent runaway scrolling during mobile table selection https://github.com/siyuan-note/siyuan/issues/20635

Danielaad901caec99
diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.ts
index 4b2e0f086e..f3707bec95 100644
--- a/app/src/protyle/util/tableControl.ts
+++ b/app/src/protyle/util/tableControl.ts
@@ -1315,8 +1315,13 @@ export class TableControl {
}
private scheduleRender() {
- cancelAnimationFrame(this.frame);
- this.frame = requestAnimationFrame(() => this.render());
+ if (this.frame) {
+ return;
+ }
+ this.frame = requestAnimationFrame(() => {
+ this.frame = 0;
+ this.render();
+ });
}
private setPosition(element: HTMLElement, left: number, top: number) {
diff --git a/app/src/protyle/util/tableTouch.test.ts b/app/src/protyle/util/tableTouch.test.ts
index ccce799277..b15099c884 100644
--- a/app/src/protyle/util/tableTouch.test.ts
+++ b/app/src/protyle/util/tableTouch.test.ts
@@ -16,7 +16,7 @@ const browserCases = async (sources: Record<string, string>) => {
".block__icons svg,.protyle-table-control svg{width:16px;height:16px}.fn__none{display:none}" +
".protyle-table-control__handle,.protyle-table-control__add,.protyle-table-control__touch-button{" +
"position:fixed;width:28px;height:28px;transform:translate(-50%,-50%)}" +
- ".protyle-table-control__width-guide{position:fixed}";
+ ".protyle-table-control__width-guide,.protyle-table-control__selection{position:fixed}";
document.head.append(fixtureStyle);
const transactions: {before: string, after: string}[] = [];
const clipboard: {textPlain: string, textHTML?: string, textSiyuan?: string}[] = [];
@@ -130,6 +130,11 @@ const browserCases = async (sources: Record<string, string>) => {
const context = load("src/protyle/util/tableCellRichContext.ts") as typeof import("./tableCellRichContext");
const virtual = load("src/protyle/util/tableVirtualizationDOM.ts") as typeof import("./tableVirtualizationDOM");
const tick = () => new Promise(resolve => setTimeout(resolve, 30));
+ const frames = async (count: number) => {
+ for (let frame = 0; frame < count; frame++) {
+ await new Promise<void>(resolve => requestAnimationFrame(() => resolve()));
+ }
+ };
const pointer = (target: EventTarget, type: string, x = 0, y = 0, id = 1) => target.dispatchEvent(new PointerEvent(type,
{bubbles: true, cancelable: true, clientX: x, clientY: y, button: 0, pointerId: id, pointerType: "touch"}));
const touchEvent = (target: Element, type: string, x = 60, y = 110, count = 1) => {
@@ -256,6 +261,78 @@ const browserCases = async (sources: Record<string, string>) => {
check.equal(f.table.rows.length, 4);
f.dispose();
}
+ for (const mode of ["cell", "row", "column"] as const) {
+ const f = fixture();
+ f.editor.style.minHeight = "1400px";
+ f.core.startTouchSelection(f.cell(3, 1), mode);
+ f.render();
+ const endpoint = f.touch("[data-endpoint='end']");
+ const rect = endpoint.getBoundingClientRect();
+ const count = f.core.getSelectedCells().length;
+ pointer(endpoint, "pointerdown", rect.left + rect.width / 2, rect.top + rect.height / 2);
+ await frames(4);
+ check.equal(f.root.scrollTop, 0, `${mode} endpoints do not scroll while held still`);
+ check.equal(f.core.getSelectedCells().length, count, "holding an endpoint does not change the selection");
+ pointer(document, "pointermove", rect.left + rect.width / 2 - 10, rect.top + rect.height / 2 - 5);
+ await frames(4);
+ check.equal(f.root.scrollTop, 0, "dragging near a fully visible table edge does not scroll the document");
+ f.root.scrollTop = 20;
+ await frames(4);
+ const header = f.cell(0, 0).getBoundingClientRect();
+ const column = f.touch("[data-mode='column']").getBoundingClientRect();
+ const row = f.touch("[data-mode='row']").getBoundingClientRect();
+ check.equal(row.top + row.height / 2, (Math.max(header.top, column.top + column.height / 2) + header.bottom) / 2,
+ "row handles follow scrolling while an endpoint is being dragged");
+ pointer(document, "pointercancel");
+ f.dispose();
+ }
+ {
+ const f = fixture();
+ f.editor.style.minHeight = "1400px";
+ f.editor.querySelectorAll<HTMLElement>("td, th").forEach(cell => cell.style.height = "150px");
+ const maxScroll = f.table.getBoundingClientRect().bottom - f.root.getBoundingClientRect().bottom;
+ f.core.startTouchSelection(f.cell(1, 0));
+ f.render();
+ const endpoint = f.touch("[data-endpoint='end']");
+ const rect = endpoint.getBoundingClientRect();
+ pointer(endpoint, "pointerdown", rect.left + rect.width / 2, rect.top + rect.height / 2);
+ pointer(document, "pointermove", 165, 490);
+ await frames(8);
+ check.ok(f.root.scrollTop > 0, "dragging to the editor viewport edge reveals hidden table rows");
+ const selection = f.touch(".protyle-table-control__selection:not(.fn__none)").getBoundingClientRect();
+ check.ok(Math.abs(selection.top - f.core.getSelectedCells()[0].getBoundingClientRect().top) <= 12,
+ "selection painting keeps up with continuous automatic scrolling");
+ await frames(24);
+ check.ok(f.root.scrollTop <= maxScroll + 10, "automatic scrolling stops after the last table row becomes visible");
+ const stopped = f.root.scrollTop;
+ await frames(4);
+ check.equal(f.root.scrollTop, stopped);
+ pointer(document, "pointerup");
+ f.dispose();
+ }
+ for (const axis of ["horizontal", "vertical"]) {
+ const f = fixture();
+ f.editor.style.minHeight = "1400px";
+ const wrapper = f.table.parentElement;
+ wrapper.style[axis === "horizontal" ? "width" : "height"] = axis === "horizontal" ? "180px" : "130px";
+ if (axis === "horizontal") f.table.style.minWidth = "270px";
+ f.core.startTouchSelection(f.cell(1, 0));
+ f.render();
+ const endpoint = f.touch("[data-endpoint='end']");
+ const rect = endpoint.getBoundingClientRect();
+ pointer(endpoint, "pointerdown", rect.left + rect.width / 2, rect.top + rect.height / 2);
+ pointer(document, "pointermove", axis === "horizontal" ? 175 : 110, axis === "horizontal" ? 110 : 128);
+ await frames(6);
+ check.ok((axis === "horizontal" ? wrapper.scrollLeft : wrapper.scrollTop) > 0,
+ `${axis} scrolling uses the table's own scroll container`);
+ check.equal(f.root.scrollTop, 0, "scrolling inside a table leaves the surrounding document in place");
+ pointer(document, "pointercancel");
+ const stopped = axis === "horizontal" ? wrapper.scrollLeft : wrapper.scrollTop;
+ await frames(3);
+ check.equal(axis === "horizontal" ? wrapper.scrollLeft : wrapper.scrollTop, stopped,
+ "canceling an endpoint drag stops automatic scrolling");
+ f.dispose();
+ }
{
const f = fixture();
f.core.startTouchSelection(f.cell(1, 0));
diff --git a/app/src/protyle/util/tableTouch.ts b/app/src/protyle/util/tableTouch.ts
index 673268c057..7aafce40e3 100644
--- a/app/src/protyle/util/tableTouch.ts
+++ b/app/src/protyle/util/tableTouch.ts
@@ -49,7 +49,7 @@ export class TableTouchControl {
private controller = new AbortController();
private gesture?: AbortController;
private scrollFrame: number;
- private dragging = false;
+ private dragging?: "controls" | "range";
private longPress?: {
cell: HTMLTableCellElement, identifier: number, clientX: number, clientY: number, timer: number, selected: boolean,
};
@@ -203,7 +203,7 @@ export class TableTouchControl {
multi.classList.toggle("block__icon--active", this.multiSelect);
const copy = this.toolbar.querySelector<HTMLButtonElement>('[data-type="copy"]');
copy.disabled = !!state?.selection && state.selection.mode === "cell" && !state.selection.rectangle;
- if (this.dragging) {
+ if (this.dragging === "controls") {
this.renderEndpoints(state);
return;
}
@@ -459,7 +459,7 @@ export class TableTouchControl {
private holdControls(event: PointerEvent) {
this.cancelGesture();
- this.dragging = true;
+ this.dragging = "controls";
this.gesture = new AbortController();
const end = (up: PointerEvent) => {
if (up.pointerId === event.pointerId) {
@@ -489,11 +489,12 @@ export class TableTouchControl {
if (!anchor) {
return;
}
- this.dragging = true;
+ this.dragging = "range";
const controller = new AbortController();
this.gesture = controller;
const signal = controller.signal;
let point = {clientX: event.clientX, clientY: event.clientY};
+ let moved = false;
const update = () => {
const current = this.getState();
if (!current?.selection) {
@@ -520,6 +521,13 @@ export class TableTouchControl {
}
move.preventDefault();
point = move;
+ if (!moved) {
+ if (Math.hypot(move.clientX - event.clientX, move.clientY - event.clientY) < Constants.SIZE_DRAG_THRESHOLD) {
+ return;
+ }
+ moved = true;
+ this.scrollFrame = requestAnimationFrame(scroll);
+ }
update();
}, {signal, passive: false});
const end = (up: PointerEvent) => {
@@ -535,31 +543,47 @@ export class TableTouchControl {
if (!this.gesture || !current?.selection) {
return;
}
- const {viewport} = current;
+ const content = this.content();
+ const contentRect = content.getBoundingClientRect();
+ const visualViewport = window.visualViewport;
+ const left = Math.max(contentRect.left, visualViewport?.offsetLeft || 0);
+ const right = Math.min(contentRect.right, window.innerWidth,
+ (visualViewport?.offsetLeft || 0) + (visualViewport?.width || window.innerWidth));
+ const top = Math.max(contentRect.top, visualViewport?.offsetTop || 0, this.toolbar.getBoundingClientRect().bottom + 8);
+ const bottom = Math.min(contentRect.bottom, window.innerHeight,
+ (visualViewport?.offsetTop || 0) + (visualViewport?.height || window.innerHeight));
+ const wrapper = current.table.parentElement;
+ const wrapperRect = wrapper.getBoundingClientRect();
const delta = (value: number, min: number, max: number) => value < min + 28 ? -10 : value > max - 28 ? 10 : 0;
- const dx = delta(point.clientX, viewport.left, viewport.right);
- const dy = delta(point.clientY, viewport.top, viewport.bottom);
- if (dx || dy) {
- current.table.parentElement.scrollLeft += dx;
- const wrapper = current.table.parentElement;
- const scrollTop = wrapper.scrollTop;
- wrapper.scrollTop += dy;
- if (wrapper.scrollTop === scrollTop) {
- this.content().scrollTop += dy;
- }
+ const scrollLeft = wrapper.scrollLeft;
+ const scrollTop = wrapper.scrollTop;
+ const contentScrollTop = content.scrollTop;
+ if (wrapper.scrollWidth > wrapper.clientWidth) {
+ wrapper.scrollLeft += delta(point.clientX, Math.max(wrapperRect.left, left), Math.min(wrapperRect.right, right));
+ }
+ if (wrapper.scrollHeight > wrapper.clientHeight) {
+ wrapper.scrollTop += delta(point.clientY, Math.max(wrapperRect.top, top), Math.min(wrapperRect.bottom, bottom));
+ }
+ // 表格内部先滚动;只有滚动视口外仍有表格内容时才滚动正文,避免越过表格继续滚动。
+ const dy = delta(point.clientY, top, bottom);
+ const tableRect = current.table.getBoundingClientRect();
+ if (wrapper.scrollTop === scrollTop && (dy < 0 && Math.max(wrapperRect.top, tableRect.top) < top ||
+ dy > 0 && Math.min(wrapperRect.bottom, tableRect.bottom) > bottom)) {
+ content.scrollTop += dy;
+ }
+ if (wrapper.scrollLeft !== scrollLeft || wrapper.scrollTop !== scrollTop || content.scrollTop !== contentScrollTop) {
update();
this.actions.render();
}
this.scrollFrame = requestAnimationFrame(scroll);
};
- this.scrollFrame = requestAnimationFrame(scroll);
}
private cancelGesture() {
this.cancelLongPress();
this.gesture?.abort();
this.gesture = undefined;
- this.dragging = false;
+ this.dragging = undefined;
cancelAnimationFrame(this.scrollFrame);
}
}