View.hasFocus is false when adding focus programmatically

I am adding focus to a cm instance programmatically using this function

export const addFocus = (cm) => {
  cm.focus();
  cm.contentDOM.focus();
};

It’s an empty cm instance. So placeholder should show. But when I dig into it, view.hasFocus property is still false when adding focus to the editor programmatically.

Is there a way to refresh the editor OR force it to rerender things? I suspect a shakedown like that might be required to get it to work smoothly with programmatic changes.

I think this is because view.hasFocus check for document.hasFocus() which is only true if the window/tab has focus from the OS. That means programmatically setting the active element (with .focus()) won’t affect view.hasFocus when the OS user isn’t focused on the web page.

1 Like

Thanks. I ended up using a hack - I rebuild the editor after I programmatically add focus to it. This seems to work.