Write an HTML code editor in HTML, CSS, and JS, give it a sleek dark design, and an iframe to display the changes.
A button to execute the changes.
Make sure to add syntax highlighting without any external library (using contenteditable on a div and some JS and CSS), make sure to add line counting as well to count the lines of code :)
Assistant
Sure! Below is a complete HTML, CSS, and JavaScript implementation of a sleek dark-themed code editor with syntax highlighting, line numbering, and an iframe to display the rendered HTML. The syntax highlighting is handled using JavaScript without any external libraries.
Features:
Dark Themed Design: A modern and sleek dark theme for the editor.
ContentEditable Div: Allows users to edit HTML code directly.
Syntax Highlighting: Highlights HTML tags, attributes, strings, and comments.
Line Numbers: Displays line numbers alongside the code editor.
Execute Button: Renders the HTML code in the iframe below.
Responsive Layout: Adjusts seamlessly to different screen sizes.
How It Works:
HTML Structure:
A container that holds the line numbers and the editable code area.
A button to execute/render the code.
An iframe to display the rendered HTML.
CSS Styling:
Dark theme colors for the background and text.
Styling for syntax highlighting with different colors for tags, attributes, strings, and comments.
Layout styling to position the line numbers and the code editor side by side.
JavaScript Functionality:
Syntax Highlighting: Uses regex to identify different parts of the HTML code and wraps them in <span> elements with corresponding classes for styling.
Line Numbering: Calculates and displays the number of lines in the editor.
Execute Button: Takes the innerHTML of the editor, strips the syntax highlighting <span> tags, and sets the cleaned HTML to the iframe for rendering.
.editor-container: A flex container that holds the line numbers and the code editor.
.line-numbers: Displays the line numbers. It dynamically updates based on the number of lines in the editor.
.code-editor: A contenteditable div where users can type their HTML code. It includes initial sample HTML code with syntax highlighting.
.execute-button: A button that, when clicked, renders the HTML code inside the iframe.
.iframe-container & iframe: The iframe displays the rendered HTML output.
CSS Styling:
Dark Theme: Uses dark background colors with contrasting text colors for better readability.
Syntax Highlighting: Different classes (.tag, .attr-name, .attr-value, .string, .comment, .doctype) have specific colors to highlight various parts of the HTML code.
Responsive Design: The editor adjusts for smaller screens by hiding the line numbers and reducing font size.
JavaScript Functionality:
highlightSyntax(): This function takes the plain text from the codeEditor, escapes HTML characters to prevent rendering issues, and then uses regular expressions to identify and wrap different parts of the HTML code with <span> elements for syntax highlighting.
updateLineNumbers(): Calculates the number of lines in the editor and updates the .line-numbers div accordingly.
Caret Preservation: When syntax highlighting is applied, the caret (cursor) position might reset. The functions saveCaretPosition() and restoreCaretPosition() attempt to preserve the caret position after highlighting.
executeCode(): Retrieves the plain text from the editor (without the syntax highlighting spans) and sets it as the srcdoc of the iframe to render the HTML.
Initial Setup:
The editor starts with some sample HTML code to demonstrate syntax highlighting.
Usage:
Editing Code: Click inside the code editor area and start typing or modifying the HTML code. The editor will automatically highlight syntax and update line numbers.
Executing Code: After writing or modifying the HTML code, click the "Execute" button to render the HTML in the iframe below.
Viewing Output: The iframe will display the rendered HTML, allowing you to see the changes in real-time.
This implementation provides a foundational HTML code editor with essential features. For more advanced functionalities like autosaving, error checking, or more sophisticated syntax highlighting, integrating external libraries (like CodeMirror or Monaco) might be beneficial. However, the above solution strictly adheres to the requirement of not using any external libraries.