initial commit

This commit is contained in:
kbariotis 2021-02-14 23:42:54 +02:00
parent e6cd97c4f2
commit 98a7707e26
8 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,20 @@
import { register } from "./register";
import { AppState } from "../types";
import { trackEvent } from "../analytics";
export const actionToggleAutoSave = register({
name: "toggleAutoSave",
perform(elements, appState) {
trackEvent("toggle", "autoSave");
return {
appState: {
...appState,
autoSave: !appState.autoSave,
},
commitToHistory: false,
};
},
checked: (appState: AppState) => appState.autoSave,
contextItemLabel: "labels.toggleAutoSave",
keyTest: (event) => false,
});

View File

@ -74,6 +74,7 @@ export {
} from "./actionClipboard"; } from "./actionClipboard";
export { actionToggleGridMode } from "./actionToggleGridMode"; export { actionToggleGridMode } from "./actionToggleGridMode";
export { actionToggleAutoSave } from "./actionToggleAutoSave";
export { actionToggleZenMode } from "./actionToggleZenMode"; export { actionToggleZenMode } from "./actionToggleZenMode";
export { actionToggleStats } from "./actionToggleStats"; export { actionToggleStats } from "./actionToggleStats";

View File

@ -48,6 +48,7 @@ export type ActionName =
| "changeOpacity" | "changeOpacity"
| "changeFontSize" | "changeFontSize"
| "toggleCanvasMenu" | "toggleCanvasMenu"
| "toggleAutoSave"
| "toggleEditMenu" | "toggleEditMenu"
| "undo" | "undo"
| "redo" | "redo"

View File

@ -13,6 +13,7 @@ export const getDefaultAppState = (): Omit<
"offsetTop" | "offsetLeft" "offsetTop" | "offsetLeft"
> => { > => {
return { return {
autoSave: false,
appearance: "light", appearance: "light",
collaborators: new Map(), collaborators: new Map(),
currentChartType: "bar", currentChartType: "bar",
@ -91,6 +92,7 @@ const APP_STATE_STORAGE_CONF = (<
>( >(
config: { [K in keyof T]: K extends keyof AppState ? T[K] : never }, config: { [K in keyof T]: K extends keyof AppState ? T[K] : never },
) => config)({ ) => config)({
autoSave: { browser: true, export: false },
appearance: { browser: true, export: false }, appearance: { browser: true, export: false },
collaborators: { browser: false, export: false }, collaborators: { browser: false, export: false },
currentChartType: { browser: true, export: false }, currentChartType: { browser: true, export: false },

View File

@ -21,6 +21,7 @@ import {
actionSelectAll, actionSelectAll,
actionSendBackward, actionSendBackward,
actionSendToBack, actionSendToBack,
actionToggleAutoSave,
actionToggleGridMode, actionToggleGridMode,
actionToggleStats, actionToggleStats,
actionToggleZenMode, actionToggleZenMode,
@ -52,13 +53,14 @@ import {
MIME_TYPES, MIME_TYPES,
POINTER_BUTTON, POINTER_BUTTON,
SCROLL_TIMEOUT, SCROLL_TIMEOUT,
AUTO_SAVE_TIMEOUT,
TAP_TWICE_TIMEOUT, TAP_TWICE_TIMEOUT,
TEXT_TO_CENTER_SNAP_THRESHOLD, TEXT_TO_CENTER_SNAP_THRESHOLD,
TOUCH_CTX_MENU_TIMEOUT, TOUCH_CTX_MENU_TIMEOUT,
ZOOM_STEP, ZOOM_STEP,
} from "../constants"; } from "../constants";
import { loadFromBlob } from "../data"; import { loadFromBlob } from "../data";
import { isValidLibrary } from "../data/json"; import { saveAsJSON, isValidLibrary } from "../data/json";
import { Library } from "../data/library"; import { Library } from "../data/library";
import { restore } from "../data/restore"; import { restore } from "../data/restore";
import { import {
@ -879,6 +881,13 @@ class App extends React.Component<ExcalidrawProps, AppState> {
.querySelector(".excalidraw") .querySelector(".excalidraw")
?.classList.toggle("Appearance_dark", this.state.appearance === "dark"); ?.classList.toggle("Appearance_dark", this.state.appearance === "dark");
if (this.state.autoSave) {
this.saveLocalSceneDebounced(
this.scene.getElementsIncludingDeleted(),
this.state,
);
}
if ( if (
this.state.editingLinearElement && this.state.editingLinearElement &&
!this.state.selectedElementIds[this.state.editingLinearElement.elementId] !this.state.selectedElementIds[this.state.editingLinearElement.elementId]
@ -1004,6 +1013,13 @@ class App extends React.Component<ExcalidrawProps, AppState> {
this.setState({ ...this.getCanvasOffsets() }); this.setState({ ...this.getCanvasOffsets() });
}, SCROLL_TIMEOUT); }, SCROLL_TIMEOUT);
private saveLocalSceneDebounced = debounce(
(elements: readonly ExcalidrawElement[], state: AppState) => {
saveAsJSON(elements, state);
},
AUTO_SAVE_TIMEOUT,
);
// Copy/paste // Copy/paste
private onCut = withBatchedUpdates((event: ClipboardEvent) => { private onCut = withBatchedUpdates((event: ClipboardEvent) => {
@ -3716,6 +3732,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
typeof this.props.zenModeEnabled === "undefined" && actionToggleZenMode, typeof this.props.zenModeEnabled === "undefined" && actionToggleZenMode,
typeof this.props.viewModeEnabled === "undefined" && typeof this.props.viewModeEnabled === "undefined" &&
actionToggleViewMode, actionToggleViewMode,
actionToggleAutoSave,
actionToggleStats, actionToggleStats,
]; ];
@ -3762,6 +3779,8 @@ class App extends React.Component<ExcalidrawProps, AppState> {
actionToggleZenMode, actionToggleZenMode,
typeof this.props.viewModeEnabled === "undefined" && typeof this.props.viewModeEnabled === "undefined" &&
actionToggleViewMode, actionToggleViewMode,
actionToggleAutoSave,
separator,
actionToggleStats, actionToggleStats,
], ],
top: clientY, top: clientY,

View File

@ -94,6 +94,7 @@ export const TITLE_TIMEOUT = 10000;
export const TOAST_TIMEOUT = 5000; export const TOAST_TIMEOUT = 5000;
export const VERSION_TIMEOUT = 30000; export const VERSION_TIMEOUT = 30000;
export const SCROLL_TIMEOUT = 500; export const SCROLL_TIMEOUT = 500;
export const AUTO_SAVE_TIMEOUT = 500;
export const ZOOM_STEP = 0.1; export const ZOOM_STEP = 0.1;

View File

@ -78,6 +78,7 @@
"ungroup": "Ungroup selection", "ungroup": "Ungroup selection",
"collaborators": "Collaborators", "collaborators": "Collaborators",
"showGrid": "Show grid", "showGrid": "Show grid",
"toggleAutoSave": "Auto save",
"addToLibrary": "Add to library", "addToLibrary": "Add to library",
"removeFromLibrary": "Remove from library", "removeFromLibrary": "Remove from library",
"libraryLoadingMessage": "Loading library…", "libraryLoadingMessage": "Loading library…",

View File

@ -40,6 +40,7 @@ export type Collaborator = {
}; };
export type AppState = { export type AppState = {
autoSave: boolean;
isLoading: boolean; isLoading: boolean;
errorMessage: string | null; errorMessage: string | null;
draggingElement: NonDeletedExcalidrawElement | null; draggingElement: NonDeletedExcalidrawElement | null;