fix: apply initialData appState for zenmode and grid stats and refactor check param for actions (#2871)
* fix: pass default value for grid mode / zen mode so it sets the value from initialData appState fixes #2870 * change checked from boolean to be a function which recieves appState and returns boolean * fix * use clsx Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
parent
e8685c5236
commit
4624ec2bd6
@ -1,21 +1,20 @@
|
|||||||
import { CODES, KEYS } from "../keys";
|
import { CODES, KEYS } from "../keys";
|
||||||
import { register } from "./register";
|
import { register } from "./register";
|
||||||
import { GRID_SIZE } from "../constants";
|
import { GRID_SIZE } from "../constants";
|
||||||
|
import { AppState } from "../types";
|
||||||
|
|
||||||
export const actionToggleGridMode = register({
|
export const actionToggleGridMode = register({
|
||||||
name: "gridMode",
|
name: "gridMode",
|
||||||
perform(elements, appState) {
|
perform(elements, appState) {
|
||||||
this.checked = !this.checked;
|
|
||||||
return {
|
return {
|
||||||
appState: {
|
appState: {
|
||||||
...appState,
|
...appState,
|
||||||
gridSize: this.checked ? GRID_SIZE : null,
|
gridSize: this.checked!(appState) ? null : GRID_SIZE,
|
||||||
},
|
},
|
||||||
commitToHistory: false,
|
commitToHistory: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
checked: false,
|
checked: (appState: AppState) => appState.gridSize !== null,
|
||||||
contextItemLabel: "labels.gridMode",
|
contextItemLabel: "labels.gridMode",
|
||||||
// Wrong event code
|
|
||||||
keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.QUOTE,
|
keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.QUOTE,
|
||||||
});
|
});
|
||||||
|
@ -3,15 +3,14 @@ import { register } from "./register";
|
|||||||
export const actionToggleStats = register({
|
export const actionToggleStats = register({
|
||||||
name: "stats",
|
name: "stats",
|
||||||
perform(elements, appState) {
|
perform(elements, appState) {
|
||||||
this.checked = !this.checked;
|
|
||||||
return {
|
return {
|
||||||
appState: {
|
appState: {
|
||||||
...appState,
|
...appState,
|
||||||
showStats: !appState.showStats,
|
showStats: !this.checked!(appState),
|
||||||
},
|
},
|
||||||
commitToHistory: false,
|
commitToHistory: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
checked: false,
|
checked: (appState) => appState.showStats,
|
||||||
contextItemLabel: "stats.title",
|
contextItemLabel: "stats.title",
|
||||||
});
|
});
|
||||||
|
@ -4,17 +4,16 @@ import { register } from "./register";
|
|||||||
export const actionToggleZenMode = register({
|
export const actionToggleZenMode = register({
|
||||||
name: "zenMode",
|
name: "zenMode",
|
||||||
perform(elements, appState) {
|
perform(elements, appState) {
|
||||||
this.checked = !this.checked;
|
|
||||||
return {
|
return {
|
||||||
appState: {
|
appState: {
|
||||||
...appState,
|
...appState,
|
||||||
zenModeEnabled: this.checked,
|
zenModeEnabled: !this.checked!(appState),
|
||||||
},
|
},
|
||||||
commitToHistory: false,
|
commitToHistory: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
checked: false,
|
checked: (appState) => appState.zenModeEnabled,
|
||||||
contextItemLabel: "buttons.zenMode",
|
contextItemLabel: "buttons.zenMode",
|
||||||
// Wrong event code
|
keyTest: (event) =>
|
||||||
keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.QUOTE,
|
!event[KEYS.CTRL_OR_CMD] && event.altKey && event.code === CODES.Z,
|
||||||
});
|
});
|
||||||
|
@ -106,7 +106,7 @@ export interface Action {
|
|||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
) => boolean;
|
) => boolean;
|
||||||
checked?: boolean;
|
checked?: (appState: Readonly<AppState>) => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActionsManagerInterface {
|
export interface ActionsManagerInterface {
|
||||||
|
@ -1254,7 +1254,6 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
if (!event[KEYS.CTRL_OR_CMD] && event.altKey && event.code === CODES.Z) {
|
if (!event[KEYS.CTRL_OR_CMD] && event.altKey && event.code === CODES.Z) {
|
||||||
this.toggleZenMode();
|
this.toggleZenMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event[KEYS.CTRL_OR_CMD] && event.code === CODES.QUOTE) {
|
if (event[KEYS.CTRL_OR_CMD] && event.code === CODES.QUOTE) {
|
||||||
this.toggleGridMode();
|
this.toggleGridMode();
|
||||||
}
|
}
|
||||||
@ -3663,6 +3662,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
top: clientY,
|
top: clientY,
|
||||||
left: clientX,
|
left: clientX,
|
||||||
actionManager: this.actionManager,
|
actionManager: this.actionManager,
|
||||||
|
appState: this.state,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -3709,6 +3709,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
top: clientY,
|
top: clientY,
|
||||||
left: clientX,
|
left: clientX,
|
||||||
actionManager: this.actionManager,
|
actionManager: this.actionManager,
|
||||||
|
appState: this.state,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import {
|
|||||||
} from "../actions/shortcuts";
|
} from "../actions/shortcuts";
|
||||||
import { Action } from "../actions/types";
|
import { Action } from "../actions/types";
|
||||||
import { ActionManager } from "../actions/manager";
|
import { ActionManager } from "../actions/manager";
|
||||||
|
import { AppState } from "../types";
|
||||||
|
|
||||||
type ContextMenuOption = "separator" | Action;
|
type ContextMenuOption = "separator" | Action;
|
||||||
|
|
||||||
@ -20,6 +21,7 @@ type ContextMenuProps = {
|
|||||||
top: number;
|
top: number;
|
||||||
left: number;
|
left: number;
|
||||||
actionManager: ActionManager;
|
actionManager: ActionManager;
|
||||||
|
appState: Readonly<AppState>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ContextMenu = ({
|
const ContextMenu = ({
|
||||||
@ -28,11 +30,11 @@ const ContextMenu = ({
|
|||||||
top,
|
top,
|
||||||
left,
|
left,
|
||||||
actionManager,
|
actionManager,
|
||||||
|
appState,
|
||||||
}: ContextMenuProps) => {
|
}: ContextMenuProps) => {
|
||||||
const isDarkTheme = !!document
|
const isDarkTheme = !!document
|
||||||
.querySelector(".excalidraw")
|
.querySelector(".excalidraw")
|
||||||
?.classList.contains("Appearance_dark");
|
?.classList.contains("Appearance_dark");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={clsx("excalidraw", {
|
className={clsx("excalidraw", {
|
||||||
@ -61,9 +63,10 @@ const ContextMenu = ({
|
|||||||
return (
|
return (
|
||||||
<li key={idx} data-testid={actionName} onClick={onCloseRequest}>
|
<li key={idx} data-testid={actionName} onClick={onCloseRequest}>
|
||||||
<button
|
<button
|
||||||
className={`context-menu-option
|
className={clsx("context-menu-option", {
|
||||||
${actionName === "deleteSelectedElements" ? "dangerous" : ""}
|
dangerous: actionName === "deleteSelectedElements",
|
||||||
${option.checked ? "checkmark" : ""}`}
|
checkmark: option.checked?.(appState),
|
||||||
|
})}
|
||||||
onClick={() => actionManager.executeAction(option)}
|
onClick={() => actionManager.executeAction(option)}
|
||||||
>
|
>
|
||||||
<div className="context-menu-option__label">{label}</div>
|
<div className="context-menu-option__label">{label}</div>
|
||||||
@ -97,6 +100,7 @@ type ContextMenuParams = {
|
|||||||
top: ContextMenuProps["top"];
|
top: ContextMenuProps["top"];
|
||||||
left: ContextMenuProps["left"];
|
left: ContextMenuProps["left"];
|
||||||
actionManager: ContextMenuProps["actionManager"];
|
actionManager: ContextMenuProps["actionManager"];
|
||||||
|
appState: Readonly<AppState>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
@ -119,6 +123,7 @@ export default {
|
|||||||
options={options}
|
options={options}
|
||||||
onCloseRequest={handleClose}
|
onCloseRequest={handleClose}
|
||||||
actionManager={params.actionManager}
|
actionManager={params.actionManager}
|
||||||
|
appState={params.appState}
|
||||||
/>,
|
/>,
|
||||||
getContextMenuNode(),
|
getContextMenuNode(),
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user