# mhd101/online_code_editor architecture

> Architecture diagram of the GitHub repository mhd101/online_code_editor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mhd101/online_code_editor
- Repository: https://github.com/mhd101/online_code_editor

## Overview

##### Project Type and Purpose
- This is a front-end only, single-page web application (“Online Code Editor”) that runs entirely in the browser.
- It uses HTML/CSS/JavaScript (with the Tailwind CSS framework) and the CodeMirror library to provide a live, syntax-highlighted code editing environment and a preview runner.

##### File-Structure Analysis
- Top-Level Files
- index.html, editor.html, features.html, aboutus.html – entry points/views
- index.css, tailwind.css, script.js, toggle.js, Parser.js – styling and custom logic
- package.json, tailwind.config.js – build/dev configuration
- codemirror/ – full source of the CodeMirror library (editor engine)
- node_modules/ – npm dependencies (only used during development/build)
- No backend directories: all logic and data handling (save, run, preview) are client-side.

##### Key Architecture Components (to include
A. User’s Browser (Client):
– Loads index.html → bootstraps the SPA
– Manages routing or switches between static HTML pages (editor, features, about)

B. UI Layer
– Navigation / View Loader (links to editor, about, features)
– Editor View (editor.html) containing:
- Code Editor Component (CodeMirror instance loaded via codemirror/lib/codemirror.js)
- Toolbar Buttons (Save, Toggle Full-Screen, Run) wired via toggle.js and script.js
- Preview Pane (iframe or sandboxed container)

C. CodeMirror Library
– Syntax-Highlighting Engine (core + addons for auto-close tags, linting, fullscreen)
– Modes & Themes (CSS/JS under codemirror/mode and codemirror/theme)
– Configuration loaded at runtime from rollup.config.js (for build) or via script.js includes

D. Code Runner & Preview Engine
– Parser.js parses user input (if needed) or sanitizes code
– script.js integrates editor content into the Preview Pane (iframe.srcdoc)
– iframe sandbox isolates execution of user HTML/CSS/JS

E. Persistence Layer (Client-side)
– Save File functionality via browser APIs (LocalStorage or FileSaver)
– No remote server – data never leaves user’s browser

F. Build & Deployment
– Tailwind CLI or PostCSS (using tailwind.config.js & tailwind.css) for styling
– npm scripts (package.json) for development/build
– Hosted on Netlify (or similar) via git push → CI/CD

##### Relationships & Data Flows
1. User types code → CodeMirror instance emits “change” events
2. Editor → script.js/Parser.js → Preview Pane (iframe)
3. User clicks “Save” → script.js triggers file download or LocalStorage write
4. User toggles fullscreen → toggle.js manipulates editor DOM and CodeMirror API
5. Build time: tailwind.css + static assets → deployed to CDN

##### Architectural Patterns / Principles
- Single-Page Architecture (client-side navigation)
- Modular Components: Editor, Preview, Controls
- Library as a Submodule: CodeMirror is included wholesale (not an external service)
- Sandbox Isolation: using iframe to run untrusted user code
- Client-Only Persistence: no backend, adheres to privacy and simplicity

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client-Side Runtime
    subgraph "Client-Side Runtime"
        direction TB
        Browser["User’s Browser"]:::external

        subgraph "UI Layer"
            direction TB
            EntryPoints["UI Entry Points"]:::ui
            subgraph "Pages"
                direction TB
                Index["index.html"]:::ui
                Features["features.html"]:::ui
                About["aboutus.html"]:::ui
                EditorPage["editor.html"]:::ui
            end
            EditorView["Editor View"]:::ui
            PreviewPane["Preview Pane (iframe)"]:::api
        end

        subgraph "Editor Components"
            direction TB
            CodeMirrorCore["CodeMirror Core"]:::lib
            CodeMirrorCSS["CodeMirror CSS"]:::lib
            CodeMirrorLib["CodeMirror Library"]:::lib
        end

        subgraph "Logic Layer"
            direction TB
            ParserJS["Parser.js"]:::ui
            ScriptJS["script.js"]:::ui
        end

        subgraph "Persistence Layer"
            direction TB
            LocalStorage["LocalStorage / FileSaver API"]:::api
        end

        Browser -->|"loads entry point"| EntryPoints
        EntryPoints -->|navigate| EditorView
        EditorView -->|"embeds"| CodeMirrorCore
        EditorView -->|"uses"| CodeMirrorCSS
        CodeMirrorCore -->|"loads modes/themes from"| CodeMirrorLib
        EditorView -->|"renders"| PreviewPane
        CodeMirrorCore -->|"emits change events to"| ScriptJS
        ScriptJS -->|"parses via"| ParserJS
        ScriptJS -->|"updates srcdoc of"| PreviewPane
        ScriptJS -->|"on Save click"| LocalStorage
    end

    %% Build & Deployment
    subgraph "Build Process & Deployment"
        direction TB
        TailwindCSS["tailwind.css"]:::ui
        TailwindConfig["tailwind.config.js"]:::ui
        PackageJSON["package.json"]:::ui
        PackageLock["package-lock.json"]:::ui
        NodeModules["node_modules/"]:::ui
        Netlify["Deployment Service (Netlify/CDN)"]:::external
        PackageJSON -->|npm scripts build| TailwindCSS
        TailwindConfig -->|configures| TailwindCSS
        PackageJSON -->|installs| NodeModules
        TailwindCSS -->|bundled with| NodeModules
        PackageJSON -->|deploy| Netlify
    end

    %% Click Events
    click Index "https://github.com/mhd101/online_code_editor/blob/main/index.html"
    click Features "https://github.com/mhd101/online_code_editor/blob/main/features.html"
    click About "https://github.com/mhd101/online_code_editor/blob/main/aboutus.html"
    click EditorPage "https://github.com/mhd101/online_code_editor/blob/main/editor.html"
    click TailwindCSS "https://github.com/mhd101/online_code_editor/blob/main/tailwind.css"
    click TailwindConfig "https://github.com/mhd101/online_code_editor/blob/main/tailwind.config.js"
    click CodeMirrorCore "https://github.com/mhd101/online_code_editor/blob/main/codemirror/lib/codemirror.js"
    click CodeMirrorCSS "https://github.com/mhd101/online_code_editor/blob/main/codemirror/lib/codemirror.css"
    click CodeMirrorLib "https://github.com/mhd101/online_code_editor/tree/main/codemirror/"
    click ScriptJS "https://github.com/mhd101/online_code_editor/blob/main/script.js"
    click ParserJS "https://github.com/mhd101/online_code_editor/blob/main/Parser.js"
    click PackageJSON "https://github.com/mhd101/online_code_editor/blob/main/package.json"
    click PackageLock "https://github.com/mhd101/online_code_editor/blob/main/package-lock.json"
    click NodeModules "https://github.com/mhd101/online_code_editor/tree/main/node_modules/"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0275d8,stroke-width:1px
    classDef lib fill:#DFF7D8,stroke:#28a745,stroke-width:1px
    classDef api fill:#FFE8C4,stroke:#f0ad4e,stroke-width:1px
    classDef external fill:#F0F0F0,stroke:#6c757d,stroke-width:1px,stroke-dasharray: 2 2
```
