An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Activate mobile block dragging on long press https://github.com/siyuan-note/siyuan/issues/20536
Daniel29201c0d4219
diff --git a/app/src/util/touchDragBridge.test.ts b/app/src/util/touchDragBridge.test.tsindex fee80265e7..6153555c3c 100644--- a/app/src/util/touchDragBridge.test.ts+++ b/app/src/util/touchDragBridge.test.ts@@ -11,6 +11,9 @@ const compiled = transpileModule(readFileSync("src/util/touchDragBridge.ts", "utclass TestElement {draggable = "false";+ isConnected = true;+ tagName = "SPAN";+ dataset: Record<string, string> = {};events: string[] = [];classList = {contains: (name: string) => this.classes.includes(name), toggle: (name: string, active: boolean) => {this.classes = this.classes.filter(value => value !== name);@@ -39,16 +42,25 @@ class TestElement {this.events.push(event.type);return true;}++ setPointerCapture() {}}class TestHTMLElement extends TestElement {}-const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel") => {+const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobile = false) => {let now = 1000;+ let hidden = false;+ let nextTimer = 0;+ const timers = new Map<number, {callback: () => void, time: number}>();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);+ const button = panelClass === "protyle-gutters" ? new TestHTMLElement([], panel) : panel;+ if (panelClass === "protyle-gutters") {+ button.tagName = "BUTTON";+ }+ const row = new TestHTMLElement([rowClass], button);row.draggable = "true";const label = new TestHTMLElement(["b3-menu__label"], row);// SVG 的 use 子节点不是 HTMLElement,触摸仍需定位到所在行和抓手。@@ -61,23 +73,31 @@ const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel") => {require: (id: string) => id === "./touchDragBridgeCore" ? core : {Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_LONGPRESS: 460, TIMEOUT_MOUSE_DRAG_DELAY: 150},isInAndroid: () => false,+ isMobile: () => mobile,ipcRenderer: {on() {}},stopScrollAnimation() {},},Date: {now: () => now},+ Element: TestElement,HTMLElement: TestHTMLElement,- DataTransfer: class {},+ DataTransfer: class { types: string[] = []; },DragEvent: class {constructor(public type: string) {}},window: {siyuan: runtime,- addEventListener() {},- setTimeout: () => 1,- clearTimeout() {},+ addEventListener: (type: string, listener: (event: object) => void) => {+ listeners.set(type, [...listeners.get(type) || [], listener]);+ },+ setTimeout: (callback: () => void, delay = 0) => {+ timers.set(++nextTimer, {callback, time: now + delay});+ return nextTimer;+ },+ clearTimeout: (id: number) => timers.delete(id),},document: {body,+ get hidden() { return hidden; },documentElement: {addEventListener() {}},addEventListener: (type: string, listener: (event: object) => void) => {listeners.set(type, [...listeners.get(type) || [], listener]);@@ -87,21 +107,43 @@ const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel") => {},});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 advance = (elapsed: number) => {+ const end = now + elapsed;+ while (true) {+ const due = Array.from(timers).filter(([, timer]) => timer.time <= end)+ .sort((a, b) => a[1].time - b[1].time)[0];+ if (!due) {+ break;+ }+ timers.delete(due[0]);+ now = due[1].time;+ due[1].callback();+ }+ now = end;+ };+ const dispatch = (type: string, target: TestElement, y = 100, elapsed = 0, mouse = false, touchCount = 1) => {+ advance(elapsed);+ if (type === "visibilitychange") {+ hidden = true;+ }+ const point = {identifier: 1, clientX: 80, clientY: y, radiusX: mouse ? 0 : 1};const event = {+ ...point,target,- touches: [point],+ touches: type === "touchend" || type === "touchcancel" ? [] :+ Array.from({length: touchCount}, (_, index) => ({...point, identifier: index + 1})),changedTouches: [point],defaultPrevented: false,pointerType: mouse ? "mouse" : "touch",+ button: 0,+ buttons: 1,+ pointerId: 1,preventDefault() { this.defaultPrevented = true; },};listeners.get(type)?.forEach(listener => listener(event));return event.defaultPrevented;};- return {row, label, handleIcon, runtime, dispatch};+ return {row, label, handleIcon, runtime, dispatch, advance};};for (const offset of [-80, 80]) {@@ -201,3 +243,99 @@ for (const panelClass of ["sy__file", "sy__outline", "layout-tab-bar", "protyle-assert.deepEqual(bridge.row.events, ["dragstart"]);});}++test("a mobile block gutter activates dragging while the finger remains still", () => {+ const bridge = createBridge("", "protyle-gutters", true);+ bridge.dispatch("touchstart", bridge.row);+ bridge.advance(459);+ assert.deepEqual(bridge.row.events, []);+ bridge.advance(1);+ assert.deepEqual(bridge.row.events, ["dragstart"]);+ assert.equal(bridge.runtime.touchDragActive, true);+ assert.equal(bridge.dispatch("touchmove", bridge.row, 140), true);+ assert.equal(bridge.dispatch("touchend", bridge.row, 140), true);+ assert.ok(bridge.row.events.includes("drop"));+ assert.equal(bridge.row.events[bridge.row.events.length - 1], "dragend");+ assert.equal(bridge.runtime.touchDragActive, false);+ assert.equal(bridge.row.draggable, "true");+});++test("short tapping a mobile block gutter preserves its menu click", () => {+ const bridge = createBridge("", "protyle-gutters", true);+ assert.equal(bridge.dispatch("touchstart", bridge.row), false);+ assert.equal(bridge.dispatch("touchend", bridge.row, 102, 100), false);+ bridge.advance(500);+ assert.deepEqual(bridge.row.events, []);+ assert.equal(bridge.row.draggable, "true");+});++test("moving a mobile block gutter before the long press cancels activation", () => {+ const bridge = createBridge("", "protyle-gutters", true);+ bridge.dispatch("touchstart", bridge.row);+ assert.equal(bridge.dispatch("touchmove", bridge.row, 110, 100), false);+ bridge.advance(500);+ assert.equal(bridge.dispatch("touchmove", bridge.row, 140), false);+ assert.equal(bridge.dispatch("touchend", bridge.row, 140), true);+ assert.deepEqual(bridge.row.events, []);+ assert.equal(bridge.row.draggable, "true");+});++test("small finger movements keep the block gutter long press pending", () => {+ const bridge = createBridge("", "protyle-gutters", true);+ bridge.dispatch("touchstart", bridge.row);+ assert.equal(bridge.dispatch("touchmove", bridge.row, 102, 100), false);+ bridge.advance(360);+ assert.deepEqual(bridge.row.events, ["dragstart"]);+ assert.equal(bridge.dispatch("touchend", bridge.row, 102), true);+ assert.deepEqual(bridge.row.events, ["dragstart", "dragend"]);+ assert.equal(bridge.row.draggable, "true");+});++for (const cancel of ["touchcancel", "blur", "visibilitychange", "additional touch"]) {+ for (const active of [false, true]) {+ test(`${cancel} cancels ${active ? "active" : "pending"} block gutter dragging`, () => {+ const bridge = createBridge("", "protyle-gutters", true);+ bridge.dispatch("touchstart", bridge.row);+ if (active) {+ bridge.advance(460);+ }+ bridge.dispatch(cancel === "additional touch" ? "touchstart" : cancel, bridge.row, 100, 0, false, 2);+ bridge.advance(500);+ assert.deepEqual(bridge.row.events, active ? ["dragstart", "dragend"] : []);+ assert.equal(bridge.runtime.touchDragActive, false);+ assert.equal(bridge.row.draggable, "true");+ });+ }+}++test("removing a block gutter cancels its pending long press", () => {+ const bridge = createBridge("", "protyle-gutters", true);+ bridge.dispatch("touchstart", bridge.row);+ bridge.row.isConnected = false;+ bridge.advance(460);+ assert.deepEqual(bridge.row.events, []);+ assert.equal(bridge.runtime.touchDragActive, false);+ assert.equal(bridge.row.draggable, "true");+});++test("database row gutters still drag without the block long-press delay", () => {+ const bridge = createBridge("", "protyle-gutters", true);+ bridge.row.parentElement.dataset.rowId = "row";+ bridge.dispatch("touchstart", bridge.row);+ assert.equal(bridge.dispatch("touchmove", bridge.row, 140, 50), true);+ assert.deepEqual(bridge.row.events, ["dragstart"]);+});++test("desktop touch block gutters keep direct dragging", () => {+ const bridge = createBridge("", "protyle-gutters");+ bridge.dispatch("touchstart", bridge.row);+ assert.equal(bridge.dispatch("touchmove", bridge.row, 140, 50), true);+ assert.deepEqual(bridge.row.events, ["dragstart"]);+});++test("mobile mouse block gutters keep direct dragging", () => {+ const bridge = createBridge("", "protyle-gutters", true);+ bridge.dispatch("pointerdown", bridge.row, 100, 0, true);+ assert.equal(bridge.dispatch("pointermove", bridge.row, 140, 50, true), true);+ assert.deepEqual(bridge.row.events, ["dragstart"]);+});diff --git a/app/src/util/touchDragBridge.ts b/app/src/util/touchDragBridge.tsindex d545d2dbb3..6b3584f9bf 100644--- a/app/src/util/touchDragBridge.ts+++ b/app/src/util/touchDragBridge.ts@@ -1,6 +1,7 @@import {stopScrollAnimation} from "../boot/globalEvent/dragover";import {Constants} from "../constants";import {isInAndroid} from "../plugin/platformUtils";+import {isMobile} from "./functions";/// #if !BROWSERimport {ipcRenderer} from "electron";/// #endif@@ -126,6 +127,7 @@ interface DragState {pendingDropSequence?: number;pendingDropTimeout?: number;restoreDraggable?: boolean;+ gutterLongPress?: {touchID: number, timer?: number, moved: boolean};}interface ForeignDragState {@@ -203,7 +205,12 @@ const updateResizePointerInput = (event: PointerEvent) => {// 触摸起始:先判断是否命中原生 Drag API(draggable="true"),命中则走原生路径;否则判断手动 mousedown 白名单const handleTouchStart = (e: TouchEvent) => {- if (e.touches.length !== 1) return;+ if (e.touches.length !== 1) {+ if (dragState?.gutterLongPress) {+ completeBridgeDrag(undefined, true);+ }+ return;+ }let target = e.target as HTMLElement;const touch = e.touches[0];@@ -236,6 +243,25 @@ const handleTouchStart = (e: TouchEvent) => {dragState = createDragState(draggable, target, touch, "touch", isMouseInput(touch));// WebKit 会接管 draggable 元素的长按并取消触摸序列,临时关闭原生拖拽以保留 touchend。suspendNativeDrag(dragState);+ if (!dragState.isMouse && isBlockGutter(draggable) && isMobile()) {+ const currentState = dragState;+ currentState.requireLongPress = true;+ currentState.lastPoint = copyDragPoint(touch);+ const longPress: DragState["gutterLongPress"] = {touchID: touch.identifier, moved: false};+ currentState.gutterLongPress = longPress;+ // 块标静止长按后直接显示选中块和拖拽影像,无需先移动手指。+ longPress.timer = window.setTimeout(() => {+ longPress.timer = undefined;+ if (dragState !== currentState || currentState.longPressCancelled || currentState.isDragging) {+ return;+ }+ if (!draggable.isConnected || document.hidden) {+ completeBridgeDrag(undefined, true);+ return;+ }+ startBridgeDrag(currentState.lastPoint || touch);+ }, Constants.TIMEOUT_LONGPRESS);+ }return;}}@@ -314,6 +340,19 @@ const handleTouchMove = (e: TouchEvent) => {// 原生 Drag 路径if (dragState) {const touch = e.touches[0];+ const longPress = dragState.gutterLongPress;+ if (longPress) {+ if (e.touches.length !== 1 || touch.identifier !== longPress.touchID) {+ completeBridgeDrag(undefined, true);+ return;+ }+ dragState.lastPoint = copyDragPoint(touch);+ if (Math.abs(touch.clientX - dragState.startX) >= Constants.SIZE_DRAG_THRESHOLD ||+ Math.abs(touch.clientY - dragState.startY) >= Constants.SIZE_DRAG_THRESHOLD) {+ longPress.moved = true;+ clearGutterLongPressTimer(dragState);+ }+ }if (!dragState.isDragging) {// 长按门槛:文件树、画廊、列表标记等触摸后短时间滑动视为滚动,放行原生滚动if (shouldYieldToScroll(dragState, touch.clientX, touch.clientY)) {@@ -355,10 +394,17 @@ const handleTouchMove = (e: TouchEvent) => {// 触摸结束:原生路径派发 drop/dragend,手动路径派发 mouseup 清理const handleTouchEnd = (e: TouchEvent) => {if (dragState) {- if (dragState.isDragging) {+ const longPress = dragState.gutterLongPress;+ const point = longPress ? Array.from(e.changedTouches).find(touch => touch.identifier === longPress.touchID) :+ e.changedTouches[0];+ if (!point) {+ return;+ }+ if (dragState.isDragging || longPress && dragState.longPressCancelled) {e.preventDefault();}- completeBridgeDrag(e.changedTouches[0], false);+ // 激活后未移动就松手只结束拖拽,不向原位置提交移动操作。+ completeBridgeDrag(point, !!longPress && (!longPress.moved || e.touches.length > 0));return;}if (!manualState) return;@@ -446,7 +492,7 @@ const armMouseClickSuppression = () => {suppressMouseClickTimeout = window.setTimeout(clearMouseClickSuppression);};-const isDesktopBlockGutter = (draggable: HTMLElement) => {+const isBlockGutter = (draggable: HTMLElement) => {const gutterElement = draggable.closest(".protyle-gutters");const buttonElement = draggable.parentElement;return !!gutterElement && buttonElement?.tagName === "BUTTON" && !buttonElement.dataset.rowId;@@ -469,7 +515,7 @@ const handlePointerDown = (event: PointerEvent) => {}const draggable = getDraggableAncestor(event.target);- if (!draggable || (!isInAndroid() && !isDesktopBlockGutter(draggable) &&+ if (!draggable || (!isInAndroid() && !isBlockGutter(draggable) &&!isDesktopFileTreeItem(draggable))) {return;}@@ -800,6 +846,7 @@ const completeBridgeDrag = (point: DragPoint | undefined, canceled: boolean, remconst cleanupDrag = () => {const completedState = dragState;dragState = null;+ clearGutterLongPressTimer(completedState);stopScrollAnimation();clearDragoverClasses();dragRefreshQueue.cancel();@@ -828,6 +875,13 @@ const cleanupDrag = () => {lastDragOverElement = null;};+const clearGutterLongPressTimer = (state: DragState | null) => {+ if (state?.gutterLongPress?.timer !== undefined) {+ window.clearTimeout(state.gutterLongPress.timer);+ state.gutterLongPress.timer = undefined;+ }+};+const resetLocalDragIdleTimeout = () => {const currentState = dragState;if (!currentState?.isDragging) {@@ -1173,6 +1227,9 @@ export const initTouchDragBridge = () => {document.addEventListener("keyup", handleDragKey, {capture: true});document.addEventListener("visibilitychange", handleVisibilityChange);window.addEventListener("blur", () => {+ if (dragState?.gutterLongPress) {+ completeBridgeDrag(undefined, true);+ }if (enablePointerBridge && dragState?.inputType === "pointer" &&shouldCancelPointerDragAfterWindowExit(!!isInAndroid(), dragState.isDragging,!!dragState.relayId)) {@@ -1186,7 +1243,7 @@ export const initTouchDragBridge = () => {// 触摸事件桥接:原生 Drag API(draggable="true")与手动 mousedown 拖拽(dock/outline/resize 把手)统一入口document.addEventListener("touchstart", handleTouchStart, {passive: false});document.addEventListener("touchmove", handleTouchMove, {passive: false});- document.addEventListener("touchend", handleTouchEnd);+ document.addEventListener("touchend", handleTouchEnd, {passive: false});document.addEventListener("touchcancel", handleCancel);document.addEventListener("contextmenu", handleContextMenu, {capture: true});};