(
options: { attempts?: number; delayMs?: number } = {}
)
| 8 | * falling through to sidebar keyboard navigation. |
| 9 | */ |
| 10 | export function focusEditorNormalMode( |
| 11 | options: { attempts?: number; delayMs?: number } = {} |
| 12 | ): void { |
| 13 | const attempts = Math.max(1, options.attempts ?? 4) |
| 14 | const delayMs = Math.max(0, options.delayMs ?? 16) |
| 15 | |
| 16 | const containsFocus = ( |
| 17 | view: NonNullable<ReturnType<typeof useStore.getState>['editorViewRef']> |
| 18 | ): boolean => { |
| 19 | if (typeof document === 'undefined') return true |
| 20 | const active = document.activeElement |
| 21 | return !!active && (active === view.dom || view.dom.contains(active)) |
| 22 | } |
| 23 | |
| 24 | const scheduleRetry = (remaining: number): void => { |
| 25 | if (remaining <= 1) return |
| 26 | window.setTimeout(() => run(remaining - 1), delayMs) |
| 27 | } |
| 28 | |
| 29 | const run = (remaining: number): void => { |
| 30 | const state = useStore.getState() |
| 31 | const view = state.editorViewRef |
| 32 | state.setFocusedPanel('editor') |
| 33 | if (!view) { |
| 34 | scheduleRetry(remaining) |
| 35 | return |
| 36 | } |
| 37 | view.focus() |
| 38 | if (state.vimMode) { |
| 39 | const cm = getCM(view) |
| 40 | if (cm?.state.vim?.insertMode) { |
| 41 | Vim.exitInsertMode(cm as Parameters<typeof Vim.exitInsertMode>[0], true) |
| 42 | } |
| 43 | } |
| 44 | if (!containsFocus(view)) scheduleRetry(remaining) |
| 45 | } |
| 46 | |
| 47 | requestAnimationFrame(() => run(attempts)) |
| 48 | } |
no test coverage detected