2020-04-10 18:09:29 -04:00
|
|
|
import oc from "open-color";
|
2020-02-19 08:25:01 -08:00
|
|
|
import { AppState, FlooredNumber } from "./types";
|
2020-01-11 20:34:21 -08:00
|
|
|
import { getDateTime } from "./utils";
|
2020-04-08 01:31:28 +03:00
|
|
|
import { t } from "./i18n";
|
2020-06-25 21:21:27 +02:00
|
|
|
import {
|
|
|
|
DEFAULT_FONT_SIZE,
|
|
|
|
DEFAULT_FONT_FAMILY,
|
|
|
|
DEFAULT_TEXT_ALIGN,
|
|
|
|
} from "./constants";
|
2020-01-11 20:34:21 -08:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const getDefaultAppState = (): AppState => {
|
2020-01-11 20:34:21 -08:00
|
|
|
return {
|
2020-03-26 18:28:26 +01:00
|
|
|
isLoading: false,
|
2020-04-03 12:50:51 +01:00
|
|
|
errorMessage: null,
|
2020-01-11 20:34:21 -08:00
|
|
|
draggingElement: null,
|
|
|
|
resizingElement: null,
|
2020-02-01 15:49:18 +04:00
|
|
|
multiElement: null,
|
2020-01-19 23:32:24 +01:00
|
|
|
editingElement: null,
|
2020-06-01 11:35:44 +02:00
|
|
|
editingLinearElement: null,
|
2020-01-11 20:34:21 -08:00
|
|
|
elementType: "selection",
|
2020-01-20 15:52:19 -08:00
|
|
|
elementLocked: false,
|
2020-01-11 20:34:21 -08:00
|
|
|
exportBackground: true,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark: false,
|
2020-04-10 18:09:29 -04:00
|
|
|
currentItemStrokeColor: oc.black,
|
2020-01-11 20:34:21 -08:00
|
|
|
currentItemBackgroundColor: "transparent",
|
2020-01-25 18:58:57 +01:00
|
|
|
currentItemFillStyle: "hachure",
|
|
|
|
currentItemStrokeWidth: 1,
|
2020-05-14 17:04:33 +02:00
|
|
|
currentItemStrokeStyle: "solid",
|
2020-01-25 18:58:57 +01:00
|
|
|
currentItemRoughness: 1,
|
|
|
|
currentItemOpacity: 100,
|
2020-05-27 15:14:50 +02:00
|
|
|
currentItemFontSize: DEFAULT_FONT_SIZE,
|
|
|
|
currentItemFontFamily: DEFAULT_FONT_FAMILY,
|
2020-04-08 21:00:27 +01:00
|
|
|
currentItemTextAlign: DEFAULT_TEXT_ALIGN,
|
2020-04-10 18:09:29 -04:00
|
|
|
viewBackgroundColor: oc.white,
|
2020-02-19 08:25:01 -08:00
|
|
|
scrollX: 0 as FlooredNumber,
|
|
|
|
scrollY: 0 as FlooredNumber,
|
2020-01-11 20:34:21 -08:00
|
|
|
cursorX: 0,
|
|
|
|
cursorY: 0,
|
2020-04-04 16:12:19 +01:00
|
|
|
cursorButton: "up",
|
2020-02-01 16:52:10 +00:00
|
|
|
scrolledOutside: false,
|
2020-04-08 01:31:28 +03:00
|
|
|
name: `${t("labels.untitled")}-${getDateTime()}`,
|
2020-04-07 14:02:42 +01:00
|
|
|
username: "",
|
2020-03-11 19:42:18 +01:00
|
|
|
isCollaborating: false,
|
2020-02-03 21:52:21 +04:00
|
|
|
isResizing: false,
|
2020-04-02 17:40:26 +09:00
|
|
|
isRotating: false,
|
2020-02-05 22:47:10 +04:00
|
|
|
selectionElement: null,
|
2020-02-15 21:03:32 +01:00
|
|
|
zoom: 1,
|
2020-03-01 14:39:03 -05:00
|
|
|
openMenu: null,
|
2020-02-21 14:34:18 -05:00
|
|
|
lastPointerDownWith: "mouse",
|
2020-03-08 10:20:55 -07:00
|
|
|
selectedElementIds: {},
|
2020-06-02 18:41:40 +02:00
|
|
|
previousSelectedElementIds: {},
|
2020-03-12 12:19:56 +01:00
|
|
|
collaborators: new Map(),
|
2020-03-28 16:59:36 -07:00
|
|
|
shouldCacheIgnoreZoom: false,
|
2020-04-05 15:58:00 +03:00
|
|
|
showShortcutsDialog: false,
|
2020-04-25 18:43:02 +05:30
|
|
|
zenModeEnabled: false,
|
2020-06-24 00:24:52 +09:00
|
|
|
gridSize: null,
|
2020-05-26 13:07:46 -07:00
|
|
|
editingGroupId: null,
|
|
|
|
selectedGroupIds: {},
|
2020-07-07 20:40:39 +05:30
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
2020-07-10 02:20:23 -07:00
|
|
|
isLibraryOpen: false,
|
2020-01-11 20:34:21 -08:00
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-01 15:49:18 +04:00
|
|
|
|
2020-07-11 13:09:40 +02:00
|
|
|
/**
|
|
|
|
* Config containing all AppState keys. Used to determine whether given state
|
|
|
|
* prop should be stripped when exporting to given storage type.
|
|
|
|
*/
|
|
|
|
const APP_STATE_STORAGE_CONF = (<
|
|
|
|
Values extends {
|
|
|
|
/** whether to keep when storing to browser storage (localStorage/IDB) */
|
|
|
|
browser: boolean;
|
|
|
|
/** whether to keep when exporting to file/database */
|
|
|
|
export: boolean;
|
|
|
|
},
|
|
|
|
T extends Record<keyof AppState, Values>
|
|
|
|
>(
|
|
|
|
config: { [K in keyof T]: K extends keyof AppState ? T[K] : never },
|
|
|
|
) => config)({
|
|
|
|
collaborators: { browser: false, export: false },
|
|
|
|
currentItemBackgroundColor: { browser: true, export: false },
|
|
|
|
currentItemFillStyle: { browser: true, export: false },
|
|
|
|
currentItemFontFamily: { browser: true, export: false },
|
|
|
|
currentItemFontSize: { browser: true, export: false },
|
|
|
|
currentItemOpacity: { browser: true, export: false },
|
|
|
|
currentItemRoughness: { browser: true, export: false },
|
|
|
|
currentItemStrokeColor: { browser: true, export: false },
|
|
|
|
currentItemStrokeStyle: { browser: true, export: false },
|
|
|
|
currentItemStrokeWidth: { browser: true, export: false },
|
|
|
|
currentItemTextAlign: { browser: true, export: false },
|
|
|
|
cursorButton: { browser: true, export: false },
|
|
|
|
cursorX: { browser: true, export: false },
|
|
|
|
cursorY: { browser: true, export: false },
|
|
|
|
draggingElement: { browser: false, export: false },
|
|
|
|
editingElement: { browser: false, export: false },
|
|
|
|
editingGroupId: { browser: true, export: false },
|
|
|
|
editingLinearElement: { browser: false, export: false },
|
|
|
|
elementLocked: { browser: true, export: false },
|
|
|
|
elementType: { browser: true, export: false },
|
|
|
|
errorMessage: { browser: false, export: false },
|
|
|
|
exportBackground: { browser: true, export: false },
|
|
|
|
gridSize: { browser: true, export: true },
|
|
|
|
height: { browser: false, export: false },
|
|
|
|
isCollaborating: { browser: false, export: false },
|
|
|
|
isLibraryOpen: { browser: false, export: false },
|
|
|
|
isLoading: { browser: false, export: false },
|
|
|
|
isResizing: { browser: false, export: false },
|
|
|
|
isRotating: { browser: false, export: false },
|
|
|
|
lastPointerDownWith: { browser: true, export: false },
|
|
|
|
multiElement: { browser: false, export: false },
|
|
|
|
name: { browser: true, export: false },
|
|
|
|
openMenu: { browser: true, export: false },
|
|
|
|
previousSelectedElementIds: { browser: true, export: false },
|
|
|
|
resizingElement: { browser: false, export: false },
|
|
|
|
scrolledOutside: { browser: true, export: false },
|
|
|
|
scrollX: { browser: true, export: false },
|
|
|
|
scrollY: { browser: true, export: false },
|
|
|
|
selectedElementIds: { browser: true, export: false },
|
|
|
|
selectedGroupIds: { browser: true, export: false },
|
|
|
|
selectionElement: { browser: false, export: false },
|
|
|
|
shouldAddWatermark: { browser: true, export: false },
|
|
|
|
shouldCacheIgnoreZoom: { browser: true, export: false },
|
|
|
|
showShortcutsDialog: { browser: false, export: false },
|
|
|
|
username: { browser: true, export: false },
|
|
|
|
viewBackgroundColor: { browser: true, export: true },
|
|
|
|
width: { browser: false, export: false },
|
|
|
|
zenModeEnabled: { browser: true, export: false },
|
|
|
|
zoom: { browser: true, export: false },
|
|
|
|
});
|
|
|
|
|
|
|
|
const _clearAppStateForStorage = <ExportType extends "export" | "browser">(
|
|
|
|
appState: Partial<AppState>,
|
|
|
|
exportType: ExportType,
|
|
|
|
) => {
|
|
|
|
type ExportableKeys = {
|
|
|
|
[K in keyof typeof APP_STATE_STORAGE_CONF]: typeof APP_STATE_STORAGE_CONF[K][ExportType] extends true
|
|
|
|
? K
|
|
|
|
: never;
|
|
|
|
}[keyof typeof APP_STATE_STORAGE_CONF];
|
|
|
|
const stateForExport = {} as { [K in ExportableKeys]?: typeof appState[K] };
|
|
|
|
for (const key of Object.keys(appState) as (keyof typeof appState)[]) {
|
2020-07-16 21:20:55 +02:00
|
|
|
const propConfig = APP_STATE_STORAGE_CONF[key];
|
|
|
|
if (!propConfig) {
|
|
|
|
console.error(
|
|
|
|
`_clearAppStateForStorage: appState key "${key}" config doesn't exist for "${exportType}" export type`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (propConfig?.[exportType]) {
|
2020-07-11 13:09:40 +02:00
|
|
|
// @ts-ignore see https://github.com/microsoft/TypeScript/issues/31445
|
|
|
|
stateForExport[key] = appState[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stateForExport;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-04 17:39:08 +04:00
|
|
|
|
2020-07-11 13:09:40 +02:00
|
|
|
export const clearAppStateForLocalStorage = (appState: Partial<AppState>) => {
|
|
|
|
return _clearAppStateForStorage(appState, "browser");
|
|
|
|
};
|
|
|
|
|
|
|
|
export const cleanAppStateForExport = (appState: Partial<AppState>) => {
|
|
|
|
return _clearAppStateForStorage(appState, "export");
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|