Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Agent 笔记:编辑区的 web view 预览(Markdown、图片、SVG、PDF)
# Agent 笔记:编辑区的 web view 预览(Markdown、图片、SVG、PDF、HTML)

状态:已实现

## 先说结论

Viva 桌面端在系统有 web 引擎时(macOS 的 WKWebView、Windows 的 WebView2),Markdown 预览、图片查看器、
SVG 图片和 PDF 都显示在编辑区内嵌的 web view(网页视图)里。页面和它用到的脚本、字体全部编进应用二进制,
SVG 图片、PDF 和 HTML 文件都显示在编辑区内嵌的 web view(网页视图)里。页面和它用到的脚本、字体全部编进应用二进制,
经应用自己的 `lithe-preview` 协议提供,不访问网络。页面能读到用户的哪个文件由宿主决定:Markdown 的链接和图片
按 Core 的 `markdown.resolveReference` 解析,查看器只能读它显示的那一个文件或缓冲区。Linux 没有 web 引擎,
按 Core 的 `markdown.resolveReference` 解析,查看器只能读它显示的那一个文件或缓冲区。HTML 文件是例外:
它运行用户自己的脚本,所以走单独的 `lithe-site` 协议和临时(ephemeral,什么都不落盘)存储,能读哪些文件由
Core 的 `htmlPreview.resolveRequest` 逐个请求决定。Linux 没有 web 引擎,
继续使用 Viva 原生的 Markdown 渲染和 Go 自绘的图片画布,两边遵循同一套图片查看规则。

## 问题
Expand Down Expand Up @@ -40,6 +42,23 @@ SVG 只能当文本看,PDF 只能交给外部应用。
- **SVG 跟随编辑**。SVG 文件按 Markdown 的方式打开:源码与图片两栏,标签栏末尾切换源码 / 两者 / 仅图片。
编辑后最多每 150 毫秒把文本交给页面一次,地址带上编辑版本号,避免引擎画出缓存的旧图;更新时保持用户的
缩放和滚动位置。改成画不出来的内容时保留上一张图,只有从未画成功过才显示失败提示。超过 4 MiB 不绘制。
- **HTML 文件按静态网站预览**(`webpreview/site.go`、`workbench/html_preview.go`)。HTML 文件和 SVG 一样
两栏打开,标签栏末尾切换源码 / 两者 / 仅预览。规则如下:
- 站点根目录由 Core 的 `htmlPreview.site` 决定:文件在工作区里时是工作区,否则是文件所在目录。页面地址是
`lithe-site://site/<相对路径>`,所以相对路径、模块脚本、`fetch` 都像在本地 Web 服务器上一样同源工作。
- 每个请求都问 Core 的 `htmlPreview.resolveRequest`:文件本身用编辑器当前文本回答(未保存的修改也能看到),
其他文件按 Core 给的规范路径和内容类型读出;以 `.` 开头的路径分量(`.env`、`.git`)和私钥扩展名返回 403,
目录返回一个跳到 `index.html` 的小页面。单个文件最大 64 MiB,编辑器文本超过 8 MiB 时不预览。
所有回答都去掉 `Access-Control-Allow-Origin` 默认值,所以页面里嵌入的远程 iframe 读不到工作区文件。
- 编辑后最多每 150 毫秒交一次文本。主框架正显示这个文件、没在加载、又拿到过旧文本时,宿主执行一段重新加载脚本:
先让页面报出滚动位置,加载完成后滚回原处。判断不依赖“开始加载”事件,因为 Windows 不报告它。
- 主框架只留在站点内。用户点到站外的链接交给 Core 的链接解析(`followDocumentLink`),网页和邮件地址用系统
应用打开;脚本发起的站外跳转直接取消。页面打开的窗口在站内时替换当前页,站外时交给系统浏览器,每秒最多一次,
因为 Windows 不告诉宿主这个窗口是不是用户点出来的。iframe 可以加载网页、`data:` 和 `blob:`。
- 页面不是我们的页面:它从不进入“就绪”状态,宿主不给它发主题等任何消息;它发来的消息只接受重新加载脚本报的
滚动位置。工具栏只有后退、前进、重新加载和“在浏览器中打开”。
- 站点页打不开(例如 WebView2 太旧,不支持临时存储)时只关掉 HTML 预览,HTML 文件从此只显示源码,
Markdown、图片等其他预览不受影响。
- **Markdown 目录画在页面里**。web 页的目录是页面右侧的大纲栏(`assets/markdown.js`):页面从渲染结果的
标题建树,按层级缩进,可以折叠,当前章节随页面滚动高亮,选中标题时像用户滚动页面一样回报 `scrolled`,
宿主照常把源码带到那一行。标签栏的“目录”按钮只发 `outline{open}` 打开或关闭;用户在页面里按 Escape 关闭时,
Expand All @@ -55,7 +74,9 @@ SVG 只能当文本看,PDF 只能交给外部应用。
切回来时原样显示。隐藏的页面最多保留 6 个,超出时关闭隐藏最久的那个,标签再次显示时重新打开。
窗口的 web host 还没准备好、系统没有 web 引擎、或者某个页面打开失败后,编辑区改用原生预览。
- **不写发行目录**。页面从二进制读取;回答用户文件时带 `Cache-Control: no-store`。页面不使用临时
(ephemeral)配置,因为 WebView2 的每个临时页面都会留下一份配置目录。WebView2 的配置目录由 Viva 放在
(ephemeral)配置,因为 WebView2 的每个临时页面都会留下一份配置目录。HTML 站点页是唯一的例外:它运行
用户的脚本,Cookie、本地存储、缓存和 Service Worker 必须和我们自己的页面、和下一个站点分开,并随页面关闭消失,
所以它是临时页面,代价是 Windows 上每打开一次留下一份配置目录(同样在用户缓存目录里)。WebView2 的配置目录由 Viva 放在
用户缓存目录(`%LOCALAPPDATA%\<应用>\WebView2`),WKWebView 的数据在用户的 `~/Library`,都不在 app bundle
或安装目录里,不影响代码签名和 Sparkle 增量更新。

