Repository navigation
feat(editor): CodeMirror editable text/code editor (#862) - #872
Conversation
Adds an owned CodeMirror 6 binding as the editable text surface for the desktop workbench. `.md`/`.markdown` open in an editable editor with a Streamdown preview toggle, and every other text format now opens editable too (superseding the read-only Shiki text viewer) — the editable fallback the workbench needs regardless of markdown. Why CodeMirror (not a WYSIWYG wrapper): a real source editor round-trips byte-for-byte (serialization is identity), so it safely backs both the markdown case and the generic fallback. The binding is owned directly over `@codemirror/*` (no `@uiw/react-codemirror`) — a thin React mount with compartments for language/theme/readOnly. Lives in `components/codemirror`, a sibling of `components/monaco`. - Reuses the editor-agnostic #805 save/dirty/conflict machinery from the SVG pane; extracts the shared save chrome into `editor-pane-save-ui.tsx`. - Language resolved lazily from the filename via `@codemirror/language-data`; `.jsonc`/`.json5` aliased to JSON. - Find: NO built-in find/replace panel. CodeMirror's `findNext`/`findPrevious` open the panel whenever the query is empty (their `searchCommand` fallback), so search is fully owned — a query StateField drives a match highlighter and selection-based next/prev, and basicSetup's search keys are overridden so nothing can reach `openSearchPanel`. - Adds a UI demo at /ui/components/codemirror (registered in the dev UI nav). The pane is desktop-only (talks to the workspace fs bridge); the editor component and its demo are host-agnostic and verified in the browser.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedAn error occurred during the review process. Please try again later. WalkthroughIntroduces a CodeMirror 6 React binding ( ChangesCodeMirror editor binding and workspace text/markdown pane
Sequence DiagramsequenceDiagram
participant User
participant EditorPaneCodeEditor
participant CodeMirrorEditor
participant WorkspaceBridge
participant SaveConflictDialog
User->>EditorPaneCodeEditor: open file (workspaceId, relPath)
EditorPaneCodeEditor->>WorkspaceBridge: readFile(relPath)
WorkspaceBridge-->>EditorPaneCodeEditor: content + mtime
EditorPaneCodeEditor->>CodeMirrorEditor: mount with initialValue + language
User->>CodeMirrorEditor: edit document
CodeMirrorEditor->>EditorPaneCodeEditor: onDocChange()
EditorPaneCodeEditor->>EditorPaneCodeEditor: bump contentVersion, mark dirty
User->>EditorPaneCodeEditor: Cmd+S
EditorPaneCodeEditor->>CodeMirrorEditor: getValue()
CodeMirrorEditor-->>EditorPaneCodeEditor: current doc string
EditorPaneCodeEditor->>WorkspaceBridge: writeFile(content, expectedMtime)
alt conflict
WorkspaceBridge-->>EditorPaneCodeEditor: mtime mismatch error
EditorPaneCodeEditor->>SaveConflictDialog: open
User->>SaveConflictDialog: Overwrite
SaveConflictDialog->>EditorPaneCodeEditor: overwriteAnyway()
EditorPaneCodeEditor->>WorkspaceBridge: writeFile(content, no mtime guard)
WorkspaceBridge-->>EditorPaneCodeEditor: ok
else success
WorkspaceBridge-->>EditorPaneCodeEditor: ok
EditorPaneCodeEditor->>EditorPaneCodeEditor: update storedMtime, re-baseline dirty
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 879f5716b4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@editor/app/`(dev)/ui/components/codemirror/page.tsx:
- Around line 101-105: The useEffect hook calls
languageFor(PRESETS[activeIndex]) and chains a .then() to handle the resolved
extension, but there is no error handling for promise rejections. If the
language load fails, this will cause an unhandled rejection and leave the UI in
an inconsistent state. Add a .catch() handler after the .then() to catch any
errors from the language loading promise and set the language to undefined as a
fallback, ensuring the cancelled flag check is also applied in the catch block.
In `@editor/scaffolds/desktop/workbench/editor-pane-code-editor.tsx`:
- Around line 312-326: The CodeMirrorEditor component in this pane does not
regain focus when the tab becomes active after being hidden, because the
autoFocus prop only affects focus at mount time. Add a useEffect hook that
watches the active prop and programmatically focuses the editor using the
handleRef reference whenever active becomes true. This ensures that when a user
switches to this tab, focus is restored to the editor so typing works
immediately without requiring a manual click.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: f7bf01c5-81ed-4acc-96e6-8703bebc314b
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (9)
editor/app/(dev)/ui/_nav.tseditor/app/(dev)/ui/components/codemirror/page.tsxeditor/components/codemirror/index.tsxeditor/components/codemirror/languages.tseditor/package.jsoneditor/scaffolds/desktop/workbench/editor-pane-code-editor.tsxeditor/scaffolds/desktop/workbench/editor-pane-save-ui.tsxeditor/scaffolds/desktop/workbench/editor-pane-svg-editor.tsxeditor/scaffolds/desktop/workbench/editor-pane-tab.tsx
When a markdown tab was in Preview and reloadFromDisk ran (clean watcher refresh, or the conflict dialog's "Reload from disk"), only the hidden CodeMirror doc was updated — the preview overlay kept rendering the stale pre-reload snapshot while the pane was re-baselined clean. Sync previewContent to the reloaded bytes too.
CodeRabbit review #872: - Lazy grammar loads (`resolveLanguage` / demo `languageFor`) had no rejection path; a failed dynamic import would surface as an unhandled rejection. Add a `.catch` that falls back to plain text. Applied to both the pane and the demo (the pane had the same pattern CodeRabbit flagged on the demo). - Tabs stay mounted across switches, so `autoFocus` (mount-only) didn't refocus when the pane became active again. Focus the editor on activate (skipping the read-only markdown preview overlay) so typing lands without a click.
Summary
Adds an owned CodeMirror 6 binding as the editable text surface for the desktop workbench. Resolves #862.
.md/.markdownopen in an editable editor with a Streamdown preview toggle.Why CodeMirror (not a WYSIWYG/Notion-style wrapper)
A real source editor round-trips byte-for-byte (serialization is identity), so it safely backs both the markdown case and the generic fallback without a lossy IR. We wire
@codemirror/*directly — a thin React mount with compartments for language/theme/readOnly — not@uiw/react-codemirror. The component lives incomponents/codemirror, a sibling ofcomponents/monaco, and is host-agnostic.What's included
components/codemirror— the owned mount (CodeMirrorEditor+CodeMirrorHandle) and lazy filename→language resolution (@codemirror/language-data;.jsonc/.json5aliased to JSON).editor-pane-code-editor.tsx— the editable pane; reuses the editor-agnostic feat(desktop): watch workspace files — refresh open editor + file tree on external change, with save-conflict guard (VSCode model) #805 save/dirty/conflict machinery from the SVG pane.editor-pane-save-ui.tsx— shared dirty badge / save-error toast / conflict dialog, extracted from the SVG pane (which now imports them).editor-pane-tab.tsx) —.md→ markdown editor, catch-all text → code editor; drops the hand-kept Shiki ext→lang map./ui/components/codemirror(registered in the dev UI nav).Notable: no built-in find/replace
CodeMirror's
findNext/findPreviousopen the search panel whenever the query is empty (theirsearchCommandfallback toopenSearchPanel), so the panel can't be cleanly suppressed while using those commands. Find is therefore fully owned: a queryStateFielddrives a match highlighter and selection-based next/prev, andbasicSetup's search keys are overridden at highest precedence so nothing can reachopenSearchPanel. The result is a minimal custom find bar (search + highlight + next/prev, no replace).Verification
tsc0 errors, oxlint clean, oxfmt clean.Mod-gnever open the built-in panel.Scope / reviewer notes
MarkdownViewer/TextViewer/ShikiBlockineditor-pane-viewers.tsxare left for a focused follow-up cleanup (kept out to keep this diff reviewable).Summary by CodeRabbit