let host call convertToExcalidrawElements when using programmatic API

This commit is contained in:
Aakansha Doshi 2023-07-14 14:50:27 +05:30
parent c1a61b06df
commit 25e2f80709
5 changed files with 58 additions and 48 deletions

View file

@ -304,7 +304,6 @@ import { jotaiStore } from "../jotai";
import { activeConfirmDialogAtom } from "./ActiveConfirmDialog"; import { activeConfirmDialogAtom } from "./ActiveConfirmDialog";
import { actionWrapTextInContainer } from "../actions/actionBoundText"; import { actionWrapTextInContainer } from "../actions/actionBoundText";
import BraveMeasureTextError from "./BraveMeasureTextError"; import BraveMeasureTextError from "./BraveMeasureTextError";
import { convertToExcalidrawElements } from "../data/transform";
import { activeEyeDropperAtom } from "./EyeDropper"; import { activeEyeDropperAtom } from "./EyeDropper";
const AppContext = React.createContext<AppClassProperties>(null!); const AppContext = React.createContext<AppClassProperties>(null!);
@ -2097,9 +2096,7 @@ class App extends React.Component<AppProps, AppState> {
} }
if (sceneData.elements) { if (sceneData.elements) {
this.scene.replaceAllElements( this.scene.replaceAllElements(sceneData.elements);
convertToExcalidrawElements(sceneData.elements),
);
} }
if (sceneData.collaborators) { if (sceneData.collaborators) {

View file

@ -41,7 +41,10 @@ import {
getDefaultLineHeight, getDefaultLineHeight,
measureBaseline, measureBaseline,
} from "../element/textElement"; } from "../element/textElement";
import { convertToExcalidrawElements } from "../data/transform"; import {
ExcalidrawProgrammaticAPI,
convertToExcalidrawElements,
} from "../data/transform";
type RestoredAppState = Omit< type RestoredAppState = Omit<
AppState, AppState,
@ -366,7 +369,7 @@ const repairBoundElement = (
}; };
export const restoreElements = ( export const restoreElements = (
elements: ImportedDataState["elements"], elements: ExcalidrawProgrammaticAPI["elements"],
/** NOTE doesn't serve for reconciliation */ /** NOTE doesn't serve for reconciliation */
localElements: readonly ExcalidrawElement[] | null | undefined, localElements: readonly ExcalidrawElement[] | null | undefined,
opts?: { refreshDimensions?: boolean; repairBindings?: boolean } | undefined, opts?: { refreshDimensions?: boolean; repairBindings?: boolean } | undefined,

View file

@ -1,5 +1,7 @@
import { convertToExcalidrawElements } from "./transform"; import {
import { ImportedDataState } from "./types"; ExcalidrawProgrammaticAPI,
convertToExcalidrawElements,
} from "./transform";
describe("Test Transform", () => { describe("Test Transform", () => {
it("should transform regular shapes", () => { it("should transform regular shapes", () => {
@ -54,7 +56,9 @@ describe("Test Transform", () => {
]; ];
expect( expect(
convertToExcalidrawElements(elements as ImportedDataState["elements"]), convertToExcalidrawElements(
elements as ExcalidrawProgrammaticAPI["elements"],
),
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
@ -76,7 +80,9 @@ describe("Test Transform", () => {
}, },
]; ];
expect( expect(
convertToExcalidrawElements(elements as ImportedDataState["elements"]), convertToExcalidrawElements(
elements as ExcalidrawProgrammaticAPI["elements"],
),
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
@ -111,7 +117,7 @@ describe("Test Transform", () => {
}, },
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(4); expect(excaldrawElements.length).toBe(4);
@ -193,7 +199,7 @@ describe("Test Transform", () => {
}, },
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(12); expect(excaldrawElements.length).toBe(12);
@ -250,7 +256,7 @@ describe("Test Transform", () => {
}, },
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(8); expect(excaldrawElements.length).toBe(8);
@ -283,7 +289,7 @@ describe("Test Transform", () => {
}, },
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(4); expect(excaldrawElements.length).toBe(4);
@ -366,7 +372,7 @@ describe("Test Transform", () => {
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(4); expect(excaldrawElements.length).toBe(4);
@ -482,7 +488,7 @@ describe("Test Transform", () => {
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(5); expect(excaldrawElements.length).toBe(5);
@ -530,7 +536,7 @@ describe("Test Transform", () => {
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(4); expect(excaldrawElements.length).toBe(4);
@ -580,7 +586,7 @@ describe("Test Transform", () => {
]; ];
const excaldrawElements = convertToExcalidrawElements( const excaldrawElements = convertToExcalidrawElements(
elements as ImportedDataState["elements"], elements as ExcalidrawProgrammaticAPI["elements"],
); );
expect(excaldrawElements.length).toBe(4); expect(excaldrawElements.length).toBe(4);
@ -622,7 +628,9 @@ describe("Test Transform", () => {
}, },
]; ];
expect( expect(
convertToExcalidrawElements(elements as ImportedDataState["elements"]), convertToExcalidrawElements(
elements as ExcalidrawProgrammaticAPI["elements"],
),
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
}); });

View file

@ -24,15 +24,43 @@ import {
import { import {
ExcalidrawBindableElement, ExcalidrawBindableElement,
ExcalidrawElement, ExcalidrawElement,
ExcalidrawFreeDrawElement,
ExcalidrawGenericElement, ExcalidrawGenericElement,
ExcalidrawImageElement, ExcalidrawImageElement,
ExcalidrawLinearElement, ExcalidrawLinearElement,
ExcalidrawSelectionElement,
ExcalidrawTextElement, ExcalidrawTextElement,
} from "../element/types"; } from "../element/types";
import { MarkOptional } from "../utility-types"; import { MarkOptional } from "../utility-types";
import { getFontString } from "../utils"; import { getFontString } from "../utils";
import { ImportedDataState, ValidContainer, ValidLinearElement } from "./types"; import { ValidContainer, ValidLinearElement } from "./types";
export interface ExcalidrawProgrammaticAPI {
elements?:
| readonly (
| Extract<
ExcalidrawElement,
| ExcalidrawSelectionElement
| ExcalidrawImageElement
| ExcalidrawFreeDrawElement
>
| ({
type: Extract<ExcalidrawLinearElement["type"], "line">;
x: number;
y: number;
} & Partial<ExcalidrawLinearElement>)
| ValidContainer
| ValidLinearElement
| ({
type: "text";
text: string;
x: number;
y: number;
id?: ExcalidrawTextElement["id"];
} & Partial<ExcalidrawTextElement>)
)[]
| null;
}
export const ELEMENTS_SUPPORTING_PROGRAMMATIC_API = [ export const ELEMENTS_SUPPORTING_PROGRAMMATIC_API = [
"rectangle", "rectangle",
"ellipse", "ellipse",
@ -307,7 +335,7 @@ const excalidrawElements = (() => {
})(); })();
export const convertToExcalidrawElements = ( export const convertToExcalidrawElements = (
elements: ImportedDataState["elements"], elements: ExcalidrawProgrammaticAPI["elements"],
): ExcalidrawElement[] => { ): ExcalidrawElement[] => {
excalidrawElements.clear(); excalidrawElements.clear();
if (!elements) { if (!elements) {

View file

@ -1,11 +1,8 @@
import { import {
ExcalidrawBindableElement, ExcalidrawBindableElement,
ExcalidrawElement, ExcalidrawElement,
ExcalidrawFreeDrawElement,
ExcalidrawGenericElement, ExcalidrawGenericElement,
ExcalidrawImageElement,
ExcalidrawLinearElement, ExcalidrawLinearElement,
ExcalidrawSelectionElement,
ExcalidrawTextElement, ExcalidrawTextElement,
FontFamilyValues, FontFamilyValues,
TextAlign, TextAlign,
@ -137,30 +134,7 @@ export interface ImportedDataState {
type?: string; type?: string;
version?: number; version?: number;
source?: string; source?: string;
elements?: elements?: readonly ExcalidrawElement[] | null;
| readonly (
| Extract<
ExcalidrawElement,
| ExcalidrawSelectionElement
| ExcalidrawImageElement
| ExcalidrawFreeDrawElement
>
| ({
type: Extract<ExcalidrawLinearElement["type"], "line">;
x: number;
y: number;
} & Partial<ExcalidrawLinearElement>)
| ValidContainer
| ValidLinearElement
| ({
type: "text";
text: string;
x: number;
y: number;
id?: ExcalidrawTextElement["id"];
} & Partial<ExcalidrawTextElement>)
)[]
| null;
appState?: Readonly< appState?: Readonly<
Partial< Partial<
AppState & { AppState & {