- Framework
- Core Concepts
- Event System
Event System
The DesignCombo Video Editor SDK uses a centralized event system for communication between components. This event-driven architecture ensures loose coupling and predictable data flow. The event system is the backbone of the state management system, handling all operations through a unified dispatch mechanism.
Event Architecture
┌─────────────┐ ┌─────────────┐ ┌─────────────┐│ UI Layer │───►│ Event Bus │───►│ State Layer ││ │ │ │ │ ││ • Buttons │ │ • Dispatch │ │ • Handlers ││ • Menus │ │ • Filter │ │ • Updates ││ • Keyboard │ │ • Subscribe │ │ • Business │└─────────────┘ └─────────────┘ └─────────────┘
Event Dispatching
Basic Event Dispatch
import { dispatch } from "@designcombo/events";import { ADD_TEXT, ADD_IMAGE } from "@designcombo/state";// Dispatch a simple eventdispatch(ADD_TEXT, {payload: {text: "Hello World",fontSize: 24,color: "#000000"},options: {targetTrackIndex: 0,isSelected: true}});
Event Structure
interface Event {type: string; // Event typepayload?: any; // Event dataoptions?: { // Event optionstargetTrackIndex?: number;targetTrackId?: string;isNewTrack?: boolean;isSelected?: boolean;scaleMode?: string;scaleAspectRatio?: number;};metadata?: { // Optional metadatatimestamp: number;source: string;id: string;};}
Event Categories
The DesignCombo event system organizes events into several categories based on their functionality:
Add Events
Events for adding new elements to the design:
ADD_TEXT
- Add text elementsADD_IMAGE
- Add imagesADD_VIDEO
- Add videosADD_AUDIO
- Add audio filesADD_SHAPE
- Add shapesADD_ILLUSTRATION
- Add illustrationsADD_TEMPLATE
- Add templatesADD_CAPTIONS
- Add captionsADD_ANIMATION
- Add animationsADD_COMPOSITION
- Add compositionsADD_RECT
- Add rectanglesADD_PROGRESS_BAR
- Add progress barsADD_PROGRESS_FRAME
- Add progress framesADD_PROGRESS_SQUARE
- Add progress squaresADD_RADIAL_AUDIO_BARS
- Add radial audio visualizationsADD_LINEAL_AUDIO_BARS
- Add linear audio visualizationsADD_WAVE_AUDIO_BARS
- Add wave audio visualizationsADD_HILL_AUDIO_BARS
- Add hill audio visualizations
Edit Events
Events for editing existing elements:
EDIT_OBJECT
- Edit object propertiesREPLACE_MEDIA
- Replace media filesEDIT_BACKGROUND_EDITOR
- Edit background
Layer Events
Events for managing layers:
LAYER_SELECT
- Select layersLAYER_COPY
- Copy layersLAYER_DELETE
- Delete layersLAYER_CLONE
- Clone layersLAYER_REPLACE
- Replace layersLAYER_MOVE
- Move layersLAYER_RENAME
- Rename layersLAYER_LOCKED
- Lock/unlock layersLAYER_HIDDEN
- Show/hide layers
Design Events
Events for managing the overall design:
DESIGN_LOAD
- Load a designDESIGN_RESIZE
- Resize the design
History Events
Events for undo/redo functionality:
HISTORY_UNDO
- Undo last actionHISTORY_REDO
- Redo last action
Active Object Events
Events for managing selected objects:
ACTIVE_PASTE
- Paste active objectsACTIVE_SPLIT
- Split active objects
Timeline Scale Events
Events for managing timeline zoom:
TIMELINE_SCALE_CHANGED
- Change timeline scale
Event Types
1. Add Events
Events for adding new elements to the timeline.
// Add text elementdispatch(ADD_TEXT, {payload: {text: "Hello World",fontSize: 48,fontFamily: "Arial",color: "#ffffff"},options: {targetTrackIndex: 0,isSelected: true}});// Add image elementdispatch(ADD_IMAGE, {payload: {src: "https://example.com/image.jpg",alt: "Sample image",width: 400,height: 300},options: {targetTrackIndex: 1,scaleMode: "fit",isSelected: true}});// Add video elementdispatch(ADD_VIDEO, {payload: {src: "https://example.com/video.mp4",duration: 10000},options: {targetTrackIndex: 2,scaleMode: "fit",isSelected: true}});// Add audio elementdispatch(ADD_AUDIO, {payload: {src: "https://example.com/audio.mp3",duration: 5000},options: {targetTrackIndex: 3,isSelected: false}});
2. Edit Events
Events for editing existing elements.
// Edit object propertiesdispatch(EDIT_OBJECT, {payload: {"item-123": {details: {text: "Updated text",fontSize: 64,color: "#ff0000"},display: {from: 0,to: 5000},playbackRate: 1.5}}});// Replace media in existing itemdispatch(REPLACE_MEDIA, {payload: {itemId: "item-123",newSrc: "https://example.com/new-image.jpg",alt: "New image"}});// Edit backgrounddispatch(EDIT_BACKGROUND_EDITOR, {payload: {type: "color",value: "#ffffff"}});
3. Layer Events
Events for managing timeline layers and items.
// Select itemsdispatch(LAYER_SELECT, {payload: {trackItemIds: ["item-1", "item-2", "item-3"]}});// Delete itemsdispatch(LAYER_DELETE, {payload: {trackItemIds: ["item-1", "item-2"]}});// Clone itemsdispatch(LAYER_CLONE, {payload: {trackItemIds: ["item-1"]}});// Copy items to clipboarddispatch(LAYER_COPY, {});
4. History Events
Events for undo/redo functionality.
// Undo last operationdispatch(HISTORY_UNDO, {});// Redo last undone operationdispatch(HISTORY_REDO, {});
5. Design Events
Events for project-level operations.
// Load designdispatch(DESIGN_LOAD, {payload: {fps: 30,tracks: [...],size: { width: 1920, height: 1080 },trackItemIds: [...],trackItemsMap: {...},transitionIds: [...],transitionsMap: {...}}});// Resize designdispatch(DESIGN_RESIZE, {payload: {width: 1920,height: 1080}});
6. Timeline Scale Events
Events for timeline zoom and scale management.
// Change timeline scaledispatch(TIMELINE_SCALE_CHANGED, {payload: {scale: {index: 7,unit: 300,zoom: 1 / 300,segments: 5}}});
7. Active Object Events
Events for managing selected objects.
// Paste active objectsdispatch(ACTIVE_PASTE, {});// Split active objects at specific timedispatch(ACTIVE_SPLIT, {options: {time: 2500}});
Bulk Operations
EDIT_BULK
Performs multiple operations in a single transaction.
dispatch(EDIT_BULK, {payload: {actions: [{type: ADD_TEXT,payload: { text: "First item" }},{type: ADD_IMAGE,payload: { src: "image.jpg" }}]}});
Event Subscriptions
Subscribe to Events
import { subscribe, unsubscribe } from "@designcombo/events";// Subscribe to specific event typeconst subscription = subscribe(ADD_TEXT, (event) => {console.log("Text added:", event.payload);});// Subscribe to multiple event typesconst multiSubscription = subscribe([ADD_TEXT, ADD_IMAGE], (event) => {console.log("Element added:", event.type, event.payload);});// Subscribe to all eventsconst allSubscription = subscribe("*", (event) => {console.log("All events:", event.type, event.payload);});// Unsubscribeunsubscribe(subscription);unsubscribe(multiSubscription);unsubscribe(allSubscription);
Event Filters
// Subscribe with filterconst filteredSubscription = subscribe("add:*", (event) => {console.log("Add event:", event.type, event.payload);}, {filter: (event) => {// Only process events with specific conditionsreturn event.payload.name.includes("Important");}});
Important Notes
- 1Event Order: Events are processed asynchronously, so the order of operations matters
- 2State Updates: All state updates are handled automatically by the state manager
- 3History: Most operations are automatically added to the undo/redo history
- 4Validation: The system validates inputs and handles errors gracefully
- 5Performance: Bulk operations should be used for multiple related changes
- 6Tracks: Items are automatically placed on appropriate tracks based on their type
- 7Timing: All items have timing information (from/to) for timeline positioning
Next Steps
- Learn about Track Items
- Understand Animations
- Explore Transitions
- Check out Examples for practical usage