obsidian-developer-docs icon indicating copy to clipboard operation
obsidian-developer-docs copied to clipboard

Inaccurate explanation for `activeEditor`

Open RyotaUshio opened this issue 2 years ago • 1 comments

This section of the dev docs contains an inaccurate explanation for Workspace.activeEditor. https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Avoid+accessing+%60workspace.activeLeaf%60+directly

If you want to access the editor in the active note, use activeEditor instead:

const editor = this.app.workspace.activeEditor;

The docs seems to say that activeEditor is an Editor object, but it's actually MarkdownFileInfo and in most cases, it's exactly the same MarkdownView as this.app.workspace.getActiveViewOfType(MarkdownView) (except for some corner cases like canvas).

So the sample code for activeEditor should be

const editor = this.app.workspace.activeEditor?.editor;

if (editor) {
    // do something
}

RyotaUshio avatar Dec 01 '23 21:12 RyotaUshio

@liamcain can you take care of this when you get a chance?

lishid avatar Dec 02 '23 05:12 lishid

I've opened a PR to update this code sample!

mateuseap avatar May 29 '24 10:05 mateuseap