Methods and Events
Complete reference guide for all Beefree SDK methods, callbacks, and events.
Overview
Beefree SDK provides a comprehensive set of methods, callback functions, and events that allow you to control the editor programmatically and respond to user interactions. This guide serves as your single source of truth for all available SDK interactions.
TypeScript Definitions: For complete type safety and detailed parameter information, reference the official TypeScript definitions: Beefree SDK Types
How Methods and Events Work
Beefree SDK is built on an event-driven architecture, a software design pattern where components communicate through events rather than direct method calls. In this architecture, methods are functions you call to trigger actions in the SDK (like saving or loading templates), while events are notifications the SDK sends back to your application when something happens (like content changes or errors). This decoupled approach allows your application to respond to SDK activities asynchronously, making integrations more flexible and scalable. Learn more about event-driven architecture on MDN Web Docs.
When you initialize Beefree SDK, you get an instance that exposes various methods for programmatic control. Additionally, you can configure callback functions in your beeConfig to respond to user actions and editor events.
const bee = new BeefreeSDK(token);
const beeInstance = bee.start(beeConfig, template);
// Use methods to control the editor
beeInstance.save();
beeInstance.preview();
// Configure callbacks to respond to events
const beeConfig = {
container: 'editor-container',
onSave: function(jsonFile, htmlFile) {
// Handle save event
},
onChange: function(jsonFile, response) {
// Handle content changes
}
};Instance Methods
These methods are available on the Beefree SDK instance and allow you to control the editor programmatically.
Core Methods
beeInstance.start(templateToLoad, userInfo, templateInfo, options)
Description: Initializes and starts the builder with optional template data
Parameters:
templateToLoad(optional): JSON string with template structureuserInfo(optional): User information object for collaborative featurestemplateInfo(optional): Template metadataoptions(optional): Additional configuration options
Returns: Promise that resolves when editor is ready
Usage:
// Basic start
await beeInstance.start();
// Start with template
await beeInstance.start(templateJson);
// Start with full configuration (NPM package)
await beeInstance.start(templateJson, userInfo, templateInfo, options);beeInstance.load(template)
Description: Loads a JSON template into the editor
Parameters:
template: JSON string with template structure
Returns: Promise that resolves when template is loaded
Usage:
await beeInstance.load(templateJson);beeInstance.reload(template)
Description: Reloads a template without showing loading dialog (seamless reload)
Parameters:
template: JSON string with template structure
Use Cases: Custom undo/redo, real-time content injection
Usage:
await beeInstance.reload(templateJson);Control Methods
beeInstance.save()
Description: Programmatically triggers the save action
Triggers:
onSavecallback with JSON and HTML filesUsage:
beeInstance.save();beeInstance.saveAsTemplate()
Description: Programmatically triggers save as template action
Triggers:
onSaveAsTemplatecallback with JSON fileUsage:
beeInstance.saveAsTemplate();beeInstance.send()
Description: Programmatically triggers send action (Email Builder only)
Triggers:
onSendcallback with HTML fileUsage:
beeInstance.send();Preview and View Methods
beeInstance.togglePreview()
Description: Toggles preview mode on/off
Triggers:
onTogglePreviewcallbackUsage:
beeInstance.togglePreview();beeInstance.toggleStructure()
Description: Toggles visibility of structure outlines in the editor
Usage:
beeInstance.toggleStructure();beeInstance.toggleMergeTagsPreview()
Description: Toggles visibility of merge tag sample content
Usage:
beeInstance.toggleMergeTagsPreview();Advanced Methods
beeInstance.execCommand(command)
Description: Executes specific editor commands for highlighting, scrolling, focusing, or selecting elements
Parameters:
command: Object defining the action and target
Actions:
highlight,scroll,focus,selectUsage:
beeInstance.execCommand({
action: 'highlight',
target: { type: 'row', id: 'row-id' }
});
beeInstance.execCommand({
action: 'scroll',
target: { type: 'block', id: 'block-id' }
});beeInstance.loadWorkspace(type)
Description: Loads a specific workspace type
Parameters:
type: Workspace type ('default', 'mixed', 'amp_only', 'html_only')
Triggers:
onLoadWorkspacecallbackUsage:
beeInstance.loadWorkspace('mixed');beeInstance.loadConfig(config)
Description: Updates editor configuration dynamically
Parameters:
config: Configuration object with updated settings
Usage:
beeInstance.loadConfig({
debug: { all: true },
language: 'es-ES'
});File Manager Methods
File Manager applications have a simplified method set focused on file selection and management workflows.
Callback Functions
Callback functions are configured in your beeConfig and are triggered by user actions or editor events.
Save and Template Callbacks
onSave
Trigger: User clicks save button or
beeInstance.save()is calledParameters:
(jsonFile, htmlFile, ampHtml, templateVersion, language)jsonFile: Template structure as JSON stringhtmlFile: Rendered HTML outputampHtml: AMP HTML version (if applicable)templateVersion: Template version numberlanguage: Template language code
Usage:
onSave: function(jsonFile, htmlFile, ampHtml, templateVersion, language) {
console.log('Template saved:', {
json: jsonFile,
html: htmlFile,
amp: ampHtml,
version: templateVersion,
lang: language
});
// Save to your backend
saveTemplate({
json: jsonFile,
html: htmlFile,
version: templateVersion
});
}onSaveAsTemplate
Trigger: User clicks "Save as Template" or
beeInstance.saveAsTemplate()is calledParameters:
(jsonFile)jsonFile: Template structure as JSON string
Usage:
onSaveAsTemplate: function(jsonFile) {
console.log('Saving as template:', jsonFile);
// Save template to library
saveToTemplateLibrary(jsonFile);
}onAutoSave
Trigger: Automatic save based on
autosaveconfigurationParameters:
(jsonFile)jsonFile: Template structure as JSON string
Usage:
onAutoSave: function(jsonFile) {
console.log('Auto-saving template');
// Perform background save
autoSaveTemplate(jsonFile);
}onSend
Trigger: User clicks send button or
beeInstance.send()is called (Email Builder only)Parameters:
(htmlFile)htmlFile: Rendered HTML ready for sending
Usage:
onSend: function(htmlFile) {
console.log('Sending email');
// Send email via your service
sendEmail({
html: htmlFile,
subject: getSubject(),
recipients: getRecipients()
});
}Content Change Callbacks
onChange
Trigger: Template content is modified (requires
trackChanges: true)Parameters:
(jsonFile, response)jsonFile: Updated template JSONresponse: Change details object with patches and metadata
Usage:
onChange: function(jsonFile, response) {
console.log('Content changed:', response);
// Track changes for analytics
trackUserAction({
action: response.description,
value: response.value,
timestamp: Date.now()
});
// Sync with other users in collaborative editing
broadcastChange(response);
}onRemoteChange
Trigger: Changes made by other users in collaborative editing sessions
Parameters:
(jsonFile, response)jsonFile: Updated template JSON from remote userresponse: Change details from remote user
Usage:
onRemoteChange: function(jsonFile, response) {
console.log('Remote user made changes:', response);
// Update UI to show remote changes
highlightRemoteChanges(response);
// Notify current user of remote activity
showCollaborationNotification(response.user);
}Loading and Workspace Callbacks
onLoad
Trigger: Template is loaded into the editor
Parameters:
(jsonFile)jsonFile: Loaded template JSON
Usage:
onLoad: function(jsonFile) {
console.log('Template loaded successfully');
// Initialize custom features
initializeCustomModules();
// Track template usage
trackTemplateLoad(jsonFile);
}onLoadWorkspace
Trigger: Workspace is successfully loaded
Parameters:
(workspace)workspace: Workspace configuration object
Usage:
onLoadWorkspace: function(workspace) {
console.log('Workspace loaded:', workspace.type);
// Update UI based on workspace
updateWorkspaceUI(workspace);
}Error and Warning Callbacks
onError
Trigger: Errors occur in the editor
Parameters:
(errorMessage)errorMessage: Error description string
Usage:
onError: function(errorMessage) {
console.error('Beefree SDK Error:', errorMessage);
// Show user-friendly error message
showErrorNotification('Something went wrong. Please try again.');
// Log error for debugging
logError({
source: 'beefree-sdk',
message: errorMessage,
timestamp: Date.now()
});
}onWarning
Trigger: Warnings are generated by the editor
Parameters:
(alertMessage)alertMessage: Warning description string
Usage:
onWarning: function(alertMessage) {
console.warn('Beefree SDK Warning:', alertMessage);
// Show warning to user if needed
if (shouldShowWarning(alertMessage)) {
showWarningNotification(alertMessage);
}
}Preview and View Callbacks
onPreview
Trigger: Preview button is clicked
Parameters:
(status)status: Boolean indicating preview state
Usage:
onPreview: function(status) {
console.log('Preview mode:', status ? 'opened' : 'closed');
// Update UI state
updatePreviewButton(status);
}onTogglePreview
Trigger: Preview is toggled on/off
Parameters:
(status)status: Boolean indicating preview state
Usage:
onTogglePreview: function(status) {
console.log('Preview toggled:', status);
// Adjust layout for preview mode
adjustLayoutForPreview(status);
}onViewChange
Trigger: User navigates between different SDK views
Parameters:
(view)view: String indicating current view
Possible Values:
'editor': Main editor view'preview': Preview mode'fileManager': File Manager opened'imageEditor': Image Editor opened
Usage:
onViewChange: function(view) {
console.log('View changed to:', view);
// Track user navigation
trackViewChange(view);
// Update application state
switch(view) {
case 'fileManager':
showFileManagerHelp();
break;
case 'preview':
hideEditingTools();
break;
case 'editor':
showEditingTools();
break;
}
}File Management Callbacks
onFilePickerInsert
Trigger: Insert button clicked in File Manager applications
Parameters:
(data)data: Object containing file information
Usage:
onFilePickerInsert: function(data) {
console.log('File selected:', data);
// Process selected file
processSelectedFile(data);
// Close file picker
closeFilePicker();
}onFilePickerCancel
Trigger: Cancel/X button clicked in File Manager applications
Parameters: None
Usage:
onFilePickerCancel: function() {
console.log('File picker cancelled');
// Handle cancellation
handleFilePickerCancel();
}Collaboration Callbacks
onComment
Trigger: Comments or comment threads change
Parameters:
(commentData)commentData: Comment information object
Usage:
onComment: function(commentData) {
console.log('Comment activity:', commentData);
// Update comment UI
updateCommentInterface(commentData);
// Notify relevant users
notifyCommentActivity(commentData);
}Event Configuration Examples
Basic Event Handling
var beeConfig = {
container: 'beefree-sdk-container',
// Essential callbacks
onSave: function(jsonFile, htmlFile) {
saveToDatabase(jsonFile, htmlFile);
},
onError: function(errorMessage) {
handleError(errorMessage);
},
onLoad: function(jsonFile) {
console.log('Editor ready');
}
};Advanced Event Handling
var beeConfig = {
container: 'beefree-sdk-container',
trackChanges: true,
commenting: true,
// Content tracking
onChange: function(jsonFile, response) {
trackChanges(response);
autoSave(jsonFile);
},
onRemoteChange: function(jsonFile, response) {
syncRemoteChanges(response);
showCollaboratorActivity(response);
},
// Collaboration
onComment: function(commentData) {
updateCommentThread(commentData);
notifyTeamMembers(commentData);
},
// View tracking
onViewChange: function(view) {
analytics.track('view_change', { view });
updateUIForView(view);
},
// Complete save handling
onSave: function(jsonFile, htmlFile, ampHtml, version, language) {
Promise.all([
saveTemplate(jsonFile),
saveHTML(htmlFile),
saveAMP(ampHtml),
updateVersion(version)
]).then(() => {
showSuccessMessage();
}).catch(handleSaveError);
}
};File Manager Configuration
var beeConfig = {
container: 'file-manager-container',
// File picker callbacks
onFilePickerInsert: function(data) {
insertFileIntoContent(data);
closeFileManager();
},
onFilePickerCancel: function() {
closeFileManager();
},
onViewChange: function(view) {
if (view === 'fileManager') {
showFileManagerInstructions();
}
}
};Method Chaining and Async Operations
Many SDK methods return promises, allowing for proper async handling:
// Sequential operations
async function loadAndPreview(templateJson) {
try {
await beeInstance.load(templateJson);
await beeInstance.preview();
console.log('Template loaded and previewed');
} catch (error) {
console.error('Operation failed:', error);
}
}
// Conditional operations
async function saveAndSend(shouldSend = false) {
await beeInstance.save();
if (shouldSend) {
await beeInstance.send();
}
}Best Practices
Error Handling
Always implement
onErrorcallback for production applicationsProvide user-friendly error messages
Log errors for debugging and monitoring
Performance
Use
onChangejudiciously withtrackChanges: trueImplement debouncing for frequent operations
Consider using
onAutoSavefor background saves
User Experience
Provide feedback for all user actions
Use
onViewChangeto guide users through different modesImplement proper loading states
Collaboration
Use
onRemoteChangeto show real-time collaborationImplement conflict resolution for simultaneous edits
Provide clear indicators of other users' activities
Development
Reference TypeScript definitions for complete type safety
Test all callback implementations thoroughly
Use method chaining for complex workflows
TypeScript Support
For complete type definitions and IntelliSense support, reference the official types:
TypeScript Definitions: Beefree SDK Types
import { BeefreeSDK, BeeConfig, SaveCallback } from '@beefree.io/sdk';
const beeConfig: BeeConfig = {
container: 'editor-container',
onSave: ((jsonFile: string, htmlFile: string, ampHtml: string | null, version: number, language: string | null) => {
// Fully typed callback
}) as SaveCallback
};This comprehensive guide covers all available methods, callbacks, and events in Beefree SDK. Use it as your reference for implementing complete editor control and event handling in your applications.
TypeScript Definitions: Beefree SDK Types
Last updated
Was this helpful?

