An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve drag modifier keys on HarmonyOS https://github.com/siyuan-note/siyuan/issues/20570
Daniel1f7be54ba8c8
diff --git a/app/src/util/touchDragBridge.test.ts b/app/src/util/touchDragBridge.test.tsindex 6153555c3c..f6d743a6d0 100644--- a/app/src/util/touchDragBridge.test.ts+++ b/app/src/util/touchDragBridge.test.ts@@ -5,9 +5,12 @@ 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;+const {parse} = require("ifdef-loader/preprocessor");+const source = readFileSync("src/util/touchDragBridge.ts", "utf8");+const compiled = (browser: boolean, mobile: boolean) => transpileModule(+ parse(source, {BROWSER: browser, MOBILE: mobile}, false, true), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+ }).outputText;class TestElement {draggable = "false";@@ -15,6 +18,8 @@ class TestElement {tagName = "SPAN";dataset: Record<string, string> = {};events: string[] = [];+ dragEvents: DragEvent[] = [];+ pointerCaptures = new Set<number>();classList = {contains: (name: string) => this.classes.includes(name), toggle: (name: string, active: boolean) => {this.classes = this.classes.filter(value => value !== name);if (active) {@@ -38,21 +43,27 @@ class TestElement {}}- dispatchEvent(event: {type: string}) {+ dispatchEvent(event: DragEvent) {this.events.push(event.type);+ this.dragEvents.push(event);return true;}- setPointerCapture() {}+ setPointerCapture(id: number) { this.pointerCaptures.add(id); }+ hasPointerCapture(id: number) { return this.pointerCaptures.has(id); }+ releasePointerCapture(id: number) { this.pointerCaptures.delete(id); }}class TestHTMLElement extends TestElement {}-const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobile = false) => {+const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobile = false,+ options: {browser?: boolean, container?: string} = {}) => {let now = 1000;let hidden = false;let nextTimer = 0;const timers = new Map<number, {callback: () => void, time: number}>();+ let nextFrame = 0;+ const frames = new Map<number, FrameRequestCallback>();const listeners = new Map<string, Array<(event: object) => void>>();const body = new TestHTMLElement();const panel = new TestHTMLElement([panelClass], body);@@ -62,17 +73,19 @@ const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobi}const row = new TestHTMLElement([rowClass], button);row.draggable = "true";+ let dropTarget: TestElement | null = row;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, {+ runInNewContext(compiled(!!options.browser, mobile), {exports,require: (id: string) => id === "./touchDragBridgeCore" ? core : {Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_LONGPRESS: 460, TIMEOUT_MOUSE_DRAG_DELAY: 150},- isInAndroid: () => false,+ isInAndroid: () => options.container === "android",+ isInHarmony: () => options.container === "harmony",isMobile: () => mobile,ipcRenderer: {on() {}},stopScrollAnimation() {},@@ -82,7 +95,7 @@ const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobiHTMLElement: TestHTMLElement,DataTransfer: class { types: string[] = []; },DragEvent: class {- constructor(public type: string) {}+ constructor(public type: string, options: DragEventInit) { Object.assign(this, options); }},window: {siyuan: runtime,@@ -94,16 +107,25 @@ const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobireturn nextTimer;},clearTimeout: (id: number) => timers.delete(id),+ requestAnimationFrame: (callback: FrameRequestCallback) => {+ frames.set(++nextFrame, callback);+ return nextFrame;+ },+ cancelAnimationFrame: (id: number) => frames.delete(id),},document: {body,get hidden() { return hidden; },- documentElement: {addEventListener() {}},+ documentElement: {+ addEventListener: (type: string, listener: (event: object) => void) => {+ listeners.set(type, [...listeners.get(type) || [], listener]);+ },+ },addEventListener: (type: string, listener: (event: object) => void) => {listeners.set(type, [...listeners.get(type) || [], listener]);},querySelectorAll: (): Element[] => [],- elementFromPoint: () => row,+ elementFromPoint: () => dropTarget,},});exports.initTouchDragBridge();@@ -121,14 +143,18 @@ const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobi}now = end;};- const dispatch = (type: string, target: TestElement, y = 100, elapsed = 0, mouse = false, touchCount = 1) => {- advance(elapsed);+ const dispatch = (type: string, target: TestElement, y = 100, elapsed = 0, mouse = false, touchCount = 1,+ overrides: Partial<PointerEvent & KeyboardEvent> = {}) => {+ if (elapsed > 0) {+ advance(elapsed);+ }if (type === "visibilitychange") {hidden = true;}const point = {identifier: 1, clientX: 80, clientY: y, radiusX: mouse ? 0 : 1};const event = {...point,+ type,target,touches: type === "touchend" || type === "touchcancel" ? [] :Array.from({length: touchCount}, (_, index) => ({...point, identifier: index + 1})),@@ -138,12 +164,24 @@ const createBridge = (rowClass = "b3-menu__item", panelClass = "av__panel", mobibutton: 0,buttons: 1,pointerId: 1,+ altKey: false,+ ctrlKey: false,+ metaKey: false,+ shiftKey: false,preventDefault() { this.defaultPrevented = true; },+ stopImmediatePropagation() {},+ ...overrides,};listeners.get(type)?.forEach(listener => listener(event));return event.defaultPrevented;};- return {row, label, handleIcon, runtime, dispatch, advance};+ const flushFrames = () => {+ const callbacks = Array.from(frames.values());+ frames.clear();+ callbacks.forEach(callback => callback(now));+ };+ return {row, label, handleIcon, runtime, dispatch, advance, flushFrames,+ setDropTarget: (target: TestElement | null) => dropTarget = target};};for (const offset of [-80, 80]) {@@ -339,3 +377,149 @@ test("mobile mouse block gutters keep direct dragging", () => {assert.equal(bridge.dispatch("pointermove", bridge.row, 140, 50, true), true);assert.deepEqual(bridge.row.events, ["dragstart"]);});++for (const container of ["harmony", "android", "std", "ios"]) {+ test(`${container} browser build selects its mouse drag path`, () => {+ const bridge = createBridge("b3-list-item", "sy__file", false, {browser: true, container});+ bridge.row.dataset.type = "navigation-file";+ bridge.dispatch("pointerdown", bridge.label, 100, 0, true);+ const bridged = container === "harmony" || container === "android";+ assert.equal(bridge.row.draggable, bridged ? "false" : "true");+ bridge.dispatch("pointermove", bridge.label, 140, 500, true);+ assert.equal(bridge.runtime.touchDragActive, bridged);+ bridge.dispatch("pointercancel", bridge.label, 140, 0, true);+ assert.equal(bridge.row.draggable, "true");+ });+}++const createHarmonyPointerBridge = (gutter = false, mobile = false) => {+ const bridge = createBridge(gutter ? "" : "b3-list-item", gutter ? "protyle-gutters" : "sy__file",+ mobile, {browser: true, container: "harmony"});+ bridge.row.dataset.type = "navigation-file";+ return {+ ...bridge,+ pointer: (type: string, altKey: boolean, y = 100, overrides: Partial<PointerEvent & KeyboardEvent> = {}) =>+ bridge.dispatch(type, bridge.label, y, 0, true, 1, {altKey, ...overrides}),+ };+};++for (const [name, gutter, mobile] of [+ ["document tree", false, false],+ ["desktop block gutter", true, false],+ ["mobile block gutter", true, true],+] as const) {+ for (const [startAlt, endAlt] of [[true, true], [true, false], [false, true], [false, false]]) {+ test(`Harmony ${name} keeps live Alt ${startAlt} to ${endAlt} through drop`, () => {+ const bridge = createHarmonyPointerBridge(gutter, mobile);+ bridge.pointer("pointerdown", startAlt);+ bridge.pointer("pointermove", startAlt, 140);+ assert.equal(bridge.row.dragEvents[0].type, "dragstart");+ assert.equal(bridge.row.dragEvents[0].altKey, startAlt);+ assert.equal(bridge.runtime.touchDragActive, true);++ // 鼠标静止时也要根据按键变化刷新拖拽提示和落点。+ bridge.pointer(endAlt ? "keydown" : "keyup", endAlt, 140, {key: "Alt"});+ bridge.flushFrames();+ const overs = bridge.row.dragEvents.filter(event => event.type === "dragover");+ assert.equal(overs[overs.length - 1].altKey, endAlt);+ bridge.pointer("pointerup", endAlt, 160);+ bridge.pointer("pointerup", endAlt, 160);+ const drops = bridge.row.dragEvents.filter(event => event.type === "drop");+ assert.equal(drops.length, 1);+ assert.equal(drops[0].altKey, endAlt);+ assert.equal(bridge.row.dragEvents.filter(event => event.type === "dragend").length, 1);+ assert.equal(drops[0].dataTransfer, bridge.row.dragEvents[0].dataTransfer);+ assert.equal(bridge.row.draggable, "true");+ assert.equal(bridge.row.pointerCaptures.size, 0);+ assert.equal(bridge.runtime.touchDragActive, false);+ assert.equal(bridge.pointer("click", endAlt), true);+ assert.equal(bridge.pointer("click", endAlt), false);+ });+ }+}++test("Harmony pointerup uses the released Alt state even without another movement", () => {+ const bridge = createHarmonyPointerBridge();+ bridge.pointer("pointerdown", true);+ bridge.pointer("pointermove", true, 140);+ bridge.pointer("pointerup", false, 140);+ assert.equal(bridge.row.dragEvents.find(event => event.type === "drop").altKey, false);+});++for (const cancel of ["blur", "pointercancel", "lostpointercapture", "visibilitychange", "escape", "released buttons"]) {+ test(`Harmony ${cancel} cancels dragging and restores the next gesture`, () => {+ const bridge = createHarmonyPointerBridge();+ bridge.pointer("pointerdown", true);+ bridge.pointer("pointermove", true, 140);+ if (cancel === "escape") {+ bridge.pointer("keydown", true, 140, {key: "Escape"});+ } else if (cancel === "released buttons") {+ bridge.pointer("pointermove", true, 140, {buttons: 0});+ } else {+ bridge.pointer(cancel, true, 140);+ }+ bridge.flushFrames();+ bridge.pointer("pointerup", true, 140);+ assert.equal(bridge.row.events.includes("drop"), false);+ assert.equal(bridge.row.events.filter(type => type === "dragend").length, 1);+ assert.equal(bridge.row.draggable, "true");+ assert.equal(bridge.row.pointerCaptures.size, 0);+ assert.equal(bridge.runtime.touchDragActive, false);++ bridge.pointer("pointerdown", false);+ bridge.pointer("pointermove", false, 140);+ bridge.pointer("pointerup", false, 140);+ assert.equal(bridge.row.events.filter(type => type === "drop").length, 1);+ });+}++test("Harmony dragging outside a drop target cleans up without a drop", () => {+ const bridge = createHarmonyPointerBridge();+ bridge.pointer("pointerdown", true);+ bridge.pointer("pointermove", true, 140);+ bridge.setDropTarget(null);+ bridge.pointer("pointerup", true, 200);+ assert.equal(bridge.row.events.includes("drop"), false);+ assert.equal(bridge.row.draggable, "true");+ assert.equal(bridge.runtime.touchDragActive, false);+});++test("Harmony pending mouse gestures keep clicks and cancel on leaving the window", () => {+ const bridge = createHarmonyPointerBridge();+ bridge.pointer("pointerdown", false);+ bridge.pointer("pointerup", false, 102);+ assert.deepEqual(bridge.row.events, []);+ assert.equal(bridge.pointer("click", false), false);+ bridge.pointer("pointerdown", false);+ bridge.pointer("pointerleave", false);+ assert.equal(bridge.row.draggable, "true");+ assert.equal(bridge.runtime.touchDragActive, false);+});++test("Harmony only bridges internal document tree and ordinary block gutter mouse sources", () => {+ for (const [rowClass, panelClass] of [["b3-menu__item", "av__panel"], ["item", "layout-tab-bar"], ["", "protyle-gutters"]]) {+ const bridge = createBridge(rowClass, panelClass, false, {browser: true, container: "harmony"});+ if (panelClass === "protyle-gutters") {+ bridge.row.parentElement.dataset.rowId = "row";+ }+ bridge.dispatch("pointerdown", bridge.label, 100, 0, true);+ bridge.dispatch("pointermove", bridge.label, 140, 500, true);+ assert.equal(bridge.row.draggable, "true");+ assert.deepEqual(bridge.row.events, []);+ }+});++test("Harmony finger scrolling in the file tree preserves its long-press behavior", () => {+ const bridge = createBridge("b3-list-item", "sy__file", false, {browser: true, container: "harmony"});+ bridge.row.dataset.type = "navigation-file";+ bridge.dispatch("pointerdown", bridge.label);+ 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.equal(bridge.runtime.touchDragActive, true);+ bridge.dispatch("touchcancel", bridge.label);+ assert.equal(bridge.row.draggable, "true");+});diff --git a/app/src/util/touchDragBridge.ts b/app/src/util/touchDragBridge.tsindex 6b3584f9bf..54fc25d1d9 100644--- a/app/src/util/touchDragBridge.ts+++ b/app/src/util/touchDragBridge.ts@@ -1,6 +1,6 @@import {stopScrollAnimation} from "../boot/globalEvent/dragover";import {Constants} from "../constants";-import {isInAndroid} from "../plugin/platformUtils";+import {isInAndroid, isInHarmony} from "../protyle/util/compatibility";import {isMobile} from "./functions";/// #if !BROWSERimport {ipcRenderer} from "electron";@@ -1202,7 +1202,7 @@ export const initTouchDragBridge = () => {document.addEventListener("pointerover", updateResizePointerInput, {capture: true, passive: true});document.addEventListener("pointermove", updateResizePointerInput, {capture: true, passive: true});- let enablePointerBridge = !!isInAndroid();+ let enablePointerBridge = !!(isInAndroid() || isInHarmony());/// #if !BROWSERenablePointerBridge = true;ipcRenderer.on(Constants.SIYUAN_BLOCK_DRAG, (event, message: BlockDragRelayMessage) => {@@ -1210,7 +1210,7 @@ export const initTouchDragBridge = () => {});/// #endifif (enablePointerBridge) {- // Android 外接鼠标、桌面普通块标和文档树由 Pointer 路径驱动,避免原生 dragstart 取消事件流。+ // Android 外接鼠标、鸿蒙和桌面普通块标及文档树由 Pointer 路径驱动,保留实时修饰键和事件流。document.addEventListener("pointerdown", handlePointerDown, {capture: true, passive: true});document.addEventListener("pointermove", handlePointerMove, {capture: true, passive: false});document.addEventListener("pointerup", handlePointerUp, {capture: true, passive: false});