Saturs

API, notikumi un spraudņi

Redaktora metodes

MetodeApraksts
getContent()satura tīrs HTML (tas pats, kas nonāk elementā <textarea>)
setContent(html)aizstāj saturu un notīra atsaukšanas vēsturi
isEmpty()true tukšam dokumentam
focus()pārvieto fokusu uz rediģēšanas apgabalu
execute(name, value)izpilda komandu, piemēram, execute('heading', 'h2'), execute('bold'), execute('undo')
setReadOnly(bool)pārslēdz tikai lasīšanas režīmu; editor.isReadOnly to nolasa
isCommandAvailable(name)false nezināmai komandai vai komandai, kas bloķēta tikai lasīšanas vai pirmkoda režīmā
getStats(){ words, characters, charactersNoSpaces } (ar spraudni WordCount)
licensePromise ar licences stāvokli, tāpat kā VEditor.checkLicense()
on(event, callback), off(event, callback)notikumi
destroy()noņem redaktoru un atkal parāda elementu <textarea>

Rīkjoslas elementi izpilda komandas ar tādu pašu nosaukumu (bold, link, insertTable, sourceEditing…). Izņēmumi ir insertDateTime, specialCharacters un emoji: tie tikai atver atlasītāju, un tiem nav komandas. Komandas iet pa vienu ceļu: redaktors atjauno atlasi, izpilda komandu, ieraksta vienu atsaukšanas soli un sinhronizē elementu <textarea>.

const editor = await VEditor.create('#content');
editor.on('change', () => preview.innerHTML = editor.getContent());
document.querySelector('#make-h2').addEventListener('click', () => editor.execute('heading', 'h2'));

Komandas ar vērtību

KomandaVērtība
heading'p', 'h1' … 'h6'
alignment'left', 'center', 'right', 'justify'
styleieraksta name opcijā styles
fontColor, fontBackgroundColor, fontSizeklase, piem., 'color-red', 'background-yellow', 'size-big'; '' to noņem
listStyle'lower-alpha', 'upper-alpha', 'lower-roman', 'upper-roman', 'decimal-leading-zero', 'circle', 'square'
listStartskaitlis vai { start, reversed }
codeBlockvaloda, piem., 'php'
insertTable{ rows, cols } (līdz 50 × 50)
insertImage{ url, alt, width, height, srcset, sizes }
resizeImageplatums procentos, piem., 50; '' noņem procentuālo platumu
mediaEmbediegulšanas adrese (https://www.youtube-nocookie.com/embed/…, https://player.vimeo.com/video/…, video vai audio fails) vai { src, title, ratio }
link{ href, text, title, newTab, download, rel }, rel kā masīvs no nofollow, sponsored, ugc
anchorenkura nosaukums; '' noņem id
insertTemplateHTML (attīrīts ar satura modeli)
insertTextteksts
replaceAllText{ query, text, matchCase }

mediaEmbed nepieņem YouTube lapu adreses (watch?v=); dialogs tās pārveido. Tabulu komandas darbojas šūnā, kurā ir kursors, un nepieņem vērtību.

Notikumi

NotikumsKad
readyredaktors ir gatavs; klausītājs, kas pievienots pēc redaktora izveides, tiek izsaukts uzreiz pēc pievienošanas (asinhroni, mikrouzdevumā)
changepēc katras satura izmaiņas
refreshpēc atlases vai komandu stāvokļa izmaiņas
uploadErroraugšupielāde neizdevās ({ file, error })
clipboardInputpirms ielīmēta vai ievilkta satura ievietošanas ({ dataTransfer, handled })
aiMetadataAI dialogā tika ģenerēti publicēšanas teksti
destroyredaktors tiek noņemts

Statiskie locekļi

LoceklisApraksts
VEditor.create(element, options)Promise ar redaktoru; vispirms ielādē valodu no options.language
VEditor.loadLanguage(code)ielādē saskarnes valodu pirms new VEditor()
VEditor.VERSION, VEditor.RELEASEDversija un izlaišanas datums
VEditor.checkLicense(key, host)Promise ar licences stāvokli
VEditor.createIframePicker({ url, title })filePicker, kas atver lapu modālā logā
VEditor.createUploader({ url, fieldName, headers, credentials })funkcija upload, kas nosūta failus ar POST
DEFAULT_PLUGINS, DEFAULT_TOOLBARindex.js eksporti jūsu pašu sarakstiem

Spraudņi

Spraudnis ir objekts { name, init(editor) }. Funkcijā init tas reģistrē shēmas noteikumus, komandas, rīkjoslas elementus un īsinājumtaustiņus:

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() { /* ietin atlasi elementā <mark> */ },
            isActive() { return false; },
        });
        editor.addKeystroke('Mod+Shift+H', 'highlight');
        // Rīkjoslas elements: { element, refresh } (refresh tiek izpildīts pēc katras atlases izmaiņas).
        editor.addComponent('highlight', () => {
            const element = document.createElement('button');
            element.type = 'button';
            element.className = 'veditor-btn';
            element.textContent = 'H';
            element.title = 'Izcēlums (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'],
});
MetodeApraksts
editor.schema.register(tag, { type, attributes, urlAttributes, required })jauns elements satura modelī
editor.schema.allowClasses(tag, classes), allowAttribute(attr, rule), allowStyle(property, rule)paplašina atļauto sarakstu
editor.schema.addOutputFilter(fn)maina serializēto izvadi (tiek izpildīts pēc attīrīšanas)
editor.addCommand(name, { execute, isEnabled, isActive, value })komanda
editor.addComponent(name, factory)rīkjoslas elements, kas atgriež { element, refresh }
editor.addKeystroke(keystroke, commandOrCallback)īsinājumtaustiņš, piem., Mod+B
editor.t(key)tulkota virkne

Spraudņu secībai ir nozīme dažos gadījumos: List pirms Anchor (satura rādītājs), Image pirms MediaEmbed (abi izmanto figure) un PasteFromOffice pēc abiem.

Kaut kas nav skaidrs? Nosūtiet atsauksmi par dokumentāciju.