Skip to content

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

:bug: Restore mobile caret after invisible long-press selection https://github.com/siyuan-note/siyuan/issues/18660

Daniel743a167c3bcb
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index ef8abe3511..3eabe56ba8 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -14,7 +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";
+import {hasVisibleSelectionText, shouldRestoreLongPressSelection} from "./touchSelection";
let clientX: number;
let clientY: number;
@@ -29,6 +29,7 @@ let isFirstMove = true;
// 长按进入多选的定时器
let longPressTimer: number;
let longPressBlockElement: HTMLElement;
+let longPressTouchRange: Range;
const popSide = (render = true) => {
if (render) {
@@ -64,16 +65,50 @@ const clearInvisibleEditorSelection = () => {
return true;
};
+const restoreInvisibleLongPressSelection = () => {
+ const editor = getCurrentEditor();
+ const selection = window.getSelection();
+ if (!editor || !selection || selection.rangeCount === 0 || !longPressBlockElement ||
+ !longPressTouchRange?.startContainer.isConnected ||
+ !longPressBlockElement.contains(longPressTouchRange.startContainer)) {
+ return false;
+ }
+ const range = selection.getRangeAt(0);
+ if (!editor.protyle.wysiwyg.element.contains(range.startContainer) ||
+ !editor.protyle.wysiwyg.element.contains(range.endContainer)) {
+ return false;
+ }
+ const startBlockElement = hasClosestBlock(range.startContainer);
+ const endBlockElement = hasClosestBlock(range.endContainer);
+ if (!shouldRestoreLongPressSelection(
+ range.collapsed,
+ range.toString(),
+ startBlockElement ? startBlockElement.getAttribute("data-node-id") : undefined,
+ endBlockElement ? endBlockElement.getAttribute("data-node-id") : undefined,
+ longPressBlockElement.getAttribute("data-node-id"),
+ )) {
+ return false;
+ }
+ const restoredRange = longPressTouchRange.cloneRange();
+ selection.removeAllRanges();
+ selection.addRange(restoredRange);
+ window.siyuan.mobile.touchRange = restoredRange.cloneRange();
+ return true;
+};
+
export const handleTouchUp = () => {
if (Date.now() - time < Constants.TIMEOUT_MULTIPLE_SELECT) {
clearLongPress();
}
- clearInvisibleEditorSelection();
+ if (!restoreInvisibleLongPressSelection()) {
+ clearInvisibleEditorSelection();
+ }
longPressBlockElement = undefined;
+ longPressTouchRange = undefined;
};
export const handleTouchSelectionChange = () => {
- if (longPressBlockElement) {
+ if (longPressBlockElement && !restoreInvisibleLongPressSelection()) {
clearInvisibleEditorSelection();
}
};
@@ -254,6 +289,7 @@ export const handleTouchEnd = (event: TouchEvent) => {
export const handleTouchStart = (event: TouchEvent) => {
time = Date.now();
longPressBlockElement = undefined;
+ longPressTouchRange = 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
@@ -300,6 +336,14 @@ export const handleTouchStart = (event: TouchEvent) => {
const blockElement = hasClosestBlock(target);
if (blockElement && editor.protyle.wysiwyg.element.contains(blockElement)) {
longPressBlockElement = blockElement;
+ const touchRange = getRangeByPoint(event.touches[0].clientX, event.touches[0].clientY);
+ const touchRangeElement = touchRange.startContainer.nodeType === Node.ELEMENT_NODE ?
+ touchRange.startContainer as Element : touchRange.startContainer.parentElement;
+ const editableElement = touchRangeElement?.closest('[contenteditable="true"]');
+ if (editableElement && blockElement.contains(editableElement)) {
+ longPressTouchRange = touchRange.cloneRange();
+ longPressTouchRange.collapse(true);
+ }
longPressTimer = window.setTimeout(() => {
clearInvisibleEditorSelection();
const selection = window.getSelection();
@@ -347,6 +391,7 @@ export const handleTouchMove = (event: TouchEvent) => {
if (clientX && clientY &&
(Math.abs(clientX - event.touches[0].clientX) >= 5 || Math.abs(clientY - event.touches[0].clientY) >= 5)) {
clearLongPress();
+ longPressTouchRange = undefined;
}
if (!clientX || !clientY ||
target.tagName === "AUDIO" ||
diff --git a/app/src/mobile/util/touchSelection.test.ts b/app/src/mobile/util/touchSelection.test.ts
index 208e8af159..d9fbdb83c3 100644
--- a/app/src/mobile/util/touchSelection.test.ts
+++ b/app/src/mobile/util/touchSelection.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {hasVisibleSelectionText} from "./touchSelection";
+import {hasVisibleSelectionText, shouldRestoreLongPressSelection} from "./touchSelection";
describe("mobile touch selection", () => {
it("rejects selections containing only block placeholders", () => {
@@ -11,4 +11,17 @@ describe("mobile touch selection", () => {
it("preserves selections containing visible text", () => {
assert.equal(hasVisibleSelectionText("\u200b内容\u200b"), true);
});
+
+ it("restores invisible cross-block selections involving the long-pressed block", () => {
+ assert.equal(shouldRestoreLongPressSelection(false, "\u200b\n", "current", "next", "current"), true);
+ assert.equal(shouldRestoreLongPressSelection(false, "\u200b\n", "current", "next", "next"), true);
+ });
+
+ it("keeps ordinary selections unchanged", () => {
+ assert.equal(shouldRestoreLongPressSelection(false, "内容", "current", "next", "current"), false);
+ assert.equal(shouldRestoreLongPressSelection(true, "", "current", "next", "current"), false);
+ assert.equal(shouldRestoreLongPressSelection(false, "\u200b", "current", "current", "current"), false);
+ assert.equal(shouldRestoreLongPressSelection(false, "\u200b", "current", "next", "other"), false);
+ assert.equal(shouldRestoreLongPressSelection(false, "\u200b", undefined, "next", "next"), false);
+ });
});
diff --git a/app/src/mobile/util/touchSelection.ts b/app/src/mobile/util/touchSelection.ts
index 13f8c96b6c..69605c470a 100644
--- a/app/src/mobile/util/touchSelection.ts
+++ b/app/src/mobile/util/touchSelection.ts
@@ -1 +1,12 @@
export const hasVisibleSelectionText = (text: string) => text.replaceAll("\u200b", "").trim() !== "";
+
+export const shouldRestoreLongPressSelection = (
+ collapsed: boolean,
+ text: string,
+ startBlockID: string | undefined,
+ endBlockID: string | undefined,
+ longPressBlockID: string | undefined,
+) => !collapsed && !hasVisibleSelectionText(text) &&
+ !!startBlockID && !!endBlockID && !!longPressBlockID &&
+ startBlockID !== endBlockID &&
+ (startBlockID === longPressBlockID || endBlockID === longPressBlockID);