类型签名从当前 MSFE 公共导出提取。初始化配置只在挂载时生效;之后通过实例方法修改状态。
创建函数
@quirekit/msfe-core挂载前校验并取得源码;安装失败时,回滚所有已经建立的部分。 参数 options:编辑器挂载与扩展配置。 返回:可独立销毁的编辑器。
createMsfeEditorts
function createMsfeEditor(options: MsfeEditorOptions): MsfeEditor创建选项
必须提供浏览器挂载元素 parent。markdown、extensions、mode 和宿主回调按需设置。
@quirekit/msfe-core浏览器编辑器的创建选项;存储和文件解码由宿主应用负责。
MsfeEditorOptionsts
export interface MsfeEditorOptions {
readonly parent: HTMLElement;
readonly markdown?: string;
readonly documentId?: string;
readonly mode?: MsfeMode;
readonly readOnly?: boolean;
readonly theme?: "light" | "dark";
readonly extensions?: readonly AnyExtension[];
readonly icons?: MsfeIconFactory;
readonly codeLanguages?: readonly CodeLanguage[];
readonly searchPanel?: ComponentViewRenderer<SearchPanelProps>;
readonly componentHost?: MsfeComponentHost;
readonly label?: string;
readonly onChange?: (event: MsfeChangeEvent) => void;
readonly onError?: (error: unknown) => void;
}编辑器实例
commands 执行命令,can() 查询适用性,chain() 将多个操作组合为一次提交。命令不可用时检查返回值及 lastCommandResult。
@quirekit/msfe-core不依赖特定框架的 MSFE 实验性公共接口。
MsfeEditorts
export interface MsfeEditor {
readonly history: MsfeHistory;
readonly scope: TaskScope;
readonly documentScope: TaskScope;
captureAnchor(range: MsfeSourceRange, policy?: RangePolicy): SourceAnchor;
querySource(name: string, range?: MsfeSourceRange): SourceQuery;
isActive(name: string, attrs?: Readonly<Record<string, unknown>>): boolean;
getAttributes(name: string): Readonly<Record<string, unknown>> | null;
getState<T>(slot: StateSlot<T>): T;
subscribeState<T>(subscription: StateSubscription<T>): () => void;
readonly commands: SingleCommands;
readonly lastCommandResult: MsfeResult | undefined;
readonly storage: Readonly<Record<string, unknown>>;
readonly element: HTMLElement;
readonly state: EditorState;
readonly documentId: string;
readonly revision: number;
readonly mode: MsfeMode;
readonly work: Readonly<MsfeWorkCounters>;
getMarkdown(): string;
captureTarget(): MsfeTarget;
run(command: MsfeCommand, target?: MsfeTarget): MsfeResult;
chain(target?: MsfeTarget): MsfeCommandChain;
chain(commands: readonly MsfeCommand[], target?: MsfeTarget): MsfeResult;
can(): CanCommands;
can(command: MsfeCommand, target?: MsfeTarget): boolean;
applyChanges(change: MsfeExternalChange): MsfeResult;
replaceDocument(markdown: string, documentId: string): MsfeResult;
setMode(mode: MsfeMode): void;
setReadOnly(readOnly: boolean): void;
setTheme(theme: "light" | "dark"): void;
setSelection(selection: EditorSelection): void;
updateViewState(update: MsfeViewStateUpdate): MsfeResult;
scrollTo(position: number): void;
focus(): void;
undo(): boolean;
redo(): boolean;
onChange(listener: (event: MsfeChangeEvent) => void): () => void;
destroy(): void;
}文档变化
事件保留增量修改与来源信息,不默认序列化整篇文档。观察者异常通过宿主错误回调报告。
@quirekit/msfe-core已接受的源码变化事件,不会立即序列化整篇文档。
MsfeChangeEventts
export interface MsfeChangeEvent {
readonly documentId: string;
readonly revision: number;
readonly changes: ChangeSet;
readonly selection: EditorSelection;
readonly origin: string;
}按任务查方法
| 任务 | 组合 | 示例 |
|---|---|---|
| 保存当前文件 | getMarkdown + onChange | 顺序保存 |
| 接收同文档外部修改 | applyChanges + documentId / revision | 外部更新 |
| 换文件 | replaceDocument + 新文档资源 | 生命周期 |
| 构建格式工具栏 | can / isActive / getAttributes / subscribeState | 工具栏 |
| 一次复合操作 | chain(...).run() | 命令 |
| 弹窗完成后写入 | captureTarget 或 captureAnchor | 目标管理 |
| 自定义临时阅读状态 | defineState + updateViewState | 事务状态 |
| 排查预览和命令 | onError / lastCommandResult / work | 诊断 |
选择器和查询尽量读取最小所需范围。实例销毁后不要继续调用旧方法或把旧 state 交给新实例。