Contents

API, events and plugins

Editor methods

MethodDescription
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)
licensePromise 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

CommandValue
heading'p', 'h1' … 'h6'
alignment'left', 'center', 'right', 'justify'
stylethe name of an entry in styles
fontColor, fontBackgroundColor, fontSizea 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'
listStarta number or { start, reversed }
codeBlocka language, e.g. 'php'
insertTable{ rows, cols } (up to 50 × 50)
insertImage{ url, alt, width, height, srcset, sizes }
resizeImagewidth in percent, e.g. 50; '' removes the percentage width
mediaEmbedan 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
anchorthe anchor name; '' removes the id
insertTemplateHTML (cleaned by the content model)
insertTexttext
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

EventWhen
readythe editor is ready; a listener added after creating the editor is called right after it is added (asynchronously, in a microtask)
changeafter every change of the content
refreshafter a change of the selection or the state of commands
uploadErroran upload failed ({ file, error })
clipboardInputbefore pasted or dropped content is inserted ({ dataTransfer, handled })
aiMetadatatexts for publishing were generated in the AI dialog
destroythe editor is being removed

Static members

MemberDescription
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.RELEASEDversion 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_TOOLBARexports 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'],
});
MethodDescription
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.