Simplify per review comments.

This commit is contained in:
Daniel J. Geiger 2023-01-26 19:31:09 -06:00
parent e6fb7e3016
commit bb96f322c6
7 changed files with 28 additions and 52 deletions

View File

@ -1,14 +1,8 @@
import { Action, isActionName } from "./types"; import { Action } from "./types";
let actions: readonly Action[] = []; export let actions: readonly Action[] = [];
let customActions: readonly Action[] = [];
export const getCustomActions = () => customActions;
export const getActions = () => actions;
export const register = <T extends Action>(action: T) => { export const register = <T extends Action>(action: T) => {
if (!isActionName(action.name)) {
customActions = customActions.concat(action);
}
actions = actions.concat(action); actions = actions.concat(action);
return action as T & { return action as T & {
keyTest?: unknown extends T["keyTest"] ? never : T["keyTest"]; keyTest?: unknown extends T["keyTest"] ? never : T["keyTest"];

View File

@ -212,14 +212,12 @@ export const ShapesSwitcher = ({
setAppState, setAppState,
onImageAction, onImageAction,
appState, appState,
onContextMenu,
}: { }: {
canvas: HTMLCanvasElement | null; canvas: HTMLCanvasElement | null;
activeTool: AppState["activeTool"]; activeTool: AppState["activeTool"];
setAppState: React.Component<any, AppState>["setState"]; setAppState: React.Component<any, AppState>["setState"];
onImageAction: (data: { pointerType: PointerType | null }) => void; onImageAction: (data: { pointerType: PointerType | null }) => void;
appState: AppState; appState: AppState;
onContextMenu?: (event: React.MouseEvent, source: string) => void;
}) => ( }) => (
<> <>
{SHAPES.map(({ value, icon, key, numericKey, fillable }, index) => { {SHAPES.map(({ value, icon, key, numericKey, fillable }, index) => {
@ -270,9 +268,6 @@ export const ShapesSwitcher = ({
onImageAction({ pointerType }); onImageAction({ pointerType });
} }
}} }}
onContextMenu={(event, source) => {
onContextMenu && onContextMenu(event, source);
}}
/> />
); );
})} })}

View File

@ -38,7 +38,7 @@ import {
} from "../actions"; } from "../actions";
import { createRedoAction, createUndoAction } from "../actions/actionHistory"; import { createRedoAction, createUndoAction } from "../actions/actionHistory";
import { ActionManager } from "../actions/manager"; import { ActionManager } from "../actions/manager";
import { getActions } from "../actions/register"; import { actions } from "../actions/register";
import { ActionResult } from "../actions/types"; import { ActionResult } from "../actions/types";
import { trackEvent } from "../analytics"; import { trackEvent } from "../analytics";
import { import {
@ -479,7 +479,7 @@ class App extends React.Component<AppProps, AppState> {
onSceneUpdated: this.onSceneUpdated, onSceneUpdated: this.onSceneUpdated,
}); });
this.history = new History(); this.history = new History();
this.actionManager.registerAll(getActions()); this.actionManager.registerAll(actions);
this.actionManager.registerActionGuards(); this.actionManager.registerActionGuards();
this.actionManager.registerAction(createUndoAction(this.history)); this.actionManager.registerAction(createUndoAction(this.history));
@ -595,7 +595,6 @@ class App extends React.Component<AppProps, AppState> {
renderTopRightUI={renderTopRightUI} renderTopRightUI={renderTopRightUI}
renderCustomStats={renderCustomStats} renderCustomStats={renderCustomStats}
renderCustomSidebar={this.props.renderSidebar} renderCustomSidebar={this.props.renderSidebar}
onContextMenu={this.handleCustomContextMenu}
showExitZenModeBtn={ showExitZenModeBtn={
typeof this.props?.zenModeEnabled === "undefined" && typeof this.props?.zenModeEnabled === "undefined" &&
this.state.zenModeEnabled this.state.zenModeEnabled
@ -5994,28 +5993,6 @@ class App extends React.Component<AppProps, AppState> {
} }
}; };
private handleCustomContextMenu = (
event: React.MouseEvent,
source: string,
) => {
event.preventDefault();
const container = this.excalidrawContainerRef.current!;
const { top: offsetTop, left: offsetLeft } =
container.getBoundingClientRect();
const left = event.clientX - offsetLeft;
const top = event.clientY - offsetTop;
this.setState({}, () => {
this.setState({
contextMenu: {
top,
left,
items: this.getContextMenuItems("custom", source),
},
});
});
};
private handleCanvasContextMenu = ( private handleCanvasContextMenu = (
event: React.PointerEvent<HTMLCanvasElement>, event: React.PointerEvent<HTMLCanvasElement>,
) => { ) => {

View File

@ -79,7 +79,6 @@ interface LayerUIProps {
onImageAction: (data: { insertOnCanvasDirectly: boolean }) => void; onImageAction: (data: { insertOnCanvasDirectly: boolean }) => void;
renderWelcomeScreen: boolean; renderWelcomeScreen: boolean;
children?: React.ReactNode; children?: React.ReactNode;
onContextMenu?: (event: React.MouseEvent, source: string) => void;
} }
const LayerUI = ({ const LayerUI = ({
@ -106,7 +105,6 @@ const LayerUI = ({
onImageAction, onImageAction,
renderWelcomeScreen, renderWelcomeScreen,
children, children,
onContextMenu,
}: LayerUIProps) => { }: LayerUIProps) => {
const device = useDevice(); const device = useDevice();
@ -335,7 +333,6 @@ const LayerUI = ({
insertOnCanvasDirectly: pointerType !== "mouse", insertOnCanvasDirectly: pointerType !== "mouse",
}); });
}} }}
onContextMenu={onContextMenu}
/> />
</Stack.Row> </Stack.Row>
</Island> </Island>
@ -431,7 +428,6 @@ const LayerUI = ({
renderSidebars={renderSidebars} renderSidebars={renderSidebars}
device={device} device={device}
renderMenu={renderMenu} renderMenu={renderMenu}
onContextMenu={onContextMenu}
welcomeScreenCenter={WelcomeScreenComponents.Center} welcomeScreenCenter={WelcomeScreenComponents.Center}
/> />
)} )}

