Skip to content

fix(editor): focus Explorer-opened Markdown for find - #8083

Merged
nwparker merged 3 commits into
stablyai:mainfrom
mmarabel:fix/markdown-find-focus
Jul 24, 2026
Merged

nwparker merged 3 commits into
stablyai:mainfrom
mmarabel:fix/markdown-find-focus

Conversation

@mmarabel

@mmarabel mmarabel commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Opening a Markdown file from Explorer now hands keyboard focus to the rich Markdown editor, so the first Command/Ctrl+F opens rich Markdown search instead of doing nothing.

ELI5

Before, clicking a Markdown file in the sidebar left your cursor "stuck" on the file name, so pressing Find did nothing. Now the file you open is ready to type in and search right away.

Root cause & fix

When a Markdown file was opened from Explorer, focus stayed on the file row while the rich editor mounted. The editor's protective auto-focus guard treated that lingering Explorer focus like any unrelated control and refused to grab focus, so the first find shortcut fired outside the editor. The fix threads an explicit focusEditor intent from Explorer file activation through a token-based pendingEditorFocusRequest in the editor store, and gives autoFocusRichEditor a force param (default false) so only genuine Explorer opens claim focus — dialogs and other inputs stay protected.

Evidence

Validated & rebased onto latest main during a bug-bash triage on 2026-07-23. Rebase was clean onto origin/main.

  • Scoped run on branch across rich-markdown-auto-focus.test.ts, useFileExplorerHandlers.test.ts, and editor.test.ts: 3 test files, 191 tests passed.
  • Reverting only the 4 fix files (tests kept) fails, proving the tests exercise the fix:
Test Files  3 failed (3)
Tests  4 failed | 187 passed (191)
 - useFileExplorerHandlers.test.ts:143 — openFile expected with focusEditor:true
 - editor.test.ts:124 — pendingEditorFocusRequest expected { fileId:'/repo/README.md', worktreeId:'wt-1' }, received undefined
  • Lint: oxlint on the 4 changed source files is clean (no warnings/errors).
  • A requires-app Electron E2E (tests/e2e/markdown-explorer-find-focus.spec.ts) covers the real open-then-find path; not run under vitest.

Trade-offs

None — pure fix.

Regression risk

Low-to-zero. autoFocusRichEditor gains a force param defaulting to false, so ordinary lazy mounts keep the prior neutral-focus guard byte-identical. New behavior is gated on options.focusEditor, set only by explicit Explorer file activation, and the pendingEditorFocusRequest is consumed by matching token and cleared on file close / close-all / target-file close, avoiding stale focus steals. A negative test locks in the non-Explorer focus guard.

Original fix by @gatsby74 (fixes #8078). Validated, rebased, and (where applicable) hardened via automated bug-bash review; original fix design preserved.

@mmarabel
mmarabel marked this pull request as ready for review July 10, 2026 09:21
@coderabbitai

coderabbitai Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Updated autoFocusRichEditor to allow focus when the active element is within a file explorer row, while retaining the guard for unrelated controls. Added unit tests for both focus outcomes and an end-to-end test covering README.md focus followed by the rich markdown editor find shortcut.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the fix and evidence, but it omits required template sections like Screenshots, AI Review Report, Security Audit, and Notes. Add the missing template sections, including Screenshots, a Testing checklist, AI Review Report, Security Audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes match #8078 by allowing Explorer-row focus handoff, preserving external focus guards, and adding regression coverage.
Out of Scope Changes check ✅ Passed The diff stays focused on the Explorer-to-editor focus fix and its tests, with no clear unrelated changes introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly matches the main change: Explorer-opened Markdown now gains focus for find.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/renderer/src/components/editor/rich-markdown-auto-focus.test.ts (1)

50-92: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider extracting shared test setup to reduce duplication.

Both new tests repeat the same pendingFrame capture, vi.stubGlobal calls for requestAnimationFrame/cancelAnimationFrame/document, and createEditor(focus) pattern. A small helper (e.g., setupFocusTest(activeElement)) would keep each test focused on its assertion and prevent the stubs from drifting.

♻️ Optional helper extraction
+function setupFocusTest(activeElement: { closest: vi.Mock }) {
+  let pendingFrame: FrameRequestCallback = () => {
+    throw new Error('expected focus frame to be scheduled')
+  }
+  const focus = vi.fn()
+  vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
+    pendingFrame = callback
+    return 7
+  })
+  vi.stubGlobal('cancelAnimationFrame', vi.fn())
+  vi.stubGlobal('document', { activeElement, body: {} })
+  return { pendingFrame: () => pendingFrame, focus }
+}
+
 it('takes focus from the Explorer file row that opened the document', () => {
-  let pendingFrame: FrameRequestCallback = () => {
-    throw new Error('expected focus frame to be scheduled')
-  }
-  const focus = vi.fn()
-  const activeElement = {
-    closest: vi.fn().mockReturnValue({})
-  }
-  vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
-    pendingFrame = callback
-    return 7
-  })
-  vi.stubGlobal('cancelAnimationFrame', vi.fn())
-  vi.stubGlobal('document', { activeElement, body: {} })
-
-  autoFocusRichEditor(createEditor(focus), null)
-  pendingFrame(0)
-
-  expect(activeElement.closest).toHaveBeenCalledWith('[data-file-explorer-row]')
-  expect(focus).toHaveBeenCalledWith('start', { scrollIntoView: false })
+  const { pendingFrame, focus } = setupFocusTest({
+    closest: vi.fn().mockReturnValue({})
+  })
+  autoFocusRichEditor(createEditor(focus), null)
+  pendingFrame(0)
+  expect(focus).toHaveBeenCalledWith('start', { scrollIntoView: false })
 })

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4a9b609f-949c-4093-a4e6-975e226ef12d

