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: Stabilize Android editor selection during IME input
Danield3e6ece437c9
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.tsindex 4190d5ba09..eb47cc9b79 100644--- a/app/src/mobile/index.ts+++ b/app/src/mobile/index.ts@@ -20,7 +20,7 @@ import {initAssets} from "../util/assets";import {bootSync, lockScreen} from "../dialog/processSystem";import {initMessage, showMessage} from "../dialog/message";import {goBack} from "./util/MobileBackFoward";-import {activeBlur, hideKeyboardToolbarByApp, showKeyboardToolbar} from "./util/keyboardToolbar";+import {activeBlur, hideKeyboardToolbar, showKeyboardToolbar} from "./util/keyboardToolbar";import {getLocalStorage,initWindowOpenOverride,@@ -279,7 +279,7 @@ window.goBack = goBack;window.showMessage = showMessage;window.processIOSPurchaseResponse = processIOSPurchaseResponse;window.showKeyboardToolbar = showKeyboardToolbar;-window.hideKeyboardToolbar = hideKeyboardToolbarByApp;+window.hideKeyboardToolbar = hideKeyboardToolbar;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.tsindex 2f0286f4a9..921b8efc47 100644--- a/app/src/mobile/util/keyboardToolbar.ts+++ b/app/src/mobile/util/keyboardToolbar.ts@@ -706,16 +706,6 @@ export const hideKeyboardToolbar = () => {}};-export const hideKeyboardToolbarByApp = () => {- hideKeyboardToolbar();- const selection = getSelection();- if (!selection || selection.isCollapsed) {- return;- }- (document.activeElement as HTMLElement)?.blur();- selection.removeAllRanges();-};-export const activeBlur = () => {const now = Date.now();if (now < keyboardLockUntil) {diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 809c17ff14..be57b13906 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -3657,12 +3657,87 @@ export class WYSIWYG {let lineBreakUndoContext: Record<string, string>;// 仅矫正从数据库外进入的占位光标,避免重置数据库内部的方向键导航。let arrowStartElement: false | HTMLElement | undefined;+ // Android 软键盘在空块中会先发送 Unidentified 并清空选区,需在实际回车或退格事件前恢复。+ let mobileUnidentifiedKeyState: { range: Range; time: number; placeholder?: HTMLBRElement };+ // Android 会在边界退格的 keyup 中再次清空选区,需用 input 事件后的最终光标立即恢复。+ let mobileUnidentifiedInputRange: Range;+ const takeMobileUnidentifiedKeyState = () => {+ const keyState = mobileUnidentifiedKeyState;+ mobileUnidentifiedKeyState = undefined;+ if (!keyState || Date.now() - keyState.time > 1000 ||+ !this.element.contains(keyState.range.startContainer)) {+ keyState?.placeholder?.remove();+ return;+ }+ return keyState;+ };this.element.addEventListener("keydown", (event: KeyboardEvent) => {+ if (isInAndroid()) {+ if (event.key === "Unidentified") {+ mobileUnidentifiedInputRange = undefined;+ mobileUnidentifiedKeyState?.placeholder?.remove();+ const selection = getSelection();+ if (selection.rangeCount > 0) {+ const range = selection.getRangeAt(0);+ if (this.element.contains(range.startContainer)) {+ let placeholder: HTMLBRElement;+ const blockElement = hasClosestBlock(range.startContainer);+ const editableElement = blockElement && getContenteditableElement(blockElement);+ if (range.collapsed && editableElement?.childNodes.length === 0) {+ placeholder = document.createElement("br");+ editableElement.appendChild(placeholder);+ focusByRange(range);+ }+ mobileUnidentifiedKeyState = {+ range: range.cloneRange(),+ time: Date.now(),+ placeholder,+ };+ } else {+ mobileUnidentifiedKeyState = undefined;+ }+ } else {+ mobileUnidentifiedKeyState = undefined;+ }+ } else {+ const unidentifiedState = takeMobileUnidentifiedKeyState();+ if (event.key === "Enter" && unidentifiedState) {+ focusByRange(unidentifiedState.range);+ } else if (unidentifiedState?.placeholder) {+ unidentifiedState.placeholder.remove();+ focusByRange(unidentifiedState.range);+ }+ }+ }if (!event.repeat && !event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey &&!event.isComposing && event.key.startsWith("Arrow")) {arrowStartElement = hasClosestBlock(getEditorRange(this.element).startContainer);}});+ this.element.addEventListener("input", (event: InputEvent) => {+ if (!isInAndroid() || event.inputType !== "deleteContentBackward") {+ return;+ }+ const selection = getSelection();+ if (selection.rangeCount === 0) {+ mobileUnidentifiedInputRange = undefined;+ return;+ }+ const range = selection.getRangeAt(0);+ mobileUnidentifiedInputRange = this.element.contains(range.startContainer) &&+ this.element.contains(range.endContainer) ? range.cloneRange() : undefined;+ });+ this.element.addEventListener("keyup", (event: KeyboardEvent) => {+ if (!isInAndroid() || event.key !== "Unidentified") {+ return;+ }+ const range = mobileUnidentifiedInputRange;+ mobileUnidentifiedInputRange = undefined;+ if (getSelection().rangeCount === 0 && range && this.element.contains(range.startContainer) &&+ this.element.contains(range.endContainer)) {+ focusByRange(range);+ }+ });// 记录组合开始时的光标位置,用于取消组合后恢复光标(输入法删空候选词会导致浏览器移动光标)let compositionRange: { range: Range } | { cell: HTMLElement; offset: number };const isAfterInlineMath = (range: Range) => {@@ -3756,6 +3831,23 @@ export class WYSIWYG {});this.element.addEventListener("beforeinput", async (event: InputEvent) => {+ const unidentifiedState = isInAndroid() ? takeMobileUnidentifiedKeyState() : undefined;+ if (event.inputType === "deleteContentBackward" && unidentifiedState) {+ unidentifiedState.placeholder?.remove();+ focusByRange(unidentifiedState.range);+ const keydownEvent = new KeyboardEvent("keydown", {+ key: "Backspace",+ code: "Backspace",+ bubbles: true,+ cancelable: true,+ });+ this.element.dispatchEvent(keydownEvent);+ if (keydownEvent.defaultPrevented) {+ event.preventDefault();+ event.stopPropagation();+ return;+ }+ }if (!isComposition) {beforeBlockquoteInput(protyle, event);}