new MathField(element: Element, config: object)
To create a mathfield, you would typically use MathLive.makeMathField()
instead of invoking directly this constructor.
Note
- Method names that begin with
$
are public. - Method names that begin with an underbar
_
are private and meant to be used only by the implementation of the class. - Method names that end with an underbar
_
are selectors. They can be invoked by callingMathField.$perform()
. Note that the selector name does not include the underbar.
For example:
mf.$perform('selectAll');
element
:
Element
The DOM element that this mathfield is attached to.
Note that element.mathfield
is this object.
Properties
element
:
Element
The DOM element this mathfield is attached to.
config
:
[string]:any
A set of key/value pairs that can be used to customize the behavior of the mathfield
id
:
string
A unique ID identifying this mathfield
keystrokeCaptionVisible
:
boolean
True if the keystroke caption panel is visible
virtualKeyboardVisible
:
boolean
True if the virtual keyboard is visible
keystrokeBuffer
:
string
The last few keystrokes, to look out for inline shortcuts
keystrokeBufferStates
:
object[]
The saved state for each of the past keystrokes
Methods
$el(): Element
Return the DOM element associated with this mathfield.
Note that this.$el().mathfield = this
$insert(s: string, options?: [string]:any)
This method can be invoked as a selector with $perform("insert")
or called explicitly.
It will insert the specified block of text at the current insertion point, according to the insertion mode specified.
After the insertion, the selection will be set according to the selectionMode
.
s
:
string
The text to be inserted
options
:
[string]:any
= {}
options.selectionMode
:
string
Describes where the selection will be after the insertion:
'placeholder'
: the selection will be the first available placeholder in the item that has been inserted (default)'after'
: the selection will be an insertion point after the item that has been inserted,'before'
: the selection will be an insertion point before the item that has been inserted'item'
: the item that was inserted will be selected
options.mode
:
string
The mode ('text'
or 'math'
) to use. Default
is the current mode.
options.format
:
string
The format of the string s
:
'auto'
: the string is interpreted as a latex fragment or command) (default)'latex'
: the string is interpreted strictly as a latex fragment
options.focus
:
boolean
If true, the mathfield will be focused after the insertion
options.feedback
:
boolean
If true, provide audio and haptic feedback
options.mode
:
string
'text' or 'math'. If empty, the current mode is used (default)
options.resetStyle
:
boolean
If true, the style after the insertion is the same as the style before (if false, the style after the insertion is the style of the last inserted atom).
$keystroke(keys: string, evt: Event): boolean
keys
:
string
A string representation of a key combination.
For example 'Alt-KeyU'
.
See W3C UIEvents
evt
:
Event
$latex(text: string, options: [string]:any): string
If text
is not empty, sets the content of the mathfield to the
text interpreted as a LaTeX expression.
If text
is empty (or omitted), return the content of the mahtfield as a
LaTeX expression.
text
:
string
options
:
[string]:any
options.suppressChangeNotifications
:
boolean
If true, the
handlers for the contentWillChange and contentDidChange notifications will
not be invoked. Default false
.
$perform(command: string | string[])
command
:
string
|
string[]
A selector, or an array whose first element is a selector, and whose subsequent elements are arguments to the selector. Note that selectors do not include a final "_". They can be passed either in camelCase or kebab-case. So:
mf.$perform('selectAll');
mf.$perform('select-all');
both calls are valid and invoke the same selector.
$revertToOriginalContent()
Revert this math field to its original content. After this method has been
called, no other methods can be called on the MathField object. To turn the
element back into a MathField, call MathLive.makeMathField()
on the
element again to get a new math field object.
$selectedText(format?: string): string
Return a textual representation of the selection in the mathfield.
format
:
string
= 'latex'
One of
'latex'
'latex-expanded'
: all macros are recursively expanded to their definition'spoken'
'spoken-text'
'spoken-ssml'
'mathML'
'json'
$selectionAtEnd(): boolean
Return true if the selection extends to the end of the selection group.
$selectionAtStart(): boolean
Return true if the selection starts at the beginning of the selection group.
$selectionDepth(): number
Return the depth of the selection group. If the selection is at the root level, returns 0. If the selection is a portion of the numerator of a fraction which is at the root level, return 1. Note that in that case, the numerator would be the "selection group".
$selectionIsCollapsed(): boolean
Return true if the length of the selection is 0, that is, if it is a single insertion point.
$setConfig(config?: [string]:any)
Update the configuration options for this mathfield.
$text(format?: string): string
Return a textual representation of the mathfield.
format
:
string
= 'latex'
One of
'latex'
'latex-expanded'
: all macros are recursively expanded to their definition'spoken'
'spoken-text'
'spoken-ssml'
'mathML'
'json'
$typedText(text: string)
Simulate a user typing the keys indicated by text.
text
:
string
A sequence of one or more characters.
_getCaretPosition()private
Return the (x,y) client coordinates of the caret
_onAnnounce(target: object, command: string, oldMathlist?: Atom[], atomsToSpeak?: Atom[])private
Announce a change in selection or content via the aria-live region.
This is the default implementation for this function. It can be overridden
via config.onAnnounce
target
:
object
typically, a MathField
command
:
string
the command that invoked the change
oldMathlist
:
Atom[]
= []
the previous value of mathlist before the change
atomsToSpeak
:
Atom[]
= []
_onKeystroke(keystroke: string, evt: Event)private
keystroke
:
string
evt
:
Event
optional, an Event corresponding to the keystroke
complete_(options: object)private
When in command mode, insert the select command and return to math mode If escape is true, the command is discared.
options
:
object
options.discard
:
boolean
if true, the command is discarded and the mode switched back to math
options.acceptSuggestion
:
boolean
if true, accept the suggestion to complete the command. Otherwise, only use what has been entered so far.
enterCommandMode_()private
getTextBeforeAnchor_(): stringprivate
Return the characters before anchor that could potentially be turned into text mode. This excludes things like 'mop' (e.g. \sin)
render()private
Lay-out the math field and generate the DOM.
This is usually done automatically, but if the font-size, or other geometric attributes are modified, outside of MathLive, this function may need to be called explicitly.
smartMode_(keystroke: string, evt: Event): booleanprivate
Consider whether to switch mode give the content before the anchor and the character being input
keystroke
:
string
evt
:
Event
a Event corresponding to the keystroke
true if the mode should change