文档

导出 HTML 和 JSON

保存可编辑的 HTML 并读取结构化 JSON。

试一试

点击 导出。回调会收到 HTML 和带 bold 标记的 JSON。

交互示例
示例结果

添加到编辑器

通过 getHTML() 保存可重新打开的文档,用 getJSON() 读取结构化输出。

import {
  Bold, EditorContent, EditorProvider, useEditor, type DocumentJSON,
} from 'yurich';

export function ExportEditor({ onSave }: {
  onSave: (draft: { html: string; json: DocumentJSON }) => void;
}) {
  const editor = useEditor({
    extensions: [Bold],
    initialValue: "<p><strong>可以发布了</strong></p>",
  });

  return (
    <EditorProvider editor={editor}>
      <EditorContent ariaLabel="编辑器" />
      <button type="button" onClick={() => onSave({
        html: editor.getHTML(),
        json: editor.getJSON(),
      })}>
        导出
      </button>
    </EditorProvider>
  );
}

行为说明

将 JSON 视为不可变。载入文档请用 HTML,见保存和恢复。

功能