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: Improve mind map palette scrolling https://github.com/siyuan-note/siyuan/issues/20856
Daniel7ccc5033eef7
diff --git a/app/src/assets/scss/protyle/_mindmap-view.scss b/app/src/assets/scss/protyle/_mindmap-view.scssindex 0a234c4474..e2d8f8680b 100644--- a/app/src/assets/scss/protyle/_mindmap-view.scss+++ b/app/src/assets/scss/protyle/_mindmap-view.scss@@ -1034,6 +1034,7 @@width: 100%;order: 0;scrollbar-width: none;+ overscroll-behavior: contain;scroll-snap-type: x mandatory;scroll-padding-left: max(0px, calc((100% / var(--mindmap-palette-columns) - 26px) / 2));diff --git a/app/src/protyle/render/listMindmap/paletteScroll.test.ts b/app/src/protyle/render/listMindmap/paletteScroll.test.tsnew file mode 100644index 0000000000..c7cfd97acb--- /dev/null+++ b/app/src/protyle/render/listMindmap/paletteScroll.test.ts@@ -0,0 +1,301 @@+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 {promisify} from "node:util";+import test from "node:test";+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";++const browserCases = async (source: string, css: string) => {+ const check = require("node:assert/strict");+ const style = document.createElement("style");+ style.textContent = css;+ document.head.append(style);+ const createElement = (tag: string, className: string) => {+ const element = document.createElement(tag);+ element.className = className;+ return element;+ };+ const View = new Function("createElement", source + "; return View;")(createElement);+ const frame = () => new Promise<void>(resolve => requestAnimationFrame(() => resolve()));+ const delay = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms));+ const until = async (condition: () => boolean, message: string) => {+ const deadline = performance.now() + 2000;+ while (!condition() && performance.now() < deadline) {+ await frame();+ }+ check.ok(condition(), message);+ };+ const make = () => {+ const host = createElement("div", "mindmap-view");+ host.style.cssText = "position:absolute;top:20px;left:20px;width:236px;height:140px";+ const palette = createElement("div", "mindmap-view__palette");+ for (let i = 0; i < 31; i++) {+ const button = createElement("button", "color__square color__square--compact");+ button.textContent = String(i);+ palette.append(button);+ }+ const manager = document.createElement("button");+ host.append(new View().makePaletteRow(palette, [manager]));+ document.body.append(host);+ const step = parseFloat(getComputedStyle(palette).gridTemplateColumns);+ const wheel = (x: number, y: number, options: WheelEventInit = {}) => {+ const event = new WheelEvent("wheel", {deltaX: x, deltaY: y, bubbles: true, cancelable: true, ...options});+ palette.dispatchEvent(event);+ return event;+ };+ const settled = () => until(() => !palette.hasAttribute("data-scrolling"), "scripted scrolling completes");+ const aligned = () => {+ const position = palette.scrollLeft;+ check.ok(Math.abs(position - Math.round(position / step) * step) < 1 ||+ Math.abs(position - (palette.scrollWidth - palette.clientWidth)) < 1, "a complete color column is visible");+ };+ return {host, palette, manager, step, wheel, settled, aligned};+ };++ // 小数位移在一帧内累计,反复小幅滚轮沿输入方向前进;移出色卡不会提前吸回。+ {+ const {host, palette, manager, step, wheel, settled, aligned} = make();+ const managerLeft = manager.getBoundingClientRect().left;+ let applied = 0;+ palette.addEventListener("click", () => applied++);+ for (let i = 0; i < 30; i++) {+ check.equal(wheel(0, .2).defaultPrevented, true);+ }+ await frame();+ check.ok(Math.abs(palette.scrollLeft - 6) < .6, "fine wheel input retains its accumulated displacement");+ palette.dispatchEvent(new PointerEvent("pointerleave", {pointerType: "mouse"}));+ check.equal(palette.hasAttribute("data-scrolling"), true, "leaving does not interrupt a wheel gesture");+ await settled();+ check.ok(Math.abs(palette.scrollLeft - step) < 1, "a short gesture advances to the next column");+ for (let i = 2; i <= 4; i++) {+ wheel(0, 13);+ await settled();+ check.ok(Math.abs(palette.scrollLeft - i * step) < 1, "short gestures do not repeatedly snap backwards");+ }+ wheel(0, -13);+ await settled();+ check.ok(Math.abs(palette.scrollLeft - 3 * step) < 1, "reverse wheel input returns one column");+ aligned();+ check.equal(manager.getBoundingClientRect().left, managerLeft, "settings remain fixed");+ check.equal(applied, 0, "browsing never applies a color");+ host.remove();+ }++ // 横向、斜向和 Shift 滚轮交给原生滚动,手势末尾的纵向抖动不会切换到脚本路径。+ {+ const {host, palette, wheel} = make();+ const outer = document.createElement("div");+ outer.style.cssText = "position:absolute;top:20px;left:20px;width:300px;height:180px;overflow:auto";+ const spacer = document.createElement("div");+ spacer.style.height = "120px";+ const tail = document.createElement("div");+ tail.style.height = "1000px";+ host.style.position = "relative";+ host.style.top = "0";+ host.style.left = "0";+ outer.append(spacer, host, tail);+ document.body.append(outer);+ outer.scrollTop = 120;+ for (const [x, y] of [[4, -5], [5, -4], [0, -3]]) {+ check.equal(wheel(x, y).defaultPrevented, false, "diagonal gestures retain their native horizontal direction");+ }+ await frame();+ check.equal(palette.scrollLeft, 0, "synthetic native gestures do not trigger scripted movement");+ check.equal(palette.hasAttribute("data-scrolling"), false);+ check.equal(wheel(0, 50, {shiftKey: true}).defaultPrevented, false);+ for (const modifier of ["ctrlKey", "metaKey"]) {+ check.equal(wheel(0, 50, {[modifier]: true}).defaultPrevented, false, "zoom input stays native");+ }+ const blocked: boolean[] = [];+ palette.addEventListener("wheel", event => blocked.push(event.defaultPrevented));+ const bounds = palette.getBoundingClientRect();+ await require("electron").ipcRenderer.invoke("palette-wheel", {+ type: "mouseWheel", x: Math.round(bounds.left + bounds.width / 2),+ y: Math.round(bounds.top + bounds.height / 2), deltaX: 60, deltaY: -4,+ });+ await until(() => palette.scrollLeft > 0, "native horizontal wheel input scrolls the real palette");+ check.ok(blocked.length > 0);+ check.ok(blocked.every(value => !value), "native wheel default actions remain enabled");+ check.equal(palette.hasAttribute("data-scrolling"), false);+ await delay(80);+ check.equal(outer.scrollTop, 120, "diagonal palette scrolling preserves the outer document position");+ await require("electron").ipcRenderer.invoke("palette-wheel", {+ type: "mouseWheel", x: Math.round(bounds.left + bounds.width / 2),+ y: Math.round(bounds.top + bounds.height / 2), deltaX: 0, deltaY: 4,+ });+ await delay(350);+ check.equal(outer.scrollTop, 120, "vertical jitter at the end of a native gesture stays inside the palette");+ outer.remove();+ }++ // 原生惯性滚动不受纵向滚轮的定时对齐干扰;新的输入可中断平滑停靠。+ {+ const {host, palette, step, wheel, settled} = make();+ wheel(0, 13);+ await frame();+ const samples = [palette.scrollLeft];+ while (palette.hasAttribute("data-scrolling")) {+ await frame();+ samples.push(palette.scrollLeft);+ check.ok(samples.length < 120, "smooth alignment completes");+ }+ check.ok(samples.some(value => value > 14 && value < step - 1), "alignment animates through intermediate positions");+ check.ok(samples.every(value => value >= 12), "short input is never pulled back to the starting column");+ wheel(0, 13);+ await until(() => palette.scrollLeft > step + 14, "the next alignment starts");+ const beforeReverse = palette.scrollLeft;+ wheel(0, -5);+ await frame();+ check.ok(palette.scrollLeft < beforeReverse, "new input interrupts the pending alignment");+ await settled();+ check.ok(Math.abs(palette.scrollLeft - step) < 1, "interrupted alignment follows the new input direction");+ wheel(0, 13);+ await frame();+ wheel(10, 0);+ check.equal(palette.hasAttribute("data-scrolling"), false, "horizontal input cancels scripted alignment");+ const nativePosition = palette.scrollLeft;+ await delay(650);+ check.equal(palette.scrollLeft, nativePosition, "a stale timer cannot change a native gesture");+ host.remove();+ }++ // 行、页单位及边界反向输入保持有效;键盘和触屏可接管尚未结束的滚动。+ for (const mode of [WheelEvent.DOM_DELTA_LINE, WheelEvent.DOM_DELTA_PAGE]) {+ const {host, palette, wheel, settled, aligned} = make();+ wheel(0, 1, {deltaMode: mode});+ await frame();+ const expected = mode === WheelEvent.DOM_DELTA_LINE ? 16 : palette.clientWidth;+ check.ok(Math.abs(palette.scrollLeft - expected) < 1);+ await settled();+ aligned();+ wheel(0, 10000);+ await frame();+ const end = palette.scrollLeft;+ wheel(0, -13);+ await frame();+ check.ok(palette.scrollLeft < end, "reverse input responds immediately at the end");+ await settled();+ aligned();+ wheel(0, -13);+ await frame();+ palette.dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowLeft", bubbles: true}));+ check.equal(palette.hasAttribute("data-scrolling"), false);+ const keyboardPosition = palette.scrollLeft;+ await delay(650);+ check.equal(palette.scrollLeft, keyboardPosition, "keyboard navigation cancels pending wheel work");+ wheel(0, -13);+ palette.dispatchEvent(new PointerEvent("pointerdown", {pointerType: "touch", pointerId: 1}));+ check.equal(palette.hasAttribute("data-scrolling"), false, "touch input restores native snapping");+ host.remove();+ }++ // 缺少 scrollend 的环境仍可完成对齐并继续使用下一次滚轮。+ {+ const {host, palette, step, wheel, settled} = make();+ palette.addEventListener("scrollend", event => event.stopImmediatePropagation(), {capture: true});+ wheel(0, 13);+ await settled();+ check.ok(Math.abs(palette.scrollLeft - step) < 1);+ wheel(0, 13);+ await settled();+ check.ok(Math.abs(palette.scrollLeft - 2 * step) < 1);+ host.remove();+ }++ // 系统要求减少动态效果时直接停靠,不播放补齐动画。+ {+ await require("electron").ipcRenderer.invoke("palette-motion", "reduce");+ const {host, palette, step, wheel} = make();+ wheel(0, 13);+ const samples: number[] = [];+ while (palette.hasAttribute("data-scrolling")) {+ await frame();+ samples.push(palette.scrollLeft);+ check.ok(samples.length < 120);+ }+ check.ok(Math.abs(palette.scrollLeft - step) < 1);+ check.equal(samples.some(value => value > 14 && value < step - 1), false,+ "reduced motion skips intermediate alignment positions");+ host.remove();+ await require("electron").ipcRenderer.invoke("palette-motion", "no-preference");+ }++ // 工具栏重绘或销毁后,待执行帧不再写入已移除的色卡。+ {+ const {host, palette, wheel} = make();+ let scrollCalls = 0;+ palette.scrollTo = () => scrollCalls++;+ wheel(0, 13);+ host.remove();+ await frame();+ await delay(650);+ check.equal(scrollCalls, 0);+ check.equal(palette.hasAttribute("data-scrolling"), false);+ }+ return "Palette scroll cases passed";+};++for (const scale of [1, 2]) {+ test(`palette scrolling preserves fine input, native gestures and smooth alignment at DPR ${scale}`, {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 30000,+ }, async () => {+ const file = createSourceFile("view.ts", readFileSync(path.join(__dirname, "view.ts"), "utf8"), ScriptTarget.Latest, true);+ const view = file.statements.find(item => isClassDeclaration(item) && item.name?.text === "ListMindmapView");+ assert.ok(view && isClassDeclaration(view));+ const method = view.members.find(item => item.name?.getText(file) === "makePaletteRow");+ assert.ok(method);+ const source = transpileModule(`class View { tooltip = {hidden: true}; ${method.getText(file)} }`, {+ compilerOptions: {target: ScriptTarget.ES2021},+ }).outputText;+ const css = ["business/_color.scss", "protyle/_mindmap-view.scss"].map(name =>+ require("sass").compile(path.resolve(__dirname, "../../../assets/scss", name),+ {logger: require("sass").Logger.silent}).css).join("\n");+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-palette-scroll-"));+ const script = path.join(temporary, "run.cjs");+ const browserSource = transpileModule(`(${browserCases.toString()})`, {+ compilerOptions: {target: ScriptTarget.ES2021},+ }).outputText.trim().replace(/^"use strict";\s*/, "").replace(/;$/, "");+ writeFileSync(script, `const {app, BrowserWindow, ipcMain} = require("electron");+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});+app.commandLine.appendSwitch("disable-gpu");+app.commandLine.appendSwitch("force-device-scale-factor", "${scale}");+app.whenReady().then(async () => {+ const win = new BrowserWindow({show: false, width: 600, height: 400,+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});+ win.webContents.debugger.attach("1.3");+ ipcMain.handle("palette-wheel", async (_event, input) => {+ await win.webContents.debugger.sendCommand("Input.dispatchMouseEvent", input);+ });+ ipcMain.handle("palette-motion", async (_event, value) => {+ await win.webContents.debugger.sendCommand("Emulation.setEmulatedMedia", {+ features: [{name: "prefers-reduced-motion", value}],+ });+ });+ try {+ await win.loadURL("data:text/html,<html><body></body></html>");+ console.log(await win.webContents.executeJavaScript(${JSON.stringify(+ `const __name = value => value; (${browserSource})(${JSON.stringify(source)}, ${JSON.stringify(css)})`)}));+ 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.match(result.stdout, /Palette scroll cases passed/);+ } finally {+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(tmpdir()));+ rmSync(temporary, {recursive: true, force: true});+ }+ });+}diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.tsindex 75c547d017..96a02b6626 100644--- a/app/src/protyle/render/listMindmap/view.ts+++ b/app/src/protyle/render/listMindmap/view.ts@@ -3921,16 +3921,64 @@ export class ListMindmapView {control.insertAdjacentHTML("beforeend",'<svg class="color__square-mark" aria-hidden="true"><use xlink:href="#iconSelect"></use></svg>');});- // 触屏沿用原生横向滚动,鼠标支持拖动和滚轮浏览;拖动完成后不触发色卡点击。+ // 触屏和横向滚轮沿用原生滚动,纵向滚轮累计小数位移;拖动完成后不触发色卡点击。let drag: {id: number, x: number, scroll: number, scale: number};let moved = false;+ let wheel: {position: number, direction: number, target?: number};+ let wheelFrame = 0;let wheelTimer: number;- const snap = () => {- if (!drag) {+ let settleTimer: number;+ let nativeWheelUntil = 0;+ const stopWheel = (restore = true) => {+ window.clearTimeout(wheelTimer);+ window.clearTimeout(settleTimer);+ cancelAnimationFrame(wheelFrame);+ wheelFrame = 0;+ if (wheel?.target !== undefined) {+ palette.scrollTo({left: palette.scrollLeft, behavior: "instant"});+ }+ wheel = undefined;+ if (restore && !drag) {palette.removeAttribute("data-scrolling");}};+ const snap = (direction = 0, position = palette.scrollLeft) => {+ if (drag || !palette.isConnected) {+ stopWheel();+ return;+ }+ cancelAnimationFrame(wheelFrame);+ wheelFrame = 0;+ const step = parseFloat(getComputedStyle(palette).gridTemplateColumns);+ if (!(step > 0)) {+ stopWheel();+ return;+ }+ // 纵向滚轮沿输入方向停靠,避免小于半列的滚动反复回到原列。+ const column = direction > 0 ? Math.ceil((position - .001) / step) :+ direction < 0 ? Math.floor((position + .001) / step) : Math.round(position / step);+ const target = Math.max(0, Math.min(palette.scrollWidth - palette.clientWidth, column * step));+ wheel = {position, direction, target};+ palette.setAttribute("data-scrolling", "");+ palette.scrollTo({left: target, behavior:+ window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "instant" : "smooth"});+ // 部分 WebView 没有 scrollend;超时后完成对齐并恢复原生吸附。+ settleTimer = window.setTimeout(() => {+ if (palette.isConnected) {+ palette.scrollLeft = target;+ }+ stopWheel();+ }, 500);+ };+ palette.addEventListener("scrollend", () => {+ if (wheel?.target !== undefined && Math.abs(palette.scrollLeft - wheel.target) < 1) {+ stopWheel();+ }+ });+ palette.addEventListener("keydown", () => stopWheel());palette.addEventListener("pointerdown", (event: PointerEvent) => {+ stopWheel();+ nativeWheelUntil = 0;moved = false;if (event.pointerType !== "mouse" || event.button !== 0 ||palette.scrollWidth <= palette.clientWidth) {@@ -3939,6 +3987,22 @@ export class ListMindmapView {drag = {id: event.pointerId, x: event.clientX, scroll: palette.scrollLeft,scale: palette.getBoundingClientRect().width / palette.offsetWidth || 1};});+ const finishDrag = (event: PointerEvent) => {+ if (drag?.id === event.pointerId) {+ drag = undefined;+ if (moved) {+ snap();+ }+ }+ };+ palette.addEventListener("pointerup", finishDrag);+ palette.addEventListener("pointercancel", finishDrag);+ palette.addEventListener("lostpointercapture", finishDrag);+ palette.addEventListener("pointerleave", () => {+ if (drag && !moved) {+ drag = undefined;+ }+ });palette.addEventListener("pointermove", (event: PointerEvent) => {if (!drag || event.pointerId !== drag.id) {return;@@ -3956,21 +4020,6 @@ export class ListMindmapView {palette.scrollLeft = drag.scroll - delta;event.preventDefault();});- const end = (event: PointerEvent) => {- if (drag?.id === event.pointerId) {- drag = undefined;- snap();- }- };- palette.addEventListener("pointerup", end);- palette.addEventListener("pointercancel", end);- palette.addEventListener("lostpointercapture", end);- palette.addEventListener("pointerleave", () => {- if (!moved) {- drag = undefined;- snap();- }- });palette.addEventListener("click", event => {if (moved && event.detail !== 0) {event.preventDefault();@@ -3980,18 +4029,45 @@ export class ListMindmapView {}, {capture: true});palette.addEventListener("wheel", (event: WheelEvent) => {if (event.ctrlKey || event.metaKey || palette.scrollWidth <= palette.clientWidth) {+ stopWheel();return;}- const delta = Math.abs(event.deltaX) > Math.abs(event.deltaY) ? event.deltaX : event.deltaY;- if (delta) {- palette.setAttribute("data-scrolling", "");- window.clearTimeout(wheelTimer);- const unit = event.deltaMode === WheelEvent.DOM_DELTA_LINE ? 16 :- event.deltaMode === WheelEvent.DOM_DELTA_PAGE ? palette.clientWidth : 1;- palette.scrollLeft += delta * unit;- wheelTimer = window.setTimeout(snap, 160);+ if (drag) {event.preventDefault();+ return;}+ // 同一横向手势即使末尾只剩纵向抖动,也由浏览器处理,避免轴向切换。+ if (event.deltaX || event.shiftKey || performance.now() < nativeWheelUntil) {+ nativeWheelUntil = performance.now() + 240;+ stopWheel();+ return;+ }+ if (!event.deltaY) {+ return;+ }+ if (!wheel || wheel.target !== undefined) {+ stopWheel(false);+ wheel = {position: palette.scrollLeft, direction: 0};+ palette.setAttribute("data-scrolling", "");+ }+ const unit = event.deltaMode === WheelEvent.DOM_DELTA_LINE ? 16 :+ event.deltaMode === WheelEvent.DOM_DELTA_PAGE ? palette.clientWidth : 1;+ wheel.position = Math.max(0, Math.min(palette.scrollWidth - palette.clientWidth,+ wheel.position + event.deltaY * unit));+ wheel.direction = Math.sign(event.deltaY);+ if (!wheelFrame) {+ wheelFrame = requestAnimationFrame(() => {+ wheelFrame = 0;+ if (!palette.isConnected) {+ stopWheel();+ } else if (wheel) {+ palette.scrollLeft = wheel.position;+ }+ });+ }+ window.clearTimeout(wheelTimer);+ wheelTimer = window.setTimeout(() => snap(wheel?.direction, wheel?.position), 160);+ event.preventDefault();}, {passive: false});const row = createElement("div", "mindmap-view__palette-row");row.style.setProperty("--mindmap-palette-columns", String(columns - controls.length));