Extensions
textStyle
Colour, background, font family and font size, as one mark.
Tiptap ships these as four extensions layered on a `textStyle` mark; here they are one mark with four attributes, so a coloured, resized word is one `<span>` rather than a nest of them. Setting one attribute keeps the others: `setColor('red')` on text already in a different font leaves the font alone.
1 · Install
One package for every extension · there is no @matrajs/textStyle to install.
npm i @matrajs/core2 · Add it to the editor
Import it and put it in the extensions array. That array is the whole feature list:
what is not in it is not in your bundle and not in the document's schema.
import { createEditor, document, paragraph, text, textStyle } from '@matrajs/core'
const editor = createEditor({
extensions: [document, paragraph, text, textStyle],
element: document.querySelector('#editor'),
})3 · Use it
Run a command, and give focus back · a click on a button takes it away.
editor.commands.setColor('…')
editor.focus()Wired to a button, with the pressed state kept in step:
const button = document.querySelector('#setColor')
button.addEventListener('click', () => {
editor.commands.setColor('…')
editor.focus()
})
const read = () => button.setAttribute('aria-pressed', String(editor.isActive('textStyle')))
editor.on('selectionChange', read)
editor.on('change', read)
read()
Both events, not one. selectionChange covers the caret moving into
it;
change covers the command that changed it without moving anything. A
toolbar wired to only the first goes stale the moment someone uses the keyboard.
Commands
Every one returns true if it changed the document and false
if it could not · a command that does not apply where the caret is says so rather than
throwing.
| Command | Call |
|---|---|
setColor |
editor.commands.setColor(color: string)
|
unsetColor |
editor.commands.unsetColor()
|
setBackgroundColor |
editor.commands.setBackgroundColor(color: string)
|
unsetBackgroundColor |
editor.commands.unsetBackgroundColor()
|
setFontFamily |
editor.commands.setFontFamily(family: string)
|
unsetFontFamily |
editor.commands.unsetFontFamily()
|
setFontSize |
editor.commands.setFontSize(size: string)
|
unsetFontSize |
editor.commands.unsetFontSize()
|
unsetTextStyle |
editor.commands.unsetTextStyle()
|
In the document
| Attribute | Default |
|---|---|
color | none |
backgroundColor | none |
fontFamily | none |
fontSize | none |
-
Reads
spanwhen HTML is pasted or loaded. -
Writes
spaningetHTML()andrenderToHTML().