View File

@ -46,7 +46,6 @@ type MobileMenuProps = {
renderSidebars: () => JSX.Element | null; renderSidebars: () => JSX.Element | null;
device: Device; device: Device;
renderMenu: () => React.ReactNode; renderMenu: () => React.ReactNode;
onContextMenu?: (event: React.MouseEvent, source: string) => void;
welcomeScreenCenter: UIWelcomeScreenComponents["Center"]; welcomeScreenCenter: UIWelcomeScreenComponents["Center"];
}; };
@ -65,7 +64,6 @@ export const MobileMenu = ({
renderSidebars, renderSidebars,
device, device,
renderMenu, renderMenu,
onContextMenu,
welcomeScreenCenter, welcomeScreenCenter,
}: MobileMenuProps) => { }: MobileMenuProps) => {
const renderToolbar = () => { const renderToolbar = () => {
@ -89,7 +87,6 @@ export const MobileMenu = ({
insertOnCanvasDirectly: pointerType !== "mouse", insertOnCanvasDirectly: pointerType !== "mouse",
}); });
}} }}
onContextMenu={onContextMenu}
/> />
</Stack.Row> </Stack.Row>
</Island> </Island>

View File

@ -26,7 +26,6 @@ type ToolButtonBaseProps = {
selected?: boolean; selected?: boolean;
className?: string; className?: string;
isLoading?: boolean; isLoading?: boolean;
onContextMenu?(event: React.MouseEvent, source: string): void;
}; };
type ToolButtonProps = type ToolButtonProps =
@ -158,11 +157,6 @@ export const ToolButton = React.forwardRef((props: ToolButtonProps, ref) => {
lastPointerTypeRef.current = null; lastPointerTypeRef.current = null;
}); });
}} }}
onContextMenu={(event) => {
if (props.onContextMenu !== undefined) {
props.onContextMenu(event, props.name ?? "");
}
}}
> >
<input <input
className={`ToolIcon_type_radio ${sizeCn}`} className={`ToolIcon_type_radio ${sizeCn}`}

View File

@ -31,6 +31,7 @@ import { ImportedLibraryData } from "../../../data/types";
import CustomFooter from "./CustomFooter"; import CustomFooter from "./CustomFooter";
import MobileFooter from "./MobileFooter"; import MobileFooter from "./MobileFooter";
import { Action, EnableFn } from "../../../actions/types"; import { Action, EnableFn } from "../../../actions/types";
import { ContextMenuItems } from "../../../components/ContextMenu";
const exampleAction: Action = { const exampleAction: Action = {
name: "example", name: "example",
@ -39,7 +40,7 @@ const exampleAction: Action = {
return { elements, appState, commitToHistory: false }; return { elements, appState, commitToHistory: false };
}, },
predicate: (elements, appState, appProps, app, data) => predicate: (elements, appState, appProps, app, data) =>
data === undefined || data.source === "editor-current-shape", data === undefined || data.source === "custom",
contextItemLabel: "labels.untitled", contextItemLabel: "labels.untitled",
}; };
const exampleEnableFn: EnableFn = (elements, appState, actionName) => const exampleEnableFn: EnableFn = (elements, appState, actionName) =>
@ -164,6 +165,28 @@ export default function App() {
}} }}
/> />
)} )}
<button
onContextMenu={(event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
const offsetLeft = excalidrawAPI?.getAppState().offsetLeft ?? 0;
const offsetTop = excalidrawAPI?.getAppState().offsetTop ?? 0;
const left = event.clientX - offsetLeft;
const top = event.clientY - offsetTop;
const items: ContextMenuItems = [];
excalidrawAPI?.actionManager
.getCustomActions({ data: { source: "custom" } })
.forEach((action) => items.push(action));
excalidrawAPI?.updateScene({
appState: {
contextMenu: { top, left, items },
},
});
}}
>
{" "}
Context Menu{" "}
</button>
<button <button
onClick={() => alert("This is dummy top right UI")} onClick={() => alert("This is dummy top right UI")}
style={{ height: "2.5rem" }} style={{ height: "2.5rem" }}