From 75da14f5effda69c584097d16e875f9debeda792 Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Wed, 21 Oct 2026 07:13:42 +0000 Subject: [PATCH] fix(restore): recover text from originalText when text is missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A text element without `text` was restored empty (and deleted on import) even when it had an `originalText`. Use the unwrapped `originalText` instead; it's rewrapped when the text is measured. Line height detection divides the element's height by its number of lines, so only use the height when it belongs to the restored `text` — it says nothing about the line height of an unwrapped `originalText`. The height is now also coerced to a number. The existing test restoring a `null` text now also nulls `originalText`, as it otherwise recovers the text from it. Signed-off-by: Mark Tolmacs --- packages/excalidraw/data/restore.ts | 8 ++++- .../excalidraw/tests/data/restore.test.ts | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/excalidraw/data/restore.ts b/packages/excalidraw/data/restore.ts index 62c02577fe..8279492d9a 100644 --- a/packages/excalidraw/data/restore.ts +++ b/packages/excalidraw/data/restore.ts @@ -804,7 +804,10 @@ export const restoreElement = ( (typeof fontFamily === "string" ? getFontFamilyByName(fontFamily) : DEFAULT_FONT_FAMILY)) as FontFamilyValues; - const text = restoreString(element.text) || ""; + // a label without `text` can still be recovered from its unwrapped + // `originalText` (rewrapped when refreshing dimensions) + const wrappedText = restoreString(element.text); + const text = wrappedText || restoreString(element.originalText) || ""; const lineHeight = (toPositiveNumber(element.lineHeight) ?? restoreLineHeight({ @@ -812,6 +815,9 @@ export const restoreElement = ( fontSize: restoredFontSize, fontFamily: restoredFontFamily, text, + // the height spans the lines of `text` as it was wrapped, so it says + // nothing about the line height of the unwrapped `originalText` + height: wrappedText ? toFiniteNumber(element.height) ?? 0 : 0, })) as ExcalidrawTextElement["lineHeight"]; element = restoreElementWithProperties(element, { fontSize: restoredFontSize, diff --git a/packages/excalidraw/tests/data/restore.test.ts b/packages/excalidraw/tests/data/restore.test.ts index 7ff7c2a188..fad432d617 100644 --- a/packages/excalidraw/tests/data/restore.test.ts +++ b/packages/excalidraw/tests/data/restore.test.ts @@ -276,6 +276,7 @@ describe("restoreElements", () => { }); textElement.text = null; + textElement.originalText = null; textElement.font = "10 unknown"; expect(textElement.isDeleted).toBe(false); @@ -1210,6 +1211,20 @@ describe("repairing malformed elements", () => { expect(text.height).toBeGreaterThan(0); }); + it("should recover text from originalText", () => { + const [text] = restoreImported([ + { type: "text", id: "text", text: null, originalText: "hi" }, + ]) as ExcalidrawTextElement[]; + + expect(text).toEqual( + expect.objectContaining({ + text: "hi", + originalText: "hi", + isDeleted: false, + }), + ); + }); + it("should coerce malformed values instead of dropping them", () => { const [rect] = restoreImported([ { @@ -1628,6 +1643,21 @@ describe("repairing malformed elements", () => { expect((rect as any).created).not.toBe(2024); }); + it("should not detect the line height from the height of wrapped text it doesn't have", () => { + const [text] = restoreImported([ + { + type: "text", + id: "text", + originalText: "a long sentence", + height: 50, + fontSize: 20, + fontFamily: FONT_FAMILY.Virgil, + }, + ]) as ExcalidrawTextElement[]; + + expect(text.lineHeight).toBe(getLineHeight(FONT_FAMILY.Virgil)); + }); + it("should bump the version of invisible linear elements once", () => { const [arrow] = restoreImported([ {