嵌入
将 Clepit 编辑器或渲染器集成到任何应用中非常简单:DOM 容器就是全部的控制点。
挂载编辑器
给容器元素设置一个 id,调用 `Editor.create({ containerId, ... })`,保留返回的 `EditorAPI`,并在清理时调用 `Editor.destroy(containerId)`。
TypeScript
import { Editor } from '@clepit/core';
import type { EditorAPI } from '@clepit/core';
// 1. Give your container a stable id
// <div id="my-editor"></div>
// 2. Create the editor and hold the returned API
const editor: EditorAPI = Editor.create({
containerId: 'my-editor',
minHeight: 400,
placeholder: 'Start writing...',
theme: 'auto',
onChange: data => console.log(data),
onReady: () => console.log('Editor ready'),
});
// 3. Extract content any time
const data = editor.data.extract();
// 4. Destroy on teardown (e.g. component unmount)
Editor.destroy('my-editor');只读渲染输出
调用 `Renderer.render({ containerId, data })`。下面的示例展示了 `DocsRenderer` 组件中使用的真实 React 模式:`useId` 生成唯一的容器 id,`useEffect` 触发渲染。
TSX
import { Renderer } from '@clepit/core';
import type { EditorData } from '@clepit/core';
import { useEffect, useId } from 'react';
type Props = { content: EditorData };
const ReadOnlyView = ({ content }: Props) => {
// useId produces a stable, unique id per component instance
const containerId = useId().replace(/[^a-z0-9]/gi, '');
useEffect(() => {
Renderer.render({
containerId,
data: content,
margins: { bottom: 0, top: 0 },
});
}, [containerId, content]);
return <div id={containerId} />;
};仅客户端渲染 / SSR
编辑器和渲染器都在 DOM 上运行,因此只能在浏览器中运行:请使用 effects(`useEffect`)或 `'use client'` 指令。它们在 SSR 期间不会工作。
不要在 `useEffect` 之外或服务端组件中调用 `Editor.create` 或 `Renderer.render`;这会导致 "document is not defined" 错误。
框架无关
核心 @clepit/core 包是纯 TypeScript,没有 React 依赖。无论你使用 React、Vue、Svelte、Angular 还是原生 TS/JS,都使用相同的 API。