diff --git a/.agents/notes/implemented/architecture/2026-11-14-web-view-previews.md b/.agents/notes/implemented/architecture/2026-11-14-web-view-previews.md index 2c2eaad6f6..6b8da822b5 100644 --- a/.agents/notes/implemented/architecture/2026-11-14-web-view-previews.md +++ b/.agents/notes/implemented/architecture/2026-11-14-web-view-previews.md @@ -40,12 +40,17 @@ SVG 只能当文本看,PDF 只能交给外部应用。 - **SVG 跟随编辑**。SVG 文件按 Markdown 的方式打开:源码与图片两栏,标签栏末尾切换源码 / 两者 / 仅图片。 编辑后最多每 150 毫秒把文本交给页面一次,地址带上编辑版本号,避免引擎画出缓存的旧图;更新时保持用户的 缩放和滚动位置。改成画不出来的内容时保留上一张图,只有从未画成功过才显示失败提示。超过 4 MiB 不绘制。 -- **Markdown 目录画在页面里**。web 页的目录是页面右上角的浮动大纲(`assets/markdown.js`):页面从渲染结果的 +- **Markdown 目录画在页面里**。web 页的目录是页面右侧的大纲栏(`assets/markdown.js`):页面从渲染结果的 标题建树,按层级缩进,可以折叠,当前章节随页面滚动高亮,选中标题时像用户滚动页面一样回报 `scrolled`, 宿主照常把源码带到那一行。标签栏的“目录”按钮只发 `outline{open}` 打开或关闭;用户在页面里按 Escape 关闭时, 页面回报 `outline{open:false}`,宿主更新按钮状态,不再回发。大纲按固定行高只创建视口附近的行,长文档也不会 一次生成上千个节点;折叠状态按“父级、层级、标题文字、同名序号”保存,重新渲染后保留。原生页仍用 Viva 的 弹出列表,选中后关闭。 +- **目录不遮挡正文**。预览宽于 680 像素时(与旧 macOS 预览相同),打开目录会让正文右侧让出目录栏的宽度, + 目录栏贴右侧占满高度,底色与页面相同;有工作台背景图时页面是透明的,目录栏也透出背景图。打开或关闭时正文 + 重新换行,页面保持顶部那一行不动。更窄时目录浮在正文上方,底色用宿主的 `--lithe-popup-background`。 + 宿主把这个变量设为 Ground:背景图下面板和编辑区颜色是透明的,Ground 仍不透明,所以浮层不会透出下面的文字。 + 插件页的弹层用的也是这个变量。 - **页面生命周期**。文档或标签打开期间页面一直存在;没有被这一帧声明的页面隐藏起来,保留滚动位置和排版, 切回来时原样显示。隐藏的页面最多保留 6 个,超出时关闭隐藏最久的那个,标签再次显示时重新打开。 窗口的 web host 还没准备好、系统没有 web 引擎、或者某个页面打开失败后,编辑区改用原生预览。 @@ -73,6 +78,8 @@ SVG 只能当文本看,PDF 只能交给外部应用。 - **web 页的目录也用 Viva 弹出列表画在页面上方**:弹层选中一个标题就关闭,不能一边看目录一边跳转; 要高亮当前章节,宿主还得逐帧接收页面滚动位置再重画。页面本来就知道自己的滚动位置和标题位置, 所以改为页面自己画,宿主只发开关。 +- **目录始终浮在正文上方**:宽预览里会挡住正文右侧;有背景图时页面底色透明,浮层下的文字会和目录叠在一起。 + 拒绝,改为宽预览让出位置、窄预览才浮起并用不透明的弹层底色。 - **隐藏的页面立即关闭,或者全部常驻**:立即关闭会在每次切标签时丢掉滚动位置并重新排版;全部常驻会让每个 打开过的预览都占一个 web 进程的内存。折中为最多保留 6 个隐藏页面。 @@ -102,7 +109,8 @@ SVG 和 PDF 可以在编辑区直接查看;页面能读到什么完全由宿 SVG 跟随编辑与失败保留,以及没有 web 引擎时的 Go 画布。 - `scripts/desktop/verify/verify-runtime-bundle-immutability.sh`:确认打开预览不会改写发行目录。 - `TestMarkdownWebPageRendersCoreHTMLAndScrollsWithTheSource` 覆盖目录按钮与页面之间的 `outline` 消息, - `TestMarkdownContentsIndentsHeadingsByLevel` 覆盖原生弹出列表按级别缩进且行不重叠。 + `TestMarkdownContentsIndentsHeadingsByLevel` 覆盖原生弹出列表按级别缩进且行不重叠, + `TestPagePopupColorStaysOpaqueOverTheBackground` 覆盖背景图下页面透明而弹层颜色不透明。 - 平台功能矩阵 `embedded-viewers`、`markdown-rendering`、`markdown-images-links`、`markdown-preview-outline` 记录了待真机验收的步骤。 @@ -112,6 +120,7 @@ SVG 和 PDF 可以在编辑区直接查看;页面能读到什么完全由宿 - `desktop/internal/workbench/preview_pages.go` - `desktop/internal/workbench/markdown_web.go` - `desktop/internal/workbench/markdown_preview.go` +- `desktop/internal/workbench/pluginpages.go`(页面主题变量) - `desktop/internal/workbench/viewer_web.go` - `desktop/internal/workbench/viewer_image.go` - `desktop/internal/workbench/svg_preview.go` diff --git a/desktop/internal/webpreview/assets/markdown.css b/desktop/internal/webpreview/assets/markdown.css index 9df7eee724..2986a54f40 100644 --- a/desktop/internal/webpreview/assets/markdown.css +++ b/desktop/internal/webpreview/assets/markdown.css @@ -249,29 +249,57 @@ audio { width: min(100%, 520px); } .diagram-error { margin: 0 0 0.75em; color: var(--alert-caution); font-size: 0.84em; } .diagram-source-fallback { margin-top: 0.75em; } -/* The outline floats at the page's top right, over the margin where the window is wide enough. +/* The outline is a column down the page's right side. Where the page is wider than 680px, as in + the macOS preview before it, the text makes room for it (body.outline-open), so it covers + nothing and shares the page's color, which is clear over a workbench background image. + Narrower, it floats over the text on the popup color, which stays opaque over the image. Its rows are a fixed height: the script lays out only those in view (markdown.js). */ +:root { + --outline-width: 280px; + --outline-inset: 12px; + --popup: var(--lithe-popup-background, #ffffff); +} + +:root[data-theme="dark"] { --popup: var(--lithe-popup-background, #1e1f22); } + .outline { position: fixed; z-index: 50; - top: 10px; - right: 12px; + top: var(--outline-inset); + right: var(--outline-inset); + bottom: var(--outline-inset); display: flex; flex-direction: column; - width: min(280px, calc(100vw - 24px)); - max-height: calc(100vh - 20px); + width: var(--outline-width); border: 1px solid var(--border); border-radius: 8px; background: var(--page); color: var(--text); - box-shadow: 0 8px 28px rgba(15, 18, 24, 0.14), 0 1px 3px rgba(15, 18, 24, 0.08); font-size: 13px; line-height: 1.3; } :root[data-theme="dark"] .outline { background: color-mix(in srgb, var(--lithe-foreground, #dfe1e5) 3%, var(--page)); - box-shadow: 0 10px 32px rgba(0, 0, 0, 0.42), 0 1px 3px rgba(0, 0, 0, 0.3); +} + +@media (min-width: 681px) { + body.outline-open { padding-right: calc(var(--outline-width) + 2 * var(--outline-inset)); } +} + +@media (max-width: 680px) { + .outline { + bottom: auto; + width: min(var(--outline-width), calc(100vw - 2 * var(--outline-inset))); + max-height: calc(100vh - 2 * var(--outline-inset)); + background: var(--popup); + box-shadow: 0 8px 28px rgba(15, 18, 24, 0.14), 0 1px 3px rgba(15, 18, 24, 0.08); + } + + :root[data-theme="dark"] .outline { + background: color-mix(in srgb, var(--lithe-foreground, #dfe1e5) 3%, var(--popup)); + box-shadow: 0 10px 32px rgba(0, 0, 0, 0.42), 0 1px 3px rgba(0, 0, 0, 0.3); + } } .outline[hidden] { display: none; } @@ -287,7 +315,7 @@ audio { width: min(100%, 520px); } } .outline-tree { - flex: 0 1 auto; + flex: 1 1 auto; min-height: 0; overflow-x: hidden; overflow-y: auto; diff --git a/desktop/internal/webpreview/assets/markdown.js b/desktop/internal/webpreview/assets/markdown.js index d47393df46..402ae80a83 100644 --- a/desktop/internal/webpreview/assets/markdown.js +++ b/desktop/internal/webpreview/assets/markdown.js @@ -540,10 +540,17 @@ } // showOutline shows the outline while it is open and the page has headings, its rows laid out - // again. Opened, it scrolls to the section. + // again. Opened, it scrolls to the section. Shown or hidden, the text narrows or widens where + // the page is wide (markdown.css) and the line at the top stays there. function showOutline() { const hidden = !outlineOpen || entries.length === 0; const opening = outline.hidden && !hidden; + if (document.body.classList.contains('outline-open') === hidden) { + if (points && expectedY === null) topLine = lineOfY(window.scrollY + margin); + document.body.classList.toggle('outline-open', !hidden); + points = null; + placeLine(topLine); + } outline.hidden = hidden; layoutOutline(); if (opening) reveal(sectionRow()); diff --git a/desktop/internal/workbench/pluginpages.go b/desktop/internal/workbench/pluginpages.go index 2aa2763231..aa19037838 100644 --- a/desktop/internal/workbench/pluginpages.go +++ b/desktop/internal/workbench/pluginpages.go @@ -188,7 +188,9 @@ func pluginPageLocale(language i18n.Language) string { } // pluginPageTheme is the window's look in the --lithe-* variables pages style -// themselves with, the names the macOS client gave them. +// themselves with, the names the macOS client gave them. Over a workbench background +// image the panel and editor colors are clear so the image shows through a page; a +// popup floats over the page's own content, so it gets the ground, which stays opaque. func (a *App) pluginPageTheme() core.PluginWebTheme { c := a.colors theme := "light" @@ -199,7 +201,7 @@ func (a *App) pluginPageTheme() core.PluginWebTheme { "--lithe-background": cssColor(c.Panel), "--lithe-surface": cssColor(c.Editor), "--lithe-header": cssColor(c.Panel), - "--lithe-popup-background": cssColor(c.Editor), + "--lithe-popup-background": cssColor(c.Ground), "--lithe-foreground": cssColor(c.Text), "--lithe-secondary-foreground": cssColor(c.Subtle), "--lithe-border": cssColor(c.Line), diff --git a/desktop/internal/workbench/workbench_background_test.go b/desktop/internal/workbench/workbench_background_test.go index a896692337..50cdc54b09 100644 --- a/desktop/internal/workbench/workbench_background_test.go +++ b/desktop/internal/workbench/workbench_background_test.go @@ -208,6 +208,26 @@ func TestChosenBackgroundIsCopiedAndPaintedBehindTheOpenProject(t *testing.T) { } } +// A page is clear over the background image, but what floats over the page's own text, such +// as the Markdown preview's outline in a narrow pane, is drawn on the opaque ground. +func TestPagePopupColorStaysOpaqueOverTheBackground(t *testing.T) { + h := newBackgroundHarness(t, t.TempDir()) + path, _ := writeBackgroundPNG(t, t.TempDir(), "sunset.png", 400, 200) + h.chooseBackground(path) + h.open(t.TempDir()) + if !h.app.backdropShown() { + t.Fatal("the open project does not show the background image") + } + variables := h.app.pluginPageTheme().Variables + if surface := variables["--lithe-surface"]; surface != "rgba(0, 0, 0, 0)" { + t.Fatalf("page surface over the background = %q, want clear", surface) + } + ground := h.app.colors.Ground + if popup := variables["--lithe-popup-background"]; ground.Alpha < 1 || popup != cssColor(ground) { + t.Fatalf("popup color over the background = %q, want the opaque ground %q", popup, cssColor(ground)) + } +} + func TestBackgroundIsRestoredAfterRestartAndRemovedWithItsCopy(t *testing.T) { dir := t.TempDir() first := newBackgroundHarness(t, dir) diff --git a/shared/platform-feature-matrix/features/markdown-preview-outline.json b/shared/platform-feature-matrix/features/markdown-preview-outline.json index 07c88a9857..bdcc22480e 100644 --- a/shared/platform-feature-matrix/features/markdown-preview-outline.json +++ b/shared/platform-feature-matrix/features/markdown-preview-outline.json @@ -2,7 +2,7 @@ "id": "markdown-preview-outline", "area": "编辑器", "group": "Markdown", - "capability": "Markdown 预览目录:右上角浮动大纲,按层级缩进与折叠,跟随当前章节并跳转到标题", + "capability": "Markdown 预览目录:右侧大纲栏,宽预览时正文让出位置不遮挡,按层级缩进与折叠,跟随当前章节并跳转到标题", "macos": { "implementationStatus": "implemented", "verificationStatus": "pending", @@ -14,7 +14,9 @@ "desktop/internal/webpreview/assets/markdown.js", "desktop/internal/webpreview/assets/markdown.css", "desktop/internal/workbench/markdown_preview_test.go", - "desktop/internal/workbench/markdown_web_test.go" + "desktop/internal/workbench/markdown_web_test.go", + "desktop/internal/workbench/pluginpages.go", + "desktop/internal/workbench/workbench_background_test.go" ] }, "windows": { @@ -28,11 +30,13 @@ "desktop/internal/webpreview/assets/markdown.js", "desktop/internal/webpreview/assets/markdown.css", "desktop/internal/workbench/markdown_preview_test.go", - "desktop/internal/workbench/markdown_web_test.go" + "desktop/internal/workbench/markdown_web_test.go", + "desktop/internal/workbench/pluginpages.go", + "desktop/internal/workbench/workbench_background_test.go" ] }, "owner": "Editor", - "verification": "在 macOS 原生 Lithe 打开含 60 节标题的 Markdown,验证默认展开、全高目录独立滚动、末尾标题跳转后保持展开、按钮与 Escape 收起;分栏中编辑保存及外部刷新保留折叠选择,删除全部标题隐藏目录,恢复标题保留展开偏好;检查短文档与无标题文档、宽预览正文避让和窄预览可收起。补充无标题时按 Escape 关闭图片后恢复标题的展开偏好验证。Viva 新桌面端(web view 页):在深浅主题打开含多级标题与 80 节以上标题的 Markdown,点“目录”按钮在页面右上角显示大纲面板:标题栏与分隔线、每深一级多缩进一档、有子标题的行带折叠箭头且折叠只改大纲不滚动页面、超长标题省略;点行或用方向键与回车选标题后源码与渲染页一起滚到该标题,当前章节高亮随页面滚动更新、折叠时落在可见的上级;编辑后重新渲染保留折叠;面板内只排版视口附近的行;按 Escape 或再点按钮关闭面板并同步按钮状态;无标题时隐藏面板与按钮。Viva 原生页(Linux 无 web 引擎):弹出标题列表按级别缩进且行不重叠。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview -run 'Markdown|Preview|Serves|EveryPage' -count=1;页面交互用无头 Chrome 驱动 markdown.js 断言折叠、跳转、键盘、虚拟化与当前章节高亮。", + "verification": "在 macOS 原生 Lithe 打开含 60 节标题的 Markdown,验证默认展开、全高目录独立滚动、末尾标题跳转后保持展开、按钮与 Escape 收起;分栏中编辑保存及外部刷新保留折叠选择,删除全部标题隐藏目录,恢复标题保留展开偏好;检查短文档与无标题文档、宽预览正文避让和窄预览可收起。补充无标题时按 Escape 关闭图片后恢复标题的展开偏好验证。Viva 新桌面端(web view 页):在深浅主题打开含多级标题与 80 节以上标题的 Markdown,点“目录”按钮显示右侧大纲栏:预览宽于 680 像素时正文让出目录栏宽度、目录栏占满高度且不遮挡正文,打开与关闭时顶部那一行不动;更窄的分栏中目录浮在正文上方且底色不透明;设置工作台背景图后重复以上检查,目录与正文不叠字;标题栏与分隔线、每深一级多缩进一档、有子标题的行带折叠箭头且折叠只改大纲不滚动页面、超长标题省略;点行或用方向键与回车选标题后源码与渲染页一起滚到该标题,当前章节高亮随页面滚动更新、折叠时落在可见的上级;编辑后重新渲染保留折叠;面板内只排版视口附近的行;按 Escape 或再点按钮关闭面板并同步按钮状态;无标题时隐藏面板与按钮。Viva 原生页(Linux 无 web 引擎):弹出标题列表按级别缩进且行不重叠。自动回归:scripts/desktop/desktop-go.sh test ./internal/workbench ./internal/webpreview -run 'Markdown|Preview|Serves|EveryPage|PagePopup' -count=1;页面交互用无头 Chrome 驱动 markdown.js 断言折叠、跳转、键盘、虚拟化、当前章节高亮、宽页正文避让与窄页不透明浮层。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36", "macos": { @@ -51,5 +55,5 @@ "verificationStatus": "not-applicable" } }, - "notes": "Viva:标签栏模式切换前的“目录”按钮(可展开样式,打开时高亮)。有系统 web 引擎(macOS WKWebView、Windows WebView2)时,大纲画在 web view 页内:页面从渲染结果的标题(带源码行)建树,右上角浮动面板,固定行高虚拟化,只创建视口附近的行;折叠状态按父级、层级、文本和同名序号组成的稳定键保存,重新渲染后保留;当前章节随页面顶行高亮。宿主用 outline{open} 消息打开或关闭面板,页面内按 Escape 关闭时回报 outline{open:false} 同步按钮。Linux 没有 web 引擎时仍用 Viva 原生弹出列表,按级别缩进,点击或回车跳转并关闭。无标题时不显示按钮。" + "notes": "Viva:标签栏模式切换前的“目录”按钮(可展开样式,打开时高亮)。有系统 web 引擎(macOS WKWebView、Windows WebView2)时,大纲画在 web view 页内:页面从渲染结果的标题(带源码行)建树,显示为右侧大纲栏,固定行高虚拟化,只创建视口附近的行;预览宽于 680 像素时正文让出目录栏宽度(与旧 macOS 预览相同),更窄时浮在正文上方,底色用宿主的 --lithe-popup-background,宿主取不透明的 Ground,工作台背景图下也不透字;折叠状态按父级、层级、文本和同名序号组成的稳定键保存,重新渲染后保留;当前章节随页面顶行高亮。宿主用 outline{open} 消息打开或关闭面板,页面内按 Escape 关闭时回报 outline{open:false} 同步按钮。Linux 没有 web 引擎时仍用 Viva 原生弹出列表,按级别缩进,点击或回车跳转并关闭。无标题时不显示按钮。" }