editor.isActive question

Say we have an action with this script:

if (editor.isActive) {
  editor.setSelectedText("I'm in place")
} else {
  editor.setSelectedRange(10000000, 0)
  editor.setSelectedText("I'm at the end")
}

If I run the action from the toolbar, it does the right thing.

But if I run the action from the action panel (on either Mac, or iOS), it never runs the .isActive logic.

Because the editor doesn’t have focus, I get it. But is there a way to get this to work from the action panel, like to discover if the editor lost focus / deactivated?

Not a big deal, I’m ok with the toolbar/panel dichotomy.

This is obviously a contrived example. What are you actually trying to do that you would need to branch based on the editor being focused?

It complicates the example, but my action allows looking up of a draft and inserting its title link.

If I’m in the process of editing, I want to insert in place. If the editor isn’t active, I want the link placed at the end of the draft.

const w = Workspace.find("Newest")
if (d = app.selectDraft(w)) {
  if (editor.isActive) {
    editor.setSelectedText(`[[${d.displayTitle}]]`)
  } else {
    editor.setSelectedRange(10000000, 0)
    editor.setSelectedText(`\n[[${d.displayTitle}]]`)
  }
}

The reason I say it’s complicating is because the selectDraft function itself ends the editor’s focus, so no matter what, the link gets added at the end of the draft. (But if I get a clue on the contrived example, I figure I might be able to also solve this issue.)

I can’t think of anyway to branch the logic that way. isActive is behaving properly, and there’s really no way the app could know if you were editing immediately before opening the action list (which, of course, stops editing) before you ran the action from there…so, to get what you are asking, yes, you would have to always run the action from the action bar so the editor remains focused.

Or just don’t try to combine these into one action, and have one that inserts at the cursor and one that does at the end.

I’m just happy that .setSelectedText inserts at the previously active cursor point, that’s the most important part, can just rely on that.