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 table cell selections https://github.com/siyuan-note/siyuan/issues/18842
Daniel4a3c2de373b2
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.tsindex 8c6651e965..b440490aed 100644--- a/app/src/mobile/util/keyboardToolbar.ts+++ b/app/src/mobile/util/keyboardToolbar.ts@@ -22,6 +22,8 @@ import {getMovingSelectionEndpoint,hasFixedSelectionEndpointChanged,hasVisibleSelectionText,+ isTableCellSelectAll,+ shouldPreserveTableCellSelectAll,type TSelectionEndpoint,} from "./touchSelection";@@ -33,6 +35,15 @@ type TAndroidBoundedSelection = {focusOffset: number,};+type TAndroidTableCellSelectAll = {+ cell: HTMLTableCellElement,+ editableElement: HTMLElement,+ expiresAt: number,+ range: Range,+};++const ANDROID_TABLE_CELL_SELECT_ALL_TIMEOUT = 2000;+let renderKeyboardToolbarTimeout: number;let scrollSelectionIntoViewTimeout: number;let clearRenderGutterAfterScroll: () => void;@@ -42,6 +53,8 @@ let preventRenderTimeout: number;let restoringAndroidBoundedSelection = false;let lastAndroidBoundedSelection: TAndroidBoundedSelection | undefined;let androidMovingSelectionEndpoint: TSelectionEndpoint | undefined;+let pendingAndroidTableCellSelectAll: TAndroidTableCellSelectAll | undefined;+let restoringAndroidTableCellSelectAll = false;export const updateMobilePluginToolbar = (protyle: IProtyle) => {const currentProtyle = getCurrentEditor()?.protyle;@@ -77,6 +90,62 @@ export const resetAndroidBoundedSelectionGesture = () => {androidMovingSelectionEndpoint = undefined;};+const rememberAndroidTableCellSelectAll = () => {+ if (!isInAndroid() || restoringAndroidTableCellSelectAll) {+ return;+ }+ const selection = getSelection();+ if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {+ return;+ }+ const range = selection.getRangeAt(0);+ const startCell = (hasClosestByTag(range.startContainer, "TD") ||+ hasClosestByTag(range.startContainer, "TH")) as HTMLTableCellElement;+ const endCell = (hasClosestByTag(range.endContainer, "TD") ||+ hasClosestByTag(range.endContainer, "TH")) as HTMLTableCellElement;+ if (!startCell || startCell !== endCell || !isTableCellSelectAll(range.toString(), startCell.textContent)) {+ if (pendingAndroidTableCellSelectAll && startCell && startCell !== pendingAndroidTableCellSelectAll.cell) {+ pendingAndroidTableCellSelectAll = undefined;+ }+ return;+ }+ const editor = getCurrentEditor();+ const editableElement = (canInput(document.activeElement) ||+ hasClosestByAttribute(range.startContainer, "contenteditable", "true", true)) as HTMLElement;+ if (!editor || !editableElement || !editor.protyle.wysiwyg.element.contains(startCell) ||+ !(editableElement === startCell || editableElement.contains(startCell) || startCell.contains(editableElement))) {+ return;+ }+ pendingAndroidTableCellSelectAll = {+ cell: startCell,+ editableElement,+ expiresAt: Date.now() + ANDROID_TABLE_CELL_SELECT_ALL_TIMEOUT,+ range: range.cloneRange(),+ };+};++const restoreRecentAndroidTableCellSelectAll = () => {+ const pendingSelection = pendingAndroidTableCellSelectAll;+ pendingAndroidTableCellSelectAll = undefined;+ if (!pendingSelection || !shouldPreserveTableCellSelectAll(pendingSelection.expiresAt, Date.now()) ||+ !pendingSelection.cell.isConnected || !pendingSelection.editableElement.isConnected ||+ !pendingSelection.range.startContainer.isConnected || !pendingSelection.range.endContainer.isConnected) {+ return false;+ }+ restoringAndroidTableCellSelectAll = true;+ try {+ pendingSelection.editableElement.focus({preventScroll: true});+ const selection = getSelection();+ selection.removeAllRanges();+ selection.addRange(pendingSelection.range);+ } finally {+ window.setTimeout(() => {+ restoringAndroidTableCellSelectAll = false;+ });+ }+ return true;+};+const getAndroidBoundedSelection = (selection: Selection, container: HTMLElement): TAndroidBoundedSelection => ({container,anchorNode: selection.anchorNode,@@ -817,26 +886,30 @@ export const hideKeyboardToolbar = () => {}};-export const hideKeyboardToolbarByApp = () => {+export const hideKeyboardToolbarByApp = (preserveTableCellSelectAll = false) => {+ if (preserveTableCellSelectAll && restoreRecentAndroidTableCellSelectAll()) {+ return true;+ }preventKeyboardToolbarRender();hideKeyboardToolbar();const editor = getCurrentEditor();const selection = getSelection();if (!editor) {- return;+ return false;}hideElements(["util"], editor.protyle);if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {- return;+ 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;+ return false;}(document.activeElement as HTMLElement)?.blur();selection.removeAllRanges();+ return false;};export const activeBlur = () => {@@ -873,6 +946,7 @@ export const initKeyboardToolbar = () => {viewportHandler();}document.addEventListener("selectionchange", () => {+ rememberAndroidTableCellSelectAll();if (preserveAndroidBoundedSelection()) {return;}diff --git a/app/src/mobile/util/touchSelection.test.ts b/app/src/mobile/util/touchSelection.test.tsindex 9d3584c490..fd8a8a334f 100644--- a/app/src/mobile/util/touchSelection.test.ts+++ b/app/src/mobile/util/touchSelection.test.ts@@ -4,7 +4,9 @@ import {getMovingSelectionEndpoint,hasFixedSelectionEndpointChanged,hasVisibleSelectionText,+ isTableCellSelectAll,shouldRestoreLongPressSelection,+ shouldPreserveTableCellSelectAll,} from "./touchSelection";describe("mobile touch selection", () => {@@ -17,6 +19,19 @@ describe("mobile touch selection", () => {assert.equal(hasVisibleSelectionText("\u200b内容\u200b"), true);});+ it("identifies table cell select all while ignoring placeholders", () => {+ assert.equal(isTableCellSelectAll("段落块", "段落块"), true);+ assert.equal(isTableCellSelectAll("\u200b段落块\u200b", "段落块"), true);+ assert.equal(isTableCellSelectAll("段落", "段落块"), false);+ assert.equal(isTableCellSelectAll("\u200b", "\u200b"), false);+ });++ it("preserves recent table cell select all once the keyboard state changes", () => {+ assert.equal(shouldPreserveTableCellSelectAll(1500, 1499), true);+ assert.equal(shouldPreserveTableCellSelectAll(1500, 1500), true);+ assert.equal(shouldPreserveTableCellSelectAll(1500, 1501), false);+ });+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 97b51d4c26..56902f7d5a 100644--- a/app/src/mobile/util/touchSelection.ts+++ b/app/src/mobile/util/touchSelection.ts@@ -1,6 +1,15 @@export type TSelectionEndpoint = "anchor" | "focus";-export const hasVisibleSelectionText = (text: string) => text.replaceAll("\u200b", "").trim() !== "";+const normalizeVisibleSelectionText = (text: string) => text.replaceAll("\u200b", "").trim();++export const hasVisibleSelectionText = (text: string) => normalizeVisibleSelectionText(text) !== "";++export const isTableCellSelectAll = (selectionText: string, cellText: string) => {+ const normalizedSelectionText = normalizeVisibleSelectionText(selectionText);+ return normalizedSelectionText !== "" && normalizedSelectionText === normalizeVisibleSelectionText(cellText);+};++export const shouldPreserveTableCellSelectAll = (expiresAt: number, now: number) => expiresAt >= now;export const getMovingSelectionEndpoint = (movingEndpoint: TSelectionEndpoint | undefined,