# vatsal112/live-code-editor architecture

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

- Interactive diagram: https://gitdiagram.com/vatsal112/live-code-editor
- Repository: https://github.com/vatsal112/live-code-editor

## Overview

##### Project Type and Purpose
- This is a full‐stack web application (“Live-Code-Editor”) that lets users sign up/sign in, write HTML/CSS/JS in a browser‐based code editor (CodeMirror), see instant preview, and CRUD (Download/Update) their code snippets stored on the server.

##### Top‐Level Components
a. Client/Web Browser
- Static pages: index.html (editor + preview), login.html, register.html, projects.html (list of saved snippets), update.html
- Code editor library: codemirror/ (all modes, addons, themes)
- Static assets: assets/css/*.css, assets/js/*.js, assets/fonts, assets/img
- Root scripts: main.js, style.css

b. API (Backend)
- Entry point: api/index.php
- Includes layer: includes/ (classes, constants, load.php)
- Business logic: user authentication, snippet CRUD

c. Data Storage
- JSON files under code/ (one .json per snippet)
- includes/tmp/data.json (temporary or metadata store)

d. Web Server
- Serves static assets and routes /api/* to the PHP application

##### Component Relationships & Data Flow
- User loads a static page (e.g., index.html) → Browser fetches HTML/CSS/JS from the Web Server
- Browser initializes CodeMirror (from codemirror/) and UI scripts (assets/js & main.js)
- On user actions (save/load/update snippet), front-end JS issues AJAX/Fetch calls to api/index.php
- api/index.php loads includes/constant.php & classes, validates/authenticates the request, reads/writes the appropriate JSON file in code/ or includes/tmp/data.json
- The API returns JSON responses; browser JS updates the UI and preview accordingly

##### Architectural Patterns & Principles
- N‐Tier separation: Presentation (Browser + CodeMirror UI), Application/API (PHP scripts + business classes), Data (JSON file storage)
- REST‐style API endpoints for snippet operations
- Modular design: distinct directories for assets, includes, code storage, and third‐party library (CodeMirror)

##### Key Technologies & Frameworks
- Frontend: HTML5, CSS3, JavaScript, CodeMirror editor
- Backend: PHP 7+, JSON file system storage (no RDBMS)
- Build/deploy: served via Apache or Nginx with PHP support

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Client Browser
  subgraph "Client Browser"
    direction TB
    Index["index.html"]:::frontend
    Login["login.html"]:::frontend
    Register["register.html"]:::frontend
    Projects["projects.html"]:::frontend
    Update["update.html"]:::frontend
    CM["CodeMirror Editor"]:::thirdparty
    CSS_DIR["assets/css/"]:::frontend
    STYLE["style.css"]:::frontend
    JS_DIR["assets/js/"]:::frontend
    MAIN_JS["main.js"]:::frontend
    Fonts["assets/fonts/"]:::frontend
    Images["assets/img/"]:::frontend
  end

  %% Web Server
  subgraph "Web Server"
    direction TB
    SA["Static Asset Handler"]:::backend
    AR["API Router"]:::backend
  end

  %% API Layer
  subgraph "API Layer"
    direction TB
    API_ENTRY["api/index.php"]:::backend
    CONST["includes/constant.php"]:::backend
    LOAD["includes/load.php"]:::backend
    CLASSES["includes/classes/"]:::backend
  end

  %% Data Store
  subgraph "Data Store"
    direction TB
    SNIPPETS["code/*.json"]:::datastore
    TMP["includes/tmp/data.json"]:::datastore
  end

  %% Connections
  Index -->|"GET static resources"| SA
  Login -->|"GET static resources"| SA
  Register -->|"GET static resources"| SA
  Projects -->|"GET static resources"| SA
  Update -->|"GET static resources"| SA
  CM -->|"GET library files"| SA
  CSS_DIR -->|"GET CSS assets"| SA
  STYLE -->|"GET CSS asset"| SA
  JS_DIR -->|"GET JS assets"| SA
  MAIN_JS -->|"GET UI scripts"| SA
  Fonts -->|"GET font assets"| SA
  Images -->|"GET image assets"| SA

  SA -->|"serve static files"| Index
  SA -->|"serve static files"| Login
  SA -->|"serve static files"| Register
  SA -->|"serve static files"| Projects
  SA -->|"serve static files"| Update
  SA -->|"serve library assets"| CM
  SA -->|"serve CSS"| CSS_DIR
  SA -->|"serve CSS"| STYLE
  SA -->|"serve JS"| JS_DIR
  SA -->|"serve JS"| MAIN_JS
  SA -->|"serve fonts"| Fonts
  SA -->|"serve images"| Images

  MAIN_JS -->|"AJAX: POST/GET /api"| AR
  AR -->|"route to API"| API_ENTRY
  API_ENTRY -->|"load config"| CONST
  API_ENTRY -->|"load bootstrap"| LOAD
  API_ENTRY -->|"load business classes"| CLASSES
  API_ENTRY -->|"read/write snippets"| SNIPPETS
  API_ENTRY -->|"read/write metadata"| TMP
  SNIPPETS -->|"return JSON data"| API_ENTRY
  TMP -->|"return JSON data"| API_ENTRY
  API_ENTRY -->|"JSON response"| MAIN_JS

  %% Click Events
  click Index "https://github.com/vatsal112/live-code-editor/blob/main/index.html"
  click Login "https://github.com/vatsal112/live-code-editor/blob/main/login.html"
  click Register "https://github.com/vatsal112/live-code-editor/blob/main/register.html"
  click Projects "https://github.com/vatsal112/live-code-editor/blob/main/projects.html"
  click Update "https://github.com/vatsal112/live-code-editor/blob/main/update.html"
  click CM "https://github.com/vatsal112/live-code-editor/tree/main/codemirror/"
  click CSS_DIR "https://github.com/vatsal112/live-code-editor/tree/main/assets/css/"
  click STYLE "https://github.com/vatsal112/live-code-editor/blob/main/style.css"
  click JS_DIR "https://github.com/vatsal112/live-code-editor/tree/main/assets/js/"
  click MAIN_JS "https://github.com/vatsal112/live-code-editor/blob/main/main.js"
  click Fonts "https://github.com/vatsal112/live-code-editor/tree/main/assets/fonts/"
  click Images "https://github.com/vatsal112/live-code-editor/tree/main/assets/img/"
  click API_ENTRY "https://github.com/vatsal112/live-code-editor/blob/main/api/index.php"
  click CONST "https://github.com/vatsal112/live-code-editor/blob/main/includes/constant.php"
  click LOAD "https://github.com/vatsal112/live-code-editor/blob/main/includes/load.php"
  click CLASSES "https://github.com/vatsal112/live-code-editor/tree/main/includes/classes/"
  click SNIPPETS "https://github.com/vatsal112/live-code-editor/tree/main/code/"
  click TMP "https://github.com/vatsal112/live-code-editor/blob/main/includes/tmp/data.json"

  %% Styles
  classDef frontend fill:#D0E8FF,stroke:#0366d6,stroke-width:2px;
  classDef backend fill:#DFFFE0,stroke:#28a745,stroke-width:2px;
  classDef datastore fill:#FFE8A1,stroke:#d96d00,stroke-width:2px;
  classDef thirdparty fill:#E8F0FF,stroke:#6a737d,stroke-width:2px,stroke-dasharray: 5 5;
```
