# zhuochun/simple-editor architecture

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

- Interactive diagram: https://gitdiagram.com/zhuochun/simple-editor
- Repository: https://github.com/zhuochun/simple-editor

## Overview

##### Project Type and Purpose
- This is a client‑side, single‑page web application (“Simple Editor”) for drafting and organizing complex documents in a non‑linear, hierarchical way.
- All data lives in the browser (localStorage or in‑memory) and can be exported/imported as plain text.
- It optionally integrates with external AI “completions” providers (OpenAI, OpenRouter) for automated content generation.

##### File‐Structure Analysis
- Root assets:
- index.html (entry point)
- style.css (presentation)
- script.js (main orchestrator)
- Feature modules (ES modules loaded by script.js):
- data.js (data model & persistence)
- dragDrop.js (drag‑and‑drop UI interactions)
- cardShortcuts.js (keyboard shortcuts, card manipulation)
- aiService.js (abstraction over external AI API calls)
- Supporting folders:
- docs/ (design specs, versioned API docs, data schema)
- examples/ (sample JSON exports for onboarding)
- No backend “src” or “api” directory: purely front‑end with external REST calls.

##### README Insights
- Confirms full client‑side execution, run via `npx serve`.
- AI keys supplied by user, configured in a sidebar.
- Uses any OpenAI‐style completions endpoint (REST).

a. Main Components
- Browser UI Layer
– index.html + CSS + DOM rendering (script.js drives UI)
- Model Layer
– data.js: holds document tree, CRUD, localStorage interface, import/export logic
- Interaction Layer
– dragDrop.js: node repositioning
– cardShortcuts.js: keyboard shortcuts, node operations
- Service Layer
– aiService.js: builds requests, handles API communication with OpenAI/OpenRouter
- External Systems
– OpenAI / OpenRouter REST API endpoints
- Hosting
– Static server (npx serve) delivering HTML/CSS/JS

b. Relationships & Data Flow
1. Browser loads index.html → script.js initializes modules.
2. User interacts via UI → interaction modules (dragDrop, shortcuts) invoke data.js.
3. data.js updates in‑memory model and persists to localStorage.
4. On “AI Draft” action → UI calls aiService → aiService sends HTTP request to external API → receives response → returns to data.js/UI for rendering.
5. Export/Import uses data.js to read/write JSON blobs.

c. Architectural Patterns & Principles
- SPA (single‑page application) with clear separation:
- Model (data.js)
- View (index.html, style.css, render logic in script.js)
- Controller/Services (script.js orchestration + aiService)
- Modular design: each concern in its own JS file
- Service abstraction: decouple AI API details from UI

d. Key Technologies & Frameworks
- Vanilla ES6 JavaScript modules
- HTML5/CSS3 for UI
- Browser localStorage (or IndexedDB) for persistence
- RESTful calls to OpenAI/OpenRouter endpoints
- Node.js npx serve for static hosting

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser Boundary
    subgraph "Browser Environment"
        direction TB
        subgraph "UI Layer"
            direction TB
            UI_HTML["index.html"]:::ui
            UI_CSS["style.css"]:::ui
            Script["script.js"]:::ui
        end
        subgraph "Model Layer"
            direction TB
            Data["data.js"]:::data
            Storage["localStorage"]:::storage
        end
        subgraph "Interaction Layer"
            direction TB
            Drag["dragDrop.js"]:::interaction
            Keys["cardShortcuts.js"]:::interaction
        end
        subgraph "Service Layer"
            direction TB
            AI["aiService.js"]:::service
        end
    end

    %% Infrastructure and External
    StaticServer["Static Server (npx serve)"]:::infra
    OpenAI["OpenAI / OpenRouter API"]:::external

    %% Data Flows
    StaticServer -->|serves| UI_HTML
    UI_HTML -->|loads| Script
    UI_CSS -->|styles| UI_HTML
    Script -->|initializes| Data
    Script -->|initializes| Drag
    Script -->|initializes| Keys
    Script -->|initializes| AI

    Drag -->|manipulates| Data
    Keys -->|manipulates| Data
    Script -->|uses| Data
    Data -->|persists| Storage
    Storage -->|reads| Data

    Script -->|on AI Draft| AI
    AI -->|HTTP request| OpenAI
    OpenAI -->|response| AI
    AI -->|returns result| Script

    %% Click Events
    click UI_HTML "https://github.com/zhuochun/simple-editor/blob/main/index.html"
    click UI_CSS "https://github.com/zhuochun/simple-editor/blob/main/style.css"
    click Script "https://github.com/zhuochun/simple-editor/blob/main/script.js"
    click Data "https://github.com/zhuochun/simple-editor/blob/main/data.js"
    click Drag "https://github.com/zhuochun/simple-editor/blob/main/dragDrop.js"
    click Keys "https://github.com/zhuochun/simple-editor/blob/main/cardShortcuts.js"
    click AI "https://github.com/zhuochun/simple-editor/blob/main/aiService.js"
    click docs "https://github.com/zhuochun/simple-editor/tree/main/docs/"
    click examples "https://github.com/zhuochun/simple-editor/tree/main/examples/"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#333,stroke-width:1px
    classDef data fill:#DFFFD0,stroke:#333,stroke-width:1px
    classDef interaction fill:#E0FFD0,stroke:#333,stroke-width:1px
    classDef service fill:#FFF5C2,stroke:#333,stroke-width:1px
    classDef storage fill:#DDDDDD,stroke:#333,stroke-width:1px
    classDef external fill:#FFD080,stroke:#333,stroke-width:1px
    classDef infra fill:#FFCCCC,stroke:#333,stroke-width:1px
```
