<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Code Editor</title>
<!-- CodeMirror CSS from CDN -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/theme/dracula.min.css">
<!-- Tailwind CSS from CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Custom styles for the editor */
.CodeMirror {
height: 70vh;
font-family: 'JetBrains Mono', 'Fira Code', monospace;
font-size: 14px;
line-height: 1.5;
border-radius: 8px;
}
.editor-container {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
}
.header {
background-color: #2d3748;
color: white;
padding: 10px 15px;
font-weight: bold;
display: flex;
justify-content: space-between;
align-items: center;
}
.language-indicator {
background-color: #4a5568;
padding: 3px 8px;
border-radius: 4px;
font-size: 12px;
}
</style>
</head>
<body class="bg-gray-100 min-h-screen p-4 md:p-8">
<div class="max-w-5xl mx-auto">
<h1 class="text-3xl font-bold text-gray-800 mb-6">Simple Code Editor</h1>
<div class="editor-container bg-white">
<div class="header">
<span>editor.js</span>
<span class="language-indicator">JavaScript</span>
</div>
<textarea id="code-editor"></textarea>
</div>
<div class="mt-6 text-gray-600 text-sm">
<p>A simple in-browser code editor with syntax highlighting for JavaScript.</p>
</div>
</div>
<!-- CodeMirror JS from CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/javascript/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/edit/matchbrackets.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/addon/edit/closebrackets.min.js"></script>
<script>
// Sample JavaScript code to initialize the editor with
const sampleCode = `/**
* A simple function that calculates the factorial of a number
* @param {number} n - The input number
* @return {number} The factorial of n
*/
function factorial(n) {
// Base case
if (n <= 1) {
return 1;
}
// Recursive case
return n * factorial(n - 1);
}
// Example usage
const number = 5;
const result = factorial(number);
console.log(\`The factorial of \${number} is \${result}\`);
// Class example
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return \`Hello, my name is \${this.name} and I am \${this.age} years old.\`;
}
}
const john = new Person("John", 30);
console.log(john.greet());`;
// Initialize CodeMirror
const editor = CodeMirror.fromTextArea(document.getElementById("code-editor"), {
mode: "javascript",
theme: "dracula",
lineNumbers: true,
matchBrackets: true,
autoCloseBrackets: true,
indentUnit: 4,
tabSize: 4,
indentWithTabs: false,
lineWrapping: false,
autofocus: true
});
// Set the editor content to the sample code
editor.setValue(sampleCode);
// Auto-resize on window resize
window.addEventListener('resize', () => {
editor.refresh();
});
</script>
</body>
</html>
This index.html file creates a simple in-browser code editor with syntax highlighting using CodeMirror. Key features include:
- CodeMirror Integration: Uses the CodeMirror library via CDN for the editor functionality
- Syntax Highlighting: Configured for JavaScript with the Dracula theme
- Line Numbers: Displayed alongside the code
- Editor Features: Includes bracket matching, auto-closing brackets, and proper indentation
- Sample Code: Initializes with JavaScript sample code showing functions, classes, and comments
- Responsive Design: Uses Tailwind CSS for the page layout with custom CSS for the editor appearance
- Visual Polish: Includes a header with filename and language indicator
The editor is fully functional and self-contained in this single HTML file.