API Reference

ContentAreaElement #

A custom element (<content-area>) with an API modeled after HTMLTextAreaElement. Wraps a contenteditable element and translates DOM mutations into Edit objects.

import {ContentAreaElement} from "@b9g/revise/contentarea.js";

customElements.define("content-area", ContentAreaElement);

Properties #

PropertyTypeDescription
valuestringThe text content of the element
selectionStartnumberThe start index of the selection
selectionEndnumberThe end index of the selection
selectionDirectionSelectionDirectionThe direction of the selection

Methods #

Events #

contentchange โ€” Dispatched when the content changes. The event's detail contains:

{
edit: Edit; // The edit describing the change
source: string | null; // The source tag, or null for user edits
}

Call event.preventDefault() to revert the DOM mutation. This lets you apply the edit to your own state and re-render instead of letting the browser's mutation stand.

Edit #

A compact, composable data structure for representing changes to strings.

import {Edit} from "@b9g/revise/edit.js";

Instance Methods #

Static Methods #

EditBuilder #

Fluent builder for constructing edits:

const edit = Edit.createBuilder("Hello World")
.retain(5)
.delete(6)
.insert(" Revise")
.build();

edit.apply("Hello World"); // "Hello Revise"

EditHistory #

Undo/redo stack that automatically composes consecutive simple edits into groups.

import {EditHistory} from "@b9g/revise/history.js";

Keyer #

Assigns stable integer keys to text positions and keeps them in sync as edits are applied.

import {Keyer} from "@b9g/revise/keyer.js";

EditableState #

A framework-agnostic state coordinator that ties together value, keyer, history, and selection.

import {EditableState} from "@b9g/revise/state.js";

const state = new EditableState({value: "initial text"});

Properties #

PropertyTypeDescription
valuestringThe current text
keyerKeyerThe keyer for stable line keys
historyEditHistoryThe undo/redo history
selectionSelectionRange | undefinedSelection computed from the last edit

Methods #