feat: support contextMenuLabel to be of function type to support dynmaic labels (#4654)
This commit is contained in:
parent
cbc6bd1ad8
commit
18c526d877
@ -123,7 +123,12 @@ export interface Action {
|
|||||||
appState: AppState,
|
appState: AppState,
|
||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
) => boolean;
|
) => boolean;
|
||||||
contextItemLabel?: string;
|
contextItemLabel?:
|
||||||
|
| string
|
||||||
|
| ((
|
||||||
|
elements: readonly ExcalidrawElement[],
|
||||||
|
appState: Readonly<AppState>,
|
||||||
|
) => string);
|
||||||
contextItemPredicate?: (
|
contextItemPredicate?: (
|
||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
|
@ -4974,6 +4974,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
actionManager: this.actionManager,
|
actionManager: this.actionManager,
|
||||||
appState: this.state,
|
appState: this.state,
|
||||||
container: this.excalidrawContainerRef.current!,
|
container: this.excalidrawContainerRef.current!,
|
||||||
|
elements,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
ContextMenu.push({
|
ContextMenu.push({
|
||||||
@ -5014,6 +5015,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
actionManager: this.actionManager,
|
actionManager: this.actionManager,
|
||||||
appState: this.state,
|
appState: this.state,
|
||||||
container: this.excalidrawContainerRef.current!,
|
container: this.excalidrawContainerRef.current!,
|
||||||
|
elements,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (type === "element") {
|
} else if (type === "element") {
|
||||||
@ -5025,6 +5027,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
actionManager: this.actionManager,
|
actionManager: this.actionManager,
|
||||||
appState: this.state,
|
appState: this.state,
|
||||||
container: this.excalidrawContainerRef.current!,
|
container: this.excalidrawContainerRef.current!,
|
||||||
|
elements,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
ContextMenu.push({
|
ContextMenu.push({
|
||||||
@ -5069,6 +5072,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
actionManager: this.actionManager,
|
actionManager: this.actionManager,
|
||||||
appState: this.state,
|
appState: this.state,
|
||||||
container: this.excalidrawContainerRef.current!,
|
container: this.excalidrawContainerRef.current!,
|
||||||
|
elements,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import {
|
|||||||
import { Action } from "../actions/types";
|
import { Action } from "../actions/types";
|
||||||
import { ActionManager } from "../actions/manager";
|
import { ActionManager } from "../actions/manager";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
||||||
|
|
||||||
export type ContextMenuOption = "separator" | Action;
|
export type ContextMenuOption = "separator" | Action;
|
||||||
|
|
||||||
@ -21,6 +22,7 @@ type ContextMenuProps = {
|
|||||||
left: number;
|
left: number;
|
||||||
actionManager: ActionManager;
|
actionManager: ActionManager;
|
||||||
appState: Readonly<AppState>;
|
appState: Readonly<AppState>;
|
||||||
|
elements: readonly NonDeletedExcalidrawElement[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const ContextMenu = ({
|
const ContextMenu = ({
|
||||||
@ -30,6 +32,7 @@ const ContextMenu = ({
|
|||||||
left,
|
left,
|
||||||
actionManager,
|
actionManager,
|
||||||
appState,
|
appState,
|
||||||
|
elements,
|
||||||
}: ContextMenuProps) => {
|
}: ContextMenuProps) => {
|
||||||
return (
|
return (
|
||||||
<Popover
|
<Popover
|
||||||
@ -52,9 +55,14 @@ const ContextMenu = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const actionName = option.name;
|
const actionName = option.name;
|
||||||
const label = option.contextItemLabel
|
let label = "";
|
||||||
? t(option.contextItemLabel)
|
if (option.contextItemLabel) {
|
||||||
: "";
|
if (typeof option.contextItemLabel === "function") {
|
||||||
|
label = t(option.contextItemLabel(elements, appState));
|
||||||
|
} else {
|
||||||
|
label = t(option.contextItemLabel);
|
||||||
|
}
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<li key={idx} data-testid={actionName} onClick={onCloseRequest}>
|
<li key={idx} data-testid={actionName} onClick={onCloseRequest}>
|
||||||
<button
|
<button
|
||||||
@ -101,6 +109,7 @@ type ContextMenuParams = {
|
|||||||
actionManager: ContextMenuProps["actionManager"];
|
actionManager: ContextMenuProps["actionManager"];
|
||||||
appState: Readonly<AppState>;
|
appState: Readonly<AppState>;
|
||||||
container: HTMLElement;
|
container: HTMLElement;
|
||||||
|
elements: readonly NonDeletedExcalidrawElement[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleClose = (container: HTMLElement) => {
|
const handleClose = (container: HTMLElement) => {
|
||||||
@ -129,6 +138,7 @@ export default {
|
|||||||
onCloseRequest={() => handleClose(params.container)}
|
onCloseRequest={() => handleClose(params.container)}
|
||||||
actionManager={params.actionManager}
|
actionManager={params.actionManager}
|
||||||
appState={params.appState}
|
appState={params.appState}
|
||||||
|
elements={params.elements}
|
||||||
/>,
|
/>,
|
||||||
getContextMenuNode(params.container),
|
getContextMenuNode(params.container),
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user