Skip to content

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

:bug: Improve Android cursor movement across inline formulas https://github.com/siyuan-note/siyuan/issues/18120

Daniel236f6bdea1a4
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index b2cc8b2924..31f87b78ac 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -16,6 +16,11 @@ body {
body.body--android {
--mobile-top-safe-area: 0px;
+
+ .protyle-wysiwyg[data-readonly="false"] span[data-type~="inline-math"] {
+ // 继承父节点的编辑状态,避免原生光标经过公式时断开输入法连接;输入前由编辑器校正公式边界。
+ -webkit-user-modify: inherit;
+ }
}
body.mobile-keyboard--open {
diff --git a/app/src/mobile/util/inlineMathSelection.ts b/app/src/mobile/util/inlineMathSelection.ts
new file mode 100644
index 0000000000..b114c55cdb
--- /dev/null
+++ b/app/src/mobile/util/inlineMathSelection.ts
@@ -0,0 +1,127 @@
+export const createInlineMathSelection = (onSettled: (editor: HTMLElement, math: HTMLElement) => void) => {
+ let previousRange: Range;
+ let previousEditor: HTMLElement;
+ let pending: ReturnType<typeof setTimeout>;
+
+ const cancel = () => {
+ clearTimeout(pending);
+ pending = undefined;
+ };
+
+ const reset = () => {
+ cancel();
+ previousRange = undefined;
+ previousEditor = undefined;
+ };
+
+ const normalize = (editor: HTMLElement, selection: Selection, composing = false) => {
+ cancel();
+ if (!editor || composing || !selection?.isCollapsed || selection.rangeCount !== 1 ||
+ !editor.contains(selection.anchorNode)) {
+ reset();
+ return false;
+ }
+ if (previousEditor !== editor) {
+ reset();
+ }
+ const range = selection.getRangeAt(0);
+ const element = range.startContainer.nodeType === 1 ? range.startContainer as Element :
+ range.startContainer.parentElement;
+ const math = element?.closest('[data-type~="inline-math"]');
+ if (!math || !editor.contains(math)) {
+ previousRange = range.cloneRange();
+ previousEditor = editor;
+ return false;
+ }
+ const parent = math.parentElement;
+ if (!parent.isContentEditable) {
+ reset();
+ return false;
+ }
+ let after = true;
+ if (previousRange?.startContainer.isConnected && editor.contains(previousRange.startContainer)) {
+ const beforeMath = range.cloneRange();
+ beforeMath.setStartBefore(math);
+ beforeMath.collapse(true);
+ after = previousRange.compareBoundaryPoints(Range.START_TO_START, beforeMath) <= 0;
+ }
+ const boundary = range.cloneRange();
+ const sibling = after ? math.nextSibling : math.previousSibling;
+ // 优先落在公式外的文本节点,避免输入法继续把插入点映射到公式内部。
+ if (sibling?.nodeType === 3) {
+ boundary.setStart(sibling, after ? 0 : sibling.textContent.length);
+ } else if (after) {
+ boundary.setStartAfter(math);
+ } else {
+ boundary.setStartBefore(math);
+ }
+ boundary.collapse(true);
+ selection.setBaseAndExtent(boundary.startContainer, boundary.startOffset,
+ boundary.startContainer, boundary.startOffset);
+ previousRange = boundary.cloneRange();
+ previousEditor = editor;
+ return true;
+ };
+
+ const update = (editor: HTMLElement, selection: Selection, composing = false) => {
+ cancel();
+ const element = selection?.anchorNode?.nodeType === 1 ? selection.anchorNode as Element :
+ selection?.anchorNode?.parentElement;
+ if (!editor || composing || !selection?.isCollapsed || !editor.contains(element) ||
+ !element?.closest('[data-type~="inline-math"]')) {
+ normalize(editor, selection, composing);
+ return;
+ }
+ const math = element.closest<HTMLElement>('[data-type~="inline-math"]');
+ // 原生光标手柄拖动时改变焦点会中断手势,停稳后再打开公式编辑。
+ pending = setTimeout(() => {
+ if (editor.isConnected && editor.contains(editor.ownerDocument.activeElement) &&
+ selection.rangeCount === 1 && selection.isCollapsed && math.isConnected &&
+ editor.contains(math) && math.contains(selection.anchorNode) && math.parentElement.isContentEditable) {
+ reset();
+ onSettled(editor, math);
+ }
+ }, 300);
+ };
+
+ const prepareInput = (editor: HTMLElement, selection: Selection) => {
+ cancel();
+ if (!editor || selection?.rangeCount !== 1) {
+ reset();
+ return false;
+ }
+ if (selection.isCollapsed) {
+ return normalize(editor, selection);
+ }
+ const range = selection.getRangeAt(0).cloneRange();
+ if (!editor.contains(range.startContainer) || !editor.contains(range.endContainer)) {
+ return false;
+ }
+ const getMath = (node: Node) => {
+ const element = node.nodeType === 1 ? node as Element : node.parentElement;
+ const math = element?.closest('[data-type~="inline-math"]');
+ return math && editor.contains(math) && math.parentElement.isContentEditable ? math : undefined;
+ };
+ const startMath = getMath(range.startContainer);
+ const endMath = getMath(range.endContainer);
+ if (!startMath && !endMath) {
+ return false;
+ }
+ // 输入和删除按完整公式处理,避免修改 KaTeX 的内部渲染节点。
+ const backward = selection.anchorNode === range.endContainer && selection.anchorOffset === range.endOffset;
+ if (startMath) {
+ range.setStartBefore(startMath);
+ }
+ if (endMath) {
+ range.setEndAfter(endMath);
+ }
+ selection.setBaseAndExtent(backward ? range.endContainer : range.startContainer,
+ backward ? range.endOffset : range.startOffset,
+ backward ? range.startContainer : range.endContainer,
+ backward ? range.startOffset : range.endOffset);
+ reset();
+ return true;
+ };
+
+ return {normalize, reset, update, prepareInput};
+};
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 63b5459164..e6f7f35ee2 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -58,6 +58,7 @@ import {
type TSelectionEndpoint,
} from "./touchSelection";
import {getVisibleViewportBounds} from "./visibleViewport";
+import {createInlineMathSelection} from "./inlineMathSelection";
import {
getTextWithoutSemanticMarkers,
stripSemanticMarkersFromRangeText
@@ -85,6 +86,14 @@ type TAndroidTableCellSelectAll = {
};
const ANDROID_TABLE_CELL_SELECT_ALL_TIMEOUT = 2000;
+const inlineMathSelection = createInlineMathSelection((editor, math) => {
+ const protyle = getCurrentEditor()?.protyle;
+ if (!protyle || protyle.disabled || protyle.toolbar.isMultiSelectMode() || protyle.wysiwyg.element !== editor) {
+ return;
+ }
+ protyle.toolbar.range = getSelection().getRangeAt(0).cloneRange();
+ protyle.toolbar.showRender(protyle, math);
+});
let renderKeyboardToolbarTimeout: number;
let scrollSelectionIntoViewTimeout: number;
@@ -983,6 +992,7 @@ export const hideKeyboardToolbar = () => {
};
export const hideKeyboardToolbarByApp = (preserveSelection = false) => {
+ inlineMathSelection.reset();
const tableCellSelectionRestored = preserveSelection && restoreRecentAndroidTableCellSelectAll();
if (tableCellSelectionRestored) {
return KeyboardHideResult.RestoreTableCellSelection;
@@ -1014,6 +1024,7 @@ export const activeBlur = (force = false) => {
console.warn(`activeBlur blocked by lock (remaining: ${keyboardLockUntil - now}ms)`);
return;
}
+ inlineMathSelection.reset();
if (window.JSAndroid && window.JSAndroid.hideKeyboard) {
window.JSAndroid.hideKeyboard();
@@ -1025,6 +1036,41 @@ export const activeBlur = (force = false) => {
};
export const initKeyboardToolbar = () => {
+ let composing = false;
+ const getMathEditor = () => {
+ const protyle = getCurrentEditor()?.protyle;
+ return isInAndroid() && protyle && !protyle.disabled && !protyle.toolbar.isMultiSelectMode() &&
+ protyle.wysiwyg.element.contains(document.activeElement) ? protyle.wysiwyg.element : undefined;
+ };
+ document.addEventListener("beforeinput", () => {
+ if (!composing) {
+ inlineMathSelection.prepareInput(getMathEditor(), getSelection());
+ }
+ }, true);
+ document.addEventListener("keydown", (event) => {
+ if (!composing && ["Backspace", "Delete", "Enter"].includes(event.key)) {
+ inlineMathSelection.prepareInput(getMathEditor(), getSelection());
+ }
+ }, true);
+ ["copy", "cut", "paste"].forEach(type => {
+ document.addEventListener(type, () => {
+ if (!composing) {
+ inlineMathSelection.prepareInput(getMathEditor(), getSelection());
+ }
+ }, true);
+ });
+ document.addEventListener("compositionstart", () => {
+ inlineMathSelection.prepareInput(getMathEditor(), getSelection());
+ composing = true;
+ inlineMathSelection.reset();
+ }, true);
+ document.addEventListener("compositionend", () => {
+ composing = false;
+ }, true);
+ document.addEventListener("selectionchange", () => {
+ inlineMathSelection.update(getMathEditor(), getSelection(), composing);
+ }, true);
+ document.addEventListener("focusout", () => inlineMathSelection.reset(), true);
if (!isInMobileApp() && window.visualViewport) {
let pendingUpdate = false;
const viewportHandler = () => {