feat(rich): 备注/工单说明/知识正文支持 Markdown 与图片附件

写一件事的时候,光有纯文本不够:要贴截图、要分段、要标重点。
这次把「正文」这条路补全,思路是**存 Markdown 而不是 HTML**——
能导出、能全文检索、能直接喂给 LLM,也不必每次渲染都重新清洗一遍 HTML。

后端
- attachment 表 + RLS + 迁移 0005;本地磁盘存储,key 由内容 sha256 决定,
  换对象存储只改 services/storage.py 一个文件
- 上传在**读流时**卡 20MB,不先落盘再判断;磁盘名由服务端生成,用户文件名只进库和响应头
- 列表带 attachment_count、详情带 attachments,避免一屏 N 次请求
- raw 直链给 <img src> 用(图片带不了 Authorization 头),只允许本工作区成员,
  非成员一律 8001 不区分「不存在」与「没权限」
- 5 个用例:上传下载、超限、跨租户、删除确认与权限、raw 鉴权

前端
- 自研零依赖 Markdown 渲染器:先转义再上标签,链接协议白名单
- RichTextEditor:工具栏 + Ctrl+B/I + 预览,粘贴或拖入图片自动上传并插入引用
- AttachmentPanel:上传/下载/删除,下载走带 Authorization 的 fetch 转 blob
- 任务备注、工单说明与验收结论、知识正文、评论全部接入

顺手修的
- TaskDetail.hydrate 漏了 setAttachments,导致附件面板永远是空的(e2e 抓到的)

71 passed;web tsc 0 错、构建 255.07 kB │ gzip 80.85 kB;
e2e:richtext 9 项 + smoke 7 + tickets 8 + knowledge 7,全绿。
This commit is contained in:
dadao
2026-10-29 01:53:46 +08:00
parent 0a3b0f3ca0
commit a24a493403
24 changed files with 1843 additions and 25 deletions
+45
View File
@@ -104,3 +104,48 @@ export const api = {
patch: <T>(path: string, body?: unknown) => raw<T>(path, { method: "PATCH", body }),
del: <T>(path: string) => raw<T>(path, { method: "DELETE" }),
};
/**
* 上传文件(multipart)。
*
* 不能用 api.post:它固定发 JSON。这里也不能自己设 Content-Type,
* 必须让浏览器带 boundary,否则 FastAPI 解析不出文件。
*/
export async function uploadFile<T>(path: string, file: File): Promise<T> {
const form = new FormData();
form.append("file", file, file.name);
const headers: Record<string, string> = { Accept: "application/json" };
const token = session.access;
if (token) headers.Authorization = `Bearer ${token}`;
headers["Idempotency-Key"] = crypto.randomUUID();
const resp = await fetch(`${BASE}${path}`, { method: "POST", headers, body: form });
let body: { code: number; message: string; data: T; request_id: string } | null = null;
try {
body = await resp.json();
} catch {
body = null;
}
if (!body) {
throw new ApiError(resp.status, `上传失败:服务返回了非 JSON 响应(HTTP ${resp.status})`);
}
if (!resp.ok || body.code !== 0) {
if (resp.status === 401) {
session.clear();
authExpired.dispatchEvent(new Event("expired"));
}
throw new ApiError(body.code, body.message, body.request_id);
}
return body.data;
}
/**
* 附件直链:`<img>` 带不了 Authorization 头,所以令牌走 query。
* 只在渲染 Markdown 图片时用,不要拿它去做普通的下载链接(那样 token 会留在浏览器历史里)。
*/
export function attachmentUrl(wsId: string, attachmentId: string): string {
const token = session.access ?? "";
return `${BASE}/workspaces/${wsId}/attachments/${attachmentId}/raw?token=${encodeURIComponent(token)}`;
}