feat(web): 富文本编辑器换成 Vditor,不再自研轮子
CI / backend (push) Canceled after 0s

上一版我自己写了 textarea + 正则工具栏,看着能用,一到粘贴图片、
中文输入法、撤销栈就露馅——这几样正是成熟组件积累的部分。按要求改成品。

- 编辑器开关 def: RichTextEditor = Vditor 4.0.1(MIT,ir 模式)
  * 运行时资源自托管:web/scripts/vendor-vditor.mjs 把 lute/i18n/icons
    同步到 public/vditor,predev/prebuild 自动执行,不走 unpkg CDN
    (内网会一直卡在加载)
- 只读渲染换 markdown-it + dompurify,删掉自写的正则渲染器
- 附件直链的令牌只在内存里存在:落库前 stripAttachmentTokens,
  渲染/装载前补回(<img> 带不了 Authorization 头,raw?token= 是必要折中)
- Vditor 不会替我们插入 upload.handler 的返回值(只当提示语 tip.show),
  所以上传完自己 insertMD

验证:pytest 70 passed;e2e richtext 13 项、smoke 7、tickets 8、
knowledge 7 全绿;tsc 0 错;主包 299 kB/gzip 75 kB,Vditor 独立
chunk 385 kB 按需加载。
This commit is contained in:
dadao
2026-10-29 02:30:10 +08:00
parent 7a7ea932a4
commit fbdf54cb4c
18 changed files with 630 additions and 419 deletions
+47
View File
@@ -0,0 +1,47 @@
/**
* 只给 vditor 的 deep import 声明一个最小类型。
*
* 不直接用 `import type Vditor from "vditor"`:它的 dist/index.d.ts 里有一句
* `import "./assets/less/index.less"`,tsc 会去解析 less 文件而报错。我们只用到
* 几个方法,自己声明反而更稳,也顺便把"我们用到的面"钉死在这里。
*/
declare module "vditor/dist/index.js" {
export interface IVditorOptions {
mode?: "ir" | "wysiwyg" | "sv";
value?: string;
height?: number | string;
placeholder?: string;
theme?: "classic" | "dark";
icon?: string;
cdn?: string;
lang?: string;
cache?: { enable?: boolean; id?: string };
counter?: { enable?: boolean };
toolbar?: Array<string | { name: string; tip?: string }>;
toolbarConfig?: { pin?: boolean; hide?: boolean };
preview?: Record<string, unknown>;
outline?: { enable?: boolean };
upload?: {
accept?: string;
multiple?: boolean;
handler?: (files: File[]) => string | null | Promise<string | null>;
};
input?: (value: string) => void;
after?: () => void;
}
export interface IVditorInstance {
getValue(): string;
setValue(markdown: string, clearStack?: boolean): void;
insertMD(markdown: string): void;
disabled(): void;
enable(): void;
destroy(): void;
disabledCache(): void;
focus(): void;
}
export default class Vditor {
constructor(element: string | HTMLElement, options?: IVditorOptions);
}
}