API, events and plugins
Editor methods
| Method | Description |
|---|---|
getContent() | clean HTML of the content (the same that goes into the <textarea>) |
setContent(html) | replaces the content and clears the undo history |
isEmpty() | true for an empty document |
focus() | moves the focus to the editing area |
execute(name, value) | runs a command, for example execute('heading', 'h2'), execute('bold'), execute('undo') |
setReadOnly(bool) | switches read-only mode; editor.isReadOnly reads it |
isCommandAvailable(name) | false for an unknown command or one blocked in read-only or source mode |
getStats() | { words, characters, charactersNoSpaces } (with the WordCount plugin) |
license | Promise with the licence state, as VEditor.checkLicense() |
on(event, callback), off(event, callback) | events |
destroy() | removes the editor and shows the <textarea> again |
Toolbar items run commands with the same name (bold, link, insertTable, sourceEditing…). The exceptions are insertDateTime, specialCharacters and emoji: they only open a picker and have no command. Commands go through one path: the editor restores the selection, runs the command, records one undo step and synchronizes the <textarea>.
const editor = await VEditor.create('#content');
editor.on('change', () => preview.innerHTML = editor.getContent());
document.querySelector('#make-h2').addEventListener('click', () => editor.execute('heading', 'h2'));
Commands with a value
| Command | Value |
|---|---|
heading | 'p', 'h1' … 'h6' |
alignment | 'left', 'center', 'right', 'justify' |
style | the name of an entry in styles |
fontColor, fontBackgroundColor, fontSize | a class, e.g. 'color-red', 'background-yellow', 'size-big'; '' removes it |
listStyle | 'lower-alpha', 'upper-alpha', 'lower-roman', 'upper-roman', 'decimal-leading-zero', 'circle', 'square' |
listStart | a number or { start, reversed } |
codeBlock | a language, e.g. 'php' |
insertTable | { rows, cols } (up to 50 × 50) |
insertImage | { url, alt, width, height, srcset, sizes } |
resizeImage | width in percent, e.g. 50; '' removes the percentage width |
mediaEmbed | an embed address (https://www.youtube-nocookie.com/embed/…, https://player.vimeo.com/video/…, a video or audio file) or { src, title, ratio } |
link | { href, text, title, newTab, download, rel }, rel as an array of nofollow, sponsored, ugc |
anchor | the anchor name; '' removes the id |
insertTemplate | HTML (cleaned by the content model) |
insertText | text |
replaceAllText | { query, text, matchCase } |
mediaEmbed does not accept YouTube page addresses (watch?v=); the dialog converts them. Table commands work on the cell with the caret and take no value.
Events
| Event | When |
|---|---|
ready | the editor is ready; a listener added after creating the editor is called right after it is added (asynchronously, in a microtask) |
change | after every change of the content |
refresh | after a change of the selection or the state of commands |
uploadError | an upload failed ({ file, error }) |
clipboardInput | before pasted or dropped content is inserted ({ dataTransfer, handled }) |
aiMetadata | texts for publishing were generated in the AI dialog |
destroy | the editor is being removed |
Static members
| Member | Description |
|---|---|
VEditor.create(element, options) | Promise with the editor; loads the language from options.language first |
VEditor.loadLanguage(code) | loads an interface language before new VEditor() |
VEditor.VERSION, VEditor.RELEASED | version and release date |
VEditor.checkLicense(key, host) | Promise with the licence state |
VEditor.createIframePicker({ url, title }) | a filePicker that opens a page in a modal window |
VEditor.createUploader({ url, fieldName, headers, credentials }) | an upload function that posts files |
DEFAULT_PLUGINS, DEFAULT_TOOLBAR | exports of index.js for your own lists |
Plugins
A plugin is an object { name, init(editor) }. In init it registers schema rules, commands, toolbar items and keystrokes:
import { VEditor, DEFAULT_PLUGINS, DEFAULT_TOOLBAR } from '/veditor/js/index.js';
const Highlight = {
name: 'highlight',
init(editor) {
editor.schema.register('mark', { type: 'inline' });
editor.addCommand('highlight', {
execute() { /* wrap the selection in <mark> */ },
isActive() { return false; },
});
editor.addKeystroke('Mod+Shift+H', 'highlight');
// A toolbar item: { element, refresh } (refresh runs after every change of the selection).
editor.addComponent('highlight', () => {
const element = document.createElement('button');
element.type = 'button';
element.className = 'veditor-btn';
element.textContent = 'H';
element.title = 'Highlight (Ctrl+Shift+H)';
element.addEventListener('click', () => editor.execute('highlight'));
return { element, refresh() { element.setAttribute('aria-pressed', String(editor.commands.get('highlight').isActive())); } };
});
},
};
await VEditor.create('#content', {
plugins: [...DEFAULT_PLUGINS, Highlight],
toolbar: [...DEFAULT_TOOLBAR, '|', 'highlight'],
});
| Method | Description |
|---|---|
editor.schema.register(tag, { type, attributes, urlAttributes, required }) | a new element in the content model |
editor.schema.allowClasses(tag, classes), allowAttribute(attr, rule), allowStyle(property, rule) | extend the whitelist |
editor.schema.addOutputFilter(fn) | change the serialized output (runs after sanitizing) |
editor.addCommand(name, { execute, isEnabled, isActive, value }) | a command |
editor.addComponent(name, factory) | a toolbar item returning { element, refresh } |
editor.addKeystroke(keystroke, commandOrCallback) | a shortcut, e.g. Mod+B |
editor.t(key) | a translated string |
Plugin order matters in a few cases: List before Anchor (table of contents), Image before MediaEmbed (both use figure) and PasteFromOffice after both.
Something unclear? Send feedback on the documentation.