# JSON Visual Editor Integration

## Overview

The Triage GPT plugin now includes a **Visual JSON Editor** powered by [JSON Crack](https://jsoncrack.com/). This provides an interactive graph-based interface for editing JSON data in the WordPress admin.

## Features

- **Visual Graph Editor**: View and understand JSON structure as interactive node graphs
- **Dual Mode**: Switch between visual and text modes
- **Real-time Validation**: Instant feedback on JSON syntax errors
- **Pan & Zoom Controls**: Navigate large JSON structures easily
- **WordPress Native**: Fully integrated into admin pages with modal interface

## Setup & Build

### Dependencies Installed
- `react` & `react-dom`: React framework
- `jsoncrack-react`: JSON visualization component
- `vite`: Build tool for React components
- `@vitejs/plugin-react`: Vite React plugin

### Build Commands

```bash
# Build everything (CSS + JS)
npm run build

# Build only JavaScript (React components)
npm run build:js

# Build only CSS (Tailwind)
npm run build:css

# Development mode (CSS watch)
npm run dev

# Development mode (JS watch)
npm run dev:js
```

### File Structure

```
triage-gpt/
├── src/
│   ├── js/
│   │   └── json-editor.jsx     # React JSON editor component
│   └── css/
│       ├── triage.css          # Main Tailwind CSS
│       └── json-editor.css     # JSON editor modal styles
├── assets/
│   └── build/
│       ├── json-editor.js      # Compiled React bundle
│       ├── json-editor.css     # Compiled modal styles
│       └── triage.css          # Compiled Tailwind CSS
├── vite.config.js              # Vite build configuration
└── package.json                # Dependencies and scripts
```

## Usage in Admin Pages

### Auto-initialization

Add the `data-json-editor` attribute to any textarea:

```php
<textarea 
  name="description[]" 
  class="widefat code" 
  rows="14" 
  data-json-editor
><?php echo esc_textarea($data); ?></textarea>
```

The JSON editor will automatically enhance these textareas with:
- **"Open Visual JSON Editor"** button - Opens the visual graph editor in a modal
- **"Validate JSON"** button - Quickly validates JSON syntax

### Manual Initialization

For dynamically created textareas:

```javascript
// After creating a textarea with data-json-editor attribute
if (window.TriageJSONEditor) {
  const textarea = document.querySelector('textarea[data-json-editor]');
  window.TriageJSONEditor.init(textarea);
}
```

### Enqueuing in Admin Pages

```php
// Enqueue JSON editor assets
$json_editor_js = TRIAGE_GPT_DIR . 'assets/build/json-editor.js';
$json_editor_css = TRIAGE_GPT_DIR . 'assets/build/json-editor.css';

if (file_exists($json_editor_js)) {
    wp_enqueue_script(
        'triage-json-editor',
        TRIAGE_GPT_URL . 'assets/build/json-editor.js',
        [],
        filemtime($json_editor_js),
        true
    );
}

if (file_exists($json_editor_css)) {
    wp_enqueue_style(
        'triage-json-editor',
        TRIAGE_GPT_URL . 'assets/build/json-editor.css',
        [],
        filemtime($json_editor_css)
    );
}
```

## Current Integration

The JSON visual editor is currently integrated into:

- **Self-Help Items** (`admin/page-triage-self-help.php`): Description field supports JSON or prose

## Performance Notes

- **Default node limit**: 500 nodes (configurable in `json-editor.jsx`)
- **Recommended size**: Up to 300-500 nodes for smooth interaction
- **Large JSON**: Consider flattening or trimming data before editing
- **Bundle size**: ~2.8MB (includes graph visualization library)

## Modal Interface

### Visual Mode
- Interactive graph showing JSON structure as nodes
- Click and drag to pan
- Zoom controls (buttons or mouse wheel)
- Center view button to reset position

### Text Mode
- Traditional textarea for direct JSON editing
- Syntax error highlighting
- Line-by-line editing

### Features
- **Tab switching**: Toggle between Visual and Text modes
- **Save validation**: Prevents saving invalid JSON
- **Real-time parsing**: Updates graph as you edit text
- **Responsive**: Works on desktop and tablet screens

## Troubleshooting

### Build Issues

If you encounter build errors:

```bash
# Clean and rebuild
rm -rf node_modules assets/build/*.js
npm install
npm run build
```

### Permission Issues

If you get permission errors:

```bash
chmod +x node_modules/.bin/*
```

### Large Bundle Warning

The JSON Crack library includes a graph layout engine (~1.4MB). This is expected and necessary for the visualization features.

## Future Enhancements

Potential additions:
- Add to more admin pages (Global Fields, Prompts, etc.)
- Dark mode support
- Custom node rendering
- Export/import JSON files
- JSON diff viewer
- Schema validation

## Technical Details

- **Framework**: React 19.x
- **Build Tool**: Vite 8.x
- **Visualization**: JSON Crack (jsoncrack-react)
- **Graph Engine**: ELK (Eclipse Layout Kernel)
- **Styling**: Custom CSS + jsoncrack-react styles

## License

The JSON editor integration uses:
- **JSON Crack** - Apache-2.0 License
- **React** - MIT License
