Extensions
mathInline
A formula in the run of text.
Only the source is stored. It is rendered into the element by whatever the application supplies — the editor carries no typesetting library, and a document never depends on one: the HTML keeps the source as the element's text, so an export reads as `E=mc^2` with no script on the page at all.
An atom, so the caret steps over it and backspace takes the whole thing. `$E=mc^2$ ` typed into a paragraph becomes one; `setMath` rewrites the one the caret is on, inline or display.
1 · Install
One package for every extension · there is no @matrajs/mathInline 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, mathInline } from '@matrajs/core'
const editor = createEditor({
extensions: [document, paragraph, text, mathInline({ … })],
element: document.querySelector('#editor'),
})3 · Use it
Run a command, and give focus back · a click on a button takes it away.
editor.commands.insertInlineMath()
editor.focus()Wired to a button, with the pressed state kept in step:
const button = document.querySelector('#insertInlineMath')
button.addEventListener('click', () => {
editor.commands.insertInlineMath()
editor.focus()
})
const read = () => button.setAttribute('aria-pressed', String(editor.isActive('mathInline')))
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 |
|---|---|
insertInlineMath |
editor.commands.insertInlineMath()
|
setMath |
editor.commands.setMath()
|
In the document
| Attribute | Default |
|---|---|
latex | none |
-
Reads
span[data-math]when HTML is pasted or loaded. -
Writes
spaningetHTML()andrenderToHTML().