Skip to content

alegauss/universal-editor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

36 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

universal-editor

Universal Editor - Modern TypeScript implementation with clean, modular, and maintainable code.

๐ŸŽฏ Overview

A modern TypeScript implementation of the Universal Editor that allows content authors to edit content directly in context. This version is written in TypeScript and generates a minified JavaScript bundle with all dependencies included.

โœจ Features

  • TypeScript: Full TypeScript implementation with type safety
  • Multiple Editor Support: Works with both TinyMCE and ProseMirror
  • Cross-frame Communication: Uses Penpal for secure parent-child frame communication
  • Type-safe Validation: Utilizes Zod for runtime type checking
  • State Management: Built with Zustand for lightweight, reactive state
  • Event-driven Architecture: Comprehensive event system for content operations
  • Minified Build: Production-ready minified bundle with all dependencies included
  • Clean Code: Fully documented TypeScript source

๐Ÿ“ฆ Installation

npm install

๐Ÿ”จ Building

Build the TypeScript source into a minified JavaScript bundle:

npm run build

This will:

  1. Compile TypeScript to JavaScript
  2. Bundle all dependencies
  3. Minify the output
  4. Generate source maps
  5. Output to dist/universal-editor.min.js (~224KB minified with all dependencies)

๐Ÿ”ง Dependencies

Runtime Dependencies

All public libraries are now properly managed via npm:

  • penpal (^6.2.2): Cross-frame communication
  • zod (^3.22.4): TypeScript-first schema validation
  • prosemirror-* : Rich text editor framework
  • zustand (^4.4.7): State management

Dev Dependencies

  • typescript (^5.3.3): TypeScript compiler
  • esbuild (^0.19.9): Fast JavaScript bundler and minifier
  • tsx (^4.7.0): TypeScript execution engine

๐Ÿš€ Usage

For Production (Built Bundle)

Use the minified bundle that includes all dependencies:

<script src="dist/universal-editor.min.js"></script>

For Development (Local)

 <script type="module" src="src/universal-editor.ts"></script>
 <script type="module" src="src/editor/editor-bridge.ts"></script>

See src/pages/example.html for a complete working example.

๐ŸŽฎ Testing the Editor

Starting the Development Server

First, start the development server:

npm run dev

This will start a Vite server at http://localhost:3000.

Viewing Example Pages

You can view the example pages directly to see the content:

  • Example 1 (Universal Editor): http://localhost:3000/pages/example.html
  • Example 2 (Technology and AI): http://localhost:3000/pages/example2.html
  • Example 3 (Travel and Nature): http://localhost:3000/pages/example3.html

These pages show different content themes with various layouts and styles.

Editing Content

To edit the content, you need to use the editor host:

http://localhost:3000/pages/editor-host.html

