From 292aa1cddbdd5149689845093327d0f6d8ba6586 Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Mon, 29 Dec 2025 17:35:00 +1100 Subject: [PATCH] fix: to support wrapping with autoResize and given dimensions --- packages/element/src/newElement.ts | 26 +++++++++++++++++++------- packages/element/src/transform.ts | 18 ++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/element/src/newElement.ts b/packages/element/src/newElement.ts index ec50a81ff2..d69f1acb62 100644 --- a/packages/element/src/newElement.ts +++ b/packages/element/src/newElement.ts @@ -252,12 +252,24 @@ export const newTextElement = ( const fontFamily = opts.fontFamily || DEFAULT_FONT_FAMILY; const fontSize = opts.fontSize || DEFAULT_FONT_SIZE; const lineHeight = opts.lineHeight || getLineHeight(fontFamily); - const text = normalizeText(opts.text); - const metrics = measureText( - text, - getFontString({ fontFamily, fontSize }), - lineHeight, - ); + const normalizedText = normalizeText(opts.text); + const originalText = opts.originalText ?? normalizedText; + const shouldUseProvidedDimensions = + opts.autoResize === false && opts.width && opts.height; + const text = shouldUseProvidedDimensions + ? wrapText( + normalizedText, + getFontString({ fontFamily, fontSize }), + opts.width, + ) + : normalizedText; + + const metrics = shouldUseProvidedDimensions + ? { + width: opts.width, + height: opts.height, + } + : measureText(text, getFontString({ fontFamily, fontSize }), lineHeight); const textAlign = opts.textAlign || DEFAULT_TEXT_ALIGN; const verticalAlign = opts.verticalAlign || DEFAULT_VERTICAL_ALIGN; const offsets = getTextElementPositionOffsets( @@ -277,7 +289,7 @@ export const newTextElement = ( width: metrics.width, height: metrics.height, containerId: opts.containerId || null, - originalText: opts.originalText ?? text, + originalText, autoResize: opts.autoResize ?? true, lineHeight, }; diff --git a/packages/element/src/transform.ts b/packages/element/src/transform.ts index 22828fe263..370da31834 100644 --- a/packages/element/src/transform.ts +++ b/packages/element/src/transform.ts @@ -581,12 +581,18 @@ export const convertToExcalidrawElements = ( const fontSize = element?.fontSize || DEFAULT_FONT_SIZE; const lineHeight = element?.lineHeight || getLineHeight(fontFamily); const text = element.text ?? ""; - const normalizedText = normalizeText(text); - const metrics = measureText( - normalizedText, - getFontString({ fontFamily, fontSize }), - lineHeight, - ); + const shouldUseProvidedDimensions = + element.autoResize === false && element.width && element.height; + const metrics = shouldUseProvidedDimensions + ? { + width: element.width, + height: element.height, + } + : measureText( + normalizeText(text), + getFontString({ fontFamily, fontSize }), + lineHeight, + ); excalidrawElement = newTextElement({ width: metrics.width,