Skip to content

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

:bug: Fix mobile long-press selection toolbars https://github.com/siyuan-note/siyuan/issues/18627

Danielc067293704d8
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index eb47cc9b79..ccb004c4bb 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -13,14 +13,20 @@ import "../assets/scss/mobile.scss";
import {Menus} from "../menus";
import {addBaseURL, parseSiYuanUriInfo, setNoteBook} from "../util/pathName";
import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";
-import {handleTouchEnd, handleTouchMove, handleTouchStart, handleTouchUp} from "./util/touch";
+import {
+ handleTouchEnd,
+ handleTouchMove,
+ handleTouchSelectionChange,
+ handleTouchStart,
+ handleTouchUp,
+} from "./util/touch";
import {fetchGet, fetchPost} from "../util/fetch";
import {initFramework} from "./util/initFramework";
import {initAssets} from "../util/assets";
import {bootSync, lockScreen} from "../dialog/processSystem";
import {initMessage, showMessage} from "../dialog/message";
import {goBack} from "./util/MobileBackFoward";
-import {activeBlur, hideKeyboardToolbar, showKeyboardToolbar} from "./util/keyboardToolbar";
+import {activeBlur, hideKeyboardToolbarByApp, showKeyboardToolbar} from "./util/keyboardToolbar";
import {
getLocalStorage,
initWindowOpenOverride,
@@ -214,6 +220,7 @@ class App {
document.addEventListener("touchmove", handleTouchMove, false);
document.addEventListener("touchend", handleTouchEnd, false);
document.addEventListener("touchcancel", handleTouchEnd, false);
+ document.addEventListener("selectionchange", handleTouchSelectionChange, true);
window.addEventListener("nativePhysicalTouchUp", handleTouchUp, false);
window.addEventListener("keyup", () => {
window.siyuan.ctrlIsPressed = false;
@@ -279,7 +286,7 @@ window.goBack = goBack;
window.showMessage = showMessage;
window.processIOSPurchaseResponse = processIOSPurchaseResponse;
window.showKeyboardToolbar = showKeyboardToolbar;
-window.hideKeyboardToolbar = hideKeyboardToolbar;
+window.hideKeyboardToolbar = hideKeyboardToolbarByApp;
window.openFileByURL = (openURL) => {
const blockInfo = parseSiYuanUriInfo(openURL);
if (blockInfo != null) {
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 921b8efc47..23437b37f3 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -18,6 +18,7 @@ import {callMobileAppShowKeyboard, canInput, keyboardLockUntil} from "./mobileAp
import {isNotEditBlock} from "../../protyle/wysiwyg/getBlock";
import {getMirror, getUndoRootID, hasUndoStateMirror, initMirror} from "../../protyle/undo/globalUndo";
import {getMobilePluginToolbarItems} from "./pluginToolbar";
+import {hasVisibleSelectionText} from "./touchSelection";
let renderKeyboardToolbarTimeout: number;
let scrollSelectionIntoViewTimeout: number;
@@ -706,6 +707,24 @@ export const hideKeyboardToolbar = () => {
}
};
+export const hideKeyboardToolbarByApp = () => {
+ preventKeyboardToolbarRender();
+ hideKeyboardToolbar();
+ const editor = getCurrentEditor();
+ const selection = getSelection();
+ if (!editor || !selection || selection.rangeCount === 0 || selection.isCollapsed) {
+ return;
+ }
+ const range = selection.getRangeAt(0);
+ if (!hasVisibleSelectionText(range.toString()) ||
+ !editor.protyle.wysiwyg.element.contains(range.startContainer) ||
+ !editor.protyle.wysiwyg.element.contains(range.endContainer)) {
+ return;
+ }
+ (document.activeElement as HTMLElement)?.blur();
+ selection.removeAllRanges();
+};
+
export const activeBlur = () => {
const now = Date.now();
if (now < keyboardLockUntil) {
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index 0a174ffc60..ef8abe3511 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -14,6 +14,7 @@ import {getCurrentEditor} from "../editor";
import {Constants} from "../../constants";
import {getEmbedChildOperationContext} from "../../protyle/wysiwyg/getBlock";
import {backModel} from "../menu/model";
+import {hasVisibleSelectionText} from "./touchSelection";
let clientX: number;
let clientY: number;
@@ -27,6 +28,7 @@ let scrollBlock: boolean;
let isFirstMove = true;
// 长按进入多选的定时器
let longPressTimer: number;
+let longPressBlockElement: HTMLElement;
const popSide = (render = true) => {
if (render) {
@@ -45,10 +47,35 @@ const clearLongPress = () => {
}
};
+const clearInvisibleEditorSelection = () => {
+ const editor = getCurrentEditor();
+ const selection = window.getSelection();
+ if (!editor || !selection || selection.rangeCount === 0) {
+ return false;
+ }
+ const range = selection.getRangeAt(0);
+ if (range.collapsed || hasVisibleSelectionText(range.toString()) ||
+ !editor.protyle.wysiwyg.element.contains(range.startContainer) ||
+ !editor.protyle.wysiwyg.element.contains(range.endContainer)) {
+ return false;
+ }
+ selection.removeAllRanges();
+ activeBlur();
+ return true;
+};
+
export const handleTouchUp = () => {
if (Date.now() - time < Constants.TIMEOUT_MULTIPLE_SELECT) {
clearLongPress();
}
+ clearInvisibleEditorSelection();
+ longPressBlockElement = undefined;
+};
+
+export const handleTouchSelectionChange = () => {
+ if (longPressBlockElement) {
+ clearInvisibleEditorSelection();
+ }
};
export const handleTouchEnd = (event: TouchEvent) => {
@@ -226,6 +253,7 @@ export const handleTouchEnd = (event: TouchEvent) => {
export const handleTouchStart = (event: TouchEvent) => {
time = Date.now();
+ longPressBlockElement = undefined;
const target = event.touches[0].target as HTMLElement;
if (0 < event.touches.length && (target.tagName === "VIDEO" || target.tagName === "AUDIO")) {
// https://github.com/siyuan-note/siyuan/issues/14569
@@ -271,11 +299,13 @@ export const handleTouchStart = (event: TouchEvent) => {
if (clientX && clientY && editor && !editor.protyle.toolbar.isMultiSelectMode()) {
const blockElement = hasClosestBlock(target);
if (blockElement && editor.protyle.wysiwyg.element.contains(blockElement)) {
+ longPressBlockElement = blockElement;
longPressTimer = window.setTimeout(() => {
+ clearInvisibleEditorSelection();
const selection = window.getSelection();
if (selection?.rangeCount > 0) {
const range = selection.getRangeAt(0);
- if (!range.collapsed && range.toString().replace(Constants.ZWSP, "") !== "" &&
+ if (!range.collapsed && hasVisibleSelectionText(range.toString()) &&
editor.protyle.wysiwyg.element.contains(range.startContainer) &&
editor.protyle.wysiwyg.element.contains(range.endContainer)) {
longPressTimer = undefined;
diff --git a/app/src/mobile/util/touchSelection.test.ts b/app/src/mobile/util/touchSelection.test.ts
new file mode 100644
index 0000000000..208e8af159
--- /dev/null
+++ b/app/src/mobile/util/touchSelection.test.ts
@@ -0,0 +1,14 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {hasVisibleSelectionText} from "./touchSelection";
+
+describe("mobile touch selection", () => {
+ it("rejects selections containing only block placeholders", () => {
+ assert.equal(hasVisibleSelectionText("\u200b"), false);
+ assert.equal(hasVisibleSelectionText("\u200b\n\u200b\u200b"), false);
+ });
+
+ it("preserves selections containing visible text", () => {
+ assert.equal(hasVisibleSelectionText("\u200b内容\u200b"), true);
+ });
+});
diff --git a/app/src/mobile/util/touchSelection.ts b/app/src/mobile/util/touchSelection.ts
new file mode 100644
index 0000000000..13f8c96b6c
--- /dev/null
+++ b/app/src/mobile/util/touchSelection.ts
@@ -0,0 +1 @@
+export const hasVisibleSelectionText = (text: string) => text.replaceAll("\u200b", "").trim() !== "";