Skip to content

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

:bug: Preserve agent composer focus on mobile https://github.com/siyuan-note/siyuan/issues/20084

Daniel688a7200db18
diff --git a/app/src/layout/dock/agent/AgentChat.focus.test.ts b/app/src/layout/dock/agent/AgentChat.focus.test.ts
new file mode 100644
index 0000000000..ac7047a559
--- /dev/null
+++ b/app/src/layout/dock/agent/AgentChat.focus.test.ts
@@ -0,0 +1,48 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, forEachChild, isCallExpression, ScriptTarget, transpileModule} from "typescript";
+
+const source = createSourceFile("AgentChat.ts", readFileSync("src/layout/dock/agent/AgentChat.ts", "utf8"),
+ ScriptTarget.Latest, true);
+let handlerSource: string;
+const visit = (node: import("typescript").Node) => {
+ if (isCallExpression(node) && node.expression.getText(source) === "this.panelElement.addEventListener" &&
+ node.arguments[0].getText(source) === '"click"') {
+ handlerSource = node.arguments[1].getText(source);
+ }
+ forEachChild(node, visit);
+};
+visit(source);
+assert.ok(handlerSource);
+const compiled = transpileModule(`(function () { return ${handlerSource}; })`, {
+ compilerOptions: {target: ScriptTarget.ES2021},
+}).outputText;
+
+const clickPanel = (closestClass?: string) => {
+ let composerFocus = 0;
+ const panelElement = {isEqualNode: (element: unknown) => element === panelElement};
+ const target = {
+ parentElement: panelElement,
+ isEqualNode: (element: unknown) => element === target,
+ classList: {contains: () => false},
+ closest: (selector: string) => selector.split(", ").includes(closestClass) ? target : null,
+ };
+ const chat = {panelElement, host: {}, composer: {focus: () => composerFocus++}};
+ const handler = runInNewContext(compiled).call(chat);
+ handler({target});
+ return composerFocus;
+};
+
+test("clicking composer content preserves the native editable focus", () => {
+ assert.equal(clickPanel(".protyle-wysiwyg"), 0);
+});
+
+test("clicking an editor utility preserves its input focus", () => {
+ assert.equal(clickPanel(".protyle-util"), 0);
+});
+
+test("clicking empty panel space still focuses the composer", () => {
+ assert.equal(clickPanel(), 1);
+});
diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.ts
index e2e4befa9a..83447c1b01 100644
--- a/app/src/layout/dock/agent/AgentChat.ts
+++ b/app/src/layout/dock/agent/AgentChat.ts
@@ -1051,8 +1051,8 @@ export class AgentChat extends Model {
if (t.closest(".b3-select")) {
return;
}
- // 编辑浮层内的点击保留原有焦点,避免输入操作作用于正文。
- if (t.closest(".protyle-util")) {
+ // 编辑器及浮层内的点击保留原有焦点和选区,避免聚焦到不可编辑的滚动容器。
+ if (t.closest(".protyle-util, .protyle-wysiwyg")) {
return;
}
if (this.composer) {