Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/18300

Vanessa21dc76879392
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index 6d858e871c..f1c2e9af69 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -27,6 +27,7 @@ import {
expandFileTree,
isFileTreeCollapsing
} from "../../layout/dock/fileTreeAnimation";
+import {bindMousePointerTouchBridge, isMousePointerTouchEvent} from "../util/mousePointerTouchBridge";
export class MobileFiles extends Model {
public element: HTMLElement;
@@ -263,7 +264,7 @@ export class MobileFiles extends Model {
startX: touch.clientX,
startY: touch.clientY,
ghostElement: null,
- startTime: Date.now(),
+ startTime: Date.now() - (isMousePointerTouchEvent(event) ? Constants.TIMEOUT_LONGPRESS : 0),
};
}, {passive: false});
@@ -493,6 +494,7 @@ export class MobileFiles extends Model {
this.clearDragIndicators();
this.touchDragState = null;
});
+ bindMousePointerTouchBridge(filesElement);
this.init();
}
diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.ts
index eea8609f88..fb7318ff7e 100644
--- a/app/src/mobile/dock/MobileOutline.ts
+++ b/app/src/mobile/dock/MobileOutline.ts
@@ -18,6 +18,7 @@ import {getDocDisplayName, isEncryptedBox} from "../../util/pathName";
import {dragOverScroll, stopScrollAnimation} from "../../boot/globalEvent/dragover";
import {escapeHtml} from "../../util/escape";
import {unicode2Emoji} from "../../emoji";
+import {bindMousePointerTouchBridge, isMousePointerTouchEvent} from "../util/mousePointerTouchBridge";
export class MobileOutline extends Model {
public tree: Tree;
@@ -226,7 +227,7 @@ export class MobileOutline extends Model {
startY: touch.clientY,
isDragging: false,
ghostElement: null,
- startTime: Date.now(),
+ startTime: Date.now() - (isMousePointerTouchEvent(event) ? Constants.TIMEOUT_LONGPRESS : 0),
selectItem: null,
};
}, {passive: false});
@@ -377,6 +378,7 @@ export class MobileOutline extends Model {
this.clearDragIndicators();
this.touchDragState = null;
});
+ bindMousePointerTouchBridge(this.element);
}
// 清理拖拽指示线
diff --git a/app/src/mobile/util/mousePointerTouchBridge.ts b/app/src/mobile/util/mousePointerTouchBridge.ts
new file mode 100644
index 0000000000..c542d0a54b
--- /dev/null
+++ b/app/src/mobile/util/mousePointerTouchBridge.ts
@@ -0,0 +1,137 @@
+import {isInAndroid} from "../../protyle/util/compatibility";
+
+type MousePointerTouchEvent = TouchEvent & {
+ siyuanMousePointer?: boolean;
+};
+
+interface TouchPoint {
+ clientX: number;
+ clientY: number;
+}
+
+export const isMousePointerTouchEvent = (event: TouchEvent) => {
+ return (event as MousePointerTouchEvent).siyuanMousePointer === true;
+};
+
+// 将移动端 Dock 内的鼠标 Pointer 事件局部转换为 Touch-like 事件,以复用已有的触摸拖拽逻辑。
+export const bindMousePointerTouchBridge = (element: Element) => {
+ if (!isInAndroid()) {
+ return;
+ }
+
+ let pointerId: number | null = null;
+ let pointerTarget: EventTarget | null = null;
+ let captureElement: Element | null = null;
+ let lastPoint: TouchPoint = {clientX: 0, clientY: 0};
+ let isDragging = false;
+ let suppressClick = false;
+
+ const dispatchTouchEvent = (type: "touchstart" | "touchmove" | "touchend" | "touchcancel", point: TouchPoint) => {
+ lastPoint = point;
+ const touch = {
+ clientX: point.clientX,
+ clientY: point.clientY,
+ target: pointerTarget,
+ } as Touch;
+ const activeTouches = type === "touchend" || type === "touchcancel" ? [] : [touch];
+ const touchEvent = new Event(type, {cancelable: true}) as MousePointerTouchEvent;
+ Object.defineProperties(touchEvent, {
+ touches: {value: activeTouches},
+ targetTouches: {value: activeTouches},
+ changedTouches: {value: [touch]},
+ });
+ touchEvent.siyuanMousePointer = true;
+ element.dispatchEvent(touchEvent);
+ return touchEvent.defaultPrevented;
+ };
+
+ const resetPointer = () => {
+ const activePointerId = pointerId;
+ pointerId = null;
+ pointerTarget = null;
+ isDragging = false;
+ if (activePointerId !== null && captureElement?.hasPointerCapture(activePointerId)) {
+ captureElement.releasePointerCapture(activePointerId);
+ }
+ captureElement = null;
+ };
+
+ const finishPointer = (type: "touchend" | "touchcancel", point: TouchPoint, event?: PointerEvent) => {
+ if (pointerId === null) {
+ return;
+ }
+ const dragged = isDragging;
+ dispatchTouchEvent(type, point);
+ resetPointer();
+ if (dragged) {
+ event?.preventDefault();
+ suppressClick = true;
+ setTimeout(() => {
+ suppressClick = false;
+ });
+ }
+ };
+
+ element.addEventListener("pointerdown", (event: PointerEvent) => {
+ if (event.pointerType !== "mouse" || event.button !== 0 || pointerId !== null) {
+ return;
+ }
+ pointerId = event.pointerId;
+ pointerTarget = event.target;
+ lastPoint = event;
+ captureElement = event.target instanceof Element ? event.target : element;
+ captureElement.setPointerCapture(event.pointerId);
+ dispatchTouchEvent("touchstart", event);
+ });
+
+ element.addEventListener("pointermove", (event: PointerEvent) => {
+ if (pointerId !== event.pointerId) {
+ return;
+ }
+ if ((event.buttons & 1) === 0) {
+ finishPointer("touchend", event, event);
+ return;
+ }
+ if (dispatchTouchEvent("touchmove", event)) {
+ isDragging = true;
+ event.preventDefault();
+ }
+ }, {passive: false});
+
+ element.addEventListener("pointerup", (event: PointerEvent) => {
+ if (pointerId === event.pointerId) {
+ finishPointer("touchend", event, event);
+ }
+ }, {passive: false});
+
+ element.addEventListener("pointercancel", (event: PointerEvent) => {
+ if (pointerId === event.pointerId) {
+ finishPointer("touchcancel", event);
+ }
+ });
+
+ element.addEventListener("lostpointercapture", (event: PointerEvent) => {
+ if (pointerId === event.pointerId) {
+ finishPointer("touchcancel", lastPoint);
+ }
+ });
+
+ element.addEventListener("click", (event) => {
+ if (suppressClick) {
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ suppressClick = false;
+ }
+ }, {capture: true});
+
+ const blockDuplicatedTouch = (event: TouchEvent) => {
+ if (pointerId !== null && !isMousePointerTouchEvent(event)) {
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ }
+ };
+ element.addEventListener("touchstart", blockDuplicatedTouch, {capture: true, passive: false});
+ element.addEventListener("touchmove", blockDuplicatedTouch, {capture: true, passive: false});
+ element.addEventListener("touchend", blockDuplicatedTouch, {capture: true, passive: false});
+ element.addEventListener("touchcancel", blockDuplicatedTouch, {capture: true, passive: false});
+};