Skip to content

pwin/turtle-editor-viewer

Repository files navigation

Turtle Editor Viewer (React + TypeScript)

A modern, React-based RDF/Turtle editor and graph visualizer built with TypeScript, featuring real-time graph visualization, SPARQL querying, and format conversion.

πŸš€ Features

  • RDF/Turtle Editing: Syntax-highlighted editor with support for Turtle, RDF/XML, JSON-LD, and DOT formats
  • Graph Visualization: Real-time graph rendering using Graphviz with interactive pan/zoom
  • SPARQL Queries: Execute SPARQL SELECT, CONSTRUCT, DESCRIBE, and ASK queries
  • Format Conversion: Convert between RDF formats (Turtle ↔ RDF/XML ↔ JSON-LD)
  • File Operations: Load/save files locally and from URLs with CORS handling
  • Modern UI: Responsive, dark-themed interface built with React
  • Type Safety: Full TypeScript coverage for better development experience

πŸ› οΈ Technology Stack

  • Frontend: React 18 + TypeScript + Vite
  • State Management: React Context API with useReducer
  • Editor: Ace Editor with syntax highlighting
  • Visualization: Viz.js/Graphviz for graph rendering
  • RDF Processing: RDF-Ext + N3.js for parsing
  • Styling: Modern CSS with CSS Modules
  • Development: ESLint + Prettier + TypeScript

πŸ—οΈ Architecture

Component Structure

src/
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ layout/           # Layout components (Header, MainLayout)
β”‚   β”œβ”€β”€ editor/           # Editor components (EditorPane, Toolbar, AceEditor)
β”‚   └── graph/            # Graph components (GraphPane, Visualization, SPARQL)
β”œβ”€β”€ services/             # Business logic
β”‚   β”œβ”€β”€ rdf-parser.ts     # RDF parsing and format detection
β”‚   β”œβ”€β”€ graph-generator.ts # DOT graph generation from RDF
β”‚   β”œβ”€β”€ sparql-engine.ts  # SPARQL query execution
β”‚   └── file-handler.ts   # File I/O and URL operations
β”œβ”€β”€ store/               # State management
β”‚   └── AppProvider.tsx  # React Context + useReducer
β”œβ”€β”€ types/               # TypeScript type definitions
└── utils/               # Utility functions

State Management

The application uses React Context with useReducer for predictable state management:

  • Editor State: Content, language, theme, loading status
  • Graph State: DOT text, SVG output, visualization options
  • RDF State: Parsed quads, subjects, prefixes, selections
  • SPARQL State: Query text, results, execution status

πŸš€ Quick Start

Prerequisites

  • Node.js 18+ and npm/yarn
  • Modern browser with ES2020 support

Installation

# Install dependencies
npm install

# Start development server
npm run dev

# Build for production
npm run build

# Preview production build
npm run preview

Development

# Type check
npm run type-check

# Lint code
npm run lint

# Format code
npx prettier --write .

πŸ“– Usage

Basic Workflow

  1. Load RDF Data:

    • Paste Turtle/RDF content into the editor
    • Upload a local RDF file
    • Load from URL (CORS permitting)
  2. Parse and Visualize:

    • Click "Get All" to extract subjects
    • Select subjects from the dropdown
    • View the generated graph visualization
  3. Query with SPARQL:

    • Write SPARQL queries in the bottom panel
    • Execute queries against the loaded RDF data
    • View results in tabular format
  4. Export Results:

    • Download the edited RDF content
    • Save graph visualizations as SVG/PNG
    • Export SPARQL results

URL Parameters

Load content directly via URL parameters:

  • ?dot=<url> - Load DOT or Turtle file from URL
  • ?rdfa=<url> - Extract RDFa from HTML page

Example:

https://yoursite.com/turtle-editor-viewer/?dot=https://example.com/data.ttl

Supported Formats

Format Extension MIME Type Description
Turtle .ttl text/turtle Terse RDF Triple Language
RDF/XML .rdf, .xml application/rdf+xml RDF XML serialization
JSON-LD .json, .jsonld application/ld+json JSON for Linked Data
DOT .dot text/plain Graphviz DOT language

πŸ”§ Configuration

Visualization Options

  • Engine: Choose Graphviz layout engine (dot, circo, fdp, neato, osage, twopi)
  • Format: Output format (SVG, PNG, JSON, XDOT, plain, PS)
  • Layout Direction: Graph orientation (LR, RL, TB, BT)
  • Display Options: Show/hide prefixes, types, subjects

Editor Settings

  • Language: Syntax highlighting mode (turtle, xml, javascript, dot)
  • Theme: Color scheme (cobalt, dawn, eclipse, github)
  • Features: Line numbers, code folding, auto-completion

πŸ›οΈ Migration from JavaScript

This React/TypeScript version replaces the original vanilla JavaScript implementation with:

Key Improvements

  • Type Safety: Full TypeScript coverage prevents runtime errors
  • Component Architecture: Modular, reusable React components
  • Modern Tooling: Vite for fast development and optimized builds
  • State Management: Predictable state with React Context
  • Error Handling: Proper error boundaries and user feedback
  • Performance: Code splitting and optimizations
  • Developer Experience: Hot reload, linting, and debugging tools

Migration Benefits

  • βœ… Maintainable codebase with clear separation of concerns
  • βœ… Type-safe RDF operations and graph generation
  • βœ… Modern development workflow with hot reload
  • βœ… Responsive UI that works on all devices
  • βœ… Extensible architecture for future features
  • βœ… Comprehensive error handling and loading states

πŸ› οΈ Development

Project Structure

The codebase follows modern React patterns:

// Type-safe RDF operations
const parser = new RDFParser()
const result = await parser.parseRDF(content, 'turtle')

// Component with proper typing
interface EditorProps {
  value: string
  onChange: (value: string) => void
  language: EditorLanguage
}

// State management with context
const { state, dispatch } = useAppContext()
dispatch({ type: 'SET_EDITOR_CONTENT', payload: content })

Custom Hooks

The application uses custom hooks for complex operations:

  • useAppContext() - Access global application state
  • useRDFParser() - RDF parsing with caching
  • useGraphGenerator() - DOT generation from RDF data

Service Layer

Business logic is separated into services:

  • RDFParser: Handles all RDF format parsing
  • GraphGenerator: Creates DOT graphs from RDF data
  • SPARQLEngine: Executes SPARQL queries
  • FileHandler: Manages file operations and URL loading

πŸ” Troubleshooting

Common Issues

CORS Errors: When loading from URLs, ensure the server supports CORS or use a proxy.

Large Files: For files >10MB, consider streaming or chunked processing.

Memory Usage: Large RDF datasets may require pagination or virtualization.

Browser Support

  • Chrome 90+
  • Firefox 88+
  • Safari 14+
  • Edge 90+

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes with tests
  4. Run npm run lint and npm run type-check
  5. Submit a pull request

πŸ“„ License

MIT License - see LICENSE file for details.

πŸ”— Links


Note: This is a modernized React/TypeScript version of the original turtle-editor-viewer. The core functionality remains the same while providing a much improved developer and user experience.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published