Core API

HalkaEditor is the headless editing kernel. Formatting, styles, and selection ship in core; lists, tables, and other features require plugins.

Try this feature →

Constructor

typescript
new HalkaEditor(root: HTMLElement, options?: {
  plugins?: HalkaPlugin[];
  shortcuts?: boolean;  // default true
  inline?: boolean;     // inline mode (no block wrapper)
})

Content

NameTypeDescription
getHTML()stringCurrent inner HTML
setHTML(html)Replace all contentReplace all content
insertHTML(html)Insert at selectionInsert at selection
insertText(text)Insert plain textInsert plain text

Text formatting

Apply semantic inline formatting and block types. Clear formatting removes tags; it does not remove inline styles.

NameTypeDescription
toggleInlineFormat(format)bold, italic, underline, code, strikethrough, sub, supbold, italic, underline, code, strikethrough, sub, sup
toggleBlockFormat(format)h1, h2, h3, blockquote, pre, ph1, h2, h3, blockquote, pre, p
transforms.clearFormatting()Remove semantic tags (strong, em, u, code, etc.)Remove semantic tags (strong, em, u, code, etc.)
hasFormat(format)booleanCheck active format
  • Collapsed cursor inside a format + toggle turns format off for the next typed characters only (existing text is unchanged)
  • Collapsed cursor outside a format sets a pending format — typed characters stay wrapped until toggled off
  • No zero-width spaces or empty tags on toggle; DOM updates when you type
  • Built-in shortcuts: Mod+B (bold), Mod+I (italic), Mod+U (underline)
typescript
editor.toggleInlineFormat('bold');
editor.toggleBlockFormat('h2');
editor.transforms.clearFormatting();

Inline & block styles

Colors, font family, font size, and text alignment via CSS. clearStyles() removes style attributes only.

NameTypeDescription
setInlineStyle(prop, value?)Apply or remove inline CSS on selectionApply or remove inline CSS on selection
setBlockStyle(prop, value?)Apply or remove block CSS on current blockApply or remove block CSS on current block
clearStyles()Remove style attributes in scope; unwrap empty spansRemove style attributes in scope; unwrap empty spans
getStyle(property)Computed style at selectionComputed style at selection
  • If a span already wraps the selection, inline style is updated in place
  • Collapsed caret with font-size or font-family sets a pending style — typed text is wrapped in a styled span
  • Color and background-color at a collapsed caret apply to the current block (not pending)
  • Removing the only style on a span unwraps it entirely
typescript
editor.setInlineStyle('color', '#e11d48');
editor.setBlockStyle('text-align', 'center');
editor.clearStyles();

clearStyles vs clearFormatting

clearStyles() — collapsed: current block; range: selection root. Finds all [style] elements, removes attributes, unwraps empty spans, then normalizes.

clearFormatting() — removes semantic tags (strong, em, u, code, etc.) without touching style attributes.

Selection

Access via editor.selection. Selection is saved on blur and restored before operations automatically.

NameTypeDescription
getRange() / setSelection(range)Get or set DOM RangeGet or set DOM Range
selection.preserveSelection(cb)Mutate DOM, restore selectionMutate DOM, restore selection
selection.isAtBlockStart() / isAtBlockEnd()booleanBlock boundaries

Block delete

When the cursor is at a block boundary, Backspace and Delete merge or remove blocks instead of only deleting characters. This is built into the core input handler — no API to call.

  • Backspace at the start of a block merges with the previous block
  • Delete at the end of a block merges with the next block
  • Empty blocks are removed when appropriate
  • Disabled in inline mode (inline: true)

Inline mode

Pass inline: true for compact single-line-style editing (footnotes, dialog fields). The editor root has no block wrapper; block formatting and block delete are disabled.

typescript
const editor = new HalkaEditor(root, { inline: true });

See Svelte UI for the in-repo InlineEditor component built on this mode.

Query & transforms

NameTypeDescription
query.isActive(tag)booleanFormat active at cursor
query.getCurrentBlock()Current block elementCurrent block element
query.findClosest(tag)Nearest ancestor with tagNearest ancestor with tag
transforms.toggleMark(tag)Toggle inline wrapperToggle inline wrapper
transforms.wrap / unwrap(tag)Wrap or unwrap selectionWrap or unwrap selection

Built-in shortcuts

Enabled by default (shortcuts: true). Plugin shortcuts are documented on each plugin page.

ShortcutAction
Mod+BToggle bold
Mod+IToggle italic
Mod+UToggle underline

Transactions & events

typescript
editor.runTransaction(() => {
  // DOM mutations — auto-normalizes and emits change
});

editor.on('change', (html) => { /* save */ });
editor.on('formatChange', () => { /* toolbar */ });

MIT · Halka Editor