Skip to content

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

:bug: Preserve mobile keyboard when clearing blank selections https://github.com/siyuan-note/siyuan/issues/20087

Daniel0181866920b6
diff --git a/app/src/mobile/util/touch.test.ts b/app/src/mobile/util/touch.test.ts
new file mode 100644
index 0000000000..b4ec1ec9dc
--- /dev/null
+++ b/app/src/mobile/util/touch.test.ts
@@ -0,0 +1,91 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import * as touchSelection from "./touchSelection";
+
+const compiled = transpileModule(readFileSync("src/mobile/util/touch.ts", "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+}).outputText;
+
+const fixture = (text = "\u200b\n", collapsed = false, inside = true) => {
+ const exports: any = {};
+ let blurCount = 0;
+ const startContainer = {};
+ const endContainer = {};
+ const range = {
+ collapsed,
+ startContainer,
+ endContainer,
+ startOffset: 1,
+ endOffset: 2,
+ cloneRange() {
+ return {...this};
+ },
+ };
+ const selection = {
+ rangeCount: 1,
+ getRangeAt: () => range,
+ removeAllRanges() {
+ this.rangeCount = 0;
+ },
+ collapseToStart() {
+ range.collapsed = true;
+ range.endContainer = range.startContainer;
+ range.endOffset = range.startOffset;
+ },
+ };
+ const mobile: any = {};
+ const editor = {protyle: {wysiwyg: {element: {contains: () => inside}}}};
+ const modules: Record<string, unknown> = {
+ "./sidebar": {getSidebarElement: (): null => null},
+ "./touchPanelGesture": {setSidebarSwipeState: () => {}},
+ "./keyboardToolbar": {
+ resetAndroidBoundedSelectionGesture: () => {},
+ activeBlur: () => blurCount++,
+ },
+ "../editor": {getCurrentEditor: () => editor},
+ "../../constants": {Constants: {TIMEOUT_MULTIPLE_SELECT: 1500}},
+ "./touchSelection": touchSelection,
+ "../../protyle/util/inlineElementMarker": {stripSemanticMarkersFromRangeText: () => text},
+ };
+ runInNewContext(compiled, {
+ exports,
+ require: (name: string) => modules[name] || {},
+ document: {querySelector: (): null => null},
+ window: {getSelection: () => selection, siyuan: {mobile}},
+ });
+ return {exports, selection, range, mobile, blurCount: () => blurCount};
+};
+
+test("releasing a blank editor selection preserves the caret and keyboard", () => {
+ const f = fixture();
+ f.exports.handleTouchUp();
+ assert.equal(f.blurCount(), 0);
+ assert.equal(f.selection.rangeCount, 1);
+ assert.equal(f.range.collapsed, true);
+ assert.equal(f.range.endContainer, f.range.startContainer);
+ assert.equal(f.range.endOffset, f.range.startOffset);
+ assert.equal(f.mobile.touchRange.collapsed, true);
+ assert.notEqual(f.mobile.touchRange, f.range);
+});
+
+test("canceling a touch over blank content preserves the caret and keyboard", () => {
+ const f = fixture();
+ f.exports.handleTouchCancel();
+ assert.equal(f.blurCount(), 0);
+ assert.equal(f.selection.rangeCount, 1);
+ assert.equal(f.range.collapsed, true);
+});
+
+test("touch cleanup leaves visible selections, existing carets and outside selections unchanged", () => {
+ for (const f of [fixture("text"), fixture("", true), fixture("\u200b", false, false)]) {
+ const before = {...f.range};
+ f.exports.handleTouchUp();
+ assert.equal(f.blurCount(), 0);
+ assert.equal(f.selection.rangeCount, 1);
+ assert.deepEqual(f.range, before);
+ assert.equal(f.mobile.touchRange, undefined);
+ }
+});
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index b85dcf4ae3..a9c2a248d4 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -91,8 +91,9 @@ const clearInvisibleEditorSelection = () => {
!editor.protyle.wysiwyg.element.contains(range.endContainer)) {
return false;
}
- selection.removeAllRanges();
- activeBlur();
+ // 空白选区折叠为光标,保持编辑焦点和键盘,避免长按或滚动中断输入。
+ selection.collapseToStart();
+ window.siyuan.mobile.touchRange = selection.getRangeAt(0).cloneRange();
return true;
};