Commands, editorCallback, other editors

I’d like my plugin to be able to do text replacement in editors other than the standard Markdown one, e.g. when using the Kanban plugin.

I can switch from using editorCallback to just using callback and try to get the current editor from the workspace as shown here.

But, I have to give a non-abstract type T to this.app.workspace.getActiveViewOfType(Constructor<T>); I don’t want to try to guess at all the different editors it might be - is there any way to be more permissive?

As a side question - can callback() and editorCallback(Editor, View) co-exist on a single command? It looks like adding editorCallback overrides the callback and makes the command only show up when in an editor. It would be nice to have the ‘safer’ editorCallback used where appropriate and fallback to callback where necessary.

In your case, you don’t need to (or cannot) use the Editor interface to edit file contents. You can instead use the Vault interface. For example

const file = this.app.workspace.getActiveFile();
if (!file) return;
await this.app.vault.modify(file, (data) => {
    data = doSomething(data);
    return data;
})

Sorry, yes, I was a bit unclear - I’m hoping that I can do something more than that. As an example, the Kanban plugin has a text field that one can type in. I’d like to be able to a) get the current text selection, and b) replace that with new text.

I’m hoping for a) a way to get any of the kinds of View that might be being used, and b) that they have a consistent interface for where the cursor is and what is selected - much in the spirit of the Editor example (Editor - Developer Documentation), using replaceSelection(). Perhaps a bit too hopeful that this is consistent across all the kinds of views people might make.

get the current text selection

This can be achieved by window.getSelection().toString().

Ah, great, thanks! That’s certainly worked to get the current selection. Replacing for all cases seems to be non-trivial though - for example, with the Kanban plugin, I’ve got a div and a text area to work with, and no text replacements seem to replace the actual text being shown:

So I think general non-Editor text replacement is going to be too hard. I’ll put the text on the clipboard and people can paste it if necessary.