@musecanto/editor-kit

MuseCanto Editor 全功能开箱即用套件,集成全部官方扩展、UI 交互与文档服务。

让文档进入你的工作流

@musecanto/editor-kit 是 MuseCanto Editor 的全功能装配层。它汇集了核心引擎、所有官方扩展(extension-*)、服务插件(plugin-*)以及默认的 React UI 容器(工具栏、气泡菜单、侧边栏),为前端应用提供一步到位的富文本编辑能力。

安装

pnpm add @musecanto/editor-kit react react-dom

[!NOTE] @musecanto/editor-kit 聚合了所需的全部功能包,在常规业务开发中通常仅需安装该包即可满足绝大部分富文本需求。


快速使用

基础挂载

在挂载容器上指定尺寸,引入核心样式并初始化编辑器:

import { useEffect, useRef } from "react";
import { createEditor, type EditorKitInstance } from "@musecanto/editor-kit";
import "@musecanto/editor-kit/style.css";

export function EditorView() {
  const containerRef = useRef<HTMLDivElement>(null);
  const editorRef = useRef<EditorKitInstance | null>(null);

  useEffect(() => {
    if (!containerRef.current) return;

    editorRef.current = createEditor({
      el: containerRef.current,
      content: "<p>欢迎使用 MuseCanto Editor</p>",
      ui: {
        locale: "zh",
      },
      autoSave: {
        onSaveContent: async (json) => {
          console.log("文档自动保存:", json);
        },
      },
    });

    return () => {
      editorRef.current?.destroy();
      editorRef.current = null;
    };
  }, []);

  return (
    <div
      ref={containerRef}
      style={{ height: "640px", border: "1px solid #e2e8f0" }}
    />
  );
}

配置规范 (EditorKitOptions)

createEditor 接受 EditorKitOptions 对象,顶层结构划分为功能开关、界面布局、自动保存与文档元数据:

interface EditorKitOptions {
  /** 挂载的 DOM 元素或 CSS 选择器 */
  el: HTMLElement | string;

  /** 初始内容,支持 HTML 字符串、JSON 对象或纯文本 */
  content?: string | Record<string, unknown>;

  /** 功能特性启用与个性化配置 */
  features?: {
    image?: ImageOptions | false;
    attachment?: AttachmentOptions | false;
    codeBlock?: Partial<CodeBlockShikiOptions> | false;
    table?: boolean;
    list?: boolean;
    textStyle?: boolean;
    link?: boolean;
    details?: boolean;
    highlightBlock?: boolean;
    emoji?: boolean;
    markdown?: boolean;
    history?: boolean;
  };

  /** UI 与交互表现层配置 */
  ui?: {
    locale?: "zh" | "en" | string;
    messages?: Record<string, Record<string, string>>;
    layout?: {
      showLeftSidebar?: boolean;
      showRightSidebar?: boolean;
      dense?: boolean;
    };
    classNames?: {
      root?: string;
      editor?: string;
      toolbar?: string;
    };
    contributions?: EditorUIContribution[];
  };

  /** 自动保存插件配置 */
  autoSave?: {
    debounceMs?: number;
    onSaveContent?: (content: Record<string, unknown>) => Promise<void> | void;
    onError?: (error: unknown) => void;
  };

  /** 文档元数据(标题、图标等)插件配置 */
  meta?: {
    onTitleChange?: (title: string) => void;
    onIconChange?: (icon: string) => void;
  };

  /** 封面图片配置 */
  cover?: {
    onCoverChange?: (cover: CoverInfo | null) => void;
    uploadHandler?: (file: File) => Promise<{ src: string }>;
  };

  /** 生命周期回调 */
  onCreate?: (instance: EditorKitInstance) => void;
  onUpdate?: (instance: EditorKitInstance) => void;
  onDestroy?: () => void;
}

特性禁用与架构保留

当在 features 中将某个扩展显式置为 false(例如 features: { image: false })时:

  • 该扩展对应的高级交互、上传行为与 UI 按钮将被完全卸载,减少内存与渲染开销。
  • 基础 Schema 节点定义依然会保留在底层契约中,已有文档中包含的相关节点(如已有图片)仍可被安全解析与展示,避免出现数据丢失或解析崩溃。

常用实例方法 (EditorKitInstance)

挂载成功后返回 EditorKitInstance 门面对象,提供丰富的方法与插件访问:

// 获取内容
const html = instance.getHTML();
const json = instance.getJSON();
const text = instance.getText();
const markdown = instance.getMarkdown();

// 焦点与编辑状态
instance.focus();
instance.setReadOnly(true);

// 访问文档服务插件
instance.plugins.autosave?.save();
instance.plugins.documentMeta?.setTitle("新文档标题");
const headings = instance.plugins.toc?.getHeadings();

// 销毁
instance.destroy();

样式引入

@musecanto/editor-kit/style.css 已经按拓扑序聚合了核心 UI 与所有官方扩展的 CSS 输出:

import "@musecanto/editor-kit/style.css";

若需定制视觉主题,可通过 CSS 变量(CSS Custom Properties)或针对 musecanto-editor-host 类名进行覆盖。

本页目录

继续探索编辑器 Playground试写并查看文档输出在 GitHub 查看源码问题反馈与参与贡献

示例使用私有 SDK。
你的内容与存储,由你掌控。