@musecanto/editor
MuseCanto Editor 无 UI 核心引擎,提供实例生命周期管理与插件运行时协议。
让文档进入你的工作流
@musecanto/editor 是整个编辑器的核心运行层。它完全不依赖 React 或任何 UI 容器,直接面向 Tiptap 与 ProseMirror,负责生命周期调度、内容订阅、快捷键派发以及轻量级插件系统。
安装
pnpm add @musecanto/editor @tiptap/core核心设计理念
- 显式扩展配置:核心包不内置任何默认 Tiptap 扩展。使用者必须显式传入
extensions: [...],从而保障最终产物零冗余代码。 - 所有权分明:
createEditor:创建并完全拥有底层的 Tiptap 实例,调用instance.destroy()时会自动销毁 Tiptap 实例与 DOM 挂载。attachEditor:将 MuseCanto 运行时附加到外部已有的 Tiptap 实例上,调用instance.destroy()时仅释放自身的插件与订阅,不销毁外部 Tiptap 实例。
- 插件运行时解耦:核心包定义了
defineEditorPlugin与类型安全的EditorPluginApis扩展槽位。
快速使用
创建独立无头实例 (createEditor)
import { createEditor } from "@musecanto/editor";
import StarterKit from "@tiptap/starter-kit";
const container = document.querySelector<HTMLElement>("#editor-root")!;
const instance = createEditor({
element: container,
content: "<p>Hello from Headless Editor</p>",
extensions: [StarterKit],
onCreate(inst) {
console.log("编辑器就绪,初始内容:", inst.getHTML());
},
onUpdate(inst) {
console.log("内容发生变更:", inst.getJSON());
},
});
// 在不需要时销毁
instance.destroy();附加到已有 Tiptap 实例 (attachEditor)
适用于已有 Tiptap 项目或需要完全接管 Tiptap 实例生命周期的场景:
import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import { attachEditor } from "@musecanto/editor";
const tiptapEditor = new Editor({
element: document.querySelector("#my-editor"),
extensions: [StarterKit],
});
const instance = attachEditor(tiptapEditor, {
documentId: "doc_1001",
onCreate(inst) {
console.log("MuseCanto 运行时已绑定");
},
});
// 仅释放 MuseCanto 运行时,保持 tiptapEditor 可用
instance.destroy();插件系统与协议 (defineEditorPlugin)
核心插件不依赖 React,通过 defineEditorPlugin 注入原生 Tiptap 扩展、导出实例 API 并监听生命周期:
import { defineEditorPlugin } from "@musecanto/editor";
export interface WordCountApi {
getCount(): number;
}
export function createWordCountPlugin() {
return defineEditorPlugin({
name: "wordCount",
api: (host) => ({
getCount: () => {
const text = host.getEditor()?.getText() ?? "";
return text.trim() ? text.trim().split(/\s+/).length : 0;
},
}),
setup({ editor, documentId }) {
console.log(`正在为文档 ${documentId} 初始化计数器插件`);
const handleUpdate = () => {
// 可监听编辑更新
};
editor.on("update", handleUpdate);
// 返回清理函数
return () => {
editor.off("update", handleUpdate);
};
},
});
}类型安全扩展 (EditorPluginApis)
通过 TypeScript 的声明合并(Declaration Merging),将插件暴露的方法合并到全局实例上:
declare module "@musecanto/editor" {
interface EditorPluginApis {
wordCount: WordCountApi;
}
}
// 消费端获得完整的类型补全
const count = instance.plugins.wordCount?.getCount();API 参考
EditorOptions
| 字段 | 类型 | 说明 |
|---|---|---|
extensions | AnyExtension[] | 必须。Tiptap 扩展数组 |
element | HTMLElement | 挂载目标 DOM 节点 |
content | Content | 初始文档内容(HTML/JSON/文本) |
documentId | string | 可选文档唯一标识,透传给所有插件 |
plugins | EditorPlugin[] | 注册的核心插件数组 |
onCreate | (instance: EditorInstance) => void | 编辑器创建就绪回调 |
onUpdate | (instance: EditorInstance) => void | 编辑器内容更新回调 |
onDestroy | () => void | 编辑器销毁回调 |
EditorInstance 接口
getHTML(): string:获取序列化 HTML 字符串。setHTML(content: string): void:重设 HTML 内容。getJSON(): JSONContent:获取文档 JSON 结构(ProseMirror 节点树)。setJSON(content: JSONContent | string): void:重设 JSON 内容。getText(): string:获取无格式纯文本。setEditable(editable: boolean): void:动态切换可编辑状态。focus(): void:聚焦到编辑器。clear(): void:清空编辑器内容。getEditor(): Editor | null:获取底层原生 TiptapEditor实例。subscribeContentChange(listener: () => void): () => void:订阅内容变更,返回退订函数。destroy(): void:销毁运行时。plugins: Partial<EditorPluginApis>:已注册插件实例 API 映射表。