Skip to content

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

:bug: Preserve mobile editor scrolling in multi-select mode https://github.com/siyuan-note/siyuan/issues/20682

Daniel8270a1848e02
diff --git a/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts b/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts
index fe60cfa81a..3113641f27 100644
--- a/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts
+++ b/app/src/protyle/wysiwyg/blockDragSelectionGesture.test.ts
@@ -134,14 +134,21 @@ test("new gestures preserve modifier clicks, right clicks, double clicks and han
});
test("ordinary touch scrolling never changes into block selection after waiting", () => {
- const f = fixture();
- f.touch("touchstart");
- assert.equal(f.touch("touchmove", 50).defaultPrevented, false);
- f.advance(2000);
- f.touch("touchmove", 220);
- f.touch("touchend", 220);
- assert.deepEqual(f.selections, []);
- assert.equal(f.document.count(), 0);
+ for (const multiSelect of [false, true]) {
+ const f = fixture();
+ if (multiSelect) {
+ f.enterMultiSelect();
+ }
+ assert.equal(f.touch("touchstart").defaultPrevented, false);
+ assert.equal(f.touch("touchmove", 50).defaultPrevented, false);
+ f.advance(2000);
+ assert.equal(f.touch("touchmove", 220).defaultPrevented, false);
+ assert.equal(f.touch("touchend", 220).defaultPrevented, false);
+ assert.deepEqual(f.selections, []);
+ assert.equal(f.document.count(), 0);
+ assert.equal(f.scrollElement.count(), 0);
+ assert.deepEqual(f.scrolls, []);
+ }
});
test("a sustained touch can cross block boundaries without changing a short native word selection", () => {
@@ -158,24 +165,39 @@ test("a sustained touch can cross block boundaries without changing a short nati
assert.equal(f.document.count(), 0);
});
-test("multi-select touch suppresses native scrolling and supports dragging after long-press activation", () => {
+test("multi-select touch supports dragging after long-press activation and keeps later taps native", () => {
const f = fixture();
f.touch("touchstart");
+ f.advance(1500);
f.enterMultiSelect();
assert.equal(f.touch("touchmove", 50).defaultPrevented, true);
f.touch("touchmove", 220);
f.touch("touchend", 220);
assert.deepEqual(f.selections, [["a", "a"], ["a", "c"]]);
- assert.equal(f.touch("touchstart").defaultPrevented, true);
+ assert.equal(f.touch("touchstart").defaultPrevented, false);
assert.equal(f.touch("touchend").defaultPrevented, false);
});
+test("a fresh touch in multi-select mode requires a sustained press before extending the selection", () => {
+ const f = fixture();
+ f.enterMultiSelect();
+ assert.equal(f.touch("touchstart").defaultPrevented, false);
+ f.advance(1500);
+ assert.equal(f.touch("touchmove", 50).defaultPrevented, true);
+ assert.equal(f.touch("touchmove", 220).defaultPrevented, true);
+ assert.equal(f.touch("touchend", 220).defaultPrevented, true);
+ assert.deepEqual(f.selections, [["a", "a"], ["a", "c"]]);
+ assert.deepEqual(f.finishes, [{source: "touch", cancelled: false}]);
+ assert.equal(f.document.count(), 0);
+});
+
test("multi-touch, native cancellation, lost buttons, escape, blur and disposal release gesture listeners", () => {
for (const end of ["multi-touch", "touchcancel", "buttons", "escape", "blur", "dispose", "disabled", "detached"]) {
const f = fixture();
if (end === "multi-touch" || end === "touchcancel") {
f.enterMultiSelect();
f.touch("touchstart");
+ f.advance(1500);
f.touch("touchmove", 120);
f.document.send(end === "multi-touch" ? "touchstart" : "touchcancel", {touches: [{}, {}]});
} else {
diff --git a/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts b/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts
index be7220aa8f..d85bf80ec4 100644
--- a/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts
+++ b/app/src/protyle/wysiwyg/blockDragSelectionGesture.ts
@@ -114,8 +114,8 @@ export const bindBlockDragSelectionGesture = (element: HTMLElement, getScrollEle
Math.abs(touch.clientY - gesture.point.clientY) < Constants.SIZE_DRAG_THRESHOLD) {
return;
}
- // 普通滑动交给浏览器滚动;多选模式或持续长按后的滑动才尝试扩选。
- if (!options.isMultiSelectMode() && Date.now() - gesture.time < Constants.TIMEOUT_MULTIPLE_SELECT) {
+ // 普通滑动交给浏览器滚动,多选模式下也需持续长按后才尝试扩选。
+ if (Date.now() - gesture.time < Constants.TIMEOUT_MULTIPLE_SELECT) {
cancel();
return;
}
@@ -179,10 +179,7 @@ export const bindBlockDragSelectionGesture = (element: HTMLElement, getScrollEle
return;
}
const touch = event.touches[0];
- if (start(event.target as HTMLElement, touch, "touch", touch.identifier) && options.isMultiSelectMode()) {
- // 多选模式中的触摸从按下时阻止原生滚动,松手未移动仍交给现有点选处理。
- event.preventDefault();
- }
+ start(event.target as HTMLElement, touch, "touch", touch.identifier);
};
const click = (event: MouseEvent) => {
if (Date.now() < suppressClickUntil) {
diff --git a/app/src/protyle/wysiwyg/blockDragSelectionScroll.test.ts b/app/src/protyle/wysiwyg/blockDragSelectionScroll.test.ts
new file mode 100644
index 0000000000..1d511ffa7d
--- /dev/null
+++ b/app/src/protyle/wysiwyg/blockDragSelectionScroll.test.ts
@@ -0,0 +1,110 @@
+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 {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+test("native touch scrolling preserves selected blocks while sustained dragging extends the range", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 30000,
+}, async () => {
+ const source = transpileModule(readFileSync("src/protyle/wysiwyg/blockDragSelectionGesture.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+ }).outputText;
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-block-selection-scroll-"));
+ const script = path.join(temporary, "run.cjs");
+ writeFileSync(script, `const assert = require("node:assert/strict");
+const {app, BrowserWindow} = require("electron");
+const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
+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: 400, height: 600,
+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true, backgroundThrottling: false}});
+ 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 evaluate = code => win.webContents.executeJavaScript(code);
+ await evaluate(
+ 'window.gestures = {}; new Function("exports", "require", ' + ${JSON.stringify(JSON.stringify(source))} +
+ ')(window.gestures, () => ({Constants: {SIZE_DRAG_THRESHOLD: 5, TIMEOUT_MULTIPLE_SELECT: 1500}}));'
+ );
+ const setup = async multi => {
+ await evaluate(
+ 'window.dispose?.(); document.body.innerHTML = ' + JSON.stringify(
+ '<style>body{margin:0}.scroll{height:400px;overflow:auto}.protyle-wysiwyg{user-select:none}' +
+ '.block{height:100px}.selected{background:#def}</style><div class="scroll">' +
+ '<div class="protyle-wysiwyg" contenteditable="false">' +
+ Array.from({length: 20}, (_, i) => '<div class="block' + (i < 2 ? ' selected' : '') +
+ '" data-id="' + i + '">Block ' + i + '</div>').join('') + '</div></div>'
+ ) + ';' +
+ 'window.root = document.querySelector(".protyle-wysiwyg");' +
+ 'window.scroller = document.querySelector(".scroll");' +
+ 'window.blocks = Array.from(root.children); window.selections = []; window.ends = [];' +
+ 'window.dispose = gestures.bindBlockDragSelectionGesture(root, () => scroller, {' +
+ 'canStart: () => true, getStartBlock: target => target.closest(".block"),' +
+ 'getBlockAtPoint: p => document.elementFromPoint(p.clientX, p.clientY)?.closest(".block"),' +
+ 'isMultiSelectMode: () => ' + multi + ',' +
+ 'select: (start, end) => { selections.push([start.dataset.id, end.dataset.id]);' +
+ 'const a = blocks.indexOf(start), b = blocks.indexOf(end);' +
+ 'blocks.forEach((block, i) => block.classList.toggle("selected", i >= Math.min(a,b) && i <= Math.max(a,b))); },' +
+ 'scroll: () => {}, finish: (source, cancelled) => ends.push({source, cancelled}) }); void 0;'
+ );
+ await delay(100);
+ };
+ const touch = (type, y) => win.webContents.debugger.sendCommand("Input.dispatchTouchEvent", {
+ type, touchPoints: type === "touchEnd" ? [] : [{x: 100, y, id: 1}],
+ });
+ const state = () => evaluate('({scroll: scroller.scrollTop, selections, ends,' +
+ ' selected: blocks.filter(block => block.classList.contains("selected")).map(block => block.dataset.id)})');
+ for (const multi of [false, true]) {
+ await setup(multi);
+ await touch("touchStart", 250);
+ for (const y of [230, 190, 150, 100]) {
+ await delay(30);
+ await touch("touchMove", y);
+ }
+ await touch("touchEnd");
+ await delay(150);
+ const result = await state();
+ assert.ok(result.scroll > 50, "a short swipe scrolls the editor in either selection mode");
+ assert.deepEqual(result.selections, []);
+ assert.deepEqual(result.ends, []);
+ assert.deepEqual(result.selected, ["0", "1"], "scrolling preserves the existing selection");
+ }
+ await setup(true);
+ await touch("touchStart", 50);
+ await delay(1600);
+ await touch("touchMove", 150);
+ await touch("touchMove", 250);
+ await touch("touchEnd");
+ const selection = await state();
+ assert.equal(selection.scroll, 0);
+ assert.deepEqual(selection.selected, ["0", "1", "2"]);
+ assert.ok(selection.selections.length > 0);
+ assert.deepEqual(selection.ends, [{source: "touch", cancelled: false}]);
+ console.log("Native block selection scrolling passed");
+ 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 {stdout} = await promisify(execFile)(require("electron") as unknown as string, [script],
+ {env, timeout: 25000, windowsHide: true});
+ assert.match(stdout, /Native block selection scrolling passed/);
+ } finally {
+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(tmpdir()));
+ assert.ok(path.basename(temporary).startsWith("siyuan-block-selection-scroll-"));
+ rmSync(temporary, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});
+ }
+});