Basic and advanced
Most editors need only the basics. Task lists, code blocks, images, tables and mentions are advanced and live in @writeon/advanced.
Advanced is split in two so it costs nothing until it is used:
| Part | Package | Loaded |
|---|---|---|
| Node types (schema) | @writeon/advanced/schema | Up front, about 2 KB |
| Behaviour (shortcuts, table editing, uploads) | @writeon/advanced | On demand |
Because the schema is always there, every document opens and displays correctly, even one with tables, before the behaviour is loaded.
import { advancedSchema } from '@writeon/advanced/schema'
import { createEditor } from '@writeon/core'
import { basic } from '@writeon/core/presets'
// The node types are small and loaded up front, so every document
// opens and renders before the advanced behaviour arrives.
export const extensions = [...basic(), advancedSchema()]
const editor = createEditor({ extensions, label: 'Notes' })
// Later, when the user needs it. Shortcuts, input rules, table editing
// and the rest come in a separate chunk.
// The document, undo history and selection are kept.
export async function turnOnAdvanced(): Promise<void> {
await editor.enable(() => import('@writeon/advanced'))
}editor.enable() adds behaviour to the running editor and keeps the document, undo history and selection. Extensions already added are skipped, so calling it twice is harmless.
If you do not need lazy loading, pass everything up front:
import { advanced, advancedSchema } from '@writeon/advanced'
createEditor({ extensions: [...basic(), advancedSchema(), ...advanced()], label: 'Notes' })What advanced adds
| Feature | Commands | Notes |
|---|---|---|
| Task lists | toggleTaskList(), toggleTaskChecked() | Mod-Shift-9, [ ] input rule |
| Code blocks | toggleCodeBlock(), setCodeBlockLanguage() | Mod-Alt-c, ``` input rule |
| Images | insertImage(), updateImage(), uploadImages() | imageUpload() for drop, paste and progress |
| Tables | insertTable(), addRowAfter(), mergeCells()… | Header row by default; Tab moves between cells |
| Mentions | insertMention({ id, label }) | Use with a suggestion menu |
Fonts, colours and alignment
@writeon/advanced/formatting (about 2 KB, loaded up front like the schema) adds three extensions configured with what your design allows:
| Extension | Stores | Renders |
|---|---|---|
fontFamily({ fonts }) | { font: 'inter' } mark, so one paragraph can mix fonts | <span data-font style="font-family: …"> |
textColor({ colors }) | { color: 'brand' } mark | <span data-color style="color: …"> |
textAlign() | align on paragraphs and headings (null is left) | style="text-align: …"; Mod-Shift-l/e/r |
import {
fontFamily,
setFontFamily,
setTextAlign,
setTextColor,
textAlign,
textColor,
} from '@writeon/advanced/formatting'
import { createEditor } from '@writeon/core'
import { basic } from '@writeon/core/presets'
// The values your design allows. Documents store only the ids.
export const extensions = [
...basic(),
fontFamily({
fonts: [
{ id: 'inter', label: 'Inter', css: 'Inter, sans-serif' },
{ id: 'serif', label: 'Serif', css: 'Georgia, serif' },
],
}),
textColor({
colors: [
{ id: 'brand', label: 'Red', value: '#C00414' },
{ id: 'muted', label: 'Gray', value: '#475467' },
],
}),
textAlign(),
]
const editor = createEditor({ extensions, label: 'Post' })
// On the selection, or on what is typed next.
editor.run(setFontFamily('serif'))
editor.run(setTextColor('brand'))
// On the selected paragraphs and headings.
editor.run(setTextAlign('center'))
// null removes the font or the colour.
editor.run(setFontFamily(null))- Only ids are stored. An id that is not configured does not fit the schema, so
validate()drops it, andtoHTML()writes nothing buttext-align,font-familyandcolorwith strict values. - Pasted text keeps a font when its
font-familystarts with one of yours, a colour when it is exactly one of yours, and alignment. - Contrast:
textColor()warns while you develop when a colour is under 4.5:1 on the background (background, default white). - Markdown has no fonts or colours:
toMarkdown()keeps the text without them. - For menus,
activeFontFamily(state)andactiveTextColor(state)give the current id, andtextAlignmentAt(state)the alignment.
Image addresses
Image src is checked by the schema: http(s) or relative URLs only. data: URLs are refused unless you allow raster ones with advancedSchema({ image: { dataUrls: true } }). SVG is never allowed, because an SVG served from your domain can run scripts.
Alt text
An image's alt is null until someone describes it (the element gets data-alt-missing, so you can show a hint), or "" when it is marked decorative. The kit's image dialog requires one of the two.