The editor host provides:

  • ๐ŸŽจ Toolbar with edit/preview mode buttons
  • ๐Ÿ“ Rich Text Editor modal for editing text content
  • ๐Ÿ–ผ๏ธ Image Editor modal for changing images
  • ๐Ÿ”„ URL Switcher to load different pages (default: http://localhost:3000/pages/example.html)
  • ๐Ÿ’พ Save Button to persist changes

How to use:

  1. Open editor-host.html in your browser
  2. Change the URL in the header if you want to edit a different page (example2.html, example3.html, etc.)
  3. Click "๐Ÿ”„ Load" to load the page
  4. Click "โœ๏ธ Edit" to enter edit mode
  5. Click on any highlighted element to edit it
  6. Make your changes in the modal editor
  7. Click "๐Ÿ’พ Save" to save (when implemented)

Example URLs to try:

  • http://localhost:3000/pages/example.html - Original Universal Editor intro
  • http://localhost:3000/pages/example2.html - Technology and AI theme
  • http://localhost:3000/pages/example3.html - Travel and Nature theme
  • Or any other URL you want to make editable!

๐Ÿ“ Content Editing

The editor supports various content types:

  • โœ๏ธ Text: Plain text content
  • ๐Ÿ“„ Rich Text: HTML formatted content
  • ๐Ÿ–ผ๏ธ Media: Images and other media
  • ๐Ÿ”— References: Links to other content
  • ๐Ÿ“ฆ Containers: Groups of components
  • ๐Ÿงฉ Components: Reusable content blocks

Marking Content as Editable

Use data attributes to define editable regions:

<!-- Text Content -->
<h1 
  data-aue-resource="urn:aem:/content/page/title" 
  data-aue-type="text"
  data-aue-prop="title">
  My Title
</h1>

<!-- Rich Text Content -->
<div 
  data-aue-resource="urn:aem:/content/page/body"
  data-aue-type="richtext"
  data-aue-prop="content">
  <p>Rich text content here...</p>
</div>

<!-- Image Content -->
<img 
  data-aue-resource="urn:aem:/content/page/hero"
  data-aue-type="media"
  data-aue-prop="image"
  src="/path/to/image.jpg" 
  alt="Hero" />

<!-- Container -->
<div 
  data-aue-resource="urn:aem:/content/page/container"
  data-aue-type="container">
  <!-- Components go here -->
</div>

๐ŸŽช Events

The editor dispatches various custom events you can listen to:

// Editor initialized and ready
document.addEventListener('aue:initialized', () => {
  console.log('Editor ready!');
});

// Entering/exiting edit mode
document.addEventListener('aue:ui-edit', () => {
  console.log('Edit mode activated');
});

document.addEventListener('aue:ui-preview', () => {
  console.log('Preview mode activated');
});

// Content operations
document.addEventListener('aue:content-update', (event) => {
  console.log('Content updated:', event.detail);
});

document.addEventListener('aue:content-add', (event) => {
  console.log('Content added:', event.detail);
});

document.addEventListener('aue:content-remove', (event) => {
  console.log('Content removed:', event.detail);
});

Available Events

Event Description
aue:initialized Editor is ready
aue:content-add Content was added
aue:content-remove Content was removed
aue:content-move Content was moved
aue:content-patch Content was patched
aue:content-update Content was updated
aue:content-copy Content was copied
aue:ui-edit Entered edit mode
aue:ui-preview Entered preview mode
aue:ui-select Element was selected

๐Ÿ› ๏ธ Development

Testing

npm test

This validates the TypeScript source structure and verifies:

  • All required imports and functions are present
  • TypeScript types are properly defined
  • Built bundle exists and is correctly generated

Building

npm run build

Compiles TypeScript and bundles into a minified JavaScript file with all dependencies.

Cleaning

npm run clean

Removes the dist/ directory with all built files.

Validation

npm run validate

Runs both tests and build to ensure everything works correctly.

๐Ÿ“ Project Structure

universal-editor/
โ”œโ”€โ”€ src/                          # TypeScript source files
โ”‚   โ”œโ”€โ”€ universal-editor.ts       # Main editor implementation
โ”‚   โ”œโ”€โ”€ build.ts                  # Build script
โ”‚   โ”œโ”€โ”€ test.ts                   # Test script
โ”‚   โ”œโ”€โ”€ editor/                   # Editor components
โ”‚   โ”‚   โ”œโ”€โ”€ editor-bridge.ts      # Bridge between iframe and host
โ”‚   โ”‚   โ””โ”€โ”€ prosemirror-toolbar.ts # ProseMirror toolbar implementation
โ”‚   โ””โ”€โ”€ pages/                    # Example HTML pages
โ”‚       โ”œโ”€โ”€ editor-host.html      # Editor host with toolbar (EDITING)
โ”‚       โ”œโ”€โ”€ example.html          # Example page 1 - Universal Editor intro
โ”‚       โ”œโ”€โ”€ example2.html         # Example page 2 - Technology and AI
โ”‚       โ””โ”€โ”€ example3.html         # Example page 3 - Travel and Nature
โ”œโ”€โ”€ dist/                         # Built files (generated)
โ”‚   โ”œโ”€โ”€ universal-editor.min.js   # Minified bundle with all dependencies
โ”‚   โ”œโ”€โ”€ universal-editor.min.js.map # Source map
โ”‚   โ”œโ”€โ”€ universal-editor.js       # Compiled TypeScript (not minified)
โ”‚   โ”œโ”€โ”€ universal-editor.d.ts     # TypeScript declarations
โ”‚   โ””โ”€โ”€ ...                       # Other compiled files
โ”œโ”€โ”€ tsconfig.json                 # TypeScript configuration
โ”œโ”€โ”€ vite.config.ts                # Vite configuration
โ”œโ”€โ”€ package.json                  # Project configuration
โ””โ”€โ”€ README.md                     # This file

Code Structure

The TypeScript code is organized as follows:

src/universal-editor.ts       # Main entry point
โ”œโ”€โ”€ Types & Interfaces        # TypeScript type definitions
โ”œโ”€โ”€ Constants                 # EditorMode, ContentType, Events, etc.
โ”œโ”€โ”€ State Management          # Zustand store for editor state
โ”œโ”€โ”€ ProseMirror Setup         # Rich text editor configuration
โ”œโ”€โ”€ Event Handlers            # DOM event listeners and handlers
โ”œโ”€โ”€ Parent Communication      # Penpal methods for cross-frame calls
โ””โ”€โ”€ Initialization            # App startup logic

Performance Metrics

File Size Comparison:

  • Original minified bundle: ~596 KB
  • TypeScript source: ~17 KB
  • New minified bundle (with all deps): ~224 KB (62% reduction)
  • With tree-shaking and gzip, production size is even smaller

๐Ÿ“„ Files

TypeScript Source

  • src/universal-editor.ts - Main editor implementation (TypeScript)
  • src/build.ts - Build script (TypeScript)
  • src/test.ts - Test script (TypeScript)
  • tsconfig.json - TypeScript configuration

Built Output

  • dist/universal-editor.min.js - Minified bundle with all dependencies
  • dist/universal-editor.min.js.map - Source map
  • dist/universal-editor.js - Compiled JavaScript (not minified)
  • dist/universal-editor.d.ts - TypeScript declarations

๐Ÿค Contributing

When contributing, please:

  1. Keep code clean and well-documented
  2. Use TypeScript for new code
  3. Follow existing code style
  4. Test your changes thoroughly
  5. Run npm run validate before submitting

๐Ÿ”— Links

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published