2020-04-10 18:09:29 -04:00
|
|
|
import oc from "open-color";
|
2020-06-25 21:21:27 +02:00
|
|
|
import {
|
|
|
|
DEFAULT_FONT_FAMILY,
|
2020-12-27 18:26:30 +02:00
|
|
|
DEFAULT_FONT_SIZE,
|
2020-06-25 21:21:27 +02:00
|
|
|
DEFAULT_TEXT_ALIGN,
|
2021-05-30 15:31:12 +01:00
|
|
|
EXPORT_SCALES,
|
2021-10-14 14:15:57 +05:30
|
|
|
THEME,
|
2020-06-25 21:21:27 +02:00
|
|
|
} from "./constants";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { t } from "./i18n";
|
2021-01-31 10:47:43 +01:00
|
|
|
import { AppState, NormalizedZoomValue } from "./types";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { getDateTime } from "./utils";
|
2020-01-11 20:34:21 -08:00
|
|
|
|
2021-05-30 15:31:12 +01:00
|
|
|
const defaultExportScale = EXPORT_SCALES.includes(devicePixelRatio)
|
|
|
|
? devicePixelRatio
|
|
|
|
: 1;
|
|
|
|
|
2020-07-27 17:18:49 +05:30
|
|
|
export const getDefaultAppState = (): Omit<
|
|
|
|
AppState,
|
2021-04-04 15:05:16 +05:30
|
|
|
"offsetTop" | "offsetLeft" | "width" | "height"
|
2020-07-27 17:18:49 +05:30
|
|
|
> => {
|
2020-01-11 20:34:21 -08:00
|
|
|
return {
|
2021-10-14 14:15:57 +05:30
|
|
|
theme: THEME.LIGHT,
|
2020-12-27 18:26:30 +02:00
|
|
|
collaborators: new Map(),
|
|
|
|
currentChartType: "bar",
|
2020-01-11 20:34:21 -08:00
|
|
|
currentItemBackgroundColor: "transparent",
|
2020-12-27 18:26:30 +02:00
|
|
|
currentItemEndArrowhead: "arrow",
|
2020-01-25 18:58:57 +01:00
|
|
|
currentItemFillStyle: "hachure",
|
2020-05-27 15:14:50 +02:00
|
|
|
currentItemFontFamily: DEFAULT_FONT_FAMILY,
|
2020-12-27 18:26:30 +02:00
|
|
|
currentItemFontSize: DEFAULT_FONT_SIZE,
|
2020-08-15 00:59:43 +09:00
|
|
|
currentItemLinearStrokeSharpness: "round",
|
2020-12-27 18:26:30 +02:00
|
|
|
currentItemOpacity: 100,
|
|
|
|
currentItemRoughness: 1,
|
2020-12-12 16:42:30 +00:00
|
|
|
currentItemStartArrowhead: null,
|
2020-12-27 18:26:30 +02:00
|
|
|
currentItemStrokeColor: oc.black,
|
|
|
|
currentItemStrokeSharpness: "sharp",
|
|
|
|
currentItemStrokeStyle: "solid",
|
|
|
|
currentItemStrokeWidth: 1,
|
|
|
|
currentItemTextAlign: DEFAULT_TEXT_ALIGN,
|
2020-04-04 16:12:19 +01:00
|
|
|
cursorButton: "up",
|
2020-12-27 18:26:30 +02:00
|
|
|
draggingElement: null,
|
|
|
|
editingElement: null,
|
|
|
|
editingGroupId: null,
|
|
|
|
editingLinearElement: null,
|
|
|
|
elementLocked: false,
|
|
|
|
elementType: "selection",
|
2022-02-02 14:31:38 +01:00
|
|
|
penMode: false,
|
|
|
|
penDetected: false,
|
2020-12-27 18:26:30 +02:00
|
|
|
errorMessage: null,
|
|
|
|
exportBackground: true,
|
2021-05-30 15:31:12 +01:00
|
|
|
exportScale: defaultExportScale,
|
2020-12-27 18:26:30 +02:00
|
|
|
exportEmbedScene: false,
|
2021-02-24 19:52:17 +05:30
|
|
|
exportWithDarkMode: false,
|
2020-12-27 18:26:30 +02:00
|
|
|
fileHandle: null,
|
|
|
|
gridSize: null,
|
2020-08-08 21:04:15 -07:00
|
|
|
isBindingEnabled: true,
|
2020-12-27 18:26:30 +02:00
|
|
|
isLibraryOpen: false,
|
|
|
|
isLoading: false,
|
2020-02-03 21:52:21 +04:00
|
|
|
isResizing: false,
|
2020-04-02 17:40:26 +09:00
|
|
|
isRotating: false,
|
2020-02-21 14:34:18 -05:00
|
|
|
lastPointerDownWith: "mouse",
|
2020-12-27 18:26:30 +02:00
|
|
|
multiElement: null,
|
|
|
|
name: `${t("labels.untitled")}-${getDateTime()}`,
|
|
|
|
openMenu: null,
|
2021-05-30 12:05:07 +02:00
|
|
|
openPopup: null,
|
2020-12-27 18:26:30 +02:00
|
|
|
pasteDialog: { shown: false, data: null },
|
2020-06-02 18:41:40 +02:00
|
|
|
previousSelectedElementIds: {},
|
2020-12-27 18:26:30 +02:00
|
|
|
resizingElement: null,
|
|
|
|
scrolledOutside: false,
|
2021-01-31 10:47:43 +01:00
|
|
|
scrollX: 0,
|
|
|
|
scrollY: 0,
|
2020-12-27 18:26:30 +02:00
|
|
|
selectedElementIds: {},
|
|
|
|
selectedGroupIds: {},
|
|
|
|
selectionElement: null,
|
2020-03-28 16:59:36 -07:00
|
|
|
shouldCacheIgnoreZoom: false,
|
2021-01-17 17:46:23 +01:00
|
|
|
showHelpDialog: false,
|
2020-12-27 18:26:30 +02:00
|
|
|
showStats: false,
|
|
|
|
startBoundElement: null,
|
2020-08-08 21:04:15 -07:00
|
|
|
suggestedBindings: [],
|
2021-01-15 20:32:46 +05:30
|
|
|
toastMessage: null,
|
2020-12-27 18:26:30 +02:00
|
|
|
viewBackgroundColor: oc.white,
|
|
|
|
zenModeEnabled: false,
|
2022-01-29 21:12:44 +01:00
|
|
|
zoom: {
|
|
|
|
value: 1 as NormalizedZoomValue,
|
|
|
|
},
|
2021-02-02 02:26:42 +05:30
|
|
|
viewModeEnabled: false,
|
2021-10-21 22:05:48 +02:00
|
|
|
pendingImageElement: null,
|
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;
|
2021-10-21 22:05:48 +02:00
|
|
|
/** server (shareLink/collab/...) */
|
|
|
|
server: boolean;
|
2020-07-11 13:09:40 +02:00
|
|
|
},
|
2021-11-01 15:24:05 +02:00
|
|
|
T extends Record<keyof AppState, Values>,
|
|
|
|
>(config: { [K in keyof T]: K extends keyof AppState ? T[K] : never }) =>
|
|
|
|
config)({
|
2021-10-21 22:05:48 +02:00
|
|
|
theme: { browser: true, export: false, server: false },
|
|
|
|
collaborators: { browser: false, export: false, server: false },
|
|
|
|
currentChartType: { browser: true, export: false, server: false },
|
|
|
|
currentItemBackgroundColor: { browser: true, export: false, server: false },
|
|
|
|
currentItemEndArrowhead: { browser: true, export: false, server: false },
|
|
|
|
currentItemFillStyle: { browser: true, export: false, server: false },
|
|
|
|
currentItemFontFamily: { browser: true, export: false, server: false },
|
|
|
|
currentItemFontSize: { browser: true, export: false, server: false },
|
|
|
|
currentItemLinearStrokeSharpness: {
|
|
|
|
browser: true,
|
|
|
|
export: false,
|
|
|
|
server: false,
|
|
|
|
},
|
|
|
|
currentItemOpacity: { browser: true, export: false, server: false },
|
|
|
|
currentItemRoughness: { browser: true, export: false, server: false },
|
|
|
|
currentItemStartArrowhead: { browser: true, export: false, server: false },
|
|
|
|
currentItemStrokeColor: { browser: true, export: false, server: false },
|
|
|
|
currentItemStrokeSharpness: { browser: true, export: false, server: false },
|
|
|
|
currentItemStrokeStyle: { browser: true, export: false, server: false },
|
|
|
|
currentItemStrokeWidth: { browser: true, export: false, server: false },
|
|
|
|
currentItemTextAlign: { browser: true, export: false, server: false },
|
|
|
|
cursorButton: { browser: true, export: false, server: false },
|
|
|
|
draggingElement: { browser: false, export: false, server: false },
|
|
|
|
editingElement: { browser: false, export: false, server: false },
|
|
|
|
editingGroupId: { browser: true, export: false, server: false },
|
|
|
|
editingLinearElement: { browser: false, export: false, server: false },
|
|
|
|
elementLocked: { browser: true, export: false, server: false },
|
|
|
|
elementType: { browser: true, export: false, server: false },
|
2022-02-02 14:31:38 +01:00
|
|
|
penMode: { browser: false, export: false, server: false },
|
|
|
|
penDetected: { browser: false, export: false, server: false },
|
2021-10-21 22:05:48 +02:00
|
|
|
errorMessage: { browser: false, export: false, server: false },
|
|
|
|
exportBackground: { browser: true, export: false, server: false },
|
|
|
|
exportEmbedScene: { browser: true, export: false, server: false },
|
|
|
|
exportScale: { browser: true, export: false, server: false },
|
|
|
|
exportWithDarkMode: { browser: true, export: false, server: false },
|
|
|
|
fileHandle: { browser: false, export: false, server: false },
|
|
|
|
gridSize: { browser: true, export: true, server: true },
|
|
|
|
height: { browser: false, export: false, server: false },
|
|
|
|
isBindingEnabled: { browser: false, export: false, server: false },
|
|
|
|
isLibraryOpen: { browser: false, export: false, server: false },
|
|
|
|
isLoading: { browser: false, export: false, server: false },
|
|
|
|
isResizing: { browser: false, export: false, server: false },
|
|
|
|
isRotating: { browser: false, export: false, server: false },
|
|
|
|
lastPointerDownWith: { browser: true, export: false, server: false },
|
|
|
|
multiElement: { browser: false, export: false, server: false },
|
|
|
|
name: { browser: true, export: false, server: false },
|
|
|
|
offsetLeft: { browser: false, export: false, server: false },
|
|
|
|
offsetTop: { browser: false, export: false, server: false },
|
|
|
|
openMenu: { browser: true, export: false, server: false },
|
|
|
|
openPopup: { browser: false, export: false, server: false },
|
|
|
|
pasteDialog: { browser: false, export: false, server: false },
|
|
|
|
previousSelectedElementIds: { browser: true, export: false, server: false },
|
|
|
|
resizingElement: { browser: false, export: false, server: false },
|
|
|
|
scrolledOutside: { browser: true, export: false, server: false },
|
|
|
|
scrollX: { browser: true, export: false, server: false },
|
|
|
|
scrollY: { browser: true, export: false, server: false },
|
|
|
|
selectedElementIds: { browser: true, export: false, server: false },
|
|
|
|
selectedGroupIds: { browser: true, export: false, server: false },
|
|
|
|
selectionElement: { browser: false, export: false, server: false },
|
|
|
|
shouldCacheIgnoreZoom: { browser: true, export: false, server: false },
|
|
|
|
showHelpDialog: { browser: false, export: false, server: false },
|
|
|
|
showStats: { browser: true, export: false, server: false },
|
|
|
|
startBoundElement: { browser: false, export: false, server: false },
|
|
|
|
suggestedBindings: { browser: false, export: false, server: false },
|
|
|
|
toastMessage: { browser: false, export: false, server: false },
|
|
|
|
viewBackgroundColor: { browser: true, export: true, server: true },
|
|
|
|
width: { browser: false, export: false, server: false },
|
|
|
|
zenModeEnabled: { browser: true, export: false, server: false },
|
|
|
|
zoom: { browser: true, export: false, server: false },
|
|
|
|
viewModeEnabled: { browser: false, export: false, server: false },
|
|
|
|
pendingImageElement: { browser: false, export: false, server: false },
|
2020-07-11 13:09:40 +02:00
|
|
|
});
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
const _clearAppStateForStorage = <
|
2021-11-01 15:24:05 +02:00
|
|
|
ExportType extends "export" | "browser" | "server",
|
2021-10-21 22:05:48 +02:00
|
|
|
>(
|
2020-07-11 13:09:40 +02:00
|
|
|
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?.[exportType]) {
|
2021-10-21 22:05:48 +02:00
|
|
|
const nextValue = appState[key];
|
|
|
|
|
|
|
|
// https://github.com/microsoft/TypeScript/issues/31445
|
|
|
|
(stateForExport as any)[key] = nextValue;
|
2020-07-11 13:09:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
};
|
2021-10-21 22:05:48 +02:00
|
|
|
|
|
|
|
export const clearAppStateForDatabase = (appState: Partial<AppState>) => {
|
|
|
|
return _clearAppStateForStorage(appState, "server");
|
|
|
|
};
|