Skip to content

Text Formatting ​

@erag/text-editor-react uses native browser editing commands for standard rich-text formatting and Range/Selection APIs for selection-aware editor interactions. Standard commands currently use document.execCommand for broad contenteditable behavior and native undo integration.


Formatting Controls & Toolbar Items ​

Include any of the following formatting keywords in your init.toolbar string:

ts
const editorConfig: EditorInit = {
    toolbar:
        'undo redo | blocks fontfamily fontsize | ' +
        'bold italic underline strikethrough superscript subscript casechange | ' +
        'forecolor backcolor | ' +
        'lineheight alignment | ' +
        'removeformat',
};

Detailed Control List ​

Toolbar ControlKeywordFunction
Blocks / HeadingsblocksDropdown menu to convert paragraph into <h1> through <h6>, <p>, <blockquote>, or <pre> code blocks.
Font FamilyfontfamilySelect a configured family such as Arial, Georgia, Courier New, Times New Roman, or Verdana.
Font SizefontsizeSelect font sizes (e.g. 8pt to 36pt).
Line HeightlineheightOpens one grouped dropdown using the configured lineHeightFormats.
BoldboldToggles <strong> / <b> bold text formatting (Ctrl+B / Cmd+B).
ItalicitalicToggles <em> / <i> italic text formatting (Ctrl+I / Cmd+I).
UnderlineunderlineToggles <u> underline formatting (Ctrl+U / Cmd+U).
StrikethroughstrikethroughToggles <del> / <s> strikethrough formatting.
SuperscriptsuperscriptToggles <sup> superscript text (e.g. x2).
SubscriptsubscriptToggles <sub> subscript text (e.g. H2O).
Change CasecasechangeDropdown menu offering UPPERCASE, lowercase, and Title Case conversions.
Text ColorforecolorColor picker dropdown for text foreground color, saved as <span style="color: …">.
Background ColorbackcolorColor picker dropdown for text highlight / background fill color.
Alignment GroupalignmentOne dropdown containing left, center, right, and justify with matching icons.
Align LeftalignleftAligns text paragraph to the left.
Align CenteraligncenterCenters text paragraph.
Align RightalignrightAligns text paragraph to the right.
JustifyalignjustifyJustifies text across full container width.
Remove FormattingremoveformatStrips inline formatting, styles, and tags from selection.

Font family and text color are written as styled spans (<span style="font-family: …"> and <span style="color: …">), so they survive the sanitizer and are kept when the HTML is saved and loaded again. Older content that still contains <font face="…"> or <font color="…"> is converted to the same styled spans when it is loaded.

The Format menubar also provides Inline code and Line height. Inline code wraps the selected text in a <code> element; with the caret inside existing inline code, it removes the <code> wrapper. It needs selected text and does nothing on a plain caret. Use Blocks → Preformatted for a <pre> code block. Line-height choices come from lineHeightFormats, are available from both the toolbar and menubar, and are applied as an inline line-height style.

ts
const editorConfig: EditorInit = {
    lineHeightFormats: [
        { label: 'Compact', value: '1.2' },
        { label: 'Normal', value: '1.5' },
        { label: 'Relaxed', value: '2' },
    ],
};

The grouped alignment control is the default toolbar presentation. The individual alignleft, aligncenter, alignright, and alignjustify keywords remain available when separate buttons are preferred.

Bold, italic, underline, and similar commands can establish the format used for subsequent typing when the caret is collapsed. Change case requires an active text selection. Clear formatting removes formatting from the selection and resets the active block to a paragraph.

The default families are Arial, Book Antiqua, Comic Sans MS, Courier New, Georgia, Helvetica, Impact, Symbol, Tahoma, Terminal, Times New Roman, Trebuchet MS, and Verdana. Default sizes are 8pt, 10pt, 12pt, 14pt, 18pt, 24pt, and 36pt.

Released under the MIT License. Copyright © Er Amit Gupta