JavaScript API

Constructor

const editor = new ZynText(textarea, options?);
ParameterTypeDescription
textareaHTMLTextAreaElementRequired. The textarea to enhance.
optionsobjectOptional. See Configuration.

Instance Methods

editor.getHTML()

Returns the editor's content as portable HTML with inline styles. This is the same HTML that is synced to the textarea on form submission.

const html = editor.getHTML();
// '<p style="margin:0 0 0.5rem">Hello <strong>world</strong></p>'

editor.setHTML(html)

Replaces the editor's content with the provided HTML string.

editor.setHTML('<h2>New Content</h2><p>Lorem ipsum...</p>');

editor.t(key, params?)

Translate a key using the editor's language. Supports placeholder substitution.

editor.t('bold');                         // 'Bold' (or translated)
editor.t('tableSize', { rows: 3, cols: 4 }); // '3 x 4'

editor.destroy()

Destroys the editor: removes all DOM elements, event listeners, and plugins. Restores the original textarea with the final content.

editor.destroy();
// Textarea is visible again, contains the last editor content

editor.on(event, callback)

Listen for an editor event. See Events.

editor.off(event, callback)

Remove an event listener.

Static Methods

ZynText.setLang(code)

Set the global default language for all new editor instances.

ZynText.setLang('ar');
// All new instances use Arabic by default

ZynText.addTranslations(code, translations)

Register a new language or extend an existing one.

ZynText.addTranslations('ko', {
  'bold': '굵게',
  'italic': '기울임'
});

ZynText.availableLanguages()

Returns an array of all registered language codes.

ZynText.availableLanguages();
// ['en', 'fr', 'es', 'pt', 'ar', 'ja', 'zh', 'ru', 'he', 'de', 'it', 'tr', 'hi', 'bn']

Instance Properties

These are available on the editor instance for advanced use cases:

PropertyTypeDescription
editor.containerHTMLDivElementThe root .zy-editor container
editor.contentElHTMLDivElementThe contenteditable area
editor.toolbarElHTMLDivElementThe toolbar DOM element
editor.textareaHTMLTextAreaElementThe original (hidden) textarea
editor.optionsobjectMerged options object
editor.selectionSelectionManagerSave/restore selection state
editor.contentContentManagerGet/set HTML, insert nodes
editor.commandsCommandBusRegister and execute commands
editor.pluginsPluginManagerAccess registered plugins
editor.toolbarToolbarToolbar instance
editor.iconsIconProviderBootstrap or SVG icon provider

ContentManager Methods

editor.content.getHTML()       // Raw inner HTML
editor.content.getOutputHTML() // Portable HTML with inline styles
editor.content.setHTML(html)   // Replace content
editor.content.insertHTML(html)// Insert at cursor
editor.content.insertNode(node)// Insert DOM node at cursor
editor.content.focus()         // Focus the editing area

SelectionManager Methods

editor.selection.save()            // Save current selection
editor.selection.restore()         // Restore saved selection
editor.selection.getRange()        // Get current Range object
editor.selection.getSelectedText() // Get selected text
editor.selection.isWithinEditor()  // Check if cursor is in editor
editor.selection.closestBlock()    // Get nearest block element

CommandBus Methods

// Execute a command
editor.commands.exec('bold');
editor.commands.exec('formatBlock', 'H2');

// Register a custom command
editor.commands.register('myCommand', (editor, value) => {
  // Custom logic
});

// Query command state (for active button highlighting)
editor.commands.register('myCommand:state', (editor) => {
  return true; // or false
});

PluginManager Methods

// Get a plugin by name
const uploadPlugin = editor.plugins.get('upload');

// Check if a plugin is registered
if (editor.plugins.get('upload')) {
  // Upload is available
}