An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve mobile text selections when opening native actions https://github.com/siyuan-note/siyuan/issues/18867
Danielc42c0133d411
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.tsindex d947c03e14..4fda3a305b 100644--- a/app/src/mobile/util/keyboardToolbar.ts+++ b/app/src/mobile/util/keyboardToolbar.ts@@ -19,10 +19,12 @@ import {isNotEditBlock} from "../../protyle/wysiwyg/getBlock";import {getMirror, getUndoRootID, hasUndoStateMirror, initMirror} from "../../protyle/undo/globalUndo";import {getMobilePluginToolbarItems} from "./pluginToolbar";import {+ getKeyboardHideResult,getMovingSelectionEndpoint,hasFixedSelectionEndpointChanged,hasVisibleSelectionText,isTableCellSelectAll,+ KeyboardHideResult,shouldHideKeyboardAfterResize,shouldPreserveTableCellSelectAll,type TSelectionEndpoint,@@ -891,30 +893,30 @@ export const hideKeyboardToolbar = () => {}};-export const hideKeyboardToolbarByApp = (preserveTableCellSelectAll = false) => {- if (preserveTableCellSelectAll && restoreRecentAndroidTableCellSelectAll()) {- return true;+export const hideKeyboardToolbarByApp = (preserveSelection = false) => {+ const tableCellSelectionRestored = preserveSelection && restoreRecentAndroidTableCellSelectAll();+ if (tableCellSelectionRestored) {+ return KeyboardHideResult.RestoreTableCellSelection;}preventKeyboardToolbarRender();hideKeyboardToolbar();const editor = getCurrentEditor();const selection = getSelection();if (!editor) {- return false;+ return KeyboardHideResult.Cleanup;}hideElements(["util"], editor.protyle);- if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {- return false;- }- const range = selection.getRangeAt(0);- if (!hasVisibleSelectionText(range.toString()) ||- !editor.protyle.wysiwyg.element.contains(range.startContainer) ||- !editor.protyle.wysiwyg.element.contains(range.endContainer)) {- return false;+ const range = selection?.rangeCount > 0 && !selection.isCollapsed ? selection.getRangeAt(0) : undefined;+ const hasVisibleEditorSelection = !!range && hasVisibleSelectionText(range.toString()) &&+ editor.protyle.wysiwyg.element.contains(range.startContainer) &&+ editor.protyle.wysiwyg.element.contains(range.endContainer);+ const result = getKeyboardHideResult(preserveSelection, tableCellSelectionRestored, hasVisibleEditorSelection);+ if (result === KeyboardHideResult.PreserveSelection || !hasVisibleEditorSelection) {+ return result;}(document.activeElement as HTMLElement)?.blur();- selection.removeAllRanges();- return false;+ selection?.removeAllRanges();+ return result;};export const activeBlur = () => {diff --git a/app/src/mobile/util/touchSelection.test.ts b/app/src/mobile/util/touchSelection.test.tsindex 7eda7ee3c6..dee7f027ac 100644--- a/app/src/mobile/util/touchSelection.test.ts+++ b/app/src/mobile/util/touchSelection.test.ts@@ -1,10 +1,12 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";import {+ getKeyboardHideResult,getMovingSelectionEndpoint,hasFixedSelectionEndpointChanged,hasVisibleSelectionText,isTableCellSelectAll,+ KeyboardHideResult,shouldHideKeyboardAfterResize,shouldRestoreLongPressSelection,shouldPreserveTableCellSelectAll,@@ -39,6 +41,18 @@ describe("mobile touch selection", () => {assert.equal(shouldHideKeyboardAfterResize(false, false), true);});+ it("preserves a visible editor selection after a passive keyboard hide", () => {+ assert.equal(getKeyboardHideResult(true, false, true), KeyboardHideResult.PreserveSelection);+ assert.equal(getKeyboardHideResult(true, false, false), KeyboardHideResult.Cleanup);+ assert.equal(getKeyboardHideResult(false, false, true), KeyboardHideResult.Cleanup);+ });++ it("restores table cell selection before considering ordinary text selection", () => {+ assert.equal(getKeyboardHideResult(true, true, true), KeyboardHideResult.RestoreTableCellSelection);+ assert.equal(getKeyboardHideResult(true, true, false), KeyboardHideResult.RestoreTableCellSelection);+ assert.equal(getKeyboardHideResult(false, true, true), KeyboardHideResult.Cleanup);+ });+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);diff --git a/app/src/mobile/util/touchSelection.ts b/app/src/mobile/util/touchSelection.tsindex 54c1dba614..148a080ebd 100644--- a/app/src/mobile/util/touchSelection.ts+++ b/app/src/mobile/util/touchSelection.ts@@ -1,5 +1,12 @@export type TSelectionEndpoint = "anchor" | "focus";+// 返回值需与 Android 端 Utils 中的键盘隐藏结果保持一致。+export const KeyboardHideResult = {+ Cleanup: 0,+ PreserveSelection: 1,+ RestoreTableCellSelection: 2,+} as const;+const normalizeVisibleSelectionText = (text: string) => text.replaceAll("\u200b", "").trim();export const hasVisibleSelectionText = (text: string) => normalizeVisibleSelectionText(text) !== "";@@ -14,6 +21,20 @@ export const shouldPreserveTableCellSelectAll = (expiresAt: number, now: number)export const shouldHideKeyboardAfterResize = (isInputFocused: boolean, preserveTableCellSelectAll: boolean) =>!isInputFocused && !preserveTableCellSelectAll;+export const getKeyboardHideResult = (+ preserveSelection: boolean,+ tableCellSelectionRestored: boolean,+ hasVisibleEditorSelection: boolean,+) => {+ if (preserveSelection && tableCellSelectionRestored) {+ return KeyboardHideResult.RestoreTableCellSelection;+ }+ if (preserveSelection && hasVisibleEditorSelection) {+ return KeyboardHideResult.PreserveSelection;+ }+ return KeyboardHideResult.Cleanup;+};+export const getMovingSelectionEndpoint = (movingEndpoint: TSelectionEndpoint | undefined,anchorChanged: boolean,