Skip to content

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

:bug: Restore touch scrolling in sortable menus and lists https://github.com/siyuan-note/siyuan/issues/19693

Daniele580a662e2e7
diff --git a/app/src/menus/Menu.ts b/app/src/menus/Menu.ts
index 3e22ecf8cf..de26c72a3b 100644
--- a/app/src/menus/Menu.ts
+++ b/app/src/menus/Menu.ts
@@ -199,6 +199,10 @@ export class Menu {
if (target.closest("input:not([type=\"file\"]), textarea, select, [contenteditable=\"true\"]")) {
return false;
}
+ // 可排序条目由触摸拖拽桥接处理,避免排序时同时下拉关闭菜单。
+ if (target.closest('[draggable="true"]')) {
+ return false;
+ }
if (target.closest(".b3-menu__title")) {
return true;
}
diff --git a/app/src/menus/menuSheetDrag.test.ts b/app/src/menus/menuSheetDrag.test.ts
new file mode 100644
index 0000000000..2f94ca0b7b
--- /dev/null
+++ b/app/src/menus/menuSheetDrag.test.ts
@@ -0,0 +1,95 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import type {Menu} from "./Menu";
+
+const source = createSourceFile("Menu.ts", readFileSync("src/menus/Menu.ts", "utf8"), ScriptTarget.ES2021, true);
+const declaration = source.statements.find(statement => isClassDeclaration(statement) && statement.name?.text === "Menu");
+const compiled = transpileModule(declaration.getText(source), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+}).outputText;
+
+const createSheet = () => {
+ let closes = 0;
+ const listeners = new Map<string, (event: object) => void>();
+ const element = {
+ id: "testMenu",
+ style: {transform: "", transition: ""},
+ classList: {contains: (name: string) => name === "b3-menu--sheet"},
+ clientHeight: 400,
+ querySelector: () => ({innerHTML: ""}),
+ addEventListener: (type: string, listener: (event: object) => void) => listeners.set(type, listener),
+ };
+ const exports = {} as {Menu: typeof Menu};
+ runInNewContext(compiled, {
+ exports,
+ isMobile: () => true,
+ performance: {now: () => 1000},
+ getComputedStyle: () => ({overflowY: "auto"}),
+ updateMenuGroupsOnMutation() {},
+ MutationObserver: class { observe() {} },
+ document: {createElement: () => ({})},
+ window: {siyuan: {languages: {back: "Back"}}, setTimeout() {}},
+ });
+ const menu = new exports.Menu(element as unknown as HTMLElement);
+ menu.closeSheet = () => { closes++; };
+ const swipe = (options: {sortable?: boolean, title?: boolean, scrollTop?: number}, offset: number) => {
+ const items = {scrollHeight: 1000, clientHeight: 400, scrollTop: options.scrollTop || 0, parentElement: element};
+ const target = {
+ parentElement: items,
+ closest: (selector: string) => {
+ if (options.sortable && selector.includes('[draggable="true"]')) {
+ return target;
+ }
+ if (options.title && selector === ".b3-menu__title") {
+ return target;
+ }
+ return selector === ".b3-menu__items" ? items : null;
+ },
+ };
+ const point = {clientX: 80, clientY: 100};
+ const event = {
+ target,
+ touches: [point],
+ changedTouches: [point],
+ cancelable: true,
+ defaultPrevented: false,
+ preventDefault() { this.defaultPrevented = true; },
+ };
+ listeners.get("touchstart")(event);
+ point.clientY += offset;
+ listeners.get("touchmove")(event);
+ const transform = element.style.transform;
+ listeners.get("touchend")(event);
+ return {transform, prevented: event.defaultPrevented};
+ };
+ return {swipe, closes: () => closes};
+};
+
+test("sorting a menu item downward does not move or dismiss its bottom sheet", () => {
+ const sheet = createSheet();
+ assert.deepEqual(sheet.swipe({sortable: true}, 200), {transform: "", prevented: false});
+ assert.equal(sheet.closes(), 0);
+});
+
+test("scrolling a sortable menu upward does not move its bottom sheet", () => {
+ const sheet = createSheet();
+ assert.deepEqual(sheet.swipe({sortable: true}, -100), {transform: "", prevented: false});
+ assert.equal(sheet.closes(), 0);
+});
+
+test("scrolling menu content back toward the top does not dismiss its bottom sheet", () => {
+ const sheet = createSheet();
+ assert.deepEqual(sheet.swipe({scrollTop: 150}, 100), {transform: "", prevented: false});
+ assert.equal(sheet.closes(), 0);
+});
+
+for (const title of [false, true]) {
+ test(`pulling the menu ${title ? "title" : "ordinary content at the top"} downward still dismisses it`, () => {
+ const sheet = createSheet();
+ assert.deepEqual(sheet.swipe({title}, 200), {transform: "translateY(200px)", prevented: true});
+ assert.equal(sheet.closes(), 1);
+ });
+}
diff --git a/app/src/util/touchDragBridge.test.ts b/app/src/util/touchDragBridge.test.ts
new file mode 100644
index 0000000000..7c32ec1308
--- /dev/null
+++ b/app/src/util/touchDragBridge.test.ts
@@ -0,0 +1,198 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import * as core from "./touchDragBridgeCore";
+
+const compiled = transpileModule(readFileSync("src/util/touchDragBridge.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+}).outputText;
+
+class TestElement {
+ draggable = "false";
+ events: string[] = [];
+ classList = {contains: (name: string) => this.classes.includes(name)};
+
+ constructor(private classes: string[] = [], public parentElement?: TestElement) {}
+
+ closest(selector: string): TestElement | null {
+ return this.classes.includes(selector.slice(1)) ? this : this.parentElement?.closest(selector) || null;
+ }
+
+ getAttribute(name: string) {
+ return name === "draggable" ? this.draggable : null;
+ }
+
+ setAttribute(name: string, value: string) {
+ if (name === "draggable") {
+ this.draggable = value;
+ }
+ }
+
+ dispatchEvent(event: {type: string}) {
+ this.events.push(event.type);
+ return true;
+ }
+}
+
+class TestHTMLElement extends TestElement {}
+
+const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel") => {
+ let now = 1000;
+ const listeners = new Map<string, Array<(event: object) => void>>();
+ const body = new TestHTMLElement();
+ const panel = new TestHTMLElement([panelClass], body);
+ const row = new TestHTMLElement([rowClass], panel);
+ row.draggable = "true";
+ const label = new TestHTMLElement(["b3-menu__label"], row);
+ // SVG 的 use 子节点不是 HTMLElement,触摸仍需定位到所在行和抓手。
+ const handle = new TestElement(["fn__grab"], row);
+ const handleIcon = new TestElement([], handle);
+ const runtime = {touchDragActive: false, touchDragGhost: null as HTMLElement | null};
+ const exports: {initTouchDragBridge?: () => void} = {};
+ runInNewContext(compiled, {
+ exports,
+ require: (id: string) => id === "./touchDragBridgeCore" ? core : {
+ Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_LONGPRESS: 460, TIMEOUT_MOUSE_DRAG_DELAY: 150},
+ isInAndroid: () => false,
+ ipcRenderer: {on() {}},
+ stopScrollAnimation() {},
+ },
+ Date: {now: () => now},
+ HTMLElement: TestHTMLElement,
+ DataTransfer: class {},
+ DragEvent: class {
+ constructor(public type: string) {}
+ },
+ window: {
+ siyuan: runtime,
+ addEventListener() {},
+ setTimeout: () => 1,
+ clearTimeout() {},
+ },
+ document: {
+ body,
+ documentElement: {addEventListener() {}},
+ addEventListener: (type: string, listener: (event: object) => void) => {
+ listeners.set(type, [...listeners.get(type) || [], listener]);
+ },
+ querySelectorAll: (): Element[] => [],
+ elementFromPoint: () => row,
+ },
+ });
+ exports.initTouchDragBridge();
+ const dispatch = (type: string, target: TestElement, y = 100, elapsed = 0, mouse = false) => {
+ now += elapsed;
+ const point = {clientX: 80, clientY: y, radiusX: mouse ? 0 : 1};
+ const event = {
+ target,
+ touches: [point],
+ changedTouches: [point],
+ defaultPrevented: false,
+ pointerType: mouse ? "mouse" : "touch",
+ preventDefault() { this.defaultPrevented = true; },
+ };
+ listeners.get(type)?.forEach(listener => listener(event));
+ return event.defaultPrevented;
+ };
+ return {row, label, handleIcon, runtime, dispatch};
+};
+
+for (const offset of [-80, 80]) {
+ test(`swiping database menu content by ${offset}px keeps native scrolling available`, () => {
+ const bridge = createBridge();
+ bridge.dispatch("touchstart", bridge.label);
+ assert.equal(bridge.row.draggable, "false");
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 100 + offset, 50), false);
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 100 + offset * 2, 600), false);
+ assert.equal(bridge.dispatch("touchend", bridge.label, 100 + offset * 2), false);
+ assert.deepEqual(bridge.row.events, []);
+ assert.equal(bridge.runtime.touchDragActive, false);
+ assert.equal(bridge.row.draggable, "true");
+ });
+}
+
+test("long pressing database menu content still allows sorting", () => {
+ const bridge = createBridge();
+ bridge.dispatch("touchstart", bridge.label);
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 140, 500), true);
+ assert.deepEqual(bridge.row.events, ["dragstart"]);
+ bridge.dispatch("touchend", bridge.label, 160);
+ assert.ok(bridge.row.events.includes("drop"));
+ assert.equal(bridge.row.events[bridge.row.events.length - 1], "dragend");
+ assert.equal(bridge.row.draggable, "true");
+ assert.equal(bridge.runtime.touchDragActive, false);
+});
+
+test("dragging the database menu handle starts sorting without a long press", () => {
+ const bridge = createBridge();
+ bridge.dispatch("touchstart", bridge.handleIcon);
+ assert.equal(bridge.dispatch("touchmove", bridge.handleIcon, 140, 50), true);
+ assert.deepEqual(bridge.row.events, ["dragstart"]);
+ bridge.dispatch("touchcancel", bridge.handleIcon);
+ assert.deepEqual(bridge.row.events, ["dragstart", "dragend"]);
+ assert.equal(bridge.row.draggable, "true");
+ assert.equal(bridge.runtime.touchDragActive, false);
+});
+
+test("tapping database menu content does not suppress its click", () => {
+ const bridge = createBridge();
+ assert.equal(bridge.dispatch("touchstart", bridge.label), false);
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 102, 50), false);
+ assert.equal(bridge.dispatch("touchend", bridge.label, 102), false);
+ assert.deepEqual(bridge.row.events, []);
+ assert.equal(bridge.row.draggable, "true");
+});
+
+test("mouse-generated touches can still drag database menu content immediately", () => {
+ const bridge = createBridge();
+ bridge.dispatch("pointerdown", bridge.label, 100, 0, true);
+ bridge.dispatch("touchstart", bridge.label, 100, 0, true);
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 140, 50, true), true);
+ assert.deepEqual(bridge.row.events, ["dragstart"]);
+});
+
+for (const [rowClass, panelClass] of [
+ ["b3-menu__item", "b3-menu"],
+ ["b3-list-item", "av__template-list"],
+ ["b3-list-item", "b3-list"],
+]) {
+ test(`${panelClass} content allows scrolling and its handle still sorts`, () => {
+ const bridge = createBridge(rowClass, panelClass);
+ bridge.dispatch("touchstart", bridge.label);
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 140, 50), false);
+ bridge.dispatch("touchcancel", bridge.label);
+ assert.deepEqual(bridge.row.events, []);
+ assert.equal(bridge.row.draggable, "true");
+
+ bridge.dispatch("touchstart", bridge.handleIcon);
+ assert.equal(bridge.dispatch("touchmove", bridge.handleIcon, 140, 50), true);
+ assert.deepEqual(bridge.row.events, ["dragstart"]);
+ });
+}
+
+test("standalone database option handles still drag immediately", () => {
+ const bridge = createBridge();
+ bridge.row.draggable = "false";
+ const handle = new TestHTMLElement(["fn__grab"], bridge.row);
+ handle.draggable = "true";
+ const icon = new TestElement([], handle);
+ bridge.dispatch("touchstart", icon);
+ assert.equal(bridge.dispatch("touchmove", icon, 140, 50), true);
+ assert.deepEqual(handle.events, ["dragstart"]);
+});
+
+for (const panelClass of ["sy__file", "sy__outline", "layout-tab-bar", "protyle-action"]) {
+ test(`${panelClass} keeps its existing scroll and long-press behavior`, () => {
+ const bridge = createBridge("item", panelClass);
+ bridge.dispatch("touchstart", bridge.label);
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 140, 50), false);
+ bridge.dispatch("touchend", bridge.label, 140);
+ assert.deepEqual(bridge.row.events, []);
+
+ bridge.dispatch("touchstart", bridge.label);
+ assert.equal(bridge.dispatch("touchmove", bridge.label, 140, 500), true);
+ assert.deepEqual(bridge.row.events, ["dragstart"]);
+ });
+}
diff --git a/app/src/util/touchDragBridge.ts b/app/src/util/touchDragBridge.ts
index 8f64e62a82..7eae690f16 100644
--- a/app/src/util/touchDragBridge.ts
+++ b/app/src/util/touchDragBridge.ts
@@ -207,7 +207,7 @@ const handleTouchStart = (e: TouchEvent) => {
if (!target.classList.contains("av__widthdrag") && !target.classList.contains("av__freeze-drag")) {
const draggable = getDraggableAncestor(target);
if (draggable) {
- dragState = createDragState(draggable, touch, "touch", isMouseInput(touch));
+ dragState = createDragState(draggable, target, touch, "touch", isMouseInput(touch));
// WebKit 会接管 draggable 元素的长按并取消触摸序列,临时关闭原生拖拽以保留 touchend。
suspendNativeDrag(dragState);
return;
@@ -361,8 +361,11 @@ const getDraggableAncestor = (el: Element): HTMLElement | null => {
return null;
};
-const createDragState = (draggableElement: HTMLElement, point: DragPoint, inputType: "touch" | "pointer",
+const createDragState = (draggableElement: HTMLElement, target: Element, point: DragPoint, inputType: "touch" | "pointer",
isMouse: boolean, pointerId?: number): DragState & LongPressGate => {
+ // 菜单和列表正文优先滚动,抓手仍可直接拖拽。
+ const isListContent = (draggableElement.classList.contains("b3-menu__item") ||
+ draggableElement.classList.contains("b3-list-item")) && !target.closest(".fn__grab");
return {
dataTransfer: null,
ghostElement: null,
@@ -374,7 +377,7 @@ const createDragState = (draggableElement: HTMLElement, point: DragPoint, inputT
startY: point.clientY,
touchStartTime: Date.now(),
// 触摸操作和 Android 外接鼠标在文件树、画廊、页签和列表操作中需长按,以避免与滚动冲突。
- requireLongPress: shouldRequireLongPress(draggableElement.closest(".sy__file") !== null ||
+ requireLongPress: shouldRequireLongPress(isListContent || draggableElement.closest(".sy__file") !== null ||
draggableElement.closest(".sy__outline") !== null ||
draggableElement.closest(".av__gallery-item") !== null ||
draggableElement.closest(".av__group-title") !== null ||
@@ -445,7 +448,7 @@ const handlePointerDown = (event: PointerEvent) => {
return;
}
- dragState = createDragState(draggable, event, "pointer", true, event.pointerId);
+ dragState = createDragState(draggable, event.target as Element, event, "pointer", true, event.pointerId);
// 原生 dragstart 会取消 Pointer 流,临时关闭 draggable 以保留 pointermove 和 pointerup。
suspendNativeDrag(dragState);
};