与编辑器扩展通信
当您创建了编辑器的扩展后,您可能想要让外部编辑器与之通信。比如通过指令或者功能栏操作
您可以从 Markdown 视图中访问 CodeMirror 6 编辑器。但是,由于 Obsidian API 实际上并未公开编辑器,因此您需要使用 @ts-expect-error
绕过 Typescript 的验证。
ts
import { EditorView } from "@codemirror/view";
// @ts-expect-error, not typed
const editorView = view.editor.cm as EditorView;
视图插件
您可以通过 EditorView.plugin()
方法访问视图插件实例。
ts
this.addCommand({
id: "example-editor-command",
name: "Example editor command",
editorCallback: (editor, view) => {
// @ts-expect-error, not typed
const editorView = view.editor.cm as EditorView;
const plugin = editorView.plugin(examplePlugin);
if (plugin) {
plugin.addPointerToSelection(editorView);
}
},
});
状态字段
您可以在编辑器视图内直接派发变动以及state effects。
ts
this.addCommand({
id: "example-editor-command",
name: "Example editor command",
editorCallback: (editor, view) => {
// @ts-expect-error, not typed
const editorView = view.editor.cm as EditorView;
editorView.dispatch({
effects: [
// ...
]
});
},
});