Skip to content

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

:bug: Enable touch moving and resizing for block popovers https://github.com/siyuan-note/siyuan/issues/20754

Danielfb3ff73189d4
diff --git a/app/src/assets/scss/business/_resize.scss b/app/src/assets/scss/business/_resize.scss
index 3c449cc721..ce7b16954e 100644
--- a/app/src/assets/scss/business/_resize.scss
+++ b/app/src/assets/scss/business/_resize.scss
@@ -175,51 +175,59 @@
}
.body--touch-resize {
- // 扩大弹窗外侧的触摸把手,避免侵占内部按钮和滚动条。
- .b3-dialog {
- .resize__r {
- width: 14px;
- right: -14px;
- }
-
- .resize__l {
- width: 14px;
- left: -14px;
- }
-
- .resize__t {
- height: 14px;
- top: -14px;
- }
-
- .resize__d {
- height: 14px;
- bottom: -14px;
- }
-
- .resize__rd, .resize__ld, .resize__rt, .resize__lt {
- width: 24px;
- height: 24px;
- }
-
- .resize__rd {
- right: -16px;
- bottom: -16px;
- }
-
- .resize__ld {
- left: -16px;
- bottom: -16px;
- }
-
- .resize__rt {
- right: -16px;
- top: -16px;
- }
-
- .resize__lt {
- left: -16px;
- top: -16px;
- }
+ // 扩大弹窗和块浮窗外侧的触摸把手,避免侵占内部按钮和滚动条。
+ .b3-dialog .resize__r,
+ .block__popover > .resize__r {
+ width: 14px;
+ right: -14px;
+ }
+
+ .b3-dialog .resize__l,
+ .block__popover > .resize__l {
+ width: 14px;
+ left: -14px;
+ }
+
+ .b3-dialog .resize__t,
+ .block__popover > .resize__t {
+ height: 14px;
+ top: -14px;
+ }
+
+ .b3-dialog .resize__d,
+ .block__popover > .resize__d {
+ height: 14px;
+ bottom: -14px;
+ }
+
+ .b3-dialog .resize__rd, .b3-dialog .resize__ld, .b3-dialog .resize__rt, .b3-dialog .resize__lt,
+ .block__popover > .resize__rd, .block__popover > .resize__ld,
+ .block__popover > .resize__rt, .block__popover > .resize__lt {
+ width: 24px;
+ height: 24px;
+ }
+
+ .b3-dialog .resize__rd,
+ .block__popover > .resize__rd {
+ right: -16px;
+ bottom: -16px;
+ }
+
+ .b3-dialog .resize__ld,
+ .block__popover > .resize__ld {
+ left: -16px;
+ bottom: -16px;
+ }
+
+ .b3-dialog .resize__rt,
+ .block__popover > .resize__rt {
+ right: -16px;
+ top: -16px;
+ }
+
+ .b3-dialog .resize__lt,
+ .block__popover > .resize__lt {
+ left: -16px;
+ top: -16px;
}
}
diff --git a/app/src/block/panelResizeTouch.test.ts b/app/src/block/panelResizeTouch.test.ts
new file mode 100644
index 0000000000..a12cb95ccd
--- /dev/null
+++ b/app/src/block/panelResizeTouch.test.ts
@@ -0,0 +1,273 @@
+import * as assert from "node:assert/strict";
+import {execFile} from "node:child_process";
+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";
+import {tmpdir} from "node:os";
+import * as path from "node:path";
+import {test} from "node:test";
+import {promisify} from "node:util";
+import {compileString} from "sass";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import type {App} from "../index";
+
+const {parse} = require("ifdef-loader/preprocessor");
+
+const browserCases = (sources: string[], css: string, mobile: boolean) => {
+ const check: typeof assert = require("node:assert/strict");
+ const core = {};
+ new Function("exports", sources[1])(core);
+ const bridge = {} as typeof import("../util/touchDragBridge");
+ new Function("exports", "require", sources[0])(bridge, (id: string) => id === "./touchDragBridgeCore" ? core : {
+ Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_LONGPRESS: 500},
+ isInAndroid: () => false, isInHarmony: () => false, ipcRenderer: {on() {}}, stopScrollAnimation() {},
+ });
+ const resize = {} as typeof import("../dialog/moveResize");
+ const closest = (element: Element, name: string) => element?.closest("." + name);
+ const dependencies = {hasClosestByClassName: closest, getTopBarHeight: () => 32, hideAllElements() {}};
+ new Function("exports", "require", sources[2])(resize, () => dependencies);
+ let sequence = 0;
+ const panels = {} as typeof import("./Panel");
+ new Function("exports", "require", sources[3])(panels, (id: string) => id === "../dialog/moveResize" ? resize : {
+ ...dependencies,
+ genUUID: () => String(++sequence), registerBlockPanelOwner() {}, updateHotkeyAfterTip: () => "",
+ setPosition: (element: HTMLElement, x: number, y: number) => {
+ element.style.left = x + "px";
+ element.style.top = y + "px";
+ },
+ });
+ // 保留实际浮窗构造、标记、事件和固定回调,仅替换编辑器的数据加载。
+ Object.defineProperty(panels.BlockPanel.prototype, "initProtyle", {
+ value: (element: HTMLElement, after?: () => void) => {
+ element.innerHTML = '<div contenteditable="true">Editable block content</div>';
+ after?.();
+ },
+ });
+ window.siyuan = {blockPanels: [], zIndex: 0, touchDragActive: false, languages: {pin: "Pin", unpin: "Unpin"},
+ config: {keymap: {general: {closeTab: {custom: ""}}, editor: {general: {openInNewTab: {custom: ""}}}}},
+ menus: {menu: {element: document.createElement("div")}},
+ } as unknown as typeof window.siyuan;
+ const style = document.createElement("style");
+ style.textContent = css + `
+body {margin:0;background:white} .block__popover {transition:none;background:white}
+.resize__move {flex:1} .block__content {min-height:0} .block__edit {min-height:600px}
+.underlay {position:absolute;inset:0}
+`;
+ document.head.append(style);
+ document.body.innerHTML = '<div class="underlay" contenteditable="true">Background editor</div>';
+ bridge.initTouchDragBridge();
+ let panel: InstanceType<typeof panels.BlockPanel>;
+ const createPanel = () => {
+ panel?.destroy();
+ panel = new panels.BlockPanel({app: {} as App, refDefs: [{refID: "reference"}], isBacklink: false, x: 100, y: 100});
+ window.siyuan.blockPanels.push(panel);
+ Object.assign(panel.element.style, {width: "300px", height: "240px", borderWidth: "0"});
+ return panel.element;
+ };
+ const touch = (type: string, target: Element, x: number, y: number, count = 1) => {
+ const point = new Touch({identifier: 1, target, clientX: x, clientY: y, radiusX: 1, radiusY: 1});
+ const event = new TouchEvent(type, {bubbles: true, cancelable: true,
+ touches: type === "touchend" || type === "touchcancel" ? [] :
+ Array.from({length: count}, (_, index) => new Touch({...point, identifier: index + 1, target,
+ clientX: x, clientY: y, radiusX: 1, radiusY: 1})), changedTouches: [point]});
+ target.dispatchEvent(event);
+ return event;
+ };
+ const pointer = (type: string, pointerType = "mouse") => document.body.dispatchEvent(
+ new PointerEvent(type, {bubbles: true, pointerType, pointerId: 1}),
+ );
+ const center = (element: Element) => {
+ const rect = element.getBoundingClientRect();
+ return {x: rect.left + rect.width / 2, y: rect.top + rect.height / 2};
+ };
+ const clean = () => {
+ check.equal(document.onmousemove, null);
+ check.equal(document.onmouseup, null);
+ check.equal(document.ondragstart, null);
+ check.equal(window.siyuan.touchDragActive, false);
+ check.equal(document.querySelector(".touch-resize-active, [data-touch-resize-corner]"), null);
+ };
+ const directions = ["move", "r", "l", "t", "d", "rd", "ld", "rt", "lt"];
+ for (const direction of directions) {
+ const element = createPanel();
+ const handle = element.querySelector<HTMLElement>(".resize__" + direction);
+ const point = center(handle);
+ const x = direction.includes("l") ? 100 : point.x;
+ const y = direction.includes("t") ? 100 : point.y;
+ const dx = direction.includes("l") ? -40 : 40;
+ const dy = direction.includes("t") ? -30 : 30;
+ touch("touchstart", handle, x, y);
+ check.equal(typeof document.onmousemove, mobile ? "object" : "function", direction);
+ if (!mobile) {
+ check.equal(element.querySelector(".resize__r").getBoundingClientRect().width, 14);
+ check.equal(element.querySelector(".resize__rd").getBoundingClientRect().width, 24);
+ if (direction.length === 2) {
+ check.equal(element.querySelectorAll("[data-touch-resize-corner]").length, 2);
+ }
+ }
+ check.equal(touch("touchmove", handle, x + dx, y + dy).defaultPrevented, !mobile, direction);
+ touch("touchend", handle, x + dx, y + dy);
+ if (!mobile) {
+ check.equal(element.dataset.pin, "true", direction + " pins the actual panel");
+ check.equal(element.querySelector('[data-type="pin"] use').getAttribute("xlink:href"), "#iconUnpin");
+ check.equal(parseFloat(element.style.left), direction === "move" ? 140 : direction.includes("l") ? 60 : 100);
+ check.equal(parseFloat(element.style.top), direction === "move" ? 130 : direction.includes("t") ? 70 : 100);
+ check.equal(parseFloat(element.style.width), direction !== "move" && /[rl]/.test(direction) ? 340 : 300);
+ check.equal(parseFloat(element.style.height), direction !== "move" && /[td]/.test(direction) ? 270 : 240);
+ } else {
+ check.equal(element.dataset.pin, "false");
+ check.equal(element.style.left, "100px");
+ check.equal(element.style.width, "300px");
+ }
+ clean();
+ }
+ if (mobile) {
+ return "Block panel mobile boundary cases passed";
+ }
+
+ // 切换回鼠标后恢复细把手,鼠标移动和缩放仍走原有实现。
+ for (const direction of ["move", "rd"]) {
+ const element = createPanel();
+ pointer("pointermove");
+ check.equal(element.querySelector(".resize__r").getBoundingClientRect().width, 4);
+ check.equal(element.querySelector(".resize__rd").getBoundingClientRect().width, 8);
+ const handle = element.querySelector(".resize__" + direction);
+ const {x, y} = center(handle);
+ handle.dispatchEvent(new MouseEvent("mousedown", {bubbles: true, clientX: x, clientY: y}));
+ document.dispatchEvent(new MouseEvent("mousemove", {bubbles: true, clientX: x + 40, clientY: y + 30}));
+ document.dispatchEvent(new MouseEvent("mouseup", {bubbles: true}));
+ check.equal(element.dataset.pin, "true");
+ check.equal(direction === "move" ? element.style.left : element.style.width, direction === "move" ? "140px" : "340px");
+ clean();
+ }
+
+ for (const direction of ["move", "rd"]) {
+ for (const cancel of ["touchcancel", "blur", "visibilitychange", "multitouch"]) {
+ const element = createPanel();
+ const handle = element.querySelector(".resize__" + direction);
+ const {x, y} = center(handle);
+ touch("touchstart", handle, x, y);
+ touch("touchmove", handle, x + 20, y + 20);
+ check.ok(document.querySelector(".touch-resize-active"));
+ if (cancel === "blur") {
+ window.dispatchEvent(new Event("blur"));
+ } else if (cancel === "visibilitychange") {
+ Object.defineProperty(document, "hidden", {configurable: true, value: true});
+ document.dispatchEvent(new Event("visibilitychange"));
+ Reflect.deleteProperty(document, "hidden");
+ } else {
+ touch(cancel === "multitouch" ? "touchstart" : cancel, handle, x + 20, y + 20, 2);
+ }
+ clean();
+ document.dispatchEvent(new MouseEvent("mousemove", {bubbles: true, clientX: x + 100, clientY: y + 100}));
+ check.equal(direction === "move" ? element.style.left : element.style.width, direction === "move" ? "120px" : "320px");
+ }
+ }
+
+ let element = createPanel();
+ const move = element.querySelector(".resize__move");
+ touch("touchstart", move, 180, 120);
+ touch("touchend", move, 180, 120);
+ check.equal(element.dataset.pin, "false", "a tap does not pin the panel");
+ clean();
+ const content = element.querySelector<HTMLElement>("[contenteditable]");
+ content.insertAdjacentHTML("beforeend", '<span class="resize__move">Nested content</span>');
+ content.insertAdjacentHTML("beforeend", '<span class="resize__r">Nested handle</span>');
+ check.equal(content.lastElementChild.getBoundingClientRect().width, 4, "nested controls retain their hit area");
+ for (const target of [content, content.firstElementChild, element.querySelector('[data-type="pin"]'),
+ element.querySelector('[data-type="close"]')]) {
+ const {x, y} = center(target);
+ touch("touchstart", target, x, y);
+ check.equal(document.onmousemove, null, "content and controls do not start dragging");
+ check.equal(touch("touchmove", target, x, y + 30).defaultPrevented, false, "content keeps native scrolling");
+ touch("touchend", target, x, y + 30);
+ clean();
+ }
+ const selection = getSelection();
+ selection.selectAllChildren(content);
+ check.match(selection.toString(), /Editable block content/, "native text selection remains available");
+ selection.removeAllRanges();
+ (element.querySelector('[data-type="pin"]') as HTMLElement).click();
+ check.equal(element.dataset.pin, "true");
+ (element.querySelector('[data-type="pin"]') as HTMLElement).click();
+ check.equal(element.dataset.pin, "false");
+ (element.querySelector('[data-type="close"]') as HTMLElement).click();
+ check.equal(element.isConnected, false, "the close button still closes the panel");
+
+ // 留下实际浮窗,随后用 Chromium 输入接口验证原生触摸事件序列及首次扩大的外侧命中区。
+ element = createPanel();
+ pointer("pointermove");
+ return "Block panel touch cases passed";
+};
+
+for (const frontend of ["app", "browser", "mobile"]) {
+ test(`block panel touch moving and resizing preserves ${frontend} interactions`, {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 30000,
+ }, async () => {
+ const mobile = frontend === "mobile";
+ const sources = ["src/util/touchDragBridge.ts", "src/util/touchDragBridgeCore.ts",
+ "src/dialog/moveResize.ts", "src/block/Panel.ts"].map(file => transpileModule(
+ parse(readFileSync(file, "utf8"), {BROWSER: frontend !== "app", MOBILE: mobile}, false, true), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+ }).outputText);
+ const css = ["_block.scss", "_resize.scss"].map(file =>
+ compileString(readFileSync("src/assets/scss/business/" + file, "utf8")).css).join("\n");
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-panel-resize-touch-test-"));
+ const script = path.join(temporary, "run.cjs");
+ const expected = mobile ? "Block panel mobile boundary cases passed" : "Block panel touch cases passed";
+ writeFileSync(script, `const {app, BrowserWindow} = require("electron");
+const check = require("node:assert/strict");
+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
+app.commandLine.appendSwitch("disable-gpu");
+app.whenReady().then(async () => {
+ const win = new BrowserWindow({show:false, width:800, height:600,
+ webPreferences:{nodeIntegration:true, contextIsolation:false, offscreen:true}});
+ try {
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ win.webContents.debugger.attach("1.3");
+ await win.webContents.debugger.sendCommand("Emulation.setTouchEmulationEnabled", {enabled:true});
+ const result = await win.webContents.executeJavaScript(${JSON.stringify(
+ "const __name = value => value; try { (" + browserCases.toString() + ")(" +
+ JSON.stringify(sources) + "," + JSON.stringify(css) + "," + mobile + "); } catch (error) { error.stack; }")});
+ check.equal(result, ${JSON.stringify(expected)});
+ if (!${mobile}) {
+ const touch = (type, x, y) => win.webContents.debugger.sendCommand("Input.dispatchTouchEvent",
+ {type, touchPoints:type === "touchEnd" ? [] : [{x,y}]});
+ await touch("touchStart", 200, 120);
+ await touch("touchMove", 240, 150);
+ await touch("touchEnd");
+ check.deepEqual(await win.webContents.executeJavaScript(
+ '[document.querySelector(".block__popover").style.left, document.querySelector(".block__popover").dataset.pin, document.onmousemove === null]'), ["140px", "true", true]);
+ await win.webContents.executeJavaScript(
+ 'document.querySelector(".block__popover").style.left="100px"; document.querySelector(".block__popover").style.top="100px"; document.body.dispatchEvent(new PointerEvent("pointermove", {bubbles:true,pointerType:"mouse"}));');
+ check.equal(await win.webContents.executeJavaScript('document.elementFromPoint(410,220).className'), "underlay");
+ await touch("touchStart", 410, 220);
+ await touch("touchMove", 450, 220);
+ await touch("touchEnd");
+ check.deepEqual(await win.webContents.executeJavaScript(
+ '[document.querySelector(".block__popover").style.width, document.onmousemove === null, document.querySelector(".touch-resize-active") === null]'), ["340px", true, true]);
+ await touch("touchStart", 250, 280);
+ await touch("touchMove", 250, 180);
+ await touch("touchEnd");
+ await win.webContents.executeJavaScript('new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))');
+ check.equal(await win.webContents.executeJavaScript('document.querySelector(".block__content").scrollTop > 0'), true);
+ }
+ console.log(result);
+ win.destroy(); app.exit(0);
+ } catch (error) {
+ console.error(error); win.destroy(); app.exit(1);
+ }
+});`, "utf8");
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const result = await promisify(execFile)(require("electron") as unknown as string, [script],
+ {env, timeout: 25000, windowsHide: true});
+ assert.ok(result.stdout.includes(expected));
+ } finally {
+ if (path.dirname(path.resolve(temporary)) === path.resolve(tmpdir()) &&
+ path.basename(temporary).startsWith("siyuan-panel-resize-touch-test-")) {
+ rmSync(temporary, {recursive: true, force: true});
+ }
+ }
+ });
+}
diff --git a/app/src/util/touchDragBridge.ts b/app/src/util/touchDragBridge.ts
index 04472de4b3..009462080f 100644
--- a/app/src/util/touchDragBridge.ts
+++ b/app/src/util/touchDragBridge.ts
@@ -190,6 +190,11 @@ let touchResizeHandle: HTMLElement | null = null;
let touchResizeEdges: HTMLElement[] = [];
let touchResizeInput = false;
+/// #if !MOBILE
+const blockPanelResizeSelector = ["rd", "ld", "rt", "lt", "r", "l", "t", "d"]
+ .map(direction => `.block__popover > .resize__${direction}`).join(", ");
+/// #endif
+
const setTouchResizeInput = (touch: boolean) => {
if (touchResizeInput === touch) {
return;
@@ -210,6 +215,9 @@ const handleTouchStart = (e: TouchEvent) => {
if (dragState?.gutterLongPress) {
completeBridgeDrag(undefined, true);
}
+ if (touchResizeHandle) {
+ cancelManualTouch();
+ }
return;
}
@@ -227,6 +235,19 @@ const handleTouchStart = (e: TouchEvent) => {
}
}
+ let blockPanelHandle: HTMLElement | null = null;
+ /// #if !MOBILE
+ if (!isMouseInput(touch) && !target.closest(".block__popover")) {
+ // 首次触摸扩大外侧把手后重新命中,浮窗内部的正文和按钮仍使用原始目标。
+ target = document.elementFromPoint(touch.clientX, touch.clientY)?.closest<HTMLElement>(
+ blockPanelResizeSelector,
+ ) || target;
+ }
+ blockPanelHandle = target.closest<HTMLElement>(
+ blockPanelResizeSelector + ", .block__popover > .block__icons > .resize__move",
+ );
+ /// #endif
+
// 部分 Android WebView 会在鼠标 Pointer 事件后继续合成 Touch 事件,此时沿用候选状态并切换为 Touch 驱动。
if (dragState?.inputType === "pointer" && isMouseInput(touch)) {
dragState.inputType = "touch";
@@ -276,7 +297,7 @@ const handleTouchStart = (e: TouchEvent) => {
return;
}
// 手动 mousedown 路径:dock / outline / resize 把手等自实现拖拽的区域
- if (!target.closest(".dock") &&
+ if (!blockPanelHandle && !target.closest(".dock") &&
// 弹窗内不能按整个 .b3-dialog 匹配,否则导致闪卡文本扩选失效 https://github.com/siyuan-note/siyuan/issues/18055
!(target.closest(".b3-dialog") && ["resize__move", "resize__rd", "resize__r", "resize__rt",
"resize__d", "resize__l", "resize__ld", "resize__lt", "resize__t"].some(cls => target.closest("." + cls))) &&
@@ -307,7 +328,7 @@ const handleTouchStart = (e: TouchEvent) => {
target.dispatchEvent(mouseEvent);
// 仅在触摸命中可操作边界后提示,不改变鼠标反馈或内容区的长按行为。
if (!isMouseInput(touch) && document.onmousemove) {
- touchResizeHandle = target.closest<HTMLElement>(
+ touchResizeHandle = blockPanelHandle || target.closest<HTMLElement>(
".layout__resize, .layout__dockresize, " +
".b3-dialog .resize__rd, .b3-dialog .resize__ld, .b3-dialog .resize__rt, " +
".b3-dialog .resize__lt, .b3-dialog .resize__r, .b3-dialog .resize__l, " +
@@ -1183,6 +1204,9 @@ const handleVisibilityChange = () => {
}
completeBridgeDrag(undefined, true);
cleanupForeignDrag("end");
+ if (touchResizeHandle) {
+ cancelManualTouch();
+ }
};
const handleCancel = () => {