diff --git a/src/data/transform.ts b/src/data/transform.ts index 8286718c23..49835764f2 100644 --- a/src/data/transform.ts +++ b/src/data/transform.ts @@ -18,6 +18,7 @@ import { TextAlign, VerticalAlign, } from "../element/types"; +import { randomId } from "../random"; import { MarkOptional } from "../utility-types"; import { ImportedDataState } from "./types"; @@ -262,9 +263,10 @@ export const convertToExcalidrawElements = ( } else { let excalidrawElement; if (element.type === "text") { - excalidrawElement = { + excalidrawElement = newTextElement({ ...element, - } as ExcalidrawTextElement; + }); + excalidrawElements.push(excalidrawElement); } else if (element.type === "arrow" || element.type === "line") { const { linearElement, startBoundElement, endBoundElement } = @@ -286,6 +288,7 @@ export const convertToExcalidrawElements = ( } else { excalidrawElement = { ...element, + id: element.id || randomId(), width: element?.width || (ELEMENTS_SUPPORTING_PROGRAMMATIC_API.includes(element.type) diff --git a/src/data/types.ts b/src/data/types.ts index c4ddf35005..c06001068b 100644 --- a/src/data/types.ts +++ b/src/data/types.ts @@ -3,6 +3,7 @@ import { ExcalidrawElement, ExcalidrawGenericElement, ExcalidrawLinearElement, + ExcalidrawTextElement, FontFamilyValues, TextAlign, VerticalAlign, @@ -46,21 +47,8 @@ export interface ImportedDataState { source?: string; elements?: | readonly ( - | (ExcalidrawElement & { - id?: ExcalidrawElement["id"]; - label?: { - text: string; - fontSize?: number; - fontFamily?: FontFamilyValues; - textAlign?: TextAlign; - verticalAlign?: VerticalAlign; - } & MarkOptional; - } & ElementConstructorOpts) | ({ - type: Exclude< - typeof ELEMENTS_SUPPORTING_PROGRAMMATIC_API[number], - "text" - >; + type: Exclude; id?: ExcalidrawElement["id"]; label?: { text: string; @@ -73,7 +61,7 @@ export interface ImportedDataState { | ({ type: "text"; text: string; - id?: ExcalidrawBindableElement["id"]; + id?: ExcalidrawTextElement["id"]; } & ElementConstructorOpts) | ({ type: ExcalidrawLinearElement["type"]; diff --git a/src/element/newElement.ts b/src/element/newElement.ts index 09b6b47e4e..804705c3d5 100644 --- a/src/element/newElement.ts +++ b/src/element/newElement.ts @@ -158,7 +158,7 @@ export const newTextElement = ( fontFamily?: FontFamilyValues; textAlign?: TextAlign; verticalAlign?: VerticalAlign; - containerId?: ExcalidrawTextContainer["id"]; + containerId?: ExcalidrawTextContainer["id"] | null; lineHeight?: ExcalidrawTextElement["lineHeight"]; strokeWidth?: ExcalidrawTextElement["strokeWidth"]; } & ElementConstructorOpts,