import React from "react" import { NotImplementedError } from "../../utils/errors" import Tools from "../constants/tools" import Layouts from "../constants/layouts" import Cursor from "../constants/cursor" import { toSnakeCase } from "../utils/utils" import EditableDiv from "../../components/editableDiv" /** * Base class to be extended */ class Widget extends React.Component{ static widgetType = "widget" constructor(props){ super(props) const {id, widgetName, canvasRef} = props console.log("Id: ", id) // this id has to be unique inside the canvas, it will be set automatically and should never be changed this.__id = id this._zIndex = 0 this.canvas = canvasRef?.current || null // this._selected = false this._disableResize = false this._disableSelection = false this.cursor = Cursor.POINTER this.icon = "" // antd icon name representing this widget this.elementRef = React.createRef() this.attrs = { styling: { backgroundColor: { tool: Tools.COLOR_PICKER, // the tool to display, can be either HTML ELement or a constant string value: "" }, foregroundColor: { tool: Tools.COLOR_PICKER, value: "" }, }, layout: "show", // enables layout use "hide" to hide layout dropdown, takes the layout from this.layout events: { event1: { tool: Tools.EVENT_HANDLER, value: "" } } } this.functions = { "load": {"args1": "number", "args2": "string"} } this.layout = Layouts.PACK this.boundingRect = { x: 0, y: 0, height: 100, width: 100 } this.state = { attrs: { // attributes // replace this with this.props }, zIndex: 0, pos: {x: 0, y: 0}, selected: false, widgetName: widgetName || 'unnamed widget' // this will later be converted to variable name } this.mousePress = this.mousePress.bind(this) this.getElement = this.getElement.bind(this) this.isSelected = this.isSelected.bind(this) this.getPos = this.getPos.bind(this) this.setPos = this.setPos.bind(this) } setComponentAdded(added=true){ // this.elementRef = document.querySelector(`[data-id="${this.__id}"]`) } componentDidMount(){ console.log("mounted: ") this.elementRef.current?.addEventListener("click", this.mousePress) } componentWillUnmount(){ this.elementRef.current?.removeEventListener("click", this.mousePress) } // TODO: add context menu items such as delete, add etc contextMenu(){ } getVariableName(){ return toSnakeCase(this.state.widgetName) } mousePress(event){ // event.preventDefault() if (!this._disableSelection){ // const widgetSelected = new CustomEvent("selection:created", { // detail: { // event, // id: this.__id, // element: this // }, // // bubbles: true // Allow the event to bubble up the DOM tree // }) // this.canvas.dispatchEvent(widgetSelected) } } select(){ this.setState({ selected: true }) console.log("selected") } deSelect(){ this.setState({ selected: false }) console.log("DeSelected") } isSelected(){ return this.state.selected } setPos(x, y){ this.setState({ pos: {x: x, y: y} }) } getPos(){ return this.state.pos } getProps(){ return this.attrs } getWidgetFunctions(){ return this.functions } getId(){ return this.__id } getElement(){ return this.elementRef.current } renderContent(){ // throw new NotImplementedError("render method has to be implemented") return (