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([ {