Skip to content

fix(editor): preserve Markdown focus handoffs - #10618

Merged
AmethystLiang merged 2 commits into
mainfrom
fix-markdown-find-focus
Jul 25, 2026
Merged

AmethystLiang merged 2 commits into
mainfrom
fix-markdown-find-focus

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Summary

Explorer-opened rich Markdown editors now claim explicit focus handoffs synchronously, so Cmd/Ctrl+F works without a document click even when Tiptap recreates the editor during mount.

Focus requests are consumed only after focus lands, scoped to the intended split-pane view, retried across destroyed editor instances, and expired after 30 seconds so stale requests cannot steal focus later.

Screenshots

No visual change.

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test
  • pnpm build
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

Validated with:

  • npx tsc --noEmit -p config/tsconfig.node.json --composite false
  • npx tsc --noEmit -p config/tsconfig.web.json --composite false
  • npx oxfmt --check -c .oxfmtrc.json <changed files>
  • npx oxlint <changed files>
  • npx vitest run --config config/vitest.config.ts src/renderer/src/components/editor --reporter=dot — 144 files, 959 tests passed
  • Focused store/editor tests — 199 tests passed
  • npx electron-vite build --mode e2e
  • markdown-explorer-find-focus.spec.ts — 1 passed
  • Existing Markdown/rich-Markdown E2Es in file-open.spec.ts, markdown-prose-reflow.spec.ts, and rich-markdown-link-bubble-stacking.spec.ts — 9 passed

AI Review Report

The review independently confirmed that the explicit focus was deferred through both Orca and Tiptap animation frames, could be cancelled during editor recreation, and was consumed before focus landed.

It flagged two additional lifecycle risks: stale requests surviving delayed mounts and multiple split panes competing for one request. The implementation now scopes requests to the target view-state ID, revalidates and cancels deferred focus at expiry, retains requests across destroyed editor instances, and consumes them only after focus is observed. Unit coverage was added for synchronous DOM focus, ordinary mounts, delayed consumption, destroyed-editor replacement, split-pane isolation, expiry retirement, and suspended deferred focus.

Cross-platform review covered macOS, Linux, and Windows. The change uses DOM focus behavior shared by all three platforms and does not alter shortcut detection or labels, paths, shell behavior, native modules, or Electron platform branches. Cmd/Ctrl handling remains in the existing platform-aware shortcut path.

Security Audit

No new external input handling, command execution, filesystem path handling, authentication, secrets, dependencies, IPC, or network behavior was introduced. The change is limited to renderer focus state and in-memory request metadata. No follow-up security work is needed.

Notes

The targeted and regression E2Es pass locally on macOS, but that cannot prove the Linux timing fix; Linux scheduled CI remains the authoritative validation.

An initial local E2E attempt used an incomplete concurrent build and failed because window.__store was absent. After a standalone E2E build completed, the target and all listed regression E2Es passed; this was a stale-build harness failure, not a product failure.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Editor focus requests now include a view-state identifier, token, and 30-second expiration timestamp. Rich markdown editors pass the view-state identifier to pending-focus handling, which matches requests to the correct view, waits for focus events, handles editor replacement, and consumes expired requests. Deferred autofocus is gated by request validity. Monaco and editor-content paths now forward and consume view-state identifiers. Tests cover focus timing, competing views, editor replacement, cleanup, and expiration.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: preserving Markdown focus handoffs.
Description check ✅ Passed The description matches the template and includes Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

When opening a file to focus it, tag the pending request with the pane's
viewStateId. This prevents split siblings from claiming each other's requests
and stops later remounts from stealing focus. Both Monaco and rich-markdown
editors now retire requests on mount.
@AmethystLiang
AmethystLiang merged commit 56d3e2c into main Jul 25, 2026
1 check passed

@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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9f0a92bd-f060-4237-83ad-70323dbeee43

📥 Commits

Reviewing files that changed from the base of the PR and between 0c8454a and d1518ce.

