This commit is contained in:
Aakansha Doshi 2023-05-03 13:41:41 +05:30
parent 7087db42c0
commit 59e8bf498d
9 changed files with 109 additions and 65 deletions

View file

@ -13,6 +13,7 @@ import { FileSystemHandle, nativeFileSystemSupported } from "./filesystem";
import { isValidExcalidrawData, isValidLibrary } from "./json";
import { restore, restoreLibraryItems } from "./restore";
import { ImportedLibraryData } from "./types";
import { convertToExcalidrawElements } from "../element/newElement";
const parseFileContents = async (blob: Blob | File) => {
let contents: string;
@ -138,14 +139,16 @@ export const loadSceneOrLibraryFromBlob = async (
type: MIME_TYPES.excalidraw,
data: restore(
{
elements: clearElementsForExport(data.elements || []),
elements: clearElementsForExport(
convertToExcalidrawElements(data.elements || []),
),
appState: {
theme: localAppState?.theme,
fileHandle: fileHandle || blob.handle || null,
...cleanAppStateForExport(data.appState || {}),
...(localAppState
? calculateScrollCenter(
data.elements || [],
convertToExcalidrawElements(data.elements || []),
localAppState,
null,
)

View file

@ -40,7 +40,7 @@ import {
getDefaultLineHeight,
measureBaseline,
} from "../element/textElement";
import { updateElementChildren } from "../element/newElement";
import { convertToExcalidrawElements } from "../element/newElement";
type RestoredAppState = Omit<
AppState,
@ -373,41 +373,37 @@ export const restoreElements = (
): ExcalidrawElement[] => {
// used to detect duplicate top-level element ids
const existingIds = new Set<string>();
const excalidrawElements = convertToExcalidrawElements(elements);
const localElementsMap = localElements ? arrayToMap(localElements) : null;
const restoredElements = (elements || []).reduce((elements, element) => {
// filtering out selection, which is legacy, no longer kept in elements,
// and causing issues if retained
if (element.type !== "selection" && !isInvisiblySmallElement(element)) {
let migratedElement: ExcalidrawElement | null = restoreElement(
element,
opts?.refreshDimensions,
);
if (migratedElement) {
const localElement = localElementsMap?.get(element.id);
if (localElement && localElement.version > migratedElement.version) {
migratedElement = bumpVersion(migratedElement, localElement.version);
}
if (existingIds.has(migratedElement.id)) {
migratedElement = { ...migratedElement, id: randomId() };
}
existingIds.add(migratedElement.id);
//@ts-ignore
if (element.children?.length) {
//@ts-ignore
const newElements = updateElementChildren(element);
if (newElements) {
elements.push(...newElements);
} else {
elements.push(migratedElement);
const restoredElements = (excalidrawElements || []).reduce(
(elements, element) => {
// filtering out selection, which is legacy, no longer kept in elements,
// and causing issues if retained
if (element.type !== "selection" && !isInvisiblySmallElement(element)) {
let migratedElement: ExcalidrawElement | null = restoreElement(
element,
opts?.refreshDimensions,
);
if (migratedElement) {
const localElement = localElementsMap?.get(element.id);
if (localElement && localElement.version > migratedElement.version) {
migratedElement = bumpVersion(
migratedElement,
localElement.version,
);
}
} else {
if (existingIds.has(migratedElement.id)) {
migratedElement = { ...migratedElement, id: randomId() };
}
existingIds.add(migratedElement.id);
elements.push(migratedElement);
}
}
}
return elements;
}, [] as ExcalidrawElement[]);
return elements;
},
[] as ExcalidrawElement[],
);
if (!opts?.repairBindings) {
return restoredElements;

View file

@ -1,4 +1,4 @@
import { ExcalidrawElement } from "../element/types";
import { ExcalidrawElement, ExcalidrawGenericElement } from "../element/types";
import {
AppState,
BinaryFiles,
@ -7,6 +7,8 @@ import {
} from "../types";
import type { cleanAppStateForExport } from "../appState";
import { VERSIONS } from "../constants";
import { MarkOptional } from "../utility-types";
import { ElementConstructorOpts } from "../element/newElement";
export interface ExportedDataState {
type: string;
@ -35,7 +37,28 @@ export interface ImportedDataState {
type?: string;
version?: number;
source?: string;
elements?: readonly ExcalidrawElement[] | null;
elements?:
| readonly (
| (ExcalidrawElement & {
children?: [
{ text: string } & MarkOptional<
ElementConstructorOpts,
"x" | "y"
>,
];
})
| {
type: Exclude<ExcalidrawGenericElement["type"], "selection">;
children?: [
{ text: string } & MarkOptional<
ElementConstructorOpts,
"x" | "y"
>,
] &
MarkOptional<ElementConstructorOpts, "x" | "y">;
}
)[]
| null;
appState?: Readonly<
Partial<
AppState & {