@mocanvas/editor · class
declare class TextManager extends EditorManagerText measurement, as the editor's own manager.
Measuring text needs a DOM — a font has to be resolved, applied and laid out
before anything can say how wide a word is — and the DOM lives in the
flagship package, not here. So this manager does not measure: it *routes*,
to whatever registerTextMeasureImplementation installed, reachable as
Editor.textMeasure. Everything it adds on top (span layout, wrapping,
truncation) is arithmetic over that one primitive, so a host with an unusual
measurer only implements the primitive and gets the rest.
Reach it as editor.text.
Methods · 5
measureElementTextNodeSpans(element: Element, opts?: {The same spans, but for text already in the DOM.
Where measureTextSpans predicts a layout, this one reads the layout
the browser actually performed — which is what an exporter needs, because
the browser's line breaking is the ground truth for what the user saw.
Uses Range rectangles over the element's text nodes, grouped into lines by
their vertical position.
Returns an empty list where there is no DOM at all (node, SSR).
measureHtml(html: string, opts: EditorTextMeasureHtmlOptions): EditorTextHtmlMeasurementMeasure one block of rich text, as a label is laid out.
measureHtmlBatch(items: readonly BatchMeasurementRequest[]): EditorTextHtmlMeasurement[]Measure many blocks in one layout pass.
The point is the *one* pass: measuring N labels one at a time forces N reflows, which is what makes a page of sticky notes slow to open.
measureText(text: string, opts: TLMeasureTextOpts): TLMeasuredTextSizeMeasure one run of text.
measureTextSpans(text: string, opts: TLMeasureTextSpanOpts): TLTextSpan[]Lay text out inside a box and report where each run of it lands.
Word-wrapping is greedy: words are added to a line until the next one would not fit, and a single word wider than the box gets a line of its own rather than being broken mid-word.
SEMANTICS-ASSUMED: a span is one line. The docs do not say whether a line is
one span or several (per word, per style run); one span per line is what
the positions are *for* — placing a caret, drawing a selection rectangle,
exporting <tspan>s — and a caller wanting words can split a line's text
without re-measuring.
Mentioned here
mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.