📒 Files selected for processing (8)
  • src/renderer/src/components/editor/EditorContent.monaco-lifecycle.test.tsx
  • src/renderer/src/components/editor/EditorContent.tsx
  • src/renderer/src/components/editor/MonacoEditor.tsx
  • src/renderer/src/components/editor/pending-editor-focus-request.test.ts
  • src/renderer/src/components/editor/pending-editor-focus-request.ts
  • src/renderer/src/components/editor/rich-markdown-auto-focus.ts
  • src/renderer/src/components/editor/useRichMarkdownPendingFocus.ts
  • src/renderer/src/store/slices/editor.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/renderer/src/store/slices/editor.test.ts
  • src/renderer/src/components/editor/rich-markdown-auto-focus.ts
  • src/renderer/src/components/editor/useRichMarkdownPendingFocus.ts

Comment on lines +566 to +575

// Why: every mount path above focuses, so an explicit open handoff is already satisfied here.
// Retiring it stops a later rich-mode remount of this same pane from stealing focus back.
const focusRequest = useAppStore.getState().pendingEditorFocusRequest
if (
focusRequest &&
matchesPendingEditorFocusRequest(focusRequest, { fileId, worktreeId, viewStateId })
) {
useAppStore.getState().consumeEditorFocusRequest(focusRequest.token)
}

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.

🎯 Functional Correctness | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Locate file and relevant symbols"
fd -a 'MonacoEditor.tsx' . || true

echo "Outline around relevant file"
file="$(fd 'MonacoEditor.tsx' . | head -n 1)"
if [ -n "${file:-}" ]; then
  echo "FILE=$file"
  wc -l "$file"
  ast-grep outline "$file" --match '*focus*' --view expanded || true
  echo "Relevant lines 480-600"
  sed -n '480,600p' "$file" | nl -ba -v480
  echo "Search focus request helpers/usages"
  rg -n "pendingEditorFocusRequest|consumeEditorFocusRequest|matchesPendingEditorFocusRequest|requestAnimationFrame|hasTextFocus|onDidChangeFocus" "$file" .
fi

Repository: stablyai/orca

Length of output: 617


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Inspect imports and store type shape from source without running repo"
file="$(fd 'MonacoEditor.tsx' . | head -n 1)"
if [ -n "${file:-}" ]; then
  sed -n '1,80p' "$file" | nl -ba
fi

echo "Find definitions of focus request terms across repo"
rg -n "pendingEditorFocusRequest|consumeEditorFocusRequest|matchesPendingEditorFocusRequest|type .*FocusRequest|interface .*FocusRequest" .

Repository: stablyai/orca

Length of output: 261


🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="./src/renderer/src/components/editor/MonacoEditor.tsx"
echo "FILE=${file}"

sed -n '520,590p' "$file"
printf '\n--- definitions/usages ---\n'
rg -n "pendingEditorFocusRequest|consumeEditorFocusRequest|matchesPendingEditorFocusRequest|requestAnimationFrame|hasTextFocus|onDidChangeFocus|autoSave" "$file"
printf '\n--- app store focus declarations ---\n'
rg -n "pendingEditorFocusRequest|consumeEditorFocusRequest|matchesPendingEditorFocusRequest|PendingEditorFocusRequest|EditorFocusRequest" .

Repository: stablyai/orca

Length of output: 13147


Consume the pending focus request only after Monaco actually receives focus.

Lines 553-560 may call editorInstance.focus() from a delayed requestAnimationFrame, but the consume block runs before that RAF callback and without checking focus state. If the RAF is delayed, the editor is unmounted/disposed, or focus is rejected/stolen, the request is retired even though this mount never received focus, preventing a later valid retry. Apply the consumption callback from a focus listener and dispose it through the editor cleanup.

[low_effort_and_c_high_reward]

enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* fix(editor): preserve Markdown focus handoffs

* fix(editor): scope focus requests to panes via viewStateId

When opening a file to focus it, tag the pending request with the pane's
viewStateId. This prevents split siblings from claiming each other's requests
and stops later remounts from stealing focus. Both Monaco and rich-markdown
editors now retire requests on mount.
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.

1 participant