convert customElementsConfig into an object

This commit is contained in:
ad1992 2022-05-04 14:26:54 +05:30
parent c93d8f4bd0
commit c4b951a0c5
9 changed files with 39 additions and 74 deletions

View File

@ -224,7 +224,6 @@ import {
withBatchedUpdatesThrottled, withBatchedUpdatesThrottled,
updateObject, updateObject,
setEraserCursor, setEraserCursor,
getCustomElementConfig,
} from "../utils"; } from "../utils";
import ContextMenu, { ContextMenuOption } from "./ContextMenu"; import ContextMenu, { ContextMenuOption } from "./ContextMenu";
import LayerUI from "./LayerUI"; import LayerUI from "./LayerUI";
@ -426,10 +425,9 @@ class App extends React.Component<AppProps, AppState> {
if (this.state.activeTool.type !== "custom") { if (this.state.activeTool.type !== "custom") {
return; return;
} }
const config = getCustomElementConfig( const config =
this.props.customElementsConfig, this.props.customElementsConfig?.[this.state.activeTool.customType];
this.state.activeTool.customType,
);
if (!config) { if (!config) {
return; return;
} }
@ -462,10 +460,9 @@ class App extends React.Component<AppProps, AppState> {
...this.scene.getElementsIncludingDeleted(), ...this.scene.getElementsIncludingDeleted(),
customElement, customElement,
]); ]);
const customElementConfig = getCustomElementConfig( const customElementConfig =
this.props.customElementsConfig, this.props.customElementsConfig?.[customElement.customType];
customElement.customType,
);
if (customElementConfig && customElementConfig.onCreate) { if (customElementConfig && customElementConfig.onCreate) {
customElementConfig.onCreate(customElement); customElementConfig.onCreate(customElement);
} }
@ -2898,10 +2895,9 @@ class App extends React.Component<AppProps, AppState> {
!hitElement?.locked !hitElement?.locked
) { ) {
if (hitElement && isCustomElement(hitElement)) { if (hitElement && isCustomElement(hitElement)) {
const config = getCustomElementConfig( const config =
this.props.customElementsConfig, this.props.customElementsConfig?.[hitElement.customType];
hitElement.customType,
);
if (!config?.transformHandles) { if (!config?.transformHandles) {
return; return;
} }
@ -5465,10 +5461,8 @@ class App extends React.Component<AppProps, AppState> {
let disableContextMenu = false; let disableContextMenu = false;
if (element && isCustomElement(element)) { if (element && isCustomElement(element)) {
const config = getCustomElementConfig( const config = this.props.customElementsConfig?.[element.customType];
this.props.customElementsConfig,
element.customType,
);
disableContextMenu = !!config?.disableContextMenu; disableContextMenu = !!config?.disableContextMenu;
} }
if (disableContextMenu) { if (disableContextMenu) {

View File

@ -205,8 +205,8 @@ export default function App() {
}; };
const getCustomElementsConfig = () => { const getCustomElementsConfig = () => {
return [ return {
{ star: {
type: "custom", type: "custom",
customType: "star", customType: "star",
displayData: { displayData: {
@ -219,7 +219,7 @@ export default function App() {
height: 60, height: 60,
disableContextMenu: true, disableContextMenu: true,
}, },
{ comment: {
type: "custom", type: "custom",
customType: "comment", customType: "comment",
displayData: { displayData: {
@ -234,7 +234,7 @@ export default function App() {
onCreate, onCreate,
disableContextMenu: true, disableContextMenu: true,
}, },
{ thumbsup: {
type: "custom", type: "custom",
customType: "thumbsup", customType: "thumbsup",
displayData: { displayData: {
@ -260,7 +260,7 @@ export default function App() {
}, },
}, },
}, },
]; };
}; };
const addTextArea = (element) => { const addTextArea = (element) => {

View File

@ -52,11 +52,10 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
...canvasActions, ...canvasActions,
}, },
}; };
const customElementsConfig: AppProps["customElementsConfig"] = const customElementsConfig = {} as AppProps["customElementsConfig"];
props.customElementsConfig?.map((customElementConfig) => ({ Object.entries(props.customElementsConfig || {}).forEach(([key, value]) => {
...DEFAULT_CUSTOM_ELEMENT_CONFIG, customElementsConfig![key] = { ...DEFAULT_CUSTOM_ELEMENT_CONFIG, ...value };
...customElementConfig, });
}));
if (canvasActions?.export) { if (canvasActions?.export) {
UIOptions.canvasActions.export.saveFileToDisk = UIOptions.canvasActions.export.saveFileToDisk =

View File

@ -25,13 +25,7 @@ import { RoughSVG } from "roughjs/bin/svg";
import { RoughGenerator } from "roughjs/bin/generator"; import { RoughGenerator } from "roughjs/bin/generator";
import { RenderConfig } from "../scene/types"; import { RenderConfig } from "../scene/types";
import { import { distance, getFontString, getFontFamilyString, isRTL } from "../utils";
distance,
getFontString,
getFontFamilyString,
isRTL,
getCustomElementConfig,
} from "../utils";
import { isPathALoop } from "../math"; import { isPathALoop } from "../math";
import rough from "roughjs/bin/rough"; import rough from "roughjs/bin/rough";
import { AppState, BinaryFiles, Zoom } from "../types"; import { AppState, BinaryFiles, Zoom } from "../types";
@ -262,10 +256,8 @@ const drawElementOnCanvas = (
} }
case "custom": { case "custom": {
const config = getCustomElementConfig( const config = renderConfig.customElementsConfig?.[element.customType];
renderConfig.customElementsConfig,
element.customType,
);
if (!config) { if (!config) {
break; break;
} }

View File

@ -2,7 +2,7 @@ import { RoughCanvas } from "roughjs/bin/canvas";
import { RoughSVG } from "roughjs/bin/svg"; import { RoughSVG } from "roughjs/bin/svg";
import oc from "open-color"; import oc from "open-color";
import { AppState, BinaryFiles, CustomElementConfig, Zoom } from "../types"; import { AppState, BinaryFiles, Zoom } from "../types";
import { import {
ExcalidrawElement, ExcalidrawElement,
NonDeletedExcalidrawElement, NonDeletedExcalidrawElement,
@ -47,11 +47,7 @@ import {
TransformHandles, TransformHandles,
TransformHandleType, TransformHandleType,
} from "../element/transformHandles"; } from "../element/transformHandles";
import { import { viewportCoordsToSceneCoords, supportsEmoji } from "../utils";
viewportCoordsToSceneCoords,
supportsEmoji,
getCustomElementConfig,
} from "../utils";
import { UserIdleState } from "../types"; import { UserIdleState } from "../types";
import { THEME_FILTER } from "../constants"; import { THEME_FILTER } from "../constants";
import { import {
@ -309,16 +305,13 @@ export const renderScene = (
) { ) {
const selections = elements.reduce((acc, element) => { const selections = elements.reduce((acc, element) => {
const isCustom = element.type === "custom"; const isCustom = element.type === "custom";
let config: CustomElementConfig; let config;
const selectionColors = []; const selectionColors = [];
if (element.type === "custom") { if (element.type === "custom") {
config = getCustomElementConfig( config = renderConfig.customElementsConfig?.[element.customType];
renderConfig.customElementsConfig,
element.customType,
)!;
} }
if (!isCustom || (isCustom && config!.transformHandles)) { if (!isCustom || (isCustom && config && config.transformHandles)) {
// local user // local user
if ( if (
appState.selectedElementIds[element.id] && appState.selectedElementIds[element.id] &&
@ -387,10 +380,11 @@ export const renderScene = (
if (locallySelectedElements.length === 1) { if (locallySelectedElements.length === 1) {
let showTransformHandles = true; let showTransformHandles = true;
if (locallySelectedElements[0].type === "custom") { if (locallySelectedElements[0].type === "custom") {
const config = getCustomElementConfig( const config =
renderConfig.customElementsConfig, renderConfig.customElementsConfig?.[
locallySelectedElements[0].customType, locallySelectedElements[0].customType
); ];
if (!config || !config.transformHandles) { if (!config || !config.transformHandles) {
showTransformHandles = false; showTransformHandles = false;
} }

View File

@ -7,7 +7,6 @@ import { getNonDeletedElements, isNonDeletedElement } from "../element";
import { LinearElementEditor } from "../element/linearElementEditor"; import { LinearElementEditor } from "../element/linearElementEditor";
import App from "../components/App"; import App from "../components/App";
import { isCustomElement } from "../element/typeChecks"; import { isCustomElement } from "../element/typeChecks";
import { getCustomElementConfig } from "../utils";
type ElementIdKey = InstanceType<typeof LinearElementEditor>["elementId"]; type ElementIdKey = InstanceType<typeof LinearElementEditor>["elementId"];
type ElementKey = ExcalidrawElement | ElementIdKey; type ElementKey = ExcalidrawElement | ElementIdKey;
@ -104,10 +103,9 @@ class Scene {
const elementsToBeStackedOnTop: ExcalidrawElement[] = []; const elementsToBeStackedOnTop: ExcalidrawElement[] = [];
nextElements.forEach((element) => { nextElements.forEach((element) => {
if (isCustomElement(element)) { if (isCustomElement(element)) {
const config = getCustomElementConfig( const config =
this.app.props.customElementsConfig, this.app.props.customElementsConfig?.[element.customType];
element.customType,
);
if (config?.stackedOnTop) { if (config?.stackedOnTop) {
elementsToBeStackedOnTop.push(element); elementsToBeStackedOnTop.push(element);
} else { } else {

View File

@ -5,7 +5,6 @@ import {
import { getElementAbsoluteCoords, getElementBounds } from "../element"; import { getElementAbsoluteCoords, getElementBounds } from "../element";
import { AppProps, AppState } from "../types"; import { AppProps, AppState } from "../types";
import { isBoundToContainer } from "../element/typeChecks"; import { isBoundToContainer } from "../element/typeChecks";
import { getCustomElementConfig } from "../utils";
export const getElementsWithinSelection = ( export const getElementsWithinSelection = (
elements: readonly NonDeletedExcalidrawElement[], elements: readonly NonDeletedExcalidrawElement[],
@ -19,8 +18,7 @@ export const getElementsWithinSelection = (
getElementBounds(element); getElementBounds(element);
const isCustom = element.type === "custom"; const isCustom = element.type === "custom";
const allowSelection = isCustom const allowSelection = isCustom
? getCustomElementConfig(customElementConfig, element.customType) ? customElementConfig?.[element.customType]?.transformHandles
?.transformHandles
: true; : true;
return ( return (
allowSelection && allowSelection &&

View File

@ -303,7 +303,7 @@ export interface ExcalidrawProps {
}>, }>,
) => void; ) => void;
renderCustomElementWidget?: (appState: AppState) => void; renderCustomElementWidget?: (appState: AppState) => void;
customElementsConfig?: CustomElementConfig[]; customElementsConfig?: Record<string, CustomElementConfig>;
onElementClick?: ( onElementClick?: (
element: NonDeleted<ExcalidrawElement>, element: NonDeleted<ExcalidrawElement>,
event: React.PointerEvent<HTMLCanvasElement>, event: React.PointerEvent<HTMLCanvasElement>,

View File

@ -11,7 +11,7 @@ import {
WINDOWS_EMOJI_FALLBACK_FONT, WINDOWS_EMOJI_FALLBACK_FONT,
} from "./constants"; } from "./constants";
import { FontFamilyValues, FontString } from "./element/types"; import { FontFamilyValues, FontString } from "./element/types";
import { AppProps, AppState, DataURL, Zoom } from "./types"; import { AppState, DataURL, Zoom } from "./types";
import { unstable_batchedUpdates } from "react-dom"; import { unstable_batchedUpdates } from "react-dom";
import { isDarwin } from "./keys"; import { isDarwin } from "./keys";
@ -629,16 +629,6 @@ export const getFrame = () => {
} }
}; };
export const getCustomElementConfig = (
customElementConfig: AppProps["customElementsConfig"],
customType: string,
) => {
if (!customElementConfig) {
return null;
}
return customElementConfig.find((config) => config.customType === customType);
};
export const isPromiseLike = ( export const isPromiseLike = (
value: any, value: any,
): value is Promise<ResolutionType<typeof value>> => { ): value is Promise<ResolutionType<typeof value>> => {