An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve agent composer height and scrolling https://github.com/siyuan-note/siyuan/issues/20807
Danielf049caf51db6
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20230304000547-ibldj1z/20260804171004-aiagt03.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20230304000547-ibldj1z/20260804171004-aiagt03.syindex 03500fbf0b..86b98cb3c6 100644--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20230304000547-ibldj1z/20260804171004-aiagt03.sy+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20230304000547-ibldj1z/20260804171004-aiagt03.sy@@ -6,7 +6,7 @@"id": "20260804171004-aiagt03","title": "AI Agent","type": "doc",- "updated": "20261021001816"+ "updated": "20261022182404"},"Children": [{@@ -496,7 +496,7 @@"HeadingLevel": 3,"Properties": {"id": "20260804170039-x813t3f",- "updated": "20260816114944"+ "updated": "20261022182404"},"Children": [{@@ -550,6 +550,79 @@}]},+ {+ "ID": "20261022182404-71cve53",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-71cve53",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "The input grows automatically as you add text and images, then scrolls when it reaches its height limit. Drag the small handle at the top upward or downward to adjust that limit; touch screens support the same gesture. Double-click the handle to restore the automatic limit. Space above and below the content makes the first and last lines easier to inspect."+ }+ ]+ },+ {+ "ID": "20261022182404-jbp15g8",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-jbp15g8",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "With the height handle focused, press "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↑"+ },+ {+ "Type": "NodeText",+ "Data": " or "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↓"+ },+ {+ "Type": "NodeText",+ "Data": " to increase or decrease the limit, "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Home"+ },+ {+ "Type": "NodeText",+ "Data": " for the minimum, or "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "End"+ },+ {+ "Type": "NodeText",+ "Data": " for the maximum available height. Press "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Esc"+ },+ {+ "Type": "NodeText",+ "Data": " during a drag to cancel it."+ }+ ]+ },{"ID": "20260804170041-m6wpz84","Type": "NodeHeading",diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20230303235619-ex5l63e/20260804171000-aiagt01.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20230303235619-ex5l63e/20260804171000-aiagt01.syindex d41c62be42..148e397f82 100644--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20230303235619-ex5l63e/20260804171000-aiagt01.sy+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20230303235619-ex5l63e/20260804171000-aiagt01.sy@@ -6,7 +6,7 @@"id": "20260804171000-aiagt01","title": "AI 智能体","type": "doc",- "updated": "20261021001816"+ "updated": "20261022182404"},"Children": [{@@ -496,7 +496,7 @@"HeadingLevel": 3,"Properties": {"id": "20260804170039-6hkjcgx",- "updated": "20260820190627"+ "updated": "20261022182404"},"Children": [{@@ -550,6 +550,79 @@}]},+ {+ "ID": "20261022182404-g6vs2ax",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-g6vs2ax",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "输入框随文字和图片自动增高,达到高度上限后滚动。向上或向下拖动输入框顶部的小把手可调整上限,触摸屏也支持拖动;双击把手可恢复自动上限。内容首尾保留滚动余量,便于完整查看首行和末行。"+ }+ ]+ },+ {+ "ID": "20261022182404-hm91uco",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-hm91uco",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "高度把手获得键盘焦点后,按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↑"+ },+ {+ "Type": "NodeText",+ "Data": " 或 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↓"+ },+ {+ "Type": "NodeText",+ "Data": " 增大或减小上限,按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Home"+ },+ {+ "Type": "NodeText",+ "Data": " 调至最小高度,按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "End"+ },+ {+ "Type": "NodeText",+ "Data": " 调至当前可用的最大高度;拖动过程中按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Esc"+ },+ {+ "Type": "NodeText",+ "Data": " 可取消调整。"+ }+ ]+ },{"ID": "20260804170041-tqg42zs","Type": "NodeHeading",diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20230304000829-9jwu3po/20260804171002-aiagt02.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20230304000829-9jwu3po/20260804171002-aiagt02.syindex f2fb1fb213..318d717cf2 100644--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20230304000829-9jwu3po/20260804171002-aiagt02.sy+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20230304000829-9jwu3po/20260804171002-aiagt02.sy@@ -6,7 +6,7 @@"id": "20260804171002-aiagt02","title": "AI 智能體","type": "doc",- "updated": "20261021001816"+ "updated": "20261022182404"},"Children": [{@@ -496,7 +496,7 @@"HeadingLevel": 3,"Properties": {"id": "20260804170039-1z57i1s",- "updated": "20260816114944"+ "updated": "20261022182404"},"Children": [{@@ -550,6 +550,79 @@}]},+ {+ "ID": "20261022182404-zoui51x",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-zoui51x",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "輸入框會隨文字和圖片自動增高,達到高度上限後捲動。向上或向下拖曳輸入框頂部的小控制點可調整上限,觸控螢幕也支援拖曳;按兩下控制點可恢復自動上限。內容首尾保留捲動空間,方便完整查看首行和末行。"+ }+ ]+ },+ {+ "ID": "20261022182404-72jon51",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-72jon51",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "高度控制點取得鍵盤焦點後,按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↑"+ },+ {+ "Type": "NodeText",+ "Data": " 或 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↓"+ },+ {+ "Type": "NodeText",+ "Data": " 增大或減小上限,按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Home"+ },+ {+ "Type": "NodeText",+ "Data": " 調至最小高度,按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "End"+ },+ {+ "Type": "NodeText",+ "Data": " 調至目前可用的最大高度;拖曳時按 "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Esc"+ },+ {+ "Type": "NodeText",+ "Data": " 可取消調整。"+ }+ ]+ },{"ID": "20260804170041-t9reg9k","Type": "NodeHeading",diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-ei5t6tt/20260804171006-aiagt04.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-ei5t6tt/20260804171006-aiagt04.syindex 385114b9ff..6fb1907597 100644--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-ei5t6tt/20260804171006-aiagt04.sy+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-ei5t6tt/20260804171006-aiagt04.sy@@ -6,7 +6,7 @@"id": "20260804171006-aiagt04","title": "AI エージェント","type": "doc",- "updated": "20261021001816"+ "updated": "20261022182404"},"Children": [{@@ -488,7 +488,7 @@"HeadingLevel": 3,"Properties": {"id": "20260804170039-9kcdqv4",- "updated": "20260816114944"+ "updated": "20261022182404"},"Children": [{@@ -542,6 +542,79 @@}]},+ {+ "ID": "20261022182404-hxp98ok",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-hxp98ok",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "入力欄は文字や画像に合わせて自動的に高くなり、上限に達するとスクロールします。入力欄の上端にある小さなハンドルを上下にドラッグすると上限を調整できます。タッチ画面でも同じ操作が可能です。ハンドルをダブルクリックすると自動の上限に戻ります。内容の前後にはスクロール用の余白があり、最初と最後の行を確認しやすくなっています。"+ }+ ]+ },+ {+ "ID": "20261022182404-yecrc0p",+ "Type": "NodeParagraph",+ "Properties": {+ "id": "20261022182404-yecrc0p",+ "updated": "20261022182404"+ },+ "Children": [+ {+ "Type": "NodeText",+ "Data": "高さのハンドルにキーボードフォーカスがあるときは、"+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↑"+ },+ {+ "Type": "NodeText",+ "Data": " または "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "↓"+ },+ {+ "Type": "NodeText",+ "Data": " で上限を増減し、"+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Home"+ },+ {+ "Type": "NodeText",+ "Data": " で最小、"+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "End"+ },+ {+ "Type": "NodeText",+ "Data": " で現在利用できる最大の高さに変更できます。ドラッグ中に "+ },+ {+ "Type": "NodeTextMark",+ "TextMarkType": "kbd",+ "TextMarkTextContent": "Esc"+ },+ {+ "Type": "NodeText",+ "Data": " を押すと調整を取り消します。"+ }+ ]+ },{"ID": "20260804170041-sri5ln6","Type": "NodeHeading",diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scssindex 87a3f3bccd..4f39ddbb77 100644--- a/app/src/assets/scss/business/_ai_agent.scss+++ b/app/src/assets/scss/business/_ai_agent.scss@@ -690,12 +690,31 @@flex-shrink: 0;}+.agent-chat__input-resize {+ position: relative;+ height: 0;++ .protyle-block-resize[data-resize-axis="height"] {+ top: -16px;+ bottom: auto;+ opacity: 1;+ }+}+.agent-chat__composer-host {min-height: 40px;- max-height: 120px;- overflow-y: auto;+ max-height: min(240px, 40vh);+ overflow: hidden;cursor: text;+ .protyle-content {+ min-height: 0;+ flex: 0 1 auto;+ padding: 8px 0 24px;+ scroll-padding-block: 8px 24px;+ box-sizing: border-box;+ }+.protyle-wysiwyg .img {& > span:nth-child(2) {width: 96px !important;@@ -1310,10 +1329,6 @@margin: 0 max(8px, env(safe-area-inset-right)) max(16px, calc(env(safe-area-inset-bottom) + 8px)) max(8px, env(safe-area-inset-left));}- .agent-chat__composer-host {- max-height: min(120px, 30vh);- }-.agent-chat__button-options {gap: 4px;}diff --git a/app/src/layout/dock/agent/AgentChat.focus.test.ts b/app/src/layout/dock/agent/AgentChat.focus.test.tsindex ac7047a559..78326ec5df 100644--- a/app/src/layout/dock/agent/AgentChat.focus.test.ts+++ b/app/src/layout/dock/agent/AgentChat.focus.test.ts@@ -43,6 +43,10 @@ test("clicking an editor utility preserves its input focus", () => {assert.equal(clickPanel(".protyle-util"), 0);});+test("clicking the composer resize handle preserves keyboard access without opening the editor", () => {+ assert.equal(clickPanel(".agent-chat__input-resize"), 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.tsindex ba0d621262..e925211a6a 100644--- a/app/src/layout/dock/agent/AgentChat.ts+++ b/app/src/layout/dock/agent/AgentChat.ts@@ -5,6 +5,7 @@ import type {App} from "../../../index";import {AgentHttpError, fetchAgentSSE, IEditorContext, ISSEResult, IToolEffects} from "./agentSSE";import {genUUID} from "../../../util/genID";import {mountComposer, type AgentComposerData} from "./AgentComposer";+import {bindAgentComposerResize} from "./AgentComposerResize";import {disabledWYSIWYG} from "../../../protyle/util/disabledWYSIWYG";import {getAllEditor} from "../../getAll";import {isCapabilityEnabled, listCapabilityManifests, lookupCapability} from "./frontendCapabilities";@@ -197,6 +198,7 @@ export class AgentChat extends Model {private panelElement: HTMLElement;private messagesContainer: HTMLElement;private composerHost: HTMLElement;+ private destroyComposerResize?: () => void;private composer: ReturnType<typeof mountComposer> | null = null;private sendBtn: HTMLElement;private stopBtn: HTMLElement;@@ -343,6 +345,7 @@ export class AgentChat extends Model {}public destroy() {+ this.destroyComposerResize?.();this.welcomeObserver?.disconnect();super.destroy();}@@ -430,6 +433,8 @@ export class AgentChat extends Model {'<span class="agent-chat__scroll-bottom ariaLabel" data-position="west" aria-label="' + L.scrollToBottom + '"><svg><use xlink:href="#iconArrowDown"></use></svg></span>' +"</div>" +'<div class="agent-chat__input-area">' ++ '<div class="agent-chat__input-resize" data-prevent-swipe="true"><span class="protyle-block-resize" data-resize-axis="height" ' ++ 'role="separator" aria-orientation="horizontal" tabindex="0" aria-label="' + escapeAriaLabel(L.height) + '"></span></div>' +'<div class="agent-chat__composer-host"></div>' +'<div class="agent-chat__buttons">' +'<div class="agent-chat__button-options">' +@@ -466,6 +471,9 @@ export class AgentChat extends Model {this.messagesContainer = panel.querySelector(".agent-chat__messages") as HTMLElement;this.composerHost = panel.querySelector(".agent-chat__composer-host") as HTMLElement;+ this.destroyComposerResize?.();+ this.destroyComposerResize = bindAgentComposerResize(panel, this.composerHost,+ panel.querySelector(".agent-chat__input-resize .protyle-block-resize"));this.sendBtn = panel.querySelector(".agent-chat__send") as HTMLElement;this.stopBtn = panel.querySelector(".agent-chat__stop") as HTMLElement;this.imageUploadButton = panel.querySelector(".agent-chat__image-upload") as HTMLButtonElement;@@ -1102,7 +1110,7 @@ export class AgentChat extends Model {return;}// 编辑器及浮层内的点击保留原有焦点和选区,避免聚焦到不可编辑的滚动容器。- if (t.closest(".protyle-util, .protyle-wysiwyg")) {+ if (t.closest(".protyle-util, .protyle-wysiwyg, .agent-chat__input-resize")) {return;}if (this.composer) {diff --git a/app/src/layout/dock/agent/AgentComposerResize.test.ts b/app/src/layout/dock/agent/AgentComposerResize.test.tsnew file mode 100644index 0000000000..66620e0116--- /dev/null+++ b/app/src/layout/dock/agent/AgentComposerResize.test.ts@@ -0,0 +1,191 @@+import * as assert from "node:assert/strict";+import {execFile} from "node:child_process";+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";+import {tmpdir} from "node:os";+import * as path from "node:path";+import {test} from "node:test";+import {promisify} from "node:util";+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";++const cases = async (source: string, css: string, mobile: boolean) => {+ const check: typeof assert = require("node:assert/strict");+ const module = {exports: {} as typeof import("./AgentComposerResize")};+ new Function("module", "exports", source)(module, module.exports);+ const style = document.createElement("style");+ style.textContent = css;+ document.head.append(style);+ document.body.innerHTML = `<div class="sy__agentChat fn__flex-column file-tree dockPanel${mobile ? " sy__agentChat--mobile" : ""}" ` ++ 'style="width:360px;height:760px;min-height:0;box-sizing:border-box">' ++ '<div class="block__icons" style="height:48px">Agent</div>' ++ '<div class="agent-chat__messages-wrap"><div class="agent-chat__messages">Messages</div></div>' ++ '<div class="agent-chat__input-area"><div class="agent-chat__input-resize" data-prevent-swipe="true">' ++ '<span class="protyle-block-resize" data-resize-axis="height" tabindex="0" role="separator"></span></div>' ++ '<div class="agent-chat__composer-host protyle protyle-lite-fragment" data-protyle-lite="true">' ++ '<div class="protyle-content"><div class="protyle-wysiwyg" spellcheck="false"></div></div></div>' ++ '<div class="agent-chat__buttons"><button class="b3-button">Send</button></div></div></div>';+ const panel = document.querySelector<HTMLElement>(".sy__agentChat");+ const composer = panel.querySelector<HTMLElement>(".agent-chat__composer-host");+ const content = composer.querySelector<HTMLElement>(".protyle-content");+ const editor = composer.querySelector<HTMLElement>(".protyle-wysiwyg");+ const handle = panel.querySelector<HTMLElement>(".protyle-block-resize");+ const tick = () => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));+ const paragraph = (text: string) => '<div class="p" data-node-id="20261022000000-abcdefg" data-type="NodeParagraph">' ++ `<div contenteditable="true" spellcheck="false">${text}</div></div>`;+ const short = () => editor.innerHTML = paragraph("Short input");+ short();+ const destroy = module.exports.bindAgentComposerResize(panel, composer, handle);+ await tick();+ const small = composer.clientHeight;+ check.ok(small < 100, `short input stays compact: ${small}`);+ const image = '<span class="img"><span></span><span><img alt="Image" ' ++ 'src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"></span></span>';+ editor.innerHTML = paragraph(image) + paragraph("After image") + paragraph("Last line");+ await tick();+ check.ok(composer.clientHeight > 120, "image and several lines can grow beyond the old limit");+ check.ok(composer.clientHeight < 240, "shorter content grows only as needed");+ const long = () => editor.innerHTML = paragraph(image) + Array.from({length: 24}, (_, i) => paragraph(`Line ${i + 1}`)).join("");+ long();+ await tick();+ check.equal(composer.clientHeight, 240);+ check.equal(composer.scrollHeight, composer.clientHeight, "only the inner editor scrolls");+ check.ok(content.scrollHeight > content.clientHeight);+ content.scrollTop = content.scrollHeight;+ await tick();+ const bottom = content.getBoundingClientRect().bottom - editor.lastElementChild.getBoundingClientRect().bottom;+ check.ok(bottom >= 23, `last line has bottom scroll space: ${bottom}`);+ content.scrollTop = 0;+ check.ok(editor.firstElementChild.getBoundingClientRect().top - content.getBoundingClientRect().top >= 7);++ const key = (name: string) => handle.dispatchEvent(new KeyboardEvent("keydown", {key: name, bubbles: true, cancelable: true}));+ handle.focus();+ key("ArrowUp");+ check.equal(composer.style.maxHeight, "264px");+ key("ArrowDown");+ check.equal(composer.style.maxHeight, "240px");+ key("End");+ check.equal(composer.style.maxHeight, "600px", `panel ${panel.clientHeight} ${getComputedStyle(panel).display} ${getComputedStyle(panel).minHeight}`);+ await tick();+ check.ok(panel.querySelector(".agent-chat__buttons").getBoundingClientRect().bottom <= panel.getBoundingClientRect().bottom);+ check.ok(panel.querySelector(".agent-chat__messages-wrap").clientHeight > 30);++ // 原生指针事件验证捕获、鼠标拖动以及触摸取消。+ handle.dispatchEvent(new MouseEvent("dblclick", {bubbles: true}));+ await tick();+ const rect = handle.getBoundingClientRect();+ const x = Math.round(rect.left + rect.width / 2);+ const y = Math.round(rect.top + rect.height / 2);+ await require("electron").ipcRenderer.invoke("composer-drag", {x, y, dy: -100, touch: false});+ check.ok(composer.clientHeight >= 338 && composer.clientHeight <= 342, "mouse dragging raises the height cap");+ check.equal(handle.classList.contains("touch-resize-active"), false);+ const preferred = composer.style.maxHeight;+ const touchRect = handle.getBoundingClientRect();+ await require("electron").ipcRenderer.invoke("composer-drag", {+ x: Math.round(touchRect.left + touchRect.width / 2), y: Math.round(touchRect.top + touchRect.height / 2),+ dy: -50, touch: true,+ });+ check.equal(composer.style.maxHeight, preferred, "touch cancellation restores the previous cap");+ check.equal(handle.classList.contains("touch-resize-active"), false);++ for (const fontSize of [16, 32]) {+ panel.style.setProperty("--b3-font-size", `${fontSize}px`);+ for (const width of [240, 360, 760]) {+ panel.style.width = `${width}px`;+ panel.style.height = "400px";+ await tick();+ check.equal(composer.style.maxHeight, "240px", "smaller panels clamp an expanded composer");+ check.ok(panel.querySelector(".agent-chat__buttons").getBoundingClientRect().bottom <= panel.getBoundingClientRect().bottom + 1);+ check.ok(content.clientWidth <= composer.clientWidth);+ content.scrollTop = content.scrollHeight;+ check.ok(editor.lastElementChild.getBoundingClientRect().bottom <= content.getBoundingClientRect().bottom - 23);+ }+ }+ panel.style.height = "760px";+ panel.style.paddingBottom = "400px";+ await tick();+ check.equal(composer.style.maxHeight, "200px", "mobile keyboard padding reduces available content height");+ panel.style.paddingBottom = "0";+ await tick();+ check.equal(composer.style.maxHeight, preferred, "reopening the available space restores the chosen cap");+ short();+ await tick();+ check.ok(composer.clientHeight < 120, "clearing long content returns to intrinsic height");+ const clickRect = handle.getBoundingClientRect();+ await require("electron").ipcRenderer.invoke("composer-drag", {+ x: Math.round(clickRect.left + clickRect.width / 2), y: Math.round(clickRect.top + clickRect.height / 2),+ dy: 0, touch: false,+ });+ check.equal(composer.style.maxHeight, preferred, "clicking without dragging does not shrink the automatic growth limit");+ handle.dispatchEvent(new MouseEvent("dblclick", {bubbles: true}));+ check.equal(composer.style.maxHeight, "240px", "double click restores the automatic cap");+ long();+ await tick();+ destroy();+ key("End");+ check.equal(composer.style.maxHeight, "", "destroy removes listeners and the temporary geometry");+ return "Composer resize and scrolling passed";+};++test("composer grows, resizes and preserves scroll space on desktop and mobile", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 60000,+}, async () => {+ const source = transpileModule(readFileSync(path.join(__dirname, "AgentComposerResize.ts"), "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+ }).outputText;+ const sass = require("sass");+ const sheets = ["base", "mobile"].map(name => ({name, css: sass.compile(path.resolve(__dirname,+ `../../../assets/scss/${name}.scss`), {logger: sass.Logger.silent}).css}));+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-composer-resize-"));+ const script = path.join(temporary, "run.cjs");+ writeFileSync(script, `const {app, BrowserWindow, ipcMain} = require("electron");+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});+app.whenReady().then(async () => {+ const win = new BrowserWindow({show: false, width: 1000, height: 900,+ webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});+ win.webContents.debugger.attach("1.3");+ ipcMain.handle("composer-drag", async (_event, {x, y, dy, touch}) => {+ const send = async (method, params) => {+ await win.webContents.debugger.sendCommand(method, params);+ await new Promise(resolve => setTimeout(resolve, 40));+ };+ if (touch) {+ await send("Emulation.setTouchEmulationEnabled", {enabled: true});+ await send("Input.dispatchTouchEvent", {type: "touchStart", touchPoints: [{x, y}]});+ await send("Input.dispatchTouchEvent", {type: "touchMove", touchPoints: [{x, y: y + dy}]});+ await send("Input.dispatchTouchEvent", {type: "touchCancel", touchPoints: []});+ await send("Emulation.setTouchEmulationEnabled", {enabled: false});+ } else {+ await send("Input.dispatchMouseEvent", {type: "mousePressed", x, y, button: "left", buttons: 1, clickCount: 1});+ await send("Input.dispatchMouseEvent", {type: "mouseMoved", x, y: y + dy, button: "left", buttons: 1});+ await send("Input.dispatchMouseEvent", {type: "mouseReleased", x, y: y + dy, button: "left", buttons: 0, clickCount: 1});+ }+ });+ try {+ for (const sheet of ${JSON.stringify(sheets)}) {+ for (const theme of ["daylight", "midnight"]) {+ await win.loadURL("data:text/html,<html><body></body></html>");+ const css = require("node:fs").readFileSync(${JSON.stringify(path.resolve(__dirname, "../../../../appearance/themes"))} + "/" + theme + "/theme.css", "utf8") + sheet.css;+ console.log(await win.webContents.executeJavaScript(+ "const __name = value => value; (" + ${JSON.stringify(cases.toString())} + ")(" ++ JSON.stringify(${JSON.stringify(source)}) + "," + JSON.stringify(css) + "," + (sheet.name === "mobile") + ")"));+ }+ }+ win.destroy();+ app.exit(0);+ } catch (error) {+ console.error(error);+ win.destroy();+ app.exit(1);+ }+});`, "utf8");+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ try {+ const result = await promisify(execFile)(require("electron"), [script], {env, windowsHide: true, timeout: 50000});+ assert.equal(result.stdout.match(/Composer resize and scrolling passed/g)?.length, 4);+ } finally {+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(tmpdir()));+ assert.ok(path.basename(temporary).startsWith("siyuan-composer-resize-"));+ rmSync(temporary, {recursive: true, force: true, maxRetries: 10, retryDelay: 100});+ }+});diff --git a/app/src/layout/dock/agent/AgentComposerResize.ts b/app/src/layout/dock/agent/AgentComposerResize.tsnew file mode 100644index 0000000000..50b60f8f3a--- /dev/null+++ b/app/src/layout/dock/agent/AgentComposerResize.ts@@ -0,0 +1,116 @@+// 上方把手调节输入框的最大高度,正文仍按实际内容自动增高或收缩。+export const bindAgentComposerResize = (panel: HTMLElement, composer: HTMLElement, handle: HTMLElement) => {+ const windowSelf = panel.ownerDocument.defaultView;+ let preferred: number | undefined;+ let maximum = 240;+ let value = 240;+ let drag: {id: number, y: number, height: number, previous?: number, moved?: boolean} | undefined;+ const update = () => {+ const style = windowSelf.getComputedStyle(panel);+ const height = Math.min(panel.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom),+ windowSelf.visualViewport?.height || windowSelf.innerHeight);+ if (!height) {+ return;+ }+ // 给标题、操作栏和会话正文留出空间,软键盘弹出时也不能把发送按钮挤出面板。+ maximum = Math.max(40, Math.floor(height - 160));+ value = Math.round(Math.max(40, Math.min(maximum, preferred ?? Math.min(240, height * .4))));+ composer.style.maxHeight = `${value}px`;+ handle.setAttribute("aria-valuemin", "40");+ handle.setAttribute("aria-valuemax", String(maximum));+ handle.setAttribute("aria-valuenow", String(value));+ };+ const finish = (cancel: boolean) => {+ if (!drag) {+ return;+ }+ const previous = drag;+ drag = undefined;+ if (cancel) {+ preferred = previous.previous;+ update();+ }+ handle.classList.remove("touch-resize-active");+ if (handle.hasPointerCapture(previous.id)) {+ handle.releasePointerCapture(previous.id);+ }+ };+ const down = (event: PointerEvent) => {+ if (event.button !== 0 || drag) {+ return;+ }+ event.preventDefault();+ event.stopPropagation();+ drag = {id: event.pointerId, y: event.clientY, height: composer.clientHeight, previous: preferred};+ handle.setPointerCapture(event.pointerId);+ handle.classList.add("touch-resize-active");+ handle.focus({preventScroll: true});+ };+ const move = (event: PointerEvent) => {+ if (!drag || drag.id !== event.pointerId) {+ return;+ }+ if (!drag.moved && Math.abs(event.clientY - drag.y) < 3) {+ return;+ }+ event.preventDefault();+ drag.moved = true;+ preferred = Math.max(40, Math.min(maximum, drag.height + drag.y - event.clientY));+ update();+ };+ const up = (event: PointerEvent) => {+ if (drag?.id === event.pointerId) {+ move(event);+ finish(false);+ }+ };+ const cancel = () => finish(true);+ const keydown = (event: KeyboardEvent) => {+ if (event.key === "Escape" && drag) {+ cancel();+ } else if (event.key === "ArrowUp" || event.key === "ArrowDown") {+ preferred = Math.max(40, Math.min(maximum, value + (event.key === "ArrowUp" ? 24 : -24)));+ update();+ } else if (event.key === "Home" || event.key === "End") {+ preferred = event.key === "Home" ? 40 : maximum;+ update();+ } else {+ return;+ }+ event.preventDefault();+ event.stopPropagation();+ };+ const reset = () => {+ finish(true);+ preferred = undefined;+ update();+ };+ handle.addEventListener("pointerdown", down);+ handle.addEventListener("pointermove", move);+ handle.addEventListener("pointerup", up);+ handle.addEventListener("pointercancel", cancel);+ handle.addEventListener("lostpointercapture", cancel);+ handle.addEventListener("keydown", keydown);+ handle.addEventListener("dblclick", reset);+ windowSelf.addEventListener("blur", cancel);+ windowSelf.addEventListener("resize", update);+ windowSelf.visualViewport?.addEventListener("resize", update);+ const observer = new ResizeObserver(update);+ observer.observe(panel);+ update();+ return () => {+ finish(true);+ observer.disconnect();+ handle.removeEventListener("pointerdown", down);+ handle.removeEventListener("pointermove", move);+ handle.removeEventListener("pointerup", up);+ handle.removeEventListener("pointercancel", cancel);+ handle.removeEventListener("lostpointercapture", cancel);+ handle.removeEventListener("keydown", keydown);+ handle.removeEventListener("dblclick", reset);+ windowSelf.removeEventListener("blur", cancel);+ windowSelf.removeEventListener("resize", update);+ windowSelf.visualViewport?.removeEventListener("resize", update);+ composer.style.maxHeight = "";+ };+};