📥 Commits

Reviewing files that changed from the base of the PR and between e4c7aab and 06a04e519307e10a8730baacf961bf1748d29cee.

📒 Files selected for processing (3)
  • src/renderer/src/components/editor/rich-markdown-auto-focus.test.ts
  • src/renderer/src/components/editor/rich-markdown-auto-focus.ts
  • tests/e2e/markdown-explorer-find-focus.spec.ts

@brennanb2025

Copy link
Copy Markdown
Contributor

Taking a look!

@brennanb2025
brennanb2025 force-pushed the fix/markdown-find-focus branch from e98e5ae to d68dcb3 Compare July 21, 2026 00:22
@brennanb2025

Copy link
Copy Markdown
Contributor

Thanks for the fix! I rebased this onto current main and tightened the implementation before pushing it to the PR branch. Explorer activation now carries a one-shot editor-focus request through openFile, so rich Markdown takes focus after its asynchronous mount without depending on Explorer DOM selectors; unrelated controls remain protected. I also retained the review nit’s shared test setup and added/updated store, handler, autofocus, and Electron regression coverage. Validation passed: 191 focused unit tests, the Electron E2E regression, typecheck, touched-file lint/format, and the max-lines ratchet.

@nwparker
nwparker force-pushed the fix/markdown-find-focus branch from d68dcb3 to aeec5c5 Compare July 23, 2026 11:48
@nwparker
nwparker merged commit 65f245f into stablyai:main Jul 24, 2026
@nwparker

Copy link
Copy Markdown
Contributor

Thanks so much for this, @gatsby74 — focus Explorer-opened Markdown for find. Reviewed, rebased on latest main, and merged. Really appreciate the contribution! 🙏

brennanb2025 added a commit that referenced this pull request Jul 24, 2026
…ines cap

#8083 pushed RichMarkdownEditor.tsx to 404 counted lines against the 400-line
.tsx cap, failing lint for every PR that merges current main. The Explorer
find-focus request is self-contained, so it moves to its own hook.
brennanb2025 added a commit that referenced this pull request Jul 24, 2026
…ry tab (#10437)

* fix(runtime): stop the headless hydration repo gate from dropping every tab

#9343 gated headless mobile-session hydration on the live repo list, but read
it as `this.store?.getRepos?.() ?? []`. A store that cannot report repos then
yields an empty set, which reads as "every repo is gone" and skips every
parseable session key — so no tabs hydrate at all. It also called getRepos on
every hydrate, including the hot floating-tab poll path that is contractually
free of repo/provider inventory work.

Resolve the inventory lazily and only for keys that parse to a repoId, and keep
`null` (unavailable) distinct from an empty list (all repos really gone). An
absent list now fails open; a known list still prunes as #9343 intended.

Fixes 3 tests that have been failing on main since #9343 landed:
orca-runtime-terminal-retirement (2) and orca-runtime (1).

* refactor(editor): extract the pending-focus effect to clear the max-lines cap

#8083 pushed RichMarkdownEditor.tsx to 404 counted lines against the 400-line
.tsx cap, failing lint for every PR that merges current main. The Explorer
find-focus request is self-contained, so it moves to its own hook.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ry tab (stablyai#10437)

* fix(runtime): stop the headless hydration repo gate from dropping every tab

stablyai#9343 gated headless mobile-session hydration on the live repo list, but read
it as `this.store?.getRepos?.() ?? []`. A store that cannot report repos then
yields an empty set, which reads as "every repo is gone" and skips every
parseable session key — so no tabs hydrate at all. It also called getRepos on
every hydrate, including the hot floating-tab poll path that is contractually
free of repo/provider inventory work.

Resolve the inventory lazily and only for keys that parse to a repoId, and keep
`null` (unavailable) distinct from an empty list (all repos really gone). An
absent list now fails open; a known list still prunes as stablyai#9343 intended.

Fixes 3 tests that have been failing on main since stablyai#9343 landed:
orca-runtime-terminal-retirement (2) and orca-runtime (1).

* refactor(editor): extract the pending-focus effect to clear the max-lines cap

stablyai#8083 pushed RichMarkdownEditor.tsx to 404 counted lines against the 400-line
.tsx cap, failing lint for every PR that merges current main. The Explorer
find-focus request is self-contained, so it moves to its own hook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Command+F does not open search after opening a Markdown file from Explorer

3 participants