# yahianshi/redpen architecture

> Architecture diagram of the GitHub repository yahianshi/redpen, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yahianshi/redpen
- Repository: https://github.com/yahianshi/redpen

## Overview

RedPen is a Chrome/Brave extension for grading student PDFs directly in Google Classroom. The main workflow detects a submission and its Drive file, opens an in-page canvas, captures stylus annotations, and autosaves flattened PDF changes to Drive. The graph also includes the standalone annotator, OAuth and revision restore, settings, and popup setup capabilities described in the README. Edges follow the shown imports and calls; other unsampled interactions are omitted.

Main entry points: [`src/content/content_script.ts`](https://github.com/yahianshi/redpen/blob/main/src/content/content_script.ts) (In-Page Grading), [`src/annotator/annotator.ts`](https://github.com/yahianshi/redpen/blob/main/src/annotator/annotator.ts) (Standalone Annotator), [`src/background/background.ts`](https://github.com/yahianshi/redpen/blob/main/src/background/background.ts) (Message Worker), [`src/services/storage.ts`](https://github.com/yahianshi/redpen/blob/main/src/services/storage.ts) (Local Storage)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_classroom["Classroom Integration"]
  node_detector["Submission Detector<br/>[detector.ts]"]
  node_content["In-Page Grading<br/>[content_script.ts]"]
end

subgraph group_grading["PDF Grading"]
  node_annotator["Standalone Annotator<br/>[annotator.ts]"]
  node_toolbar["Grading Toolbar<br/>[toolbar.ts]"]
  node_viewer["PDF Canvas Viewer<br/>[canvasViewer.ts]"]
  node_stylus["Stylus Engine<br/>[stylus.ts]"]
  node_flattener["PDF Flattener<br/>[flattener.ts]"]
end

subgraph group_google["Google Services"]
  node_storage[("Local Storage<br/>[storage.ts]")]
  node_background["Message Worker<br/>[background.ts]"]
  node_auth["OAuth Manager<br/>[auth.ts]"]
  node_drive["Drive and Classroom API<br/>[drive.ts]"]
end

subgraph group_extension["Extension Setup"]
  node_popup["Setup Popup<br/>[popup.ts]"]
  node_options["Extension Options<br/>[options.ts]"]
end

node_teacher(("Teacher"))
node_classroom_site(("Google Classroom"))
node_google_services(("Google Drive / Classroom"))

node_teacher -->|"grades in"| node_classroom_site
node_classroom_site -->|"hosts page"| node_detector
node_detector -->|"reports submission"| node_content
node_teacher -->|"opens grading"| node_content
node_content -->|"creates"| node_toolbar
node_content -->|"creates"| node_viewer
node_content -->|"handles annotations"| node_stylus
node_content -->|"flattens PDF"| node_flattener
node_content -->|"caches session"| node_storage
node_viewer -->|"forwards pointer input"| node_stylus
node_annotator -->|"creates"| node_toolbar
node_annotator -->|"creates"| node_viewer
node_annotator -->|"configures"| node_stylus
node_annotator -->|"flattens PDF"| node_flattener
node_annotator -->|"loads and saves"| node_storage
node_annotator -->|"requests Drive actions"| node_background
node_content -->|"sends extension messages"| node_background
node_background -->|"gets OAuth token"| node_auth
node_drive -->|"gets valid token"| node_auth
node_background -->|"dispatches API operations"| node_drive
node_drive -->|"calls APIs"| node_google_services
node_popup -->|"reads settings"| node_storage
node_options -->|"updates settings"| node_storage
node_popup -.->|"auth setup"| node_background

click node_detector "https://github.com/yahianshi/redpen/blob/main/src/content/detector.ts"
click node_content "https://github.com/yahianshi/redpen/blob/main/src/content/content_script.ts"
click node_annotator "https://github.com/yahianshi/redpen/blob/main/src/annotator/annotator.ts"
click node_toolbar "https://github.com/yahianshi/redpen/blob/main/src/ui/toolbar.ts"
click node_viewer "https://github.com/yahianshi/redpen/blob/main/src/ui/canvasViewer.ts"
click node_stylus "https://github.com/yahianshi/redpen/blob/main/src/engine/stylus.ts"
click node_flattener "https://github.com/yahianshi/redpen/blob/main/src/services/flattener.ts"
click node_storage "https://github.com/yahianshi/redpen/blob/main/src/services/storage.ts"
click node_background "https://github.com/yahianshi/redpen/blob/main/src/background/background.ts"
click node_auth "https://github.com/yahianshi/redpen/blob/main/src/services/auth.ts"
click node_drive "https://github.com/yahianshi/redpen/blob/main/src/services/drive.ts"
click node_popup "https://github.com/yahianshi/redpen/blob/main/src/popup/popup.ts"
click node_options "https://github.com/yahianshi/redpen/blob/main/src/options/options.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_detector,node_content toneBlue
class node_annotator,node_toolbar,node_viewer,node_stylus,node_flattener toneAmber
class node_storage,node_background,node_auth,node_drive toneMint
class node_popup,node_options toneRose
class node_teacher,node_classroom_site,node_google_services toneIndigo
```

## Components

### Classroom Integration

- [Submission Detector](https://github.com/yahianshi/redpen/blob/main/src/content/detector.ts): `src/content/detector.ts`
- [In-Page Grading](https://github.com/yahianshi/redpen/blob/main/src/content/content_script.ts): `src/content/content_script.ts`

### PDF Grading

- [Standalone Annotator](https://github.com/yahianshi/redpen/blob/main/src/annotator/annotator.ts): `src/annotator/annotator.ts`
- [Grading Toolbar](https://github.com/yahianshi/redpen/blob/main/src/ui/toolbar.ts): `src/ui/toolbar.ts`
- [PDF Canvas Viewer](https://github.com/yahianshi/redpen/blob/main/src/ui/canvasViewer.ts): `src/ui/canvasViewer.ts`
- [Stylus Engine](https://github.com/yahianshi/redpen/blob/main/src/engine/stylus.ts): `src/engine/stylus.ts`
- [PDF Flattener](https://github.com/yahianshi/redpen/blob/main/src/services/flattener.ts): `src/services/flattener.ts`

### Google Services

- [Local Storage](https://github.com/yahianshi/redpen/blob/main/src/services/storage.ts): `src/services/storage.ts`
- [Message Worker](https://github.com/yahianshi/redpen/blob/main/src/background/background.ts): `src/background/background.ts`
- [OAuth Manager](https://github.com/yahianshi/redpen/blob/main/src/services/auth.ts): `src/services/auth.ts`
- [Drive and Classroom API](https://github.com/yahianshi/redpen/blob/main/src/services/drive.ts): `src/services/drive.ts`

### Extension Setup

- [Setup Popup](https://github.com/yahianshi/redpen/blob/main/src/popup/popup.ts): `src/popup/popup.ts`
- [Extension Options](https://github.com/yahianshi/redpen/blob/main/src/options/options.ts): `src/options/options.ts`

### Other components

- **Teacher**
- **Google Classroom**
- **Google Drive / Classroom**

## Connections

- Teacher → Google Classroom: grades in (evidence: `README.md`)
- Google Classroom → Submission Detector: hosts page (evidence: `README.md`)
- Submission Detector → In-Page Grading: reports submission (evidence: `src/content/content_script.ts`)
- Teacher → In-Page Grading: opens grading (evidence: `README.md`)
- In-Page Grading → Grading Toolbar: creates (evidence: `src/content/content_script.ts`)
- In-Page Grading → PDF Canvas Viewer: creates (evidence: `src/content/content_script.ts`)
- In-Page Grading → Stylus Engine: handles annotations (evidence: `src/content/content_script.ts`)
- In-Page Grading → PDF Flattener: flattens PDF (evidence: `src/content/content_script.ts`)
- In-Page Grading → Local Storage: caches session (evidence: `src/content/content_script.ts`)
- PDF Canvas Viewer → Stylus Engine: forwards pointer input (evidence: `src/ui/canvasViewer.ts`)
- Standalone Annotator → Grading Toolbar: creates (evidence: `src/annotator/annotator.ts`)
- Standalone Annotator → PDF Canvas Viewer: creates (evidence: `src/annotator/annotator.ts`)
- Standalone Annotator → Stylus Engine: configures (evidence: `src/annotator/annotator.ts`)
- Standalone Annotator → PDF Flattener: flattens PDF (evidence: `src/annotator/annotator.ts`)
- Standalone Annotator → Local Storage: loads and saves (evidence: `src/annotator/annotator.ts`)
- Standalone Annotator → Message Worker: requests Drive actions (evidence: `src/annotator/annotator.ts`)
- In-Page Grading → Message Worker: sends extension messages (evidence: `src/content/content_script.ts`)
- Message Worker → OAuth Manager: gets OAuth token (evidence: `src/background/background.ts`)
- Drive and Classroom API → OAuth Manager: gets valid token (evidence: `src/services/drive.ts`)
- Message Worker → Drive and Classroom API: dispatches API operations (evidence: `src/background/background.ts`)
- Drive and Classroom API → Google Drive / Classroom: calls APIs (evidence: `src/services/drive.ts`)
- Setup Popup → Local Storage: reads settings (evidence: `src/popup/popup.ts`)
- Extension Options → Local Storage: updates settings (evidence: `src/options/options.ts`)
- Setup Popup → Message Worker: auth setup