Expand All @@ -80,6 +101,13 @@ SVG 只能当文本看,PDF 只能交给外部应用。
所以改为页面自己画,宿主只发开关。
- **目录始终浮在正文上方**:宽预览里会挡住正文右侧;有背景图时页面底色透明,浮层下的文字会和目录叠在一起。
拒绝,改为宽预览让出位置、窄预览才浮起并用不透明的弹层底色。
- **HTML 文件也用 `lithe-preview` 协议**:用户脚本会和我们的页面同源,能读写它们的存储、调用它们的路由,拒绝。
单独的 `lithe-site` 协议让两者在浏览器眼里是不同的源。
- **HTML 站点页使用持久存储**:用户脚本写下的 Cookie 和本地存储会在不同站点、不同次打开之间共享并留在磁盘上,
拒绝。
- **在本机起一个 HTTP 服务器提供站点**:本机任何进程都能按端口读到工作区文件,还要处理端口占用,拒绝。
- **像旧 macOS 预览那样让主框架跳到远程网页**:Viva 的预览没有地址栏,远程页面在主框架里还会拿到页面桥接,
暂不做;完整浏览器以后作为插件交付。
- **隐藏的页面立即关闭,或者全部常驻**:立即关闭会在每次切标签时丢掉滚动位置并重新排版;全部常驻会让每个
打开过的预览都占一个 web 进程的内存。折中为最多保留 6 个隐藏页面。

Expand All @@ -98,6 +126,11 @@ SVG 和 PDF 可以在编辑区直接查看;页面能读到什么完全由宿
- Markdown 目录有两种外观:web 页是可折叠、跟随当前章节的大纲,Linux 原生页是按级别缩进的弹出列表。
标题树和折叠只存在于页面脚本,属于视图呈现,不进 Core;两边的标题都来自同一份渲染结果或源码标题。
- Linux 仍然看不到 HTML 和富内容,SVG 只能看源码,PDF 只能交给外部应用。
- HTML 预览比旧 macOS 的内嵌浏览器小:没有地址栏、页面缩放、页内查找、开发者工具、JavaScript 对话框、
下载、打印和 `Range` 请求(音视频不能拖动进度);后退和前进按钮一直可点,因为 Viva 不报告能否后退。
这些需要时在 Viva fork 里补接口再接入,不在应用层绕过。
- Windows 上的站点地址是 `https://lithe-site.viva-app.invalid/site/...`,Viva 把路径第一段当作主机名去掉,
所以页面里以 `/` 开头的绝对路径(`/css/a.css`)在 Windows 上找不到文件,macOS 正常;需要在 Viva fork 修复。
- Windows 上 WebView2 能否解码 TIFF、HEIC,以及自定义协议下的 PDF 查看器是否可用,还没有真机验证;
解码失败时会落到“无法显示”和“用外部应用打开”。

