diff --git a/packages/chat/chat/app.js b/packages/chat/chat/app.js index 81af9b4..ee661a8 100644 --- a/packages/chat/chat/app.js +++ b/packages/chat/chat/app.js @@ -889,11 +889,24 @@ function positionPop(pop, btn) { left = Math.max(8, Math.min(left, host.width - width - 8)); pop.style.left = Math.round(left) + "px"; - // 向上展开:输入区上缘以上能有多高,就用多高 - var above = r.top - 10; - pop.style.bottom = "calc(100% + 6px)"; - pop.style.top = "auto"; - pop.style.maxHeight = Math.max(220, Math.round(above)) + "px"; + // 菜单的下缘贴着输入区上缘(bottom: 100% + 6px),所以上面能有多高要从输入区算, + // 不是从按钮算:按钮在输入区里面,从它算会多出一截,菜单顶部就伸出窗口、 + // 被页面框裁掉,最前面的模型滚不到。不留 220px 的下限,窗口矮就矮着,菜单自己滚。 + var pad = 8, gap = 6; + pop.style.maxHeight = ""; + var natural = pop.scrollHeight + 2; + var above = host.top - gap - pad; + var below = innerHeight - r.bottom - gap - pad; + if (natural > above && below > above) { + // 上面放不下、下面更宽裕:从按钮下面往下展开 + pop.style.top = Math.round(r.bottom - host.top + gap) + "px"; + pop.style.bottom = "auto"; + pop.style.maxHeight = Math.max(0, Math.floor(below)) + "px"; + } else { + pop.style.bottom = "calc(100% + " + gap + "px)"; + pop.style.top = "auto"; + pop.style.maxHeight = Math.max(0, Math.floor(above)) + "px"; + } } function openPop(id, btn) { diff --git a/packages/chat/package.json b/packages/chat/package.json index c602b57..29c0411 100644 --- a/packages/chat/package.json +++ b/packages/chat/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/page-chat", - "version": "0.1.13", + "version": "0.1.14", "description": "在 magpie 窗口里加一个「对话」页面:用 magpie 自己的模型、MCP 服务器、指令和技能聊天,支持图片与 LaTeX。A chat page inside magpie's window: talk with magpie's own models, MCP servers, instructions and skills, with images and LaTeX.", "author": "Magic_zero (https://github.com/Magiczerowxy)", "files": [ diff --git a/packages/chat/pop-clamp.gui.mjs b/packages/chat/pop-clamp.gui.mjs new file mode 100644 index 0000000..53068c9 --- /dev/null +++ b/packages/chat/pop-clamp.gui.mjs @@ -0,0 +1,56 @@ +// The model picker opens above the composer. With many models in a short +// window its top used to run out of the page, where the page frame clips it, +// so the first models could not be scrolled to. This drives the real page in +// Chromium and WebKit against a stub bootstrap with 60 models. +// Run: NODE_PATH= node pop-clamp.gui.mjs +import { createServer } from "node:http" +import { readFileSync, existsSync } from "node:fs" +import { join, extname } from "node:path" +import { createRequire } from "node:module" + +const require = createRequire(import.meta.url) +const { chromium, webkit } = require("playwright") +const root = join(import.meta.dirname, "chat") +const types = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml" } +const models = Array.from({ length: 60 }, (_, i) => ({ id: "p/model-" + i, name: "Model " + i, provider: "p", efforts: [] })) + +const srv = createServer((req, res) => { + const path = new URL(req.url, "http://x").pathname + if (path === "/api/bootstrap") { + res.setHeader("content-type", "application/json") + return res.end(JSON.stringify({ models, settings: { model: "p/model-0" }, library: { instructions: [], skills: [], mcp: [] }, version: "0" })) + } + if (path.startsWith("/api/")) { res.setHeader("content-type", "application/json"); return res.end("[]") } + const f = join(root, path === "/" ? "index.html" : path) + if (!f.startsWith(root) || !existsSync(f)) { res.statusCode = 404; return res.end() } + res.setHeader("content-type", types[extname(f)] || "application/octet-stream") + res.end(readFileSync(f)) +}).listen(0, "127.0.0.1") +await new Promise((r) => srv.once("listening", r)) +const url = "http://127.0.0.1:" + srv.address().port + "/" + +let bad = 0 +for (const [name, engine] of [["chromium", chromium], ["webkit", webkit]]) { + const browser = await engine.launch() + for (const h of [320, 420, 600, 900]) { + const page = await browser.newPage({ viewport: { width: 1000, height: h } }) + await page.goto(url) + await page.waitForFunction(() => document.querySelector("#modelBtn") && !document.querySelector("#modelBtn").disabled) + await page.click("#modelBtn") + const r = await page.evaluate(async () => { + const pop = document.querySelector("#modelPop") + const b = pop.getBoundingClientRect() + pop.scrollTop = 0 + const first = pop.querySelector(".pop-item").getBoundingClientRect() + const p = pop.getBoundingClientRect() + return { top: b.top, bottom: b.bottom, vh: innerHeight, firstVisible: first.top >= p.top - 0.5 && first.bottom <= p.bottom + 0.5, scrolls: pop.scrollHeight > pop.clientHeight } + }) + const ok = r.top >= 0 && r.bottom <= r.vh && r.firstVisible && r.scrolls + console.log(ok ? "ok " : "FAIL", name, h, JSON.stringify(r)) + if (!ok) bad++ + await page.close() + } + await browser.close() +} +srv.close() +process.exit(bad ? 1 : 0)