Skip to content

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

:bug: Preserve mobile editing focus when long pressing blank space https://github.com/siyuan-note/siyuan/issues/20087

Daniel2a9ee4fd9d37
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index 24fcb94093..cab146345d 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -17,6 +17,7 @@ import {addBaseURL, parseSiYuanUriInfo, setNoteBook} from "../util/pathName";
import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";
import {
handleTouchCancel,
+ handleTouchContextMenu,
handleTouchEnd,
handleTouchMove,
handleTouchSelectionChange,
@@ -257,6 +258,7 @@ class App {
document.addEventListener("touchmove", handleTouchMove, false);
document.addEventListener("touchend", handleTouchEnd, false);
document.addEventListener("touchcancel", handleTouchCancel, false);
+ document.addEventListener("contextmenu", handleTouchContextMenu, true);
document.addEventListener("selectionchange", handleTouchSelectionChange, true);
window.addEventListener("nativePhysicalTouchUp", handleTouchUp, false);
window.addEventListener("keyup", () => {
diff --git a/app/src/mobile/util/touch.test.ts b/app/src/mobile/util/touch.test.ts
index b4ec1ec9dc..931a880ccc 100644
--- a/app/src/mobile/util/touch.test.ts
+++ b/app/src/mobile/util/touch.test.ts
@@ -12,9 +12,11 @@ const compiled = transpileModule(readFileSync("src/mobile/util/touch.ts", "utf8"
const fixture = (text = "\u200b\n", collapsed = false, inside = true) => {
const exports: any = {};
let blurCount = 0;
- const startContainer = {};
- const endContainer = {};
- const range = {
+ let focusCount = 0;
+ let keyboardHidden = false;
+ const startContainer = {isConnected: true};
+ const endContainer = {isConnected: true};
+ let range = {
collapsed,
startContainer,
endContainer,
@@ -30,6 +32,10 @@ const fixture = (text = "\u200b\n", collapsed = false, inside = true) => {
removeAllRanges() {
this.rangeCount = 0;
},
+ addRange(value: typeof range) {
+ range = value;
+ this.rangeCount = 1;
+ },
collapseToStart() {
range.collapsed = true;
range.endContainer = range.startContainer;
@@ -37,7 +43,15 @@ const fixture = (text = "\u200b\n", collapsed = false, inside = true) => {
},
};
const mobile: any = {};
- const editor = {protyle: {wysiwyg: {element: {contains: () => inside}}}};
+ const element = {tagName: "DIV", dataset: {}, contains: () => inside, closest: (): null => null};
+ const editor = {protyle: {wysiwyg: {element}, toolbar: {isMultiSelectMode: () => false}}};
+ const body = {};
+ const document: any = {body, activeElement: body, querySelector: (): null => null,
+ getElementById: () => ({classList: {contains: () => keyboardHidden}})};
+ const editable = {isContentEditable: true, isConnected: true, focus: () => {
+ focusCount++;
+ document.activeElement = editable;
+ }};
const modules: Record<string, unknown> = {
"./sidebar": {getSidebarElement: (): null => null},
"./touchPanelGesture": {setSidebarSwipeState: () => {}},
@@ -47,16 +61,31 @@ const fixture = (text = "\u200b\n", collapsed = false, inside = true) => {
},
"../editor": {getCurrentEditor: () => editor},
"../../constants": {Constants: {TIMEOUT_MULTIPLE_SELECT: 1500}},
+ "../../protyle/util/hasClosest": {hasClosestBlock: (): null => null, hasClosestByAttribute: (): null => null,
+ hasClosestByClassName: (): null => null},
+ "../../protyle/util/compatibility": {isIPhone: () => false},
"./touchSelection": touchSelection,
"../../protyle/util/inlineElementMarker": {stripSemanticMarkersFromRangeText: () => text},
};
runInNewContext(compiled, {
exports,
require: (name: string) => modules[name] || {},
- document: {querySelector: (): null => null},
- window: {getSelection: () => selection, siyuan: {mobile}},
+ document,
+ getSelection: () => selection,
+ window: {innerWidth: 384, innerHeight: 808, getSelection: () => selection, siyuan: {mobile}},
});
- return {exports, selection, range, mobile, blurCount: () => blurCount};
+ const startBlankTouch = (target = element, count = 1) => {
+ document.activeElement = editable;
+ exports.handleTouchStart({target, touches: Array.from({length: count}, () =>
+ ({target, clientX: 100, clientY: 100}))});
+ };
+ const contextMenu = (target = element) => {
+ const event = {target, defaultPrevented: false, preventDefault() { this.defaultPrevented = true; }};
+ exports.handleTouchContextMenu(event);
+ return event;
+ };
+ return {exports, selection, range, mobile, document, editable, element, editor, startBlankTouch, contextMenu,
+ hideKeyboard: () => { keyboardHidden = true; }, focusCount: () => focusCount, blurCount: () => blurCount};
};
test("releasing a blank editor selection preserves the caret and keyboard", () => {
@@ -89,3 +118,44 @@ test("touch cleanup leaves visible selections, existing carets and outside selec
assert.equal(f.mobile.touchRange, undefined);
}
});
+
+test("long pressing editor padding restores the caret after native WebView focus loss", () => {
+ const f = fixture("", true);
+ f.startBlankTouch();
+ f.document.activeElement = f.document.body;
+ f.selection.removeAllRanges();
+ assert.equal(f.contextMenu().defaultPrevented, true);
+ assert.equal(f.document.activeElement, f.editable);
+ assert.equal(f.focusCount(), 1);
+ assert.equal(f.selection.rangeCount, 1);
+ assert.equal(f.selection.getRangeAt().startContainer, f.range.startContainer);
+ assert.equal(f.selection.getRangeAt().startOffset, f.range.startOffset);
+ assert.equal(f.mobile.touchRange.collapsed, true);
+ assert.equal(f.contextMenu().defaultPrevented, false);
+});
+
+test("blank-touch recovery leaves text gestures, multiple fingers and hidden keyboards alone", () => {
+ for (const kind of ["text", "multiple", "hidden", "outside"]) {
+ const f = fixture("", true, kind !== "outside");
+ if (kind === "hidden") f.hideKeyboard();
+ f.startBlankTouch(kind === "text" ? {...f.element} : f.element, kind === "multiple" ? 2 : 1);
+ f.document.activeElement = f.document.body;
+ assert.equal(f.contextMenu().defaultPrevented, false, kind);
+ assert.equal(f.focusCount(), 0, kind);
+ }
+});
+
+test("blank-touch recovery expires on release or cancellation and does not steal another input's focus", () => {
+ for (const kind of ["release", "cancel", "detached", "newInput", "otherTarget", "newEditor"]) {
+ const f = fixture("", true);
+ f.startBlankTouch();
+ f.document.activeElement = f.document.body;
+ if (kind === "release") f.exports.handleTouchUp();
+ if (kind === "cancel") f.exports.handleTouchCancel();
+ if (kind === "detached") f.range.startContainer.isConnected = false;
+ if (kind === "newInput") f.document.activeElement = {};
+ if (kind === "newEditor") f.editor.protyle.wysiwyg.element = {...f.element};
+ assert.equal(f.contextMenu(kind === "otherTarget" ? {...f.element} : f.element).defaultPrevented, false, kind);
+ assert.equal(f.focusCount(), 0, kind);
+ }
+});
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index a9c2a248d4..52c84cf25b 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -52,6 +52,7 @@ let preventSwipe = false;
let longPressTimer: number;
let longPressBlockElement: HTMLElement;
let longPressTouchRange: Range;
+let blankTouchFocus: {editorElement: HTMLElement, editableElement: HTMLElement, range: Range};
const sideMaskElement = document.querySelector(".side-mask") as HTMLElement;
@@ -139,6 +140,26 @@ export const handleTouchUp = () => {
}
longPressBlockElement = undefined;
longPressTouchRange = undefined;
+ blankTouchFocus = undefined;
+};
+
+export const handleTouchContextMenu = (event: MouseEvent) => {
+ const saved = blankTouchFocus;
+ if (!saved || event.target !== saved.editorElement ||
+ getCurrentEditor()?.protyle.wysiwyg.element !== saved.editorElement ||
+ !saved.editableElement.isConnected || !saved.range.startContainer.isConnected ||
+ !saved.range.endContainer.isConnected ||
+ (document.activeElement !== document.body && document.activeElement !== saved.editableElement)) {
+ return;
+ }
+ blankTouchFocus = undefined;
+ // WebView 长按非编辑区域时会先失焦,在原生菜单阶段恢复本轮按下前的编辑位置。
+ event.preventDefault();
+ saved.editableElement.focus({preventScroll: true});
+ const selection = window.getSelection();
+ selection.removeAllRanges();
+ selection.addRange(saved.range);
+ window.siyuan.mobile.touchRange = saved.range.cloneRange();
};
export const handleTouchSelectionChange = () => {
@@ -317,6 +338,7 @@ export const handleTouchEnd = (event: TouchEvent) => {
};
const resetTouchGesture = () => {
+ blankTouchFocus = undefined;
isFirstMove = true;
clientX = null;
clientY = null;
@@ -380,6 +402,17 @@ export const handleTouchStart = (event: TouchEvent) => {
return;
}
const editor = getCurrentEditor();
+ const activeElement = document.activeElement as HTMLElement;
+ const selection = window.getSelection();
+ if (event.touches.length === 1 && editor && !editor.protyle.disabled &&
+ target === editor.protyle.wysiwyg.element && activeElement?.isContentEditable &&
+ target.contains(activeElement) && selection?.rangeCount > 0 &&
+ document.getElementById("keyboardToolbar")?.classList.contains("fn__none") === false) {
+ const range = selection.getRangeAt(0);
+ if (target.contains(range.startContainer) && target.contains(range.endContainer)) {
+ blankTouchFocus = {editorElement: target, editableElement: activeElement, range: range.cloneRange()};
+ }
+ }
if (getSelection().rangeCount > 0 && hasClosestBlock(event.target as Element) &&
editor && !editor.protyle.disabled && event.touches[0].clientY > window.innerHeight / 2 &&
document.querySelector("#keyboardToolbar").classList.contains("fn__none")) {