import { useEffect, useState } from "react" import { ColorPicker, Input, InputNumber, Select } from "antd" import { capitalize } from "../utils/common" import Tools from "./constants/tools.js" /** * * @param {boolean} isOpen * @param {import("./widgets/base.js").Widget} activeWidget * @param {React.Dispatch>} setActiveWidget * @returns */ function CanvasToolBar({isOpen, activeWidget, setActiveWidget}){ const [toolbarOpen, setToolbarOpen] = useState(isOpen) useEffect(() => { setToolbarOpen(isOpen) }, [isOpen]) const handleWidgetNameChange = (e) => { activeWidget?.setWidgetName(e.target.value) // Update widget's internal state const updatedWidget = { ...activeWidget } // Create a shallow copy of the widget setActiveWidget(updatedWidget) // Update the state with the modified widget } const handleChange = (attrPath, value, callback) => { // console.log("Value: ", attrPath, value) activeWidget?.setAttrValue(attrPath, value) // Update widget's internal state const updatedWidget = { ...activeWidget } if (callback){ callback(value) } setActiveWidget(updatedWidget) } const renderWidgets = (obj, parentKey = "") => { return Object.entries(obj).map(([key, val], i) => { // console.log("parent key: ", parentKey) // Build a unique identifier for keys that handle nested structures const keyName = parentKey ? `${parentKey}.${key}` : key // Check if the current value is an object and has a "tool" property if (typeof val === "object" && val.tool) { // Render widgets based on the tool type return (
{ parentKey ?
{val.label}
:
{capitalize(key)}
} { val.tool === Tools.NUMBER_INPUT && ( handleChange(keyName, value, val.onChange)} /> )} { val.tool === Tools.COLOR_PICKER && ( handleChange(keyName, value.toHexString(), val.onChange)} /> )} { val.tool === Tools.SELECT_DROPDOWN && (

{renderWidgets(activeWidget?.state?.attrs || {})}
) } export default CanvasToolBar