Skip to content

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

:bug: Keep Android agent selection endpoints stable https://github.com/siyuan-note/siyuan/issues/18744

Daniel12d0350b3417
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index ae35e6ecf2..8c6651e965 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -18,7 +18,20 @@ 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";
+import {
+ getMovingSelectionEndpoint,
+ hasFixedSelectionEndpointChanged,
+ hasVisibleSelectionText,
+ type TSelectionEndpoint,
+} from "./touchSelection";
+
+type TAndroidBoundedSelection = {
+ container: HTMLElement,
+ anchorNode: Node,
+ anchorOffset: number,
+ focusNode: Node,
+ focusOffset: number,
+};
let renderKeyboardToolbarTimeout: number;
let scrollSelectionIntoViewTimeout: number;
@@ -27,13 +40,8 @@ let showUtil = false;
let preventRender = false;
let preventRenderTimeout: number;
let restoringAndroidBoundedSelection = false;
-let lastAndroidBoundedSelection: {
- container: HTMLElement,
- anchorNode: Node,
- anchorOffset: number,
- focusNode: Node,
- focusOffset: number,
-};
+let lastAndroidBoundedSelection: TAndroidBoundedSelection | undefined;
+let androidMovingSelectionEndpoint: TSelectionEndpoint | undefined;
export const updateMobilePluginToolbar = (protyle: IProtyle) => {
const currentProtyle = getCurrentEditor()?.protyle;
@@ -60,6 +68,44 @@ export const updateMobilePluginToolbar = (protyle: IProtyle) => {
});
};
+const clearAndroidBoundedSelection = () => {
+ lastAndroidBoundedSelection = undefined;
+ androidMovingSelectionEndpoint = undefined;
+};
+
+export const resetAndroidBoundedSelectionGesture = () => {
+ androidMovingSelectionEndpoint = undefined;
+};
+
+const getAndroidBoundedSelection = (selection: Selection, container: HTMLElement): TAndroidBoundedSelection => ({
+ container,
+ anchorNode: selection.anchorNode,
+ anchorOffset: selection.anchorOffset,
+ focusNode: selection.focusNode,
+ focusOffset: selection.focusOffset,
+});
+
+const hasSelectionPointChanged = (node: Node, offset: number, previousNode: Node, previousOffset: number) =>
+ node !== previousNode || offset !== previousOffset;
+
+const restoreAndroidBoundedSelection = (selection: Selection, restored: TAndroidBoundedSelection) => {
+ lastAndroidBoundedSelection = restored;
+ restoringAndroidBoundedSelection = true;
+ try {
+ selection.setBaseAndExtent(
+ restored.anchorNode,
+ restored.anchorOffset,
+ restored.focusNode,
+ restored.focusOffset,
+ );
+ } finally {
+ window.setTimeout(() => {
+ restoringAndroidBoundedSelection = false;
+ });
+ }
+ return true;
+};
+
const getAndroidSelectionContainer = (selection: Selection) => {
const previousContainer = lastAndroidBoundedSelection?.container;
if (previousContainer?.classList.contains("agent-chat__body") &&
@@ -87,44 +133,90 @@ const preserveAndroidBoundedSelection = () => {
const selection = getSelection();
if (!selection || selection.rangeCount === 0 || selection.isCollapsed ||
!selection.anchorNode || !selection.focusNode) {
- lastAndroidBoundedSelection = undefined;
+ clearAndroidBoundedSelection();
return false;
}
const container = getAndroidSelectionContainer(selection);
if (!container) {
- lastAndroidBoundedSelection = undefined;
+ clearAndroidBoundedSelection();
return false;
}
const contains = (node: Node) => node === container || container.contains(node);
const anchorInside = contains(selection.anchorNode);
const focusInside = contains(selection.focusNode);
+ const current = getAndroidBoundedSelection(selection, container);
+ const previous = lastAndroidBoundedSelection;
+ const previousAvailable = previous?.container === container &&
+ previous.anchorNode.isConnected && previous.focusNode.isConnected &&
+ contains(previous.anchorNode) && contains(previous.focusNode);
+ if (container.classList.contains("agent-chat__body")) {
+ if (!previousAvailable) {
+ androidMovingSelectionEndpoint = undefined;
+ if (anchorInside && focusInside) {
+ lastAndroidBoundedSelection = current;
+ } else {
+ clearAndroidBoundedSelection();
+ }
+ return false;
+ }
+ const anchorChanged = hasSelectionPointChanged(
+ current.anchorNode,
+ current.anchorOffset,
+ previous.anchorNode,
+ previous.anchorOffset,
+ );
+ const focusChanged = hasSelectionPointChanged(
+ current.focusNode,
+ current.focusOffset,
+ previous.focusNode,
+ previous.focusOffset,
+ );
+ androidMovingSelectionEndpoint = getMovingSelectionEndpoint(
+ androidMovingSelectionEndpoint,
+ anchorChanged,
+ focusChanged,
+ );
+ if (!androidMovingSelectionEndpoint) {
+ if (anchorInside && focusInside) {
+ lastAndroidBoundedSelection = current;
+ return false;
+ }
+ return restoreAndroidBoundedSelection(selection, previous);
+ }
+ const movingAnchor = androidMovingSelectionEndpoint === "anchor";
+ const movingEndpointInside = movingAnchor ? anchorInside : focusInside;
+ if (!movingEndpointInside || hasFixedSelectionEndpointChanged(
+ androidMovingSelectionEndpoint,
+ anchorChanged,
+ focusChanged,
+ )) {
+ return restoreAndroidBoundedSelection(selection, {
+ container,
+ anchorNode: movingAnchor && anchorInside ? current.anchorNode : previous.anchorNode,
+ anchorOffset: movingAnchor && anchorInside ? current.anchorOffset : previous.anchorOffset,
+ focusNode: !movingAnchor && focusInside ? current.focusNode : previous.focusNode,
+ focusOffset: !movingAnchor && focusInside ? current.focusOffset : previous.focusOffset,
+ });
+ }
+ lastAndroidBoundedSelection = current;
+ return false;
+ }
+ androidMovingSelectionEndpoint = undefined;
if (anchorInside && focusInside) {
- lastAndroidBoundedSelection = {
- container,
- anchorNode: selection.anchorNode,
- anchorOffset: selection.anchorOffset,
- focusNode: selection.focusNode,
- focusOffset: selection.focusOffset,
- };
+ lastAndroidBoundedSelection = current;
return false;
}
- const previous = lastAndroidBoundedSelection;
- if (!previous || previous.container !== container || anchorInside === focusInside ||
- !previous.anchorNode.isConnected || !previous.focusNode.isConnected) {
- lastAndroidBoundedSelection = undefined;
+ if (!previousAvailable || anchorInside === focusInside) {
+ clearAndroidBoundedSelection();
return false;
}
- restoringAndroidBoundedSelection = true;
- selection.setBaseAndExtent(
- anchorInside ? selection.anchorNode : previous.anchorNode,
- anchorInside ? selection.anchorOffset : previous.anchorOffset,
- focusInside ? selection.focusNode : previous.focusNode,
- focusInside ? selection.focusOffset : previous.focusOffset,
- );
- window.setTimeout(() => {
- restoringAndroidBoundedSelection = false;
+ return restoreAndroidBoundedSelection(selection, {
+ container,
+ anchorNode: anchorInside ? current.anchorNode : previous.anchorNode,
+ anchorOffset: anchorInside ? current.anchorOffset : previous.anchorOffset,
+ focusNode: focusInside ? current.focusNode : previous.focusNode,
+ focusOffset: focusInside ? current.focusOffset : previous.focusOffset,
});
- return true;
};
const preventKeyboardToolbarRender = () => {
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index c4d1993e34..c5a618a758 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -7,7 +7,7 @@ import {
} from "../../protyle/util/hasClosest";
import {closeModel, closePanel} from "./closePanel";
import {popMenu} from "../menu";
-import {activeBlur} from "./keyboardToolbar";
+import {activeBlur, resetAndroidBoundedSelectionGesture} from "./keyboardToolbar";
import {isChromeBrowser, isInAndroid, isInHarmony, isIPhone} from "../../protyle/util/compatibility";
import {getRangeByPoint} from "../../protyle/util/selection";
import {getCurrentEditor} from "../editor";
@@ -98,6 +98,7 @@ const restoreInvisibleLongPressSelection = () => {
};
export const handleTouchUp = () => {
+ resetAndroidBoundedSelectionGesture();
if (Date.now() - time < Constants.TIMEOUT_MULTIPLE_SELECT) {
clearLongPress();
}
diff --git a/app/src/mobile/util/touchSelection.test.ts b/app/src/mobile/util/touchSelection.test.ts
index d9fbdb83c3..9d3584c490 100644
--- a/app/src/mobile/util/touchSelection.test.ts
+++ b/app/src/mobile/util/touchSelection.test.ts
@@ -1,6 +1,11 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {hasVisibleSelectionText, shouldRestoreLongPressSelection} from "./touchSelection";
+import {
+ getMovingSelectionEndpoint,
+ hasFixedSelectionEndpointChanged,
+ hasVisibleSelectionText,
+ shouldRestoreLongPressSelection,
+} from "./touchSelection";
describe("mobile touch selection", () => {
it("rejects selections containing only block placeholders", () => {
@@ -24,4 +29,24 @@ describe("mobile touch selection", () => {
assert.equal(shouldRestoreLongPressSelection(false, "\u200b", "current", "next", "other"), false);
assert.equal(shouldRestoreLongPressSelection(false, "\u200b", undefined, "next", "next"), false);
});
+
+ it("identifies the endpoint that starts moving", () => {
+ assert.equal(getMovingSelectionEndpoint(undefined, true, false), "anchor");
+ assert.equal(getMovingSelectionEndpoint(undefined, false, true), "focus");
+ assert.equal(getMovingSelectionEndpoint(undefined, false, false), undefined);
+ assert.equal(getMovingSelectionEndpoint(undefined, true, true), undefined);
+ });
+
+ it("keeps tracking the same moving endpoint for the current gesture", () => {
+ assert.equal(getMovingSelectionEndpoint("anchor", false, true), "anchor");
+ assert.equal(getMovingSelectionEndpoint("focus", true, false), "focus");
+ });
+
+ it("detects changes to the fixed endpoint", () => {
+ assert.equal(hasFixedSelectionEndpointChanged("anchor", true, false), false);
+ assert.equal(hasFixedSelectionEndpointChanged("anchor", false, true), true);
+ assert.equal(hasFixedSelectionEndpointChanged("focus", false, true), false);
+ assert.equal(hasFixedSelectionEndpointChanged("focus", true, false), true);
+ assert.equal(hasFixedSelectionEndpointChanged(undefined, true, true), false);
+ });
});
diff --git a/app/src/mobile/util/touchSelection.ts b/app/src/mobile/util/touchSelection.ts
index 69605c470a..97b51d4c26 100644
--- a/app/src/mobile/util/touchSelection.ts
+++ b/app/src/mobile/util/touchSelection.ts
@@ -1,5 +1,24 @@
+export type TSelectionEndpoint = "anchor" | "focus";
+
export const hasVisibleSelectionText = (text: string) => text.replaceAll("\u200b", "").trim() !== "";
+export const getMovingSelectionEndpoint = (
+ movingEndpoint: TSelectionEndpoint | undefined,
+ anchorChanged: boolean,
+ focusChanged: boolean,
+) => {
+ if (movingEndpoint || anchorChanged === focusChanged) {
+ return movingEndpoint;
+ }
+ return anchorChanged ? "anchor" : "focus";
+};
+
+export const hasFixedSelectionEndpointChanged = (
+ movingEndpoint: TSelectionEndpoint | undefined,
+ anchorChanged: boolean,
+ focusChanged: boolean,
+) => movingEndpoint === "anchor" ? focusChanged : movingEndpoint === "focus" && anchorChanged;
+
export const shouldRestoreLongPressSelection = (
collapsed: boolean,
text: string,