MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / focusEditorNormalMode

Function focusEditorNormalMode

packages/app-core/src/lib/editor-focus.ts:10–48  ·  view source on GitHub ↗
(
  options: { attempts?: number; delayMs?: number } = {}
)

Source from the content-addressed store, hash-verified

8 * falling through to sidebar keyboard navigation.
9 */
10export 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}

Callers 15

handlerFunction · 0.90
EditorPaneFunction · 0.90
BreadcrumbFunction · 0.90
closePaletteFunction · 0.90
runCommandFunction · 0.90
commitThemeFunction · 0.90
openFunction · 0.90
closeFunction · 0.90
alertEditorErrorFunction · 0.90
closeFunction · 0.90
openMatchFunction · 0.90
closeFunction · 0.90

Calls 1

runFunction · 0.70

Tested by

no test coverage detected