Expand All @@ -106,7 +139,10 @@ SVG 和 PDF 可以在编辑区直接查看;页面能读到什么完全由宿
- `scripts/desktop/desktop-go.sh test ./internal/webpreview ./internal/workbench -count=1`:覆盖页面只回答
干净路径和内嵌文件、跳转拦截、图片只按 Core 解析结果提供、远程图片共享下载与失败重试、Markdown 页随编辑
渲染与双向滚动、隐藏页面上限、web view 打不开时回落原生、图片查看器的工具栏与缩放、PDF 查看器、
SVG 跟随编辑与失败保留,以及没有 web 引擎时的 Go 画布。
SVG 跟随编辑与失败保留,以及没有 web 引擎时的 Go 画布。HTML 预览由 `TestSite*`(请求按 Core 回答、
主框架留在站内、窗口节流、编辑后重新加载并滚回原处、只接受滚动位置消息)、
`TestHTMLPreviewSharedFixture`(Core 命令按共享 fixture `editor/html-preview-v1.json` 回答)和 `TestHTMLFile*`(两栏、跟随编辑、工具栏、站外链接、
打不开时只关 HTML 预览)覆盖。
- `scripts/desktop/verify/verify-runtime-bundle-immutability.sh`:确认打开预览不会改写发行目录。
- `TestMarkdownWebPageRendersCoreHTMLAndScrollsWithTheSource` 覆盖目录按钮与页面之间的 `outline` 消息,
`TestMarkdownContentsIndentsHeadingsByLevel` 覆盖原生弹出列表按级别缩进且行不重叠,
Expand All @@ -124,4 +160,6 @@ SVG 和 PDF 可以在编辑区直接查看;页面能读到什么完全由宿
- `desktop/internal/workbench/viewer_web.go`
- `desktop/internal/workbench/viewer_image.go`
- `desktop/internal/workbench/svg_preview.go`
- `desktop/internal/workbench/html_preview.go`
- `desktop/internal/core/markdown.go`
- `desktop/internal/core/html_preview.go`
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

状态:已实现

> Viva 迁移状态(2026-11-08):本文记录旧 macOS 客户端的 HTML 预览与安全约束。Viva 目前只有文本编辑器,尚未接入 HTML 预览;下列功能和旧 WebKit 测试不能当作 Viva 已完成或已验证,迁移时仍需逐项满足安全与交互要求。
> Viva 迁移状态(2026-11-14):本文记录旧 macOS 客户端的 HTML 预览与安全约束。Viva 桌面端已经有 HTML 预览(`lithe-site` 协议上的静态站点、Core 决定每个请求、访问规则沿用本文的隐藏文件与私钥拒绝),决策见 [编辑区的 web view 预览](../architecture/2026-11-14-web-view-previews.md);但它比本文的浏览器小:没有地址栏、缩放、页内查找、开发者工具、JavaScript 对话框、下载、打印,主框架不跳到远程网页。下列这些功能和旧 WebKit 测试不能当作 Viva 已完成或已验证。

## 先说结论

Expand Down
50 changes: 50 additions & 0 deletions desktop/internal/core/html_preview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package core

import "context"

// HTMLPreviewSite is where the static site an HTML file is previewed as lies: Root is the folder
// the page may load from, Address the file's path from it as a relative URL, its segments
// percent-encoded, and MIME the content type the file is served as.
type HTMLPreviewSite struct {
Root string `json:"root"`
Address string `json:"address"`
MIME string `json:"mime"`
}

