@musecanto/plugin-document-meta
文档标题、图标、封面大图、描述摘要与独立元数据持久化。
让文档进入你的工作流
@musecanto/plugin-document-meta 提供了 Notion 风格的文档级元信息管理体系。包含页面顶部的封面背景图(支持上传、位置调整与预设图库)、文档 Emoji 图标选择器、独立可编辑标题栏,以及摘要描述的同步与存储。
1. 包定位与依赖关系
- NPM 包名:
@musecanto/plugin-document-meta - 依赖说明:原生模式下依赖
@musecanto/editor;React UI 依赖为可选对等依赖。
pnpm add @musecanto/plugin-document-meta2. 开箱使用 (@musecanto/editor-kit)
在 @musecanto/editor-kit 中直接通过顶层 meta 与 cover 字段进行配置:
import { createEditor } from "@musecanto/editor-kit";
const editor = createEditor({
el: "#editor",
meta: {
showSection: true, // 是否在文档顶部渲染封面与标题区
onMetaChange: (meta) => {
console.log("文档元信息变动:", meta.name, meta.emojiInfo);
},
onSaveMeta: async (meta) => {
await fetch("/api/document/meta", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(meta),
});
},
},
cover: {
presetImages: [
"/covers/gradient-blue.jpg",
"/covers/minimal-geometric.jpg",
],
onUpload: async (file) => {
const res = await myUploadApi(file);
return { url: res.url };
},
},
});3. 原生独立插件注册
import { createEditor } from "@musecanto/editor";
import { createDocumentMetaPlugin } from "@musecanto/plugin-document-meta";
import StarterKit from "@tiptap/starter-kit";
const editor = createEditor({
element: document.querySelector("#editor")!,
extensions: [StarterKit],
plugins: [
createDocumentMetaPlugin({
onMetaChange: (meta) => console.log("Meta updated", meta),
}),
],
});4. 实例方法调用
该插件会向 instance.plugins.documentMeta 挂载方法,可在外部业务逻辑中直接读写元数据:
// 读写标题
const currentTitle = editor.plugins.documentMeta?.getTitle();
editor.plugins.documentMeta?.setTitle("全新项目规划方案");
// 读写 Emoji 图标
editor.plugins.documentMeta?.setEmoji("🚀");
// 读写封面
editor.plugins.documentMeta?.setCover({
url: "https://example.com/cover.jpg",
position: 0.5, // 垂直定位比例 (0–1)
});
// 获取完整元数据实体
const fullMeta = editor.plugins.documentMeta?.getMeta();5. 配置项参考
MetaOptions
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
showSection | boolean | true | 是否在文档顶部渲染封面与标题交互区 |
onFetchMeta | (signal?: AbortSignal) => Promise<EditorMeta> | - | 异步初始拉取元数据处理函数 |
onSaveMeta | (meta: EditorMeta) => Promise<void> | - | 元数据持久化保存处理函数 |
onMetaChange | (meta: EditorMeta) => void | - | 元数据发生变动时的实时回调 |
CoverOptions
| 字段 | 类型 | 说明 |
|---|---|---|
onUpload | (file: File) => Promise<{ url: string }> | 封面图片上传函数 |
presetImages | string[] | 系统内置推荐封面图片 URL 数组 |
proxyUrl | string | 封面图片的防盗链代理前缀 |