Skip to content

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

:bug: Close database cell inputs when navigating on mobile

Danielcb54766dee32
diff --git a/app/src/mobile/editor.ts b/app/src/mobile/editor.ts
index 8c9181a4c1..26acaeae73 100644
--- a/app/src/mobile/editor.ts
+++ b/app/src/mobile/editor.ts
@@ -23,6 +23,7 @@ import {stickyRow} from "../protyle/render/av/row";
import {invalidateTrackedRanges} from "../protyle/util/trackedRange";
import {getActiveMobileSecondaryEditor} from "./util/secondaryEditors";
import {closeMobileBacklinkSheets} from "./util/backlinkPanels";
+import {closeAVCellEditor} from "../protyle/render/av/cellEditor";
export const getCurrentEditor = () => {
return getActiveMobileSecondaryEditor() || window.siyuan.mobile.popEditor || window.siyuan.mobile.editor;
@@ -105,6 +106,7 @@ export const loadMobileFileById = (app: App, id: string, action: TProtyleAction[
fail();
return;
}
+ closeAVCellEditor();
const avPanelElement = document.querySelector(".av__panel");
if (avPanelElement && !avPanelElement.classList.contains("fn__none")) {
avPanelElement.dispatchEvent(new CustomEvent("click", {detail: "close"}));
diff --git a/app/src/mobile/util/setEmpty.ts b/app/src/mobile/util/setEmpty.ts
index 426d2d54e7..88d9060e9c 100644
--- a/app/src/mobile/util/setEmpty.ts
+++ b/app/src/mobile/util/setEmpty.ts
@@ -8,8 +8,10 @@ import {setTitle} from "../../util/processTitle";
import {clearMobileBarsScroll} from "./mobileBars";
import {updateMobileTopBarLayout} from "./mobileTopBar";
import {invalidateTrackedRanges} from "../../protyle/util/trackedRange";
+import {closeAVCellEditor} from "../../protyle/render/av/cellEditor";
export const setEmpty = (app: App) => {
+ closeAVCellEditor();
if (window.siyuan.mobile.editor?.protyle) {
invalidateTrackedRanges(window.siyuan.mobile.editor.protyle);
}
diff --git a/app/src/mobile/util/touch.ts b/app/src/mobile/util/touch.ts
index b149edd400..e5d457ebac 100644
--- a/app/src/mobile/util/touch.ts
+++ b/app/src/mobile/util/touch.ts
@@ -21,6 +21,7 @@ import {stripSemanticMarkersFromRangeText} from "../../protyle/util/inlineElemen
import {getTouchAxis, shouldStartLongPressMultiSelect} from "./touchGesture";
import {getMobileBlockSelectionElement} from "./blockSelection";
import {updateMultiSelectToolbar} from "./multiSelectToolbar";
+import {closeAVCellEditor} from "../../protyle/render/av/cellEditor";
import {
getOpeningSidebar,
getOpenSidebarReleaseAction,
@@ -100,6 +101,7 @@ const popSidebar = (side: MobileSidebarSide, render = true) => {
return;
}
}
+ closeAVCellEditor();
const otherSidebar = side === "left" ? "right" : "left";
getSidebarElement(otherSidebar)?.style.removeProperty("transform");
sidebarElement.style.transform = "translateX(0px)";
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index 69e39c513a..c219a57c63 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -58,6 +58,7 @@ import {
} from "./richText";
import {openAVRichTextEditor} from "./richTextEditor";
import {getAVData} from "./virtualScroll";
+import {AV_CELL_EDITOR_CLOSE_EVENT} from "./cellEditor";
export {cellValueIsEmpty} from "./cellValue";
@@ -649,6 +650,10 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type
${html}
</div>`);
const avMaskElement = document.querySelector(".av__mask");
+ avMaskElement.addEventListener(AV_CELL_EDITOR_CLOSE_EVENT, () => {
+ updateCellValueByInput(protyle, type, blockElement, cellElements, false);
+ avMaskElement.remove();
+ }, {once: true});
if (options?.destroyCallback) {
const parentElement = avMaskElement.parentElement;
const observer = new MutationObserver(() => {
@@ -778,7 +783,8 @@ export const popTextCell = (protyle: IProtyle, cellElements: HTMLElement[], type
});
};
-const updateCellValueByInput = (protyle: IProtyle, type: TAVCol, blockElement: HTMLElement, cellElements: HTMLElement[]) => {
+const updateCellValueByInput = (protyle: IProtyle, type: TAVCol, blockElement: HTMLElement, cellElements: HTMLElement[],
+ restoreFocus = true) => {
const viewType = blockElement.getAttribute("data-av-type") as TAVView;
if (viewType === "table" && !cellElements[0].dataset.avId) {
const rowElement = hasClosestByClassName(cellElements[0], "av__row");
@@ -817,7 +823,9 @@ const updateCellValueByInput = (protyle: IProtyle, type: TAVCol, blockElement: H
document.querySelectorAll(".av__mask").forEach((item) => {
item.remove();
});
- focusBlock(blockElement);
+ if (restoreFocus) {
+ focusBlock(blockElement);
+ }
return;
}
updateCellsValue(protyle, blockElement, type === "checkbox" ? {
@@ -832,7 +840,7 @@ const updateCellValueByInput = (protyle: IProtyle, type: TAVCol, blockElement: H
addDragFill(cellElements[0]);
}
// 单元格编辑中 ctrl+p 光标定位
- if (!document.querySelector(".b3-dialog")) {
+ if (restoreFocus && !document.querySelector(".b3-dialog")) {
focusBlock(blockElement);
}
document.querySelectorAll(".av__mask").forEach((item) => {
diff --git a/app/src/protyle/render/av/cellEditor.test.ts b/app/src/protyle/render/av/cellEditor.test.ts
new file mode 100644
index 0000000000..b59aebe7ce
--- /dev/null
+++ b/app/src/protyle/render/av/cellEditor.test.ts
@@ -0,0 +1,78 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {describe, it} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const source = readFileSync("src/protyle/render/av/cell.ts", "utf8");
+const compiled = transpileModule(source.slice(source.indexOf("const updateCellValueByInput ="),
+ source.indexOf("export const updateCellsValue =")), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},
+}).outputText;
+
+const createEditor = (changed: boolean) => {
+ const saved: unknown[][] = [];
+ let focusCount = 0;
+ let removed = false;
+ const input = {value: "updated value", dataset: {changed: changed ? "true" : undefined}};
+ const mask = {
+ querySelector: () => input,
+ remove: () => {
+ removed = true;
+ },
+ };
+ const protyle = {};
+ const block = {
+ getAttribute: (name: string) => name === "data-av-type" ? "table" : "database-id",
+ };
+ const cells = [{dataset: {}, classList: {add: () => {}}}];
+ const context = {
+ document: {
+ querySelector: (selector: string) => selector === ".av__mask" ? mask : null,
+ querySelectorAll: () => [mask],
+ },
+ hasClosestByClassName: (_element: unknown, className: string) =>
+ className === "av__row" ? {dataset: {id: "row-id"}} : null,
+ updateCellsValue: (...args: unknown[]) => saved.push(args),
+ addDragFill: () => {},
+ focusBlock: () => focusCount++,
+ };
+ const submit = runInNewContext(`${compiled}\nupdateCellValueByInput;`, context);
+ return {
+ close: (restoreFocus = false) => submit(protyle, "block", block, cells, restoreFocus),
+ saved,
+ protyle,
+ block,
+ cells,
+ isRemoved: () => removed,
+ focusCount: () => focusCount,
+ };
+};
+
+describe("database cell editor navigation", () => {
+ it("submits edited content to the original cell without focusing the departing document", () => {
+ const editor = createEditor(true);
+ editor.close();
+ assert.equal(editor.saved.length, 1);
+ assert.deepEqual(editor.saved[0], [editor.protyle, editor.block, "updated value", editor.cells]);
+ assert.equal(editor.isRemoved(), true);
+ assert.equal(editor.focusCount(), 0);
+ });
+
+ it("closes unchanged content without submitting or restoring focus", () => {
+ const editor = createEditor(false);
+ editor.close();
+ assert.equal(editor.saved.length, 0);
+ assert.equal(editor.isRemoved(), true);
+ assert.equal(editor.focusCount(), 0);
+ });
+
+ it("preserves focus restoration when finishing editing inside the document", () => {
+ for (const changed of [false, true]) {
+ const editor = createEditor(changed);
+ editor.close(true);
+ assert.equal(editor.focusCount(), 1);
+ assert.equal(editor.isRemoved(), true);
+ }
+ });
+});
diff --git a/app/src/protyle/render/av/cellEditor.ts b/app/src/protyle/render/av/cellEditor.ts
new file mode 100644
index 0000000000..225dfb723c
--- /dev/null
+++ b/app/src/protyle/render/av/cellEditor.ts
@@ -0,0 +1,11 @@
+export const AV_CELL_EDITOR_CLOSE_EVENT = "siyuan-av-cell-editor-close";
+
+export const closeAVCellEditor = () => {
+ document.querySelectorAll(".av__mask:not(.av__richtext-mask)").forEach(element => {
+ // 先结束输入法组合输入,再由编辑器提交原单元格的值。
+ if (element.contains(document.activeElement)) {
+ (document.activeElement as HTMLElement).blur();
+ }
+ element.dispatchEvent(new CustomEvent(AV_CELL_EDITOR_CLOSE_EVENT));
+ });
+};