// HTMLPreviewSite asks Core for the site of the HTML file at documentPath: the workspace when the
// file lies in workspaceRoot, which may be empty, and the file's folder otherwise.
func (c *Client) HTMLPreviewSite(ctx context.Context, documentPath, workspaceRoot string) (HTMLPreviewSite, error) {
var response HTMLPreviewSite
err := c.Execute(ctx, "htmlPreview.site", htmlPreviewDocument{documentPath, workspaceRoot}, &response)
return response, err
}

// HTMLPreviewResource is what one request of an HTML file's site serves. Kind is "document", the
// file itself, served from the editor's text; "file" with the canonical Path to read without
// following another link and the MIME to serve it as; "directory", a folder whose index.html the
// page goes on to; "denied", a hidden file or a key the site refuses (HTTP 403); or "none", which
// is not served (HTTP 404).
type HTMLPreviewResource struct {
Kind string `json:"kind"`
Path string `json:"path,omitempty"`
MIME string `json:"mime,omitempty"`
}

// ResolveHTMLPreviewRequest asks Core what the request for path, from the root of the site of the
// HTML file at documentPath, serves. path is as the web engine reports it: percent-decoded,
// without a query, and empty for the root. The answer is checked on the file system, so it runs
// off the window thread.
func (c *Client) ResolveHTMLPreviewRequest(ctx context.Context, documentPath, workspaceRoot, path string) (HTMLPreviewResource, error) {
var response HTMLPreviewResource
err := c.Execute(ctx, "htmlPreview.resolveRequest", struct {
htmlPreviewDocument
Path string `json:"path"`
}{htmlPreviewDocument{documentPath, workspaceRoot}, path}, &response)
return response, err
}

// htmlPreviewDocument names the HTML file a preview command is about.
type htmlPreviewDocument struct {
DocumentPath string `json:"documentPath"`
WorkspaceRoot string `json:"workspaceRoot,omitempty"`
}
93 changes: 93 additions & 0 deletions desktop/internal/core/html_preview_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package core

import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"

"github.com/1lck/Lithe-IDEA/desktop/internal/contractfixture"
)

// TestHTMLPreviewSharedFixture: Core names the site of an HTML file and answers the site's
// requests as the shared fixture describes, for a page in the workspace, in a hidden folder and
// outside the workspace.
func TestHTMLPreviewSharedFixture(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("editor/html-preview-v1.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Files []string
Cases []struct {
Name string
Document string
InWorkspace bool
Site struct{ Root, Address, MIME string }
Requests []struct{ Path, Kind, File, MIME string }
}
InvalidDocuments []string
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
if len(fixture.Cases) == 0 {
t.Fatal("the fixture has no cases")
}
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
under := func(relative string) string { return filepath.Join(root, filepath.FromSlash(relative)) }
for _, entry := range fixture.Files {
path := under(entry)
if strings.HasSuffix(entry, "/") {
if err := os.MkdirAll(path, 0o700); err != nil {
t.Fatal(err)
}
continue
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
}

client := NewClient()
for _, c := range fixture.Cases {
t.Run(c.Name, func(t *testing.T) {
document, workspace := under(c.Document), ""
if c.InWorkspace {
workspace = root
}
site, err := client.HTMLPreviewSite(testContext(t), document, workspace)
if err != nil {
t.Fatal(err)
}
if want := (HTMLPreviewSite{Root: under(c.Site.Root), Address: c.Site.Address, MIME: c.Site.MIME}); site != want {
t.Fatalf("the site is %+v, want %+v", site, want)
}
for _, request := range c.Requests {
got, err := client.ResolveHTMLPreviewRequest(testContext(t), document, workspace, request.Path)
if err != nil {
t.Fatal(err)
}
want := HTMLPreviewResource{Kind: request.Kind}
if request.File != "" {
want.Path, want.MIME = under(request.File), request.MIME
}
if got != want {
t.Errorf("%q is %+v, want %+v", request.Path, got, want)
}
}
})
}
for _, document := range fixture.InvalidDocuments {
if _, err := client.HTMLPreviewSite(testContext(t), document, ""); err == nil {
t.Errorf("%q was given a site", document)
}
}
}
10 changes: 10 additions & 0 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading