A flexible, reusable code editor component with multiple file handling modes
npm install superleap-code-editorA flexible, reusable React code editor component with multiple file handling modes, built with Monaco Editor and Ant Design.
- 🚀 Multiple File Sources: Support for workflow data, form content, and URL-based files
- 📁 File Explorer: Tree-based file navigation with Ant Design components
- 🎨 Monaco Editor: Full-featured code editor with syntax highlighting
- 🔧 Resizable Panels: Splitter-based layout with collapsible panels
- ⌨️ Keyboard Shortcuts: VS Code-like shortcuts (Ctrl+B to toggle panels)
- 🎯 TypeScript: Full TypeScript support with type definitions
- 📦 Tree Shakeable: Optimized bundle size with tree shaking support
``bash`
npm install @superleap/code-editoror
yarn add @superleap/code-editoror
pnpm add @superleap/code-editor
This package requires the following peer dependencies:
`bash`
npm install react react-dom antd @monaco-editor/react @tabler/icons-react
`tsx
import { CodeEditorWrapper } from '@superleap/code-editor';
import '@superleap/code-editor/styles';
function App() {
const config = {
formConfig: {
codeContent: 'console.log("Hello World!");',
fileName: 'example.js',
language: 'javascript'
}
};
return
}
`
For code that's stored with function IDs and versions:
`tsx
import { WorkflowCodeEditor } from '@superleap/code-editor';
function App() {
const fetchFunction = async (functionId: string, version: string) => {
const response = await fetch(/api/functions/${functionId}/${version});
return response.text();
};
const fetchVersionList = async (functionId: string) => {
const response = await fetch(/api/functions/${functionId}/versions);
return response.json();
};
return (
version="2.1.0"
versionList={['1.0.0', '2.0.0', '2.1.0']}
fetchFunction={fetchFunction}
fetchVersionList={fetchVersionList}
/>
);
}
`
For code stored as strings in form data:
`tsx
import { FormCodeEditor } from '@superleap/code-editor';
function App() {
const [code, setCode] = useState('function example() { return "Hello"; }');
return (
fileName="script.js"
language="javascript"
readOnly={false}
/>
);
}
`
For files accessible via URLs:
`tsx
import { URLCodeEditor } from '@superleap/code-editor';
function App() {
const fileUrls = [
{
url: 'https://example.com/script.js',
name: 'script.js',
type: 'js'
},
{
url: 'https://example.com/style.css',
name: 'style.css',
type: 'css'
}
];
return
}
`
`tsx
import { CodeEditorWrapper } from '@superleap/code-editor';
import type { CodeEditorConfig } from '@superleap/code-editor';
function App() {
const config: CodeEditorConfig = {
workflowConfig: {
functionId: 'my-function',
version: '1.0.0',
versionList: ['1.0.0', '1.1.0'],
fetchFunction: async (id, version) => {
// Your fetch logic
return 'function myFunction() { return "Hello"; }';
},
fetchVersionList: async (id) => {
// Your version fetch logic
return ['1.0.0', '1.1.0'];
}
},
settings: {
readOnly: false,
autoSave: true,
theme: 'vs-light',
fontSize: 14
}
};
return
}
`
`tsx
import {
FileExplorer,
CodeEditor,
EditorLayout,
useFileManager,
createFileProvider
} from '@superleap/code-editor';
function CustomEditor() {
const fileProvider = useMemo(() => createFileProvider(config), [config]);
const { files, activeFile, fileContent, setActiveFile, updateFileContent } = useFileManager(fileProvider);
return (
activeFile={activeFile}
onFileSelect={setActiveFile}
/>
}
editor={
onChange={updateFileContent}
language="javascript"
/>
}
activePanel="explorer"
setActivePanel={() => {}}
/>
);
}
`
#### CodeEditorWrapper
Main wrapper component that handles all file operations.
Props:
- config: CodeEditorConfig - Configuration objectrightPanel?: React.ReactNode
- - Optional right panel content
#### WorkflowCodeEditor
Pre-configured component for workflow-based files.
Props:
- functionId: string - Function identifierversion: string
- - Current versionversionList: string[]
- - Available versionsfetchFunction: (id: string, version: string) => Promise
- - Function to fetch codefetchVersionList: (id: string) => Promise
- - Function to fetch versions
#### FormCodeEditor
Pre-configured component for form-based files.
Props:
- codeContent: string - Code contentfileName?: string
- - File name (default: 'code.js')language?: string
- - Language for syntax highlighting (default: 'javascript')readOnly?: boolean
- - Read-only mode (default: false)
#### URLCodeEditor
Pre-configured component for URL-based files.
Props:
- fileUrls: Array<{url: string, name: string, type: string}> - Array of file URLsrightPanel?: React.ReactNode
- - Optional right panel
#### useFileManager
Hook for managing file operations.
Returns:
- files: FileNode[] - File treeactiveFile: string | null
- - Currently active file IDfileContent: string
- - Content of active fileloading: boolean
- - Loading stateerror: string | null
- - Error statesetActiveFile: (fileId: string) => void
- - Set active fileupdateFileContent: (content: string) => void
- - Update file contentrefreshFiles: () => void
- - Refresh file listgetFileById: (fileId: string) => FileNode | null
- - Get file by ID
#### CodeEditorConfig
`typescript`
interface CodeEditorConfig {
workflowConfig?: {
functionId: string;
version: string;
versionList: string[];
fetchFunction: (functionId: string, version: string) => Promise
fetchVersionList: (functionId: string) => Promise
};
formConfig?: {
codeContent: string;
fileName?: string;
language?: string;
};
urlConfig?: {
fileUrls: Array<{
url: string;
name: string;
type: 'js' | 'css' | 'html' | 'json' | 'ts' | 'jsx' | 'tsx';
}>;
};
settings?: {
readOnly?: boolean;
autoSave?: boolean;
theme?: string;
fontSize?: number;
};
}
#### FileNode
`typescript`
interface FileNode {
id: string;
name: string;
type: 'file' | 'folder';
path: string;
content?: string;
children?: FileNode[];
metadata?: {
size?: number;
lastModified?: Date;
language?: string;
url?: string;
};
}
- Ctrl + B - Toggle left panel visibilityCtrl + Shift + B
- - Toggle right panel visibility
The package includes built-in styles. Import them in your app:
`tsx`
import '@superleap/code-editor/styles';
For custom styling, you can override the CSS classes or use Ant Design's theme customization.
`bashInstall dependencies
npm install
MIT
Contributions are welcome! Please feel free to submit a Pull Request.