mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-28 08:14:42 +03:00
fix(restore): recover text from originalText when text is missing
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 <mark@lazycat.hu>
This commit is contained in:
2 files changed
+37
-1
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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([
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user