Editor component is the main code editing interface, built on top of CodeMirror 6. It provides syntax highlighting, language detection, theme support, and real-time content change handling.
Usage
Features
Syntax Highlighting
The Editor automatically detects file language based on file extension and applies appropriate syntax highlighting:- JavaScript/TypeScript (
.js,.jsx,.ts,.tsx,.mjs,.cjs) - Python (
.py) - HTML (
.html,.htm) - CSS (
.css) - JSON (
.json) - Markdown (
.md,.markdown) - Rust (
.rs)
Language Detection
Language extensions are loaded dynamically using thegetLanguageExtension() utility:
Content Change Handling
The editor tracks document changes and updates the file state:Theme Support
Custom themes are applied using CodeMirror extensions:CodeMirror Extensions
The Editor uses the following CodeMirror extensions:Provides essential editor features (line numbers, bracket matching, etc.)
Enables undo/redo functionality
Configures keyboard shortcuts using
defaultKeymap and historyKeymapCustom theme extension for background and foreground colors
Applies syntax highlighting based on theme configuration
Listens for document changes and triggers content updates
Returns the appropriate language extension based on file extension
Context Integration
The Editor component relies on theuseEditor hook from the EditorContext:
Consumed Properties
Array of currently open files
Path of the currently active file being edited
Function to set the active file path
Function to close a file tab
Function called when editor content changes. Updates the file’s content and marks it as modified
Current theme configuration object
State Management
The component uses React refs to maintain CodeMirror instances:- Active file path changes
- Active file name changes
- Theme colors change
Empty State
When no file is open, the editor displays:Example: Full Integration
Reference
Source:src/components/Editor.tsx
Dependencies:
codemirror- CodeMirror 6 core@codemirror/commands- Editor commands@codemirror/state- Editor state management@codemirror/view- Editor view@codemirror/lang-*- Language support packages