Commands: Search, History & Math
Find/replace, undo/redo and math insertion.
import {
findNext,
replaceAll,
toggleSearchState,
undo,
redo,
insertMathInline,
insertMathDisplay,
} from "@stesura/core/commands";Search
Search commands act on the search plugin’s state: a SearchQuery plus an optional range. Set the query with the setSearchState helper (not a command); the commands below then navigate and replace its matches. All of them return false without the plugin or with an invalid query.
import { setSearchState } from "@stesura/core/helpers";
import { SearchQuery } from "@stesura/core/plugins";
view.dispatch(
setSearchState(view.state.tr, new SearchQuery({ search: "foo", replace: "bar" }))
);setSearchState(tr, query, range?) also opens the search UI. SearchQuery options: search, replace?, caseSensitive?, wholeWord?, regexp?, literal?, skipDeletions? (skip track-changes deletions), filter?. See Search for the plugin and UI.
findNext / findPrev
Select the next / previous match relative to the selection, wrapping at the range boundary. findNextNoWrap / findPrevNoWrap fail once past the last (or before the first) match instead.
findNext: Command
findPrev: Command
findNextNoWrap: Command
findPrevNoWrap: CommandreplaceNext
Replace the selected match and select the next one. When the selection is not a match yet, it selects the next match instead of replacing. replaceNextNoWrap does not wrap at the end of the range.
replaceNext: Command
replaceNextNoWrap: CommandreplaceCurrent
Replace the selected match in place, leaving the selection on the result. Refused unless the selection is exactly a match.
replaceCurrent: CommandreplaceAll
Replace every match in the search range, in one transaction.
replaceAll: CommandSearch UI State
enableSearchState: Command // open the search UI with an empty query; refused when already open
disableSearchState: Command // close it, resetting the query; refused when already closed
toggleSearchState: Command // open or close, resetting the query either way
clearSearchState(): Command // clear query, range and decorations, and close the UIHistory
undo and redo are re-exports of prosemirror-history’s commands. canUndo / canRedo are state readers, not commands.
undo: Command
redo: Command
canUndo(state: EditorState): boolean
canRedo(state: EditorState): booleanMath
Both commands insert a math node, optionally prefilled, and put the caret at the end of its LaTeX source. Refused inside code blocks and where the schema lacks or disallows the node.
insertMathInline
Insert an inline math node.
insertMathInline(initialText?: string): Command
// insertMathInline("x^2 + y^2 = r^2")insertMathDisplay
Insert a display (block) math node. From a collapsed text cursor it splits the block at the caret and inserts the equation between the two halves.
insertMathDisplay(initialText?: string): CommandStyling an equation
An equation is styled as a whole, never per character. Select it (a node selection) and these commands apply: setFontColor, setFontSize, incrementFontSize, setHighlight, setBackgroundColor, and their clearing counterparts. clearFormatting removes it all.
Everything else, such as toggleBold, toggleItalic, setFontFamily and setCaps, is refused on a selected equation. KaTeX’s font shorthand resets family, weight, style and variant, so those marks could never render. A selection that spans an equation and ordinary text is not refused: the text gets the formatting and the formula ignores it. With the caret or a selection inside the LaTeX source, every formatting command is refused, because the source cannot carry marks.
The styling is stored on the node’s mathMarks attribute as mark JSON, not as real marks: a block container’s mark set is empty, so math_display could not carry them. Read it with mathNodeMarks(node) (marks) or mathNodeStyle(node) (resolved colour, size and background) from @stesura/core/helpers.
It round-trips through DOCX as a w:rPr on every OMML run. Word styles math per run; on import, runs that disagree collapse to the first with a warning.
Next Steps
- Search — Search plugin, query semantics, and UI
- Commands: Core — Basic document manipulation