API 参考
EditorInstance
SVeditor.create() 返回的编辑器实例的方法和属性。
EditorInstance
SVeditor.create() 返回一个 EditorInstance 对象,同时也作为 onCreate 和 onUpdate 回调的参数传入。
内容方法
interface EditorInstance {
/** 获取文档 HTML 字符串 */
getHTML(): string;
/** 从 HTML 字符串设置文档内容 */
setHTML(html: string): void;
/** 获取文档纯文本 */
getText(): string;
/** 获取 Tiptap JSON 对象 */
getJSON(): object;
}示例
const editor = SVeditor.create({ el: '#editor', content: '<p>Hello</p>' });
// 读取内容
const html = editor.getHTML(); // '<p>Hello</p>'
const text = editor.getText(); // 'Hello'
const json = editor.getJSON(); // { type: 'doc', content: [...] }
// 设置内容
editor.setHTML('<p>新内容</p>');文档元数据
标题、表情、封面等字段的类型为 FetchMetaObject。持久化请在 extensionsOptions.meta 中配置(onFetchMeta、onSaveMeta、onMetaChange)。宿主侧也可通过实例读写并合并更新:
interface EditorInstance {
/** 当前文档元数据(标题、表情、封面等) */
getMeta(): FetchMetaObject;
/**
* 将元数据浅合并进编辑器状态。
* 与顶部元数据区编辑效果一致;若配置了 `onMetaChange` 会一并触发。
*/
setMeta(meta: Partial<FetchMetaObject>): void;
}示例
const editor = SVeditor.create({
el: '#editor',
content: '<p>Hello</p>',
extensionsOptions: {
meta: {
onMetaChange: (meta) => {
console.log('meta 已更新', meta.name, meta.emojiInfo);
},
},
},
});
editor.setMeta({
name: '来自宿主',
emojiInfo: JSON.stringify({ emoji: '😍', label: 'Smiling face with heart-eyes' }),
});
const meta = editor.getMeta();详见 EditorOptions(extensionsOptions.meta)与 类型定义(FetchMetaObject)。
版本历史
快照列表、恢复与手动创建均在编辑器内通过 右侧栏「版本历史」 与 extensionsOptions.versionHistory 配置完成;不在 EditorInstance 上暴露快照增删改查方法。若要在应用侧自行持久化版本,请使用 getJSON() / setJSON() 等内容与业务存储配合实现。
详见 版本历史。
生命周期
interface EditorInstance {
/** 销毁编辑器并释放所有资源 */
destroy(): void;
}导航离开或组件卸载时务必调用 destroy(),防止内存泄漏。