@tldraw/editor 5.3.0 → 5.4.0-next.ef99ab47e5ce

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -275,13 +275,17 @@ class TextManager extends EditorManager {
275
275
  });
276
276
  if (opts.overflow === "truncate-ellipsis" && didTruncate) {
277
277
  elm.textContent = "\u2026";
278
- const ellipsisWidth = Math.ceil(this.measureElementTextNodeSpans(elm).spans[0].box.w);
278
+ const ellipsisSpan = this.measureElementTextNodeSpans(elm).spans[0];
279
+ const ellipsisWidth = ellipsisSpan ? Math.ceil(ellipsisSpan.box.w) : 0;
279
280
  elm.style.setProperty("width", `${elementWidth - ellipsisWidth}px`);
280
281
  elm.textContent = normalizedText;
281
282
  const truncatedSpans = this.measureElementTextNodeSpans(elm, {
282
283
  shouldTruncateToFirstLine: true
283
284
  }).spans;
284
285
  const lastSpan = truncatedSpans[truncatedSpans.length - 1];
286
+ if (!lastSpan) {
287
+ return truncatedSpans;
288
+ }
285
289
  truncatedSpans.push({
286
290
  text: "\u2026",
287
291
  box: {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/lib/editor/managers/TextManager/TextManager.ts"],
4
- "sourcesContent": ["import { BoxModel, TLDefaultHorizontalAlignStyle } from '@tldraw/tlschema'\nimport { objectMapKeys } from '@tldraw/utils'\nimport type { Editor } from '../../Editor'\nimport { EditorManager } from '../EditorManager'\n\n/**\n * The whole-pixel line-height for a given font size and tldraw's unitless line-height\n * multiplier. tldraw's theme stores line-height as a multiplier (e.g. 1.35); resolving it\n * to a whole pixel keeps line spacing identical across rendering engines, which otherwise\n * disagree on fractional line boxes (WebKit snaps them to whole pixels, Blink keeps the\n * fraction) and let multi-line text drift apart. Apply it everywhere line-height is used \u2014\n * measurement, on-canvas render, and export \u2014 so geometry and rendering agree.\n * See https://github.com/tldraw/tldraw/issues/8970.\n *\n * @public\n */\nexport function resolveLineHeightPx(fontSize: number, lineHeight: number): number {\n\treturn Math.round(fontSize * lineHeight)\n}\n\nconst fixNewLines = /\\r?\\n|\\r/g\n\nfunction normalizeTextForDom(text: string) {\n\treturn text\n\t\t.replace(fixNewLines, '\\n')\n\t\t.split('\\n')\n\t\t.map((x) => x || ' ')\n\t\t.join('\\n')\n}\n\nconst textAlignmentsForLtr = {\n\tstart: 'left',\n\t'start-legacy': 'left',\n\tmiddle: 'center',\n\t'middle-legacy': 'center',\n\tend: 'right',\n\t'end-legacy': 'right',\n}\n\ninterface PoolItem {\n\tel: HTMLDivElement\n\thtml: string\n\tappliedStyleKeys: string[]\n}\n\n/** @public */\nexport interface BatchMeasurementRequest {\n\thtml: string\n\topts: TLMeasureTextOpts\n}\n\n/** @public */\nexport type TLMeasuredTextSize = BoxModel & {\n\tscrollWidth: number\n}\n\n/** @public */\nexport interface TLMeasureTextOpts {\n\tfontStyle: string\n\tfontWeight: string\n\tfontFamily: string\n\tfontSize: number\n\t/** This must be a number, e.g. 1.35, not a pixel value. */\n\tlineHeight: number\n\t/**\n\t * When maxWidth is a number, the text will be wrapped to that maxWidth. When maxWidth\n\t * is null, the text will be measured without wrapping, but explicit line breaks and\n\t * space are preserved.\n\t */\n\tmaxWidth: null | number\n\tminWidth?: null | number\n\t// todo: make this a number so that it is consistent with other TLMeasureTextSpanOpts\n\tpadding: string\n\totherStyles?: Record<string, string>\n\tdisableOverflowWrapBreaking?: boolean\n\tmeasureScrollWidth?: boolean\n}\n\n/** @public */\nexport interface TLMeasureTextSpanOpts {\n\toverflow: 'wrap' | 'truncate-ellipsis' | 'truncate-clip'\n\twidth: number\n\theight: number\n\tpadding: number\n\tfontSize: number\n\tfontWeight: string\n\tfontFamily: string\n\tfontStyle: string\n\tlineHeight: number\n\ttextAlign: TLDefaultHorizontalAlignStyle\n\totherStyles?: Record<string, string>\n\tmeasureScrollWidth?: boolean\n}\n\nconst spaceCharacterRegex = /\\s/\n\n// Iterate by grapheme cluster (e.g. emoji sequences, flags, accented letters)\n// rather than by code point, so a single glyph is measured as one unit.\nconst graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })\n\nconst initialDefaultStyles = Object.freeze({\n\t'overflow-wrap': 'break-word',\n\t'word-break': 'auto',\n\twidth: null,\n\theight: null,\n\t'max-width': null,\n\t'min-width': null,\n})\n\n/** @public */\nexport class TextManager extends EditorManager {\n\tprivate elm: HTMLDivElement\n\tprivate poolElms: PoolItem[] = []\n\n\tconstructor(editor: Editor) {\n\t\tsuper(editor)\n\t\tthis.elm = this.createMeasurementEl()\n\t\tthis.editor.getContainer().appendChild(this.elm)\n\t\tthis.register(() => {\n\t\t\tthis.elm.remove()\n\t\t\tfor (const { el } of this.poolElms) {\n\t\t\t\tel.remove()\n\t\t\t}\n\t\t\tthis.poolElms.length = 0\n\t\t})\n\t}\n\n\tprivate createMeasurementEl(): HTMLDivElement {\n\t\tconst elm = this.editor.getContainerDocument().createElement('div')\n\t\telm.classList.add('tl-text')\n\t\telm.classList.add('tl-text-measure')\n\t\telm.setAttribute('dir', 'auto')\n\t\telm.tabIndex = -1\n\t\tfor (const key of objectMapKeys(initialDefaultStyles)) {\n\t\t\telm.style.setProperty(key, initialDefaultStyles[key])\n\t\t}\n\n\t\treturn elm\n\t}\n\n\tprivate resetElementStyles(el: HTMLElement, appliedStyleKeys: string[]) {\n\t\tfor (const key of appliedStyleKeys) {\n\t\t\tif (key in initialDefaultStyles) {\n\t\t\t\tel.style.setProperty(key, initialDefaultStyles[key as keyof typeof initialDefaultStyles])\n\t\t\t} else {\n\t\t\t\tel.style.removeProperty(key)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate setElementStyles(el: HTMLElement, styles: Record<string, string | undefined | null>) {\n\t\ttype StyleValue = string | null\n\t\ttype RestoreEntry = [prop: string, value: StyleValue]\n\n\t\tconst restore: RestoreEntry[] = []\n\n\t\tfor (const [key, nextValue] of Object.entries(styles)) {\n\t\t\tconst oldValue = el.style.getPropertyValue(key)\n\n\t\t\tif (typeof nextValue === 'string') {\n\t\t\t\tif (oldValue === nextValue) continue\n\t\t\t\trestore.push([key, oldValue || null])\n\t\t\t\tel.style.setProperty(key, nextValue)\n\t\t\t} else {\n\t\t\t\tif (!oldValue) continue\n\t\t\t\trestore.push([key, oldValue])\n\t\t\t\tel.style.removeProperty(key)\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\tfor (const [key, value] of restore) {\n\t\t\t\tif (value === null || value === '') el.style.removeProperty(key)\n\t\t\t\telse el.style.setProperty(key, value)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getMeasureStyles(opts: TLMeasureTextOpts): Record<string, string | undefined> {\n\t\treturn {\n\t\t\t'font-family': opts.fontFamily,\n\t\t\t'font-style': opts.fontStyle,\n\t\t\t'font-weight': opts.fontWeight,\n\t\t\t'font-size': opts.fontSize + 'px',\n\t\t\t'line-height': `${resolveLineHeightPx(opts.fontSize, opts.lineHeight)}px`,\n\t\t\t// Unitless multiplier consumed by the .tl-rich-text h1\u2013h6 rule (see editor.css),\n\t\t\t// so heading measurement matches on-canvas rendering.\n\t\t\t'--tl-rich-text-heading-line-height': `${opts.lineHeight}`,\n\t\t\tpadding: opts.padding,\n\t\t\t'max-width': opts.maxWidth ? opts.maxWidth + 'px' : undefined,\n\t\t\t'min-width': opts.minWidth ? opts.minWidth + 'px' : undefined,\n\t\t\t'overflow-wrap': opts.disableOverflowWrapBreaking ? 'normal' : 'break-word',\n\t\t\t...opts.otherStyles,\n\t\t}\n\t}\n\n\tprivate ensurePoolSize(size: number) {\n\t\tif (this.poolElms.length >= size) return\n\n\t\tconst fragment = this.editor.getContainerDocument().createDocumentFragment()\n\t\twhile (this.poolElms.length < size) {\n\t\t\tconst el = this.createMeasurementEl()\n\t\t\tthis.poolElms.push({ el, html: '', appliedStyleKeys: [] })\n\t\t\tfragment.appendChild(el)\n\t\t}\n\t\tthis.editor.getContainer().appendChild(fragment)\n\t}\n\n\tprivate getPoolItem(index: number): PoolItem {\n\t\tthis.ensurePoolSize(index + 1)\n\t\treturn this.poolElms[index]\n\t}\n\n\tmeasureHtmlBatch(requests: BatchMeasurementRequest[]): TLMeasuredTextSize[] {\n\t\tif (requests.length === 0) return []\n\n\t\twhile (this.poolElms.length > requests.length) {\n\t\t\tconst { el } = this.poolElms.pop()!\n\t\t\tel.remove()\n\t\t}\n\n\t\tfor (let i = 0; i < requests.length; i++) {\n\t\t\tconst { html, opts } = requests[i]\n\t\t\tconst poolItem = this.getPoolItem(i)\n\n\t\t\tconst { el } = poolItem\n\t\t\tthis.resetElementStyles(el, poolItem.appliedStyleKeys)\n\t\t\tconst styles = this.getMeasureStyles(opts)\n\t\t\tthis.setElementStyles(el, styles)\n\t\t\tpoolItem.appliedStyleKeys = Object.keys(styles)\n\t\t\t// Skip innerHTML parsing if the content hasn't changed\n\t\t\tif (poolItem.html !== html) {\n\t\t\t\tel.innerHTML = html\n\t\t\t\tpoolItem.html = html\n\t\t\t}\n\t\t}\n\n\t\tconst results: TLMeasuredTextSize[] = []\n\t\tfor (let i = 0; i < requests.length; i++) {\n\t\t\tconst el = this.getPoolItem(i).el\n\t\t\tconst scrollWidth = requests[i].opts.measureScrollWidth ? el.scrollWidth : 0\n\t\t\tconst rect = el.getBoundingClientRect()\n\t\t\tresults.push({\n\t\t\t\tx: 0,\n\t\t\t\ty: 0,\n\t\t\t\tw: rect.width,\n\t\t\t\th: rect.height,\n\t\t\t\tscrollWidth,\n\t\t\t})\n\t\t}\n\n\t\treturn results\n\t}\n\n\tmeasureText(textToMeasure: string, opts: TLMeasureTextOpts): TLMeasuredTextSize {\n\t\tconst div = this.editor.getContainerDocument().createElement('div')\n\t\tdiv.textContent = normalizeTextForDom(textToMeasure)\n\t\treturn this.measureHtml(div.innerHTML, opts)\n\t}\n\n\tmeasureHtml(html: string, opts: TLMeasureTextOpts): TLMeasuredTextSize {\n\t\tconst { elm } = this\n\n\t\tconst restoreStyles = this.setElementStyles(elm, this.getMeasureStyles(opts))\n\n\t\ttry {\n\t\t\telm.innerHTML = html\n\n\t\t\tconst scrollWidth = opts.measureScrollWidth ? elm.scrollWidth : 0\n\t\t\tconst rect = elm.getBoundingClientRect()\n\n\t\t\treturn {\n\t\t\t\tx: 0,\n\t\t\t\ty: 0,\n\t\t\t\tw: rect.width,\n\t\t\t\th: rect.height,\n\t\t\t\tscrollWidth,\n\t\t\t}\n\t\t} finally {\n\t\t\trestoreStyles()\n\t\t}\n\t}\n\n\t/**\n\t * Given an html element, measure the position of each span of unbroken\n\t * word/white-space characters within any text nodes it contains.\n\t */\n\tmeasureElementTextNodeSpans(\n\t\telement: HTMLElement,\n\t\t{ shouldTruncateToFirstLine = false }: { shouldTruncateToFirstLine?: boolean } = {}\n\t): { spans: { box: BoxModel; text: string }[]; didTruncate: boolean } {\n\t\tconst spans = []\n\n\t\t// Measurements of individual spans are relative to the containing element\n\t\tconst elmBounds = element.getBoundingClientRect()\n\t\tconst offsetX = -elmBounds.left\n\t\tconst offsetY = -elmBounds.top\n\n\t\t// we measure by creating a range that spans each character in the elements text node\n\t\tconst range = new Range()\n\t\tconst textNode = element.childNodes[0]\n\t\tlet idx = 0\n\n\t\tlet currentSpan = null\n\t\tlet prevCharWasSpaceCharacter = null\n\t\tlet prevCharTop = 0\n\t\tlet prevCharLeftForRTLTest = 0\n\t\tlet didTruncate = false\n\t\tfor (const childNode of element.childNodes) {\n\t\t\tif (childNode.nodeType !== Node.TEXT_NODE) continue\n\n\t\t\tfor (const { segment } of graphemeSegmenter.segment(childNode.textContent ?? '')) {\n\t\t\t\t// place the range around the grapheme we're interested in\n\t\t\t\trange.setStart(textNode, idx)\n\t\t\t\trange.setEnd(textNode, idx + segment.length)\n\t\t\t\t// measure the range. some browsers return multiple rects for the\n\t\t\t\t// first char in a new line - one for the line break, and one for\n\t\t\t\t// the character itself. we're only interested in the character.\n\t\t\t\tconst rects = range.getClientRects()\n\t\t\t\tconst rect = rects[rects.length - 1]\n\n\t\t\t\t// some graphemes produce no layout rectangle (e.g. zero-width\n\t\t\t\t// spaces or newlines). skip them rather than crashing, but keep\n\t\t\t\t// advancing the index so later graphemes stay aligned.\n\t\t\t\t// See https://github.com/tldraw/tldraw/issues/9112.\n\t\t\t\tif (!rect) {\n\t\t\t\t\tidx += segment.length\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\t// calculate the position of the character relative to the element\n\t\t\t\tconst top = rect.top + offsetY\n\t\t\t\tconst left = rect.left + offsetX\n\t\t\t\tconst right = rect.right + offsetX\n\t\t\t\tconst isRTL = left < prevCharLeftForRTLTest\n\n\t\t\t\tconst isSpaceCharacter = spaceCharacterRegex.test(segment)\n\t\t\t\tif (\n\t\t\t\t\t// If we're at a word boundary...\n\t\t\t\t\tisSpaceCharacter !== prevCharWasSpaceCharacter ||\n\t\t\t\t\t// ...or we're on a different line...\n\t\t\t\t\ttop !== prevCharTop ||\n\t\t\t\t\t// ...or we're at the start of the text and haven't created a span yet...\n\t\t\t\t\t!currentSpan\n\t\t\t\t) {\n\t\t\t\t\t// ...then we're at a span boundary!\n\n\t\t\t\t\tif (currentSpan) {\n\t\t\t\t\t\t// if we're truncating to a single line & we just finished the first line, stop there\n\t\t\t\t\t\tif (shouldTruncateToFirstLine && top !== prevCharTop) {\n\t\t\t\t\t\t\tdidTruncate = true\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// otherwise add the span to the list ready to start a new one\n\t\t\t\t\t\tspans.push(currentSpan)\n\t\t\t\t\t}\n\n\t\t\t\t\t// start a new span\n\t\t\t\t\tcurrentSpan = {\n\t\t\t\t\t\tbox: { x: left, y: top, w: rect.width, h: rect.height },\n\t\t\t\t\t\ttext: segment,\n\t\t\t\t\t}\n\t\t\t\t\tprevCharLeftForRTLTest = left\n\t\t\t\t} else {\n\t\t\t\t\t// Looks like we're in RTL mode, so we need to adjust the left position.\n\t\t\t\t\tif (isRTL) {\n\t\t\t\t\t\tcurrentSpan.box.x = left\n\t\t\t\t\t}\n\n\t\t\t\t\t// otherwise we just need to extend the current span with the next character\n\t\t\t\t\tcurrentSpan.box.w = isRTL ? currentSpan.box.w + rect.width : right - currentSpan.box.x\n\t\t\t\t\tcurrentSpan.text += segment\n\t\t\t\t}\n\n\t\t\t\tif (segment === '\\n') {\n\t\t\t\t\tprevCharLeftForRTLTest = 0\n\t\t\t\t}\n\n\t\t\t\tprevCharWasSpaceCharacter = isSpaceCharacter\n\t\t\t\tprevCharTop = top\n\t\t\t\tidx += segment.length\n\t\t\t}\n\t\t}\n\n\t\t// Add the last span\n\t\tif (currentSpan) {\n\t\t\tspans.push(currentSpan)\n\t\t}\n\n\t\treturn { spans, didTruncate }\n\t}\n\n\t/**\n\t * Measure text into individual spans. Spans are created by rendering the\n\t * text, then dividing it up according to line breaks and word boundaries.\n\t *\n\t * It works by having the browser render the text, then measuring the\n\t * position of each character. You can use this to replicate the text-layout\n\t * algorithm of the current browser in e.g. an SVG export.\n\t */\n\tmeasureTextSpans(\n\t\ttextToMeasure: string,\n\t\topts: TLMeasureTextSpanOpts\n\t): { text: string; box: BoxModel }[] {\n\t\tif (textToMeasure === '') return []\n\n\t\tconst { elm } = this\n\n\t\tconst shouldTruncateToFirstLine =\n\t\t\topts.overflow === 'truncate-ellipsis' || opts.overflow === 'truncate-clip'\n\t\tconst elementWidth = Math.ceil(opts.width - opts.padding * 2)\n\t\tconst newStyles = {\n\t\t\t'font-family': opts.fontFamily,\n\t\t\t'font-style': opts.fontStyle,\n\t\t\t'font-weight': opts.fontWeight,\n\t\t\t'font-size': opts.fontSize + 'px',\n\t\t\t'line-height': `${resolveLineHeightPx(opts.fontSize, opts.lineHeight)}px`,\n\t\t\twidth: `${elementWidth}px`,\n\t\t\theight: 'min-content',\n\t\t\t'text-align': textAlignmentsForLtr[opts.textAlign],\n\t\t\t'overflow-wrap': shouldTruncateToFirstLine ? 'anywhere' : 'break-word',\n\t\t\t'word-break': shouldTruncateToFirstLine ? 'break-all' : 'normal',\n\t\t\t...opts.otherStyles,\n\t\t}\n\t\tconst restoreStyles = this.setElementStyles(elm, newStyles)\n\n\t\ttry {\n\t\t\tconst normalizedText = normalizeTextForDom(textToMeasure)\n\n\t\t\t// Render the text into the measurement element:\n\t\t\telm.textContent = normalizedText\n\n\t\t\t// actually measure the text:\n\t\t\tconst { spans, didTruncate } = this.measureElementTextNodeSpans(elm, {\n\t\t\t\tshouldTruncateToFirstLine,\n\t\t\t})\n\n\t\t\tif (opts.overflow === 'truncate-ellipsis' && didTruncate) {\n\t\t\t\t// we need to measure the ellipsis to know how much space it takes up\n\t\t\t\telm.textContent = '\u2026'\n\t\t\t\tconst ellipsisWidth = Math.ceil(this.measureElementTextNodeSpans(elm).spans[0].box.w)\n\n\t\t\t\t// then, we need to subtract that space from the width we have and measure again:\n\t\t\t\telm.style.setProperty('width', `${elementWidth - ellipsisWidth}px`)\n\t\t\t\telm.textContent = normalizedText\n\t\t\t\tconst truncatedSpans = this.measureElementTextNodeSpans(elm, {\n\t\t\t\t\tshouldTruncateToFirstLine: true,\n\t\t\t\t}).spans\n\n\t\t\t\t// Finally, we add in our ellipsis at the end of the last span. We\n\t\t\t\t// have to do this after measuring, not before, because adding the\n\t\t\t\t// ellipsis changes how whitespace might be getting collapsed by the\n\t\t\t\t// browser.\n\t\t\t\tconst lastSpan = truncatedSpans[truncatedSpans.length - 1]!\n\t\t\t\ttruncatedSpans.push({\n\t\t\t\t\ttext: '\u2026',\n\t\t\t\t\tbox: {\n\t\t\t\t\t\tx: Math.min(lastSpan.box.x + lastSpan.box.w, opts.width - opts.padding - ellipsisWidth),\n\t\t\t\t\t\ty: lastSpan.box.y,\n\t\t\t\t\t\tw: ellipsisWidth,\n\t\t\t\t\t\th: lastSpan.box.h,\n\t\t\t\t\t},\n\t\t\t\t})\n\n\t\t\t\treturn truncatedSpans\n\t\t\t}\n\n\t\t\treturn spans\n\t\t} finally {\n\t\t\trestoreStyles()\n\t\t}\n\t}\n}\n"],
5
- "mappings": "AACA,SAAS,qBAAqB;AAE9B,SAAS,qBAAqB;AAav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C5D,2BAA2B;AAAA,QAC5B,CAAC,EAAE;AAMH,cAAM,WAAW,eAAe,eAAe,SAAS,CAAC;AACzD,uBAAe,KAAK;AAAA,UACnB,MAAM;AAAA,UACN,KAAK;AAAA,YACJ,GAAG,KAAK,IAAI,SAAS,IAAI,IAAI,SAAS,IAAI,GAAG,KAAK,QAAQ,KAAK,UAAU,aAAa;AAAA,YACtF,GAAG,SAAS,IAAI;AAAA,YAChB,GAAG;AAAA,YACH,GAAG,SAAS,IAAI;AAAA,UACjB;AAAA,QACD,CAAC;AAED,eAAO;AAAA,MACR;AAEA,aAAO;AAAA,IACR,UAAE;AACD,oBAAc;AAAA,IACf;AAAA,EACD;AACD;",
4
+ "sourcesContent": ["import { BoxModel, TLDefaultHorizontalAlignStyle } from '@tldraw/tlschema'\nimport { objectMapKeys } from '@tldraw/utils'\nimport type { Editor } from '../../Editor'\nimport { EditorManager } from '../EditorManager'\n\n/**\n * The whole-pixel line-height for a given font size and tldraw's unitless line-height\n * multiplier. tldraw's theme stores line-height as a multiplier (e.g. 1.35); resolving it\n * to a whole pixel keeps line spacing identical across rendering engines, which otherwise\n * disagree on fractional line boxes (WebKit snaps them to whole pixels, Blink keeps the\n * fraction) and let multi-line text drift apart. Apply it everywhere line-height is used \u2014\n * measurement, on-canvas render, and export \u2014 so geometry and rendering agree.\n * See https://github.com/tldraw/tldraw/issues/8970.\n *\n * @public\n */\nexport function resolveLineHeightPx(fontSize: number, lineHeight: number): number {\n\treturn Math.round(fontSize * lineHeight)\n}\n\nconst fixNewLines = /\\r?\\n|\\r/g\n\nfunction normalizeTextForDom(text: string) {\n\treturn text\n\t\t.replace(fixNewLines, '\\n')\n\t\t.split('\\n')\n\t\t.map((x) => x || ' ')\n\t\t.join('\\n')\n}\n\nconst textAlignmentsForLtr = {\n\tstart: 'left',\n\t'start-legacy': 'left',\n\tmiddle: 'center',\n\t'middle-legacy': 'center',\n\tend: 'right',\n\t'end-legacy': 'right',\n}\n\ninterface PoolItem {\n\tel: HTMLDivElement\n\thtml: string\n\tappliedStyleKeys: string[]\n}\n\n/** @public */\nexport interface BatchMeasurementRequest {\n\thtml: string\n\topts: TLMeasureTextOpts\n}\n\n/** @public */\nexport type TLMeasuredTextSize = BoxModel & {\n\tscrollWidth: number\n}\n\n/** @public */\nexport interface TLMeasureTextOpts {\n\tfontStyle: string\n\tfontWeight: string\n\tfontFamily: string\n\tfontSize: number\n\t/** This must be a number, e.g. 1.35, not a pixel value. */\n\tlineHeight: number\n\t/**\n\t * When maxWidth is a number, the text will be wrapped to that maxWidth. When maxWidth\n\t * is null, the text will be measured without wrapping, but explicit line breaks and\n\t * space are preserved.\n\t */\n\tmaxWidth: null | number\n\tminWidth?: null | number\n\t// todo: make this a number so that it is consistent with other TLMeasureTextSpanOpts\n\tpadding: string\n\totherStyles?: Record<string, string>\n\tdisableOverflowWrapBreaking?: boolean\n\tmeasureScrollWidth?: boolean\n}\n\n/** @public */\nexport interface TLMeasureTextSpanOpts {\n\toverflow: 'wrap' | 'truncate-ellipsis' | 'truncate-clip'\n\twidth: number\n\theight: number\n\tpadding: number\n\tfontSize: number\n\tfontWeight: string\n\tfontFamily: string\n\tfontStyle: string\n\tlineHeight: number\n\ttextAlign: TLDefaultHorizontalAlignStyle\n\totherStyles?: Record<string, string>\n\tmeasureScrollWidth?: boolean\n}\n\nconst spaceCharacterRegex = /\\s/\n\n// Iterate by grapheme cluster (e.g. emoji sequences, flags, accented letters)\n// rather than by code point, so a single glyph is measured as one unit.\nconst graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })\n\nconst initialDefaultStyles = Object.freeze({\n\t'overflow-wrap': 'break-word',\n\t'word-break': 'auto',\n\twidth: null,\n\theight: null,\n\t'max-width': null,\n\t'min-width': null,\n})\n\n/** @public */\nexport class TextManager extends EditorManager {\n\tprivate elm: HTMLDivElement\n\tprivate poolElms: PoolItem[] = []\n\n\tconstructor(editor: Editor) {\n\t\tsuper(editor)\n\t\tthis.elm = this.createMeasurementEl()\n\t\tthis.editor.getContainer().appendChild(this.elm)\n\t\tthis.register(() => {\n\t\t\tthis.elm.remove()\n\t\t\tfor (const { el } of this.poolElms) {\n\t\t\t\tel.remove()\n\t\t\t}\n\t\t\tthis.poolElms.length = 0\n\t\t})\n\t}\n\n\tprivate createMeasurementEl(): HTMLDivElement {\n\t\tconst elm = this.editor.getContainerDocument().createElement('div')\n\t\telm.classList.add('tl-text')\n\t\telm.classList.add('tl-text-measure')\n\t\telm.setAttribute('dir', 'auto')\n\t\telm.tabIndex = -1\n\t\tfor (const key of objectMapKeys(initialDefaultStyles)) {\n\t\t\telm.style.setProperty(key, initialDefaultStyles[key])\n\t\t}\n\n\t\treturn elm\n\t}\n\n\tprivate resetElementStyles(el: HTMLElement, appliedStyleKeys: string[]) {\n\t\tfor (const key of appliedStyleKeys) {\n\t\t\tif (key in initialDefaultStyles) {\n\t\t\t\tel.style.setProperty(key, initialDefaultStyles[key as keyof typeof initialDefaultStyles])\n\t\t\t} else {\n\t\t\t\tel.style.removeProperty(key)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate setElementStyles(el: HTMLElement, styles: Record<string, string | undefined | null>) {\n\t\ttype StyleValue = string | null\n\t\ttype RestoreEntry = [prop: string, value: StyleValue]\n\n\t\tconst restore: RestoreEntry[] = []\n\n\t\tfor (const [key, nextValue] of Object.entries(styles)) {\n\t\t\tconst oldValue = el.style.getPropertyValue(key)\n\n\t\t\tif (typeof nextValue === 'string') {\n\t\t\t\tif (oldValue === nextValue) continue\n\t\t\t\trestore.push([key, oldValue || null])\n\t\t\t\tel.style.setProperty(key, nextValue)\n\t\t\t} else {\n\t\t\t\tif (!oldValue) continue\n\t\t\t\trestore.push([key, oldValue])\n\t\t\t\tel.style.removeProperty(key)\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\tfor (const [key, value] of restore) {\n\t\t\t\tif (value === null || value === '') el.style.removeProperty(key)\n\t\t\t\telse el.style.setProperty(key, value)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getMeasureStyles(opts: TLMeasureTextOpts): Record<string, string | undefined> {\n\t\treturn {\n\t\t\t'font-family': opts.fontFamily,\n\t\t\t'font-style': opts.fontStyle,\n\t\t\t'font-weight': opts.fontWeight,\n\t\t\t'font-size': opts.fontSize + 'px',\n\t\t\t'line-height': `${resolveLineHeightPx(opts.fontSize, opts.lineHeight)}px`,\n\t\t\t// Unitless multiplier consumed by the .tl-rich-text h1\u2013h6 rule (see editor.css),\n\t\t\t// so heading measurement matches on-canvas rendering.\n\t\t\t'--tl-rich-text-heading-line-height': `${opts.lineHeight}`,\n\t\t\tpadding: opts.padding,\n\t\t\t'max-width': opts.maxWidth ? opts.maxWidth + 'px' : undefined,\n\t\t\t'min-width': opts.minWidth ? opts.minWidth + 'px' : undefined,\n\t\t\t'overflow-wrap': opts.disableOverflowWrapBreaking ? 'normal' : 'break-word',\n\t\t\t...opts.otherStyles,\n\t\t}\n\t}\n\n\tprivate ensurePoolSize(size: number) {\n\t\tif (this.poolElms.length >= size) return\n\n\t\tconst fragment = this.editor.getContainerDocument().createDocumentFragment()\n\t\twhile (this.poolElms.length < size) {\n\t\t\tconst el = this.createMeasurementEl()\n\t\t\tthis.poolElms.push({ el, html: '', appliedStyleKeys: [] })\n\t\t\tfragment.appendChild(el)\n\t\t}\n\t\tthis.editor.getContainer().appendChild(fragment)\n\t}\n\n\tprivate getPoolItem(index: number): PoolItem {\n\t\tthis.ensurePoolSize(index + 1)\n\t\treturn this.poolElms[index]\n\t}\n\n\tmeasureHtmlBatch(requests: BatchMeasurementRequest[]): TLMeasuredTextSize[] {\n\t\tif (requests.length === 0) return []\n\n\t\twhile (this.poolElms.length > requests.length) {\n\t\t\tconst { el } = this.poolElms.pop()!\n\t\t\tel.remove()\n\t\t}\n\n\t\tfor (let i = 0; i < requests.length; i++) {\n\t\t\tconst { html, opts } = requests[i]\n\t\t\tconst poolItem = this.getPoolItem(i)\n\n\t\t\tconst { el } = poolItem\n\t\t\tthis.resetElementStyles(el, poolItem.appliedStyleKeys)\n\t\t\tconst styles = this.getMeasureStyles(opts)\n\t\t\tthis.setElementStyles(el, styles)\n\t\t\tpoolItem.appliedStyleKeys = Object.keys(styles)\n\t\t\t// Skip innerHTML parsing if the content hasn't changed\n\t\t\tif (poolItem.html !== html) {\n\t\t\t\tel.innerHTML = html\n\t\t\t\tpoolItem.html = html\n\t\t\t}\n\t\t}\n\n\t\tconst results: TLMeasuredTextSize[] = []\n\t\tfor (let i = 0; i < requests.length; i++) {\n\t\t\tconst el = this.getPoolItem(i).el\n\t\t\tconst scrollWidth = requests[i].opts.measureScrollWidth ? el.scrollWidth : 0\n\t\t\tconst rect = el.getBoundingClientRect()\n\t\t\tresults.push({\n\t\t\t\tx: 0,\n\t\t\t\ty: 0,\n\t\t\t\tw: rect.width,\n\t\t\t\th: rect.height,\n\t\t\t\tscrollWidth,\n\t\t\t})\n\t\t}\n\n\t\treturn results\n\t}\n\n\tmeasureText(textToMeasure: string, opts: TLMeasureTextOpts): TLMeasuredTextSize {\n\t\tconst div = this.editor.getContainerDocument().createElement('div')\n\t\tdiv.textContent = normalizeTextForDom(textToMeasure)\n\t\treturn this.measureHtml(div.innerHTML, opts)\n\t}\n\n\tmeasureHtml(html: string, opts: TLMeasureTextOpts): TLMeasuredTextSize {\n\t\tconst { elm } = this\n\n\t\tconst restoreStyles = this.setElementStyles(elm, this.getMeasureStyles(opts))\n\n\t\ttry {\n\t\t\telm.innerHTML = html\n\n\t\t\tconst scrollWidth = opts.measureScrollWidth ? elm.scrollWidth : 0\n\t\t\tconst rect = elm.getBoundingClientRect()\n\n\t\t\treturn {\n\t\t\t\tx: 0,\n\t\t\t\ty: 0,\n\t\t\t\tw: rect.width,\n\t\t\t\th: rect.height,\n\t\t\t\tscrollWidth,\n\t\t\t}\n\t\t} finally {\n\t\t\trestoreStyles()\n\t\t}\n\t}\n\n\t/**\n\t * Given an html element, measure the position of each span of unbroken\n\t * word/white-space characters within any text nodes it contains.\n\t */\n\tmeasureElementTextNodeSpans(\n\t\telement: HTMLElement,\n\t\t{ shouldTruncateToFirstLine = false }: { shouldTruncateToFirstLine?: boolean } = {}\n\t): { spans: { box: BoxModel; text: string }[]; didTruncate: boolean } {\n\t\tconst spans = []\n\n\t\t// Measurements of individual spans are relative to the containing element\n\t\tconst elmBounds = element.getBoundingClientRect()\n\t\tconst offsetX = -elmBounds.left\n\t\tconst offsetY = -elmBounds.top\n\n\t\t// we measure by creating a range that spans each character in the elements text node\n\t\tconst range = new Range()\n\t\tconst textNode = element.childNodes[0]\n\t\tlet idx = 0\n\n\t\tlet currentSpan = null\n\t\tlet prevCharWasSpaceCharacter = null\n\t\tlet prevCharTop = 0\n\t\tlet prevCharLeftForRTLTest = 0\n\t\tlet didTruncate = false\n\t\tfor (const childNode of element.childNodes) {\n\t\t\tif (childNode.nodeType !== Node.TEXT_NODE) continue\n\n\t\t\tfor (const { segment } of graphemeSegmenter.segment(childNode.textContent ?? '')) {\n\t\t\t\t// place the range around the grapheme we're interested in\n\t\t\t\trange.setStart(textNode, idx)\n\t\t\t\trange.setEnd(textNode, idx + segment.length)\n\t\t\t\t// measure the range. some browsers return multiple rects for the\n\t\t\t\t// first char in a new line - one for the line break, and one for\n\t\t\t\t// the character itself. we're only interested in the character.\n\t\t\t\tconst rects = range.getClientRects()\n\t\t\t\tconst rect = rects[rects.length - 1]\n\n\t\t\t\t// some graphemes produce no layout rectangle (e.g. zero-width\n\t\t\t\t// spaces or newlines). skip them rather than crashing, but keep\n\t\t\t\t// advancing the index so later graphemes stay aligned.\n\t\t\t\t// See https://github.com/tldraw/tldraw/issues/9112.\n\t\t\t\tif (!rect) {\n\t\t\t\t\tidx += segment.length\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\t// calculate the position of the character relative to the element\n\t\t\t\tconst top = rect.top + offsetY\n\t\t\t\tconst left = rect.left + offsetX\n\t\t\t\tconst right = rect.right + offsetX\n\t\t\t\tconst isRTL = left < prevCharLeftForRTLTest\n\n\t\t\t\tconst isSpaceCharacter = spaceCharacterRegex.test(segment)\n\t\t\t\tif (\n\t\t\t\t\t// If we're at a word boundary...\n\t\t\t\t\tisSpaceCharacter !== prevCharWasSpaceCharacter ||\n\t\t\t\t\t// ...or we're on a different line...\n\t\t\t\t\ttop !== prevCharTop ||\n\t\t\t\t\t// ...or we're at the start of the text and haven't created a span yet...\n\t\t\t\t\t!currentSpan\n\t\t\t\t) {\n\t\t\t\t\t// ...then we're at a span boundary!\n\n\t\t\t\t\tif (currentSpan) {\n\t\t\t\t\t\t// if we're truncating to a single line & we just finished the first line, stop there\n\t\t\t\t\t\tif (shouldTruncateToFirstLine && top !== prevCharTop) {\n\t\t\t\t\t\t\tdidTruncate = true\n\t\t\t\t\t\t\tbreak\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// otherwise add the span to the list ready to start a new one\n\t\t\t\t\t\tspans.push(currentSpan)\n\t\t\t\t\t}\n\n\t\t\t\t\t// start a new span\n\t\t\t\t\tcurrentSpan = {\n\t\t\t\t\t\tbox: { x: left, y: top, w: rect.width, h: rect.height },\n\t\t\t\t\t\ttext: segment,\n\t\t\t\t\t}\n\t\t\t\t\tprevCharLeftForRTLTest = left\n\t\t\t\t} else {\n\t\t\t\t\t// Looks like we're in RTL mode, so we need to adjust the left position.\n\t\t\t\t\tif (isRTL) {\n\t\t\t\t\t\tcurrentSpan.box.x = left\n\t\t\t\t\t}\n\n\t\t\t\t\t// otherwise we just need to extend the current span with the next character\n\t\t\t\t\tcurrentSpan.box.w = isRTL ? currentSpan.box.w + rect.width : right - currentSpan.box.x\n\t\t\t\t\tcurrentSpan.text += segment\n\t\t\t\t}\n\n\t\t\t\tif (segment === '\\n') {\n\t\t\t\t\tprevCharLeftForRTLTest = 0\n\t\t\t\t}\n\n\t\t\t\tprevCharWasSpaceCharacter = isSpaceCharacter\n\t\t\t\tprevCharTop = top\n\t\t\t\tidx += segment.length\n\t\t\t}\n\t\t}\n\n\t\t// Add the last span\n\t\tif (currentSpan) {\n\t\t\tspans.push(currentSpan)\n\t\t}\n\n\t\treturn { spans, didTruncate }\n\t}\n\n\t/**\n\t * Measure text into individual spans. Spans are created by rendering the\n\t * text, then dividing it up according to line breaks and word boundaries.\n\t *\n\t * It works by having the browser render the text, then measuring the\n\t * position of each character. You can use this to replicate the text-layout\n\t * algorithm of the current browser in e.g. an SVG export.\n\t */\n\tmeasureTextSpans(\n\t\ttextToMeasure: string,\n\t\topts: TLMeasureTextSpanOpts\n\t): { text: string; box: BoxModel }[] {\n\t\tif (textToMeasure === '') return []\n\n\t\tconst { elm } = this\n\n\t\tconst shouldTruncateToFirstLine =\n\t\t\topts.overflow === 'truncate-ellipsis' || opts.overflow === 'truncate-clip'\n\t\tconst elementWidth = Math.ceil(opts.width - opts.padding * 2)\n\t\tconst newStyles = {\n\t\t\t'font-family': opts.fontFamily,\n\t\t\t'font-style': opts.fontStyle,\n\t\t\t'font-weight': opts.fontWeight,\n\t\t\t'font-size': opts.fontSize + 'px',\n\t\t\t'line-height': `${resolveLineHeightPx(opts.fontSize, opts.lineHeight)}px`,\n\t\t\twidth: `${elementWidth}px`,\n\t\t\theight: 'min-content',\n\t\t\t'text-align': textAlignmentsForLtr[opts.textAlign],\n\t\t\t'overflow-wrap': shouldTruncateToFirstLine ? 'anywhere' : 'break-word',\n\t\t\t'word-break': shouldTruncateToFirstLine ? 'break-all' : 'normal',\n\t\t\t...opts.otherStyles,\n\t\t}\n\t\tconst restoreStyles = this.setElementStyles(elm, newStyles)\n\n\t\ttry {\n\t\t\tconst normalizedText = normalizeTextForDom(textToMeasure)\n\n\t\t\t// Render the text into the measurement element:\n\t\t\telm.textContent = normalizedText\n\n\t\t\t// actually measure the text:\n\t\t\tconst { spans, didTruncate } = this.measureElementTextNodeSpans(elm, {\n\t\t\t\tshouldTruncateToFirstLine,\n\t\t\t})\n\n\t\t\tif (opts.overflow === 'truncate-ellipsis' && didTruncate) {\n\t\t\t\t// we need to measure the ellipsis to know how much space it takes up\n\t\t\t\telm.textContent = '\u2026'\n\t\t\t\tconst ellipsisSpan = this.measureElementTextNodeSpans(elm).spans[0]\n\t\t\t\tconst ellipsisWidth = ellipsisSpan ? Math.ceil(ellipsisSpan.box.w) : 0\n\n\t\t\t\t// then, we need to subtract that space from the width we have and measure again:\n\t\t\t\telm.style.setProperty('width', `${elementWidth - ellipsisWidth}px`)\n\t\t\t\telm.textContent = normalizedText\n\t\t\t\tconst truncatedSpans = this.measureElementTextNodeSpans(elm, {\n\t\t\t\t\tshouldTruncateToFirstLine: true,\n\t\t\t\t}).spans\n\n\t\t\t\t// Finally, we add in our ellipsis at the end of the last span. We\n\t\t\t\t// have to do this after measuring, not before, because adding the\n\t\t\t\t// ellipsis changes how whitespace might be getting collapsed by the\n\t\t\t\t// browser.\n\t\t\t\tconst lastSpan = truncatedSpans[truncatedSpans.length - 1]\n\t\t\t\t// If nothing measurable remained, return the (possibly empty)\n\t\t\t\t// spans as-is rather than dereferencing a missing last span.\n\t\t\t\tif (!lastSpan) {\n\t\t\t\t\treturn truncatedSpans\n\t\t\t\t}\n\t\t\t\ttruncatedSpans.push({\n\t\t\t\t\ttext: '\u2026',\n\t\t\t\t\tbox: {\n\t\t\t\t\t\tx: Math.min(lastSpan.box.x + lastSpan.box.w, opts.width - opts.padding - ellipsisWidth),\n\t\t\t\t\t\ty: lastSpan.box.y,\n\t\t\t\t\t\tw: ellipsisWidth,\n\t\t\t\t\t\th: lastSpan.box.h,\n\t\t\t\t\t},\n\t\t\t\t})\n\n\t\t\t\treturn truncatedSpans\n\t\t\t}\n\n\t\t\treturn spans\n\t\t} finally {\n\t\t\trestoreStyles()\n\t\t}\n\t}\n}\n"],
5
+ "mappings": "AACA,SAAS,qBAAqB;AAE9B,SAAS,qBAAqB;AAavB,SAAS,oBAAoB,UAAkB,YAA4B;AACjF,SAAO,KAAK,MAAM,WAAW,UAAU;AACxC;AAEA,MAAM,cAAc;AAEpB,SAAS,oBAAoB,MAAc;AAC1C,SAAO,KACL,QAAQ,aAAa,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AA,QACR;AACA,uBAAe,KAAK;AAAA,UACnB,MAAM;AAAA,UACN,KAAK;AAAA,YACJ,GAAG,KAAK,IAAI,SAAS,IAAI,IAAI,SAAS,IAAI,GAAG,KAAK,QAAQ,KAAK,UAAU,aAAa;AAAA,YACtF,GAAG,SAAS,IAAI;AAAA,YAChB,GAAG;AAAA,YACH,GAAG,SAAS,IAAI;AAAA,UACjB;AAAA,QACD,CAAC;AAED,eAAO;AAAA,MACR;AAEA,aAAO;AAAA,IACR,UAAE;AACD,oBAAc;AAAA,IACf;AAAA,EACD;AACD;",
6
6
  "names": []
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/lib/hooks/useViewportHeight.ts"],
4
- "sourcesContent": ["import { useLayoutEffect, useState } from 'react'\nimport { useMaybeEditor } from './useEditor'\n\n/*!\n * BSD License: https://github.com/outline/rich-markdown-editor/blob/main/LICENSE\n * Copyright (c) 2020 General Outline, Inc (https://www.getoutline.com/) and individual contributors.\n *\n * Returns the height of the viewport.\n * This is mainly to account for virtual keyboards on mobile devices.\n *\n * N.B. On iOS, you have to take into account the offsetTop as well so that you get an accurate position\n * while using the virtual keyboard.\n */\n/** @public */\nexport function useViewportHeight(): number {\n\tconst editor = useMaybeEditor()\n\tconst win = editor?.getContainerWindow() ?? window\n\tconst vv = win.visualViewport\n\tconst [height, setHeight] = useState<number>(() =>\n\t\tvv ? vv.height + vv.offsetTop : win.innerHeight\n\t)\n\n\tuseLayoutEffect(() => {\n\t\tconst win = editor?.getContainerWindow() ?? window\n\t\tconst handleResize = () => {\n\t\t\tconst vv = win.visualViewport\n\t\t\tsetHeight(() => (vv ? vv.height + vv.offsetTop : win.innerHeight))\n\t\t}\n\n\t\twin.visualViewport?.addEventListener('resize', handleResize)\n\t\twin.visualViewport?.addEventListener('scroll', handleResize)\n\n\t\treturn () => {\n\t\t\twin.visualViewport?.removeEventListener('resize', handleResize)\n\t\t\twin.visualViewport?.removeEventListener('scroll', handleResize)\n\t\t}\n\t}, [editor])\n\treturn height\n}\n"],
5
- "mappings": "AAAA,SAAS,iBAAiB,gBAAgB;AAC1C,SAAS,sBAAsB;AAE/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,SAAS,oBAA4B;AAC3C,QAAM,SAAS,eAAe;AAC9B,QAAM,MAAM,QAAQ,mBAAmB,KAAK;AAC5C,QAAM,KAAK,IAAI;AACf,QAAM,CAAC,QAAQ,SAAS,IAAI;AAAA,IAAiB,MAC5C,KAAK,GAAG,SAAS,GAAG,YAAY,IAAI;AAAA,EACrC;AAEA,kBAAgB,MAAM;AACrB,UAAMA,OAAM,QAAQ,mBAAmB,KAAK;AAC5C,UAAM,eAAe,MAAM;AAC1B,YAAMC,MAAKD,KAAI;AACf,gBAAU,MAAOC,MAAKA,IAAG,SAASA,IAAG,YAAYD,KAAI,WAAY;AAAA,IAClE;AAEA,IAAAA,KAAI,gBAAgB,iBAAiB,UAAU,YAAY;AAC3D,IAAAA,KAAI,gBAAgB,iBAAiB,UAAU,YAAY;AAE3D,WAAO,MAAM;AACZ,MAAAA,KAAI,gBAAgB,oBAAoB,UAAU,YAAY;AAC9D,MAAAA,KAAI,gBAAgB,oBAAoB,UAAU,YAAY;AAAA,IAC/D;AAAA,EACD,GAAG,CAAC,MAAM,CAAC;AACX,SAAO;AACR;",
4
+ "sourcesContent": ["import { useLayoutEffect, useState } from 'react'\nimport { useMaybeEditor } from './useEditor'\n\n/*!\n * BSD License: https://github.com/outline/rich-markdown-editor/blob/main/LICENSE\n * Copyright (c) 2020 General Outline, Inc (https://www.getoutline.com/) and individual contributors.\n *\n * Returns the height of the viewport.\n * This is mainly to account for virtual keyboards on mobile devices.\n *\n * N.B. On iOS, you have to take into account the offsetTop as well so that you get an accurate position\n * while using the virtual keyboard.\n */\n/**\n * The height of the visible viewport, including the visual viewport's own offset.\n *\n * @deprecated Read `window.visualViewport` directly: this flattens the visible rectangle to a\n * single scalar, and keeping UI clear of the software keyboard usually needs the other edges too.\n * Unused since v3.14; will be removed in a future release.\n *\n * @public\n */\nexport function useViewportHeight(): number {\n\tconst editor = useMaybeEditor()\n\tconst win = editor?.getContainerWindow() ?? window\n\tconst vv = win.visualViewport\n\tconst [height, setHeight] = useState<number>(() =>\n\t\tvv ? vv.height + vv.offsetTop : win.innerHeight\n\t)\n\n\tuseLayoutEffect(() => {\n\t\tconst win = editor?.getContainerWindow() ?? window\n\t\tconst handleResize = () => {\n\t\t\tconst vv = win.visualViewport\n\t\t\tsetHeight(() => (vv ? vv.height + vv.offsetTop : win.innerHeight))\n\t\t}\n\n\t\twin.visualViewport?.addEventListener('resize', handleResize)\n\t\twin.visualViewport?.addEventListener('scroll', handleResize)\n\n\t\treturn () => {\n\t\t\twin.visualViewport?.removeEventListener('resize', handleResize)\n\t\t\twin.visualViewport?.removeEventListener('scroll', handleResize)\n\t\t}\n\t}, [editor])\n\treturn height\n}\n"],
5
+ "mappings": "AAAA,SAAS,iBAAiB,gBAAgB;AAC1C,SAAS,sBAAsB;AAE/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmBO,SAAS,oBAA4B;AAC3C,QAAM,SAAS,eAAe;AAC9B,QAAM,MAAM,QAAQ,mBAAmB,KAAK;AAC5C,QAAM,KAAK,IAAI;AACf,QAAM,CAAC,QAAQ,SAAS,IAAI;AAAA,IAAiB,MAC5C,KAAK,GAAG,SAAS,GAAG,YAAY,IAAI;AAAA,EACrC;AAEA,kBAAgB,MAAM;AACrB,UAAMA,OAAM,QAAQ,mBAAmB,KAAK;AAC5C,UAAM,eAAe,MAAM;AAC1B,YAAMC,MAAKD,KAAI;AACf,gBAAU,MAAOC,MAAKA,IAAG,SAASA,IAAG,YAAYD,KAAI,WAAY;AAAA,IAClE;AAEA,IAAAA,KAAI,gBAAgB,iBAAiB,UAAU,YAAY;AAC3D,IAAAA,KAAI,gBAAgB,iBAAiB,UAAU,YAAY;AAE3D,WAAO,MAAM;AACZ,MAAAA,KAAI,gBAAgB,oBAAoB,UAAU,YAAY;AAC9D,MAAAA,KAAI,gBAAgB,oBAAoB,UAAU,YAAY;AAAA,IAC/D;AAAA,EACD,GAAG,CAAC,MAAM,CAAC;AACX,SAAO;AACR;",
6
6
  "names": ["win", "vv"]
7
7
  }
@@ -1,8 +1,8 @@
1
- const version = "5.3.0";
1
+ const version = "5.4.0-next.ef99ab47e5ce";
2
2
  const publishDates = {
3
3
  major: "2026-05-06T16:28:18.473Z",
4
- minor: "2026-08-05T11:39:54.978Z",
5
- patch: "2026-08-05T11:39:54.978Z"
4
+ minor: "2026-08-05T11:52:36.664Z",
5
+ patch: "2026-08-05T11:52:36.664Z"
6
6
  };
7
7
  export {
8
8
  publishDates,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/version.ts"],
4
- "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.3.0'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-08-05T11:39:54.978Z',\n\tpatch: '2026-08-05T11:39:54.978Z',\n}\n"],
4
+ "sourcesContent": ["// This file is automatically generated by internal/scripts/refresh-assets.ts.\n// Do not edit manually. Or do, I'm a comment, not a cop.\n\nexport const version = '5.4.0-next.ef99ab47e5ce'\nexport const publishDates = {\n\tmajor: '2026-05-06T16:28:18.473Z',\n\tminor: '2026-08-05T11:52:36.664Z',\n\tpatch: '2026-08-05T11:52:36.664Z',\n}\n"],
5
5
  "mappings": "AAGO,MAAM,UAAU;AAChB,MAAM,eAAe;AAAA,EAC3B,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO;AACR;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tldraw/editor",
3
3
  "description": "tldraw infinite canvas SDK (editor).",
4
- "version": "5.3.0",
4
+ "version": "5.4.0-next.ef99ab47e5ce",
5
5
  "author": {
6
6
  "name": "tldraw Inc.",
7
7
  "email": "hello@tldraw.com"
@@ -49,12 +49,12 @@
49
49
  "@tiptap/core": "^3.12.1",
50
50
  "@tiptap/pm": "^3.12.1",
51
51
  "@tiptap/react": "^3.12.1",
52
- "@tldraw/state": "5.3.0",
53
- "@tldraw/state-react": "5.3.0",
54
- "@tldraw/store": "5.3.0",
55
- "@tldraw/tlschema": "5.3.0",
56
- "@tldraw/utils": "5.3.0",
57
- "@tldraw/validate": "5.3.0",
52
+ "@tldraw/state": "5.4.0-next.ef99ab47e5ce",
53
+ "@tldraw/state-react": "5.4.0-next.ef99ab47e5ce",
54
+ "@tldraw/store": "5.4.0-next.ef99ab47e5ce",
55
+ "@tldraw/tlschema": "5.4.0-next.ef99ab47e5ce",
56
+ "@tldraw/utils": "5.4.0-next.ef99ab47e5ce",
57
+ "@tldraw/validate": "5.4.0-next.ef99ab47e5ce",
58
58
  "classnames": "^2.5.1",
59
59
  "eventemitter3": "^4.0.7",
60
60
  "idb": "^7.1.1",
package/src/index.ts CHANGED
@@ -329,6 +329,7 @@ export {
329
329
  export { useTransform } from './lib/hooks/useTransform'
330
330
  export { isCursorInViewport } from './lib/utils/collaborators'
331
331
  export { useTLSchemaFromUtils, useTLStore } from './lib/hooks/useTLStore'
332
+ // oxlint-disable-next-line typescript/no-deprecated
332
333
  export { useViewportHeight } from './lib/hooks/useViewportHeight'
333
334
  export {
334
335
  LicenseManager,
@@ -7877,9 +7877,19 @@ export class Editor extends EventEmitter<TLEventMap> {
7877
7877
  */
7878
7878
  alignShapes(
7879
7879
  shapes: TLShapeId[] | TLShape[],
7880
- operation: 'left' | 'center-horizontal' | 'right' | 'top' | 'center-vertical' | 'bottom'
7880
+ operation:
7881
+ | 'left'
7882
+ | 'center-horizontal'
7883
+ | 'right'
7884
+ | 'top'
7885
+ | 'center-vertical'
7886
+ | 'bottom'
7887
+ | 'center'
7881
7888
  ): this {
7882
7889
  if (this.getIsReadonly()) return this
7890
+ if (operation === 'center') {
7891
+ return this.alignShapes(shapes, 'center-horizontal').alignShapes(shapes, 'center-vertical')
7892
+ }
7883
7893
 
7884
7894
  const { clusters: shapeClustersToAlign, allBounds } = this.getShapeClusters(shapes, 'align')
7885
7895
 
@@ -10840,9 +10850,15 @@ export class Editor extends EventEmitter<TLEventMap> {
10840
10850
  /** @internal */
10841
10851
  private _shiftKeyTimeout = -1 as any
10842
10852
 
10843
- /** @internal */
10853
+ /**
10854
+ * Release the shift modifier: clear its debounce timer id, drop the atom, and
10855
+ * dispatch a synthetic `key_up` so `inputs.keys` and tool `onKeyUp` handlers update.
10856
+ * Runs both as the 150ms debounce timer callback and when a pointer down flushes it.
10857
+ * @internal
10858
+ */
10844
10859
  @bind
10845
- _setShiftKeyTimeout() {
10860
+ _releaseShiftKey() {
10861
+ this._shiftKeyTimeout = -1
10846
10862
  this.inputs.setShiftKey(false)
10847
10863
  this.dispatch({
10848
10864
  type: 'keyboard',
@@ -10860,9 +10876,13 @@ export class Editor extends EventEmitter<TLEventMap> {
10860
10876
  /** @internal */
10861
10877
  private _altKeyTimeout = -1 as any
10862
10878
 
10863
- /** @internal */
10879
+ /**
10880
+ * Release the alt modifier. See {@link Editor._releaseShiftKey}.
10881
+ * @internal
10882
+ */
10864
10883
  @bind
10865
- _setAltKeyTimeout() {
10884
+ _releaseAltKey() {
10885
+ this._altKeyTimeout = -1
10866
10886
  this.inputs.setAltKey(false)
10867
10887
  this.dispatch({
10868
10888
  type: 'keyboard',
@@ -10880,9 +10900,13 @@ export class Editor extends EventEmitter<TLEventMap> {
10880
10900
  /** @internal */
10881
10901
  private _ctrlKeyTimeout = -1 as any
10882
10902
 
10883
- /** @internal */
10903
+ /**
10904
+ * Release the ctrl modifier. See {@link Editor._releaseShiftKey}.
10905
+ * @internal
10906
+ */
10884
10907
  @bind
10885
- _setCtrlKeyTimeout() {
10908
+ _releaseCtrlKey() {
10909
+ this._ctrlKeyTimeout = -1
10886
10910
  this.inputs.setCtrlKey(false)
10887
10911
  this.dispatch({
10888
10912
  type: 'keyboard',
@@ -10900,9 +10924,13 @@ export class Editor extends EventEmitter<TLEventMap> {
10900
10924
  /** @internal */
10901
10925
  private _metaKeyTimeout = -1 as any
10902
10926
 
10903
- /** @internal */
10927
+ /**
10928
+ * Release the meta modifier. See {@link Editor._releaseShiftKey}.
10929
+ * @internal
10930
+ */
10904
10931
  @bind
10905
- _setMetaKeyTimeout() {
10932
+ _releaseMetaKey() {
10933
+ this._metaKeyTimeout = -1
10906
10934
  this.inputs.setMetaKey(false)
10907
10935
  this.dispatch({
10908
10936
  type: 'keyboard',
@@ -10917,6 +10945,56 @@ export class Editor extends EventEmitter<TLEventMap> {
10917
10945
  })
10918
10946
  }
10919
10947
 
10948
+ /**
10949
+ * Flush any modifier still sitting in its 150ms release-debounce window, so a new
10950
+ * interaction (a pointer down) starts with correct modifier state instead of a
10951
+ * just-released key still being counted as held. A pending timer is exactly
10952
+ * "released but still counted as held"; a genuinely-held modifier has no timer
10953
+ * (-1) and is left alone.
10954
+ *
10955
+ * Each modifier is released through the same path its timer would have taken
10956
+ * (`_releaseShiftKey` and friends), which dispatches the synthetic `key_up` so
10957
+ * stale codes (e.g. `ShiftLeft`) leave `inputs.keys` and tool `onKeyUp` handlers run.
10958
+ * We clear every pending modifier atom and timer *first*, then dispatch: a synthetic
10959
+ * `key_up` reports all currently-held modifiers, so releasing them one at a time
10960
+ * would make each event re-confirm the not-yet-released ones and cancel their flush.
10961
+ * @internal
10962
+ */
10963
+ private _releaseDebouncedModifiers() {
10964
+ const releaseShift = this._shiftKeyTimeout !== -1
10965
+ const releaseAlt = this._altKeyTimeout !== -1
10966
+ const releaseCtrl = this._ctrlKeyTimeout !== -1
10967
+ const releaseMeta = this._metaKeyTimeout !== -1
10968
+
10969
+ if (releaseShift) {
10970
+ clearTimeout(this._shiftKeyTimeout)
10971
+ this._shiftKeyTimeout = -1
10972
+ this.inputs.setShiftKey(false)
10973
+ }
10974
+ if (releaseAlt) {
10975
+ clearTimeout(this._altKeyTimeout)
10976
+ this._altKeyTimeout = -1
10977
+ this.inputs.setAltKey(false)
10978
+ }
10979
+ if (releaseCtrl) {
10980
+ clearTimeout(this._ctrlKeyTimeout)
10981
+ this._ctrlKeyTimeout = -1
10982
+ this.inputs.setCtrlKey(false)
10983
+ }
10984
+ if (releaseMeta) {
10985
+ clearTimeout(this._metaKeyTimeout)
10986
+ this._metaKeyTimeout = -1
10987
+ this.inputs.setMetaKey(false)
10988
+ }
10989
+
10990
+ // Dispatch the synthetic key_up for each flushed modifier (same path its 150ms
10991
+ // timer would run): clears stale codes from inputs.keys and fires tool onKeyUp.
10992
+ if (releaseShift) this._releaseShiftKey()
10993
+ if (releaseAlt) this._releaseAltKey()
10994
+ if (releaseCtrl) this._releaseCtrlKey()
10995
+ if (releaseMeta) this._releaseMetaKey()
10996
+ }
10997
+
10920
10998
  /** @internal */
10921
10999
  private _restoreToolId = 'select'
10922
11000
 
@@ -11055,7 +11133,7 @@ export class Editor extends EventEmitter<TLEventMap> {
11055
11133
  this._shiftKeyTimeout = -1
11056
11134
  inputs.setShiftKey(true)
11057
11135
  } else if (!info.shiftKey && inputs.getShiftKey() && this._shiftKeyTimeout === -1) {
11058
- this._shiftKeyTimeout = this.timers.setTimeout(this._setShiftKeyTimeout, 150)
11136
+ this._shiftKeyTimeout = this.timers.setTimeout(this._releaseShiftKey, 150)
11059
11137
  }
11060
11138
 
11061
11139
  if (info.altKey) {
@@ -11063,7 +11141,7 @@ export class Editor extends EventEmitter<TLEventMap> {
11063
11141
  this._altKeyTimeout = -1
11064
11142
  inputs.setAltKey(true)
11065
11143
  } else if (!info.altKey && inputs.getAltKey() && this._altKeyTimeout === -1) {
11066
- this._altKeyTimeout = this.timers.setTimeout(this._setAltKeyTimeout, 150)
11144
+ this._altKeyTimeout = this.timers.setTimeout(this._releaseAltKey, 150)
11067
11145
  }
11068
11146
 
11069
11147
  if (info.ctrlKey) {
@@ -11071,7 +11149,7 @@ export class Editor extends EventEmitter<TLEventMap> {
11071
11149
  this._ctrlKeyTimeout = -1
11072
11150
  inputs.setCtrlKey(true)
11073
11151
  } else if (!info.ctrlKey && inputs.getCtrlKey() && this._ctrlKeyTimeout === -1) {
11074
- this._ctrlKeyTimeout = this.timers.setTimeout(this._setCtrlKeyTimeout, 150)
11152
+ this._ctrlKeyTimeout = this.timers.setTimeout(this._releaseCtrlKey, 150)
11075
11153
  }
11076
11154
 
11077
11155
  if (info.metaKey && info.name !== 'key_up') {
@@ -11081,7 +11159,7 @@ export class Editor extends EventEmitter<TLEventMap> {
11081
11159
  this._metaKeyTimeout = -1
11082
11160
  inputs.setMetaKey(true)
11083
11161
  } else if (!info.metaKey && inputs.getMetaKey() && this._metaKeyTimeout === -1) {
11084
- this._metaKeyTimeout = this.timers.setTimeout(this._setMetaKeyTimeout, 150)
11162
+ this._metaKeyTimeout = this.timers.setTimeout(this._releaseMetaKey, 150)
11085
11163
  }
11086
11164
 
11087
11165
  if (!inputs.getIsPointing()) {
@@ -11289,6 +11367,10 @@ export class Editor extends EventEmitter<TLEventMap> {
11289
11367
  // If we're in pen mode and the input is not a pen type, then stop here
11290
11368
  if (isPenMode && !isPen) return
11291
11369
 
11370
+ // A pointer down starts a new interaction, so flush any modifier that's
11371
+ // still lingering in its release-debounce window: treat it as released now.
11372
+ this._releaseDebouncedModifiers()
11373
+
11292
11374
  if (!this.inputs.getIsPanning()) {
11293
11375
  // Start a long press timeout
11294
11376
  this._longPressTimeout = this.timers.setTimeout(() => {
@@ -420,6 +420,48 @@ describe('TextManager', () => {
420
420
  expect(Array.isArray(result)).toBe(true)
421
421
  })
422
422
 
423
+ it('should not throw when the truncated remeasure yields no spans', () => {
424
+ // The text truncates on the first pass, but once we narrow the width
425
+ // to make room for the ellipsis, nothing measurable remains (e.g. the
426
+ // measurement element can't be laid out). This must not crash.
427
+ vi.spyOn(textManager, 'measureElementTextNodeSpans')
428
+ .mockReturnValueOnce({
429
+ spans: [{ text: 'Hello Wo', box: { x: 0, y: 0, w: 80, h: 16 } }],
430
+ didTruncate: true,
431
+ })
432
+ .mockReturnValueOnce({
433
+ spans: [{ text: '…', box: { x: 0, y: 0, w: 10, h: 16 } }],
434
+ didTruncate: false,
435
+ })
436
+ .mockReturnValueOnce({
437
+ spans: [],
438
+ didTruncate: false,
439
+ })
440
+
441
+ const opts = { ...defaultOpts, overflow: 'truncate-ellipsis' as const }
442
+ let result: ReturnType<typeof textManager.measureTextSpans> | undefined
443
+ expect(() => (result = textManager.measureTextSpans('Hello World', opts))).not.toThrow()
444
+ expect(result).toEqual([])
445
+ })
446
+
447
+ it('should not throw when the ellipsis itself cannot be measured', () => {
448
+ // If measuring the ellipsis returns no spans, fall back to a zero width
449
+ // rather than dereferencing a missing span.
450
+ vi.spyOn(textManager, 'measureElementTextNodeSpans')
451
+ .mockReturnValueOnce({
452
+ spans: [{ text: 'Hello Wo', box: { x: 0, y: 0, w: 80, h: 16 } }],
453
+ didTruncate: true,
454
+ })
455
+ .mockReturnValueOnce({ spans: [], didTruncate: false })
456
+ .mockReturnValueOnce({
457
+ spans: [{ text: 'Hello Wo', box: { x: 0, y: 0, w: 80, h: 16 } }],
458
+ didTruncate: false,
459
+ })
460
+
461
+ const opts = { ...defaultOpts, overflow: 'truncate-ellipsis' as const }
462
+ expect(() => textManager.measureTextSpans('Hello World', opts)).not.toThrow()
463
+ })
464
+
423
465
  it('should handle truncate-clip overflow', () => {
424
466
  vi.spyOn(textManager, 'measureElementTextNodeSpans').mockReturnValue({
425
467
  spans: [
@@ -438,7 +438,8 @@ export class TextManager extends EditorManager {
438
438
  if (opts.overflow === 'truncate-ellipsis' && didTruncate) {
439
439
  // we need to measure the ellipsis to know how much space it takes up
440
440
  elm.textContent = '…'
441
- const ellipsisWidth = Math.ceil(this.measureElementTextNodeSpans(elm).spans[0].box.w)
441
+ const ellipsisSpan = this.measureElementTextNodeSpans(elm).spans[0]
442
+ const ellipsisWidth = ellipsisSpan ? Math.ceil(ellipsisSpan.box.w) : 0
442
443
 
443
444
  // then, we need to subtract that space from the width we have and measure again:
444
445
  elm.style.setProperty('width', `${elementWidth - ellipsisWidth}px`)
@@ -451,7 +452,12 @@ export class TextManager extends EditorManager {
451
452
  // have to do this after measuring, not before, because adding the
452
453
  // ellipsis changes how whitespace might be getting collapsed by the
453
454
  // browser.
454
- const lastSpan = truncatedSpans[truncatedSpans.length - 1]!
455
+ const lastSpan = truncatedSpans[truncatedSpans.length - 1]
456
+ // If nothing measurable remained, return the (possibly empty)
457
+ // spans as-is rather than dereferencing a missing last span.
458
+ if (!lastSpan) {
459
+ return truncatedSpans
460
+ }
455
461
  truncatedSpans.push({
456
462
  text: '…',
457
463
  box: {
@@ -11,7 +11,15 @@ import { useMaybeEditor } from './useEditor'
11
11
  * N.B. On iOS, you have to take into account the offsetTop as well so that you get an accurate position
12
12
  * while using the virtual keyboard.
13
13
  */
14
- /** @public */
14
+ /**
15
+ * The height of the visible viewport, including the visual viewport's own offset.
16
+ *
17
+ * @deprecated Read `window.visualViewport` directly: this flattens the visible rectangle to a
18
+ * single scalar, and keeping UI clear of the software keyboard usually needs the other edges too.
19
+ * Unused since v3.14; will be removed in a future release.
20
+ *
21
+ * @public
22
+ */
15
23
  export function useViewportHeight(): number {
16
24
  const editor = useMaybeEditor()
17
25
  const win = editor?.getContainerWindow() ?? window
package/src/version.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  // This file is automatically generated by internal/scripts/refresh-assets.ts.
2
2
  // Do not edit manually. Or do, I'm a comment, not a cop.
3
3
 
4
- export const version = '5.3.0'
4
+ export const version = '5.4.0-next.ef99ab47e5ce'
5
5
  export const publishDates = {
6
6
  major: '2026-05-06T16:28:18.473Z',
7
- minor: '2026-08-05T11:39:54.978Z',
8
- patch: '2026-08-05T11:39:54.978Z',
7
+ minor: '2026-08-05T11:52:36.664Z',
8
+ patch: '2026-08-05T11:52:36.664Z',
9
9
  }