refactor: improve types around dataState and libraryData (#3427)

This commit is contained in:
David Luzar 2021-04-10 19:17:49 +02:00 committed by GitHub
parent c19c8ecd27
commit a7cbe68ae8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 84 additions and 55 deletions

View file

@ -4,7 +4,7 @@ import {
ExcalidrawSelectionElement,
} from "../element/types";
import { AppState, NormalizedZoomValue } from "../types";
import { DataState, ImportedDataState } from "./types";
import { ImportedDataState } from "./types";
import { isInvisiblySmallElement, getNormalizedDimensions } from "../element";
import { isLinearElementType } from "../element/typeChecks";
import { randomId } from "../random";
@ -16,6 +16,16 @@ import {
} from "../constants";
import { getDefaultAppState } from "../appState";
type RestoredAppState = Omit<
AppState,
"offsetTop" | "offsetLeft" | "width" | "height"
>;
export type RestoredDataState = {
elements: ExcalidrawElement[];
appState: RestoredAppState;
};
const getFontFamilyByName = (fontFamilyName: string): FontFamily => {
for (const [id, fontFamilyString] of Object.entries(FONT_FAMILY)) {
if (fontFamilyString.includes(fontFamilyName)) {
@ -144,7 +154,7 @@ export const restoreElements = (
export const restoreAppState = (
appState: ImportedDataState["appState"],
localAppState: Partial<AppState> | null,
): DataState["appState"] => {
): RestoredAppState => {
appState = appState || {};
const defaultAppState = getDefaultAppState();
@ -186,7 +196,7 @@ export const restore = (
* Supply `null` if you can't get access to it.
*/
localAppState: Partial<AppState> | null | undefined,
): DataState => {
): RestoredDataState => {
return {
elements: restoreElements(data?.elements),
appState: restoreAppState(data?.appState, localAppState || null),