2024-08-08 16:21:19 +05:30
|
|
|
import React from "react"
|
|
|
|
|
import { NotImplementedError } from "../../utils/errors"
|
|
|
|
|
|
|
|
|
|
import Tools from "../constants/tools"
|
|
|
|
|
import Layouts from "../constants/layouts"
|
|
|
|
|
import Cursor from "../constants/cursor"
|
2024-09-09 19:06:03 +05:30
|
|
|
import { toSnakeCase } from "../utils/utils"
|
|
|
|
|
import EditableDiv from "../../components/editableDiv"
|
2024-08-08 16:21:19 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class to be extended
|
|
|
|
|
*/
|
|
|
|
|
class Widget extends React.Component{
|
|
|
|
|
|
2024-08-08 22:49:14 +05:30
|
|
|
static widgetType = "widget"
|
2024-08-08 16:21:19 +05:30
|
|
|
|
2024-08-08 22:49:14 +05:30
|
|
|
constructor(props){
|
2024-08-08 16:21:19 +05:30
|
|
|
super(props)
|
2024-08-08 22:49:14 +05:30
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
const {id, widgetName, canvasRef} = props
|
2024-08-08 22:49:14 +05:30
|
|
|
console.log("Id: ", id)
|
2024-08-08 16:21:19 +05:30
|
|
|
// this id has to be unique inside the canvas, it will be set automatically and should never be changed
|
2024-08-08 22:49:14 +05:30
|
|
|
this.__id = id
|
2024-08-08 16:21:19 +05:30
|
|
|
this._zIndex = 0
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
this.canvas = canvasRef?.current || null
|
|
|
|
|
|
|
|
|
|
// this._selected = false
|
2024-08-08 16:21:19 +05:30
|
|
|
this._disableResize = false
|
|
|
|
|
this._disableSelection = false
|
|
|
|
|
|
|
|
|
|
this.cursor = Cursor.POINTER
|
|
|
|
|
|
|
|
|
|
this.icon = "" // antd icon name representing this widget
|
|
|
|
|
|
|
|
|
|
this.elementRef = React.createRef()
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
this.attrs = {
|
2024-08-08 16:21:19 +05:30
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 22:49:14 +05:30
|
|
|
this.state = {
|
|
|
|
|
attrs: { // attributes
|
|
|
|
|
// replace this with this.props
|
|
|
|
|
},
|
2024-09-09 19:06:03 +05:30
|
|
|
zIndex: 0,
|
|
|
|
|
pos: {x: 0, y: 0},
|
|
|
|
|
selected: false,
|
|
|
|
|
widgetName: widgetName || 'unnamed widget' // this will later be converted to variable name
|
2024-08-08 22:49:14 +05:30
|
|
|
}
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-08 16:21:19 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
setComponentAdded(added=true){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this.elementRef = document.querySelector(`[data-id="${this.__id}"]`)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
2024-08-08 22:49:14 +05:30
|
|
|
console.log("mounted: ")
|
|
|
|
|
this.elementRef.current?.addEventListener("click", this.mousePress)
|
2024-08-08 16:21:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount(){
|
2024-08-08 22:49:14 +05:30
|
|
|
this.elementRef.current?.removeEventListener("click", this.mousePress)
|
2024-08-08 16:21:19 +05:30
|
|
|
}
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
// TODO: add context menu items such as delete, add etc
|
|
|
|
|
contextMenu(){
|
2024-08-08 16:21:19 +05:30
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getVariableName(){
|
|
|
|
|
return toSnakeCase(this.state.widgetName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mousePress(event){
|
|
|
|
|
// event.preventDefault()
|
2024-08-08 16:21:19 +05:30
|
|
|
if (!this._disableSelection){
|
|
|
|
|
|
2024-09-09 19:24:43 +05:30
|
|
|
// 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)
|
2024-08-08 16:21:19 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select(){
|
2024-09-09 19:24:43 +05:30
|
|
|
this.setState({
|
2024-09-09 19:06:03 +05:30
|
|
|
selected: true
|
2024-09-09 19:24:43 +05:30
|
|
|
})
|
|
|
|
|
console.log("selected")
|
2024-08-08 16:21:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deSelect(){
|
2024-09-09 19:24:43 +05:30
|
|
|
this.setState({
|
2024-09-09 19:06:03 +05:30
|
|
|
selected: false
|
2024-09-09 19:24:43 +05:30
|
|
|
})
|
|
|
|
|
console.log("DeSelected")
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isSelected(){
|
|
|
|
|
return this.state.selected
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setPos(x, y){
|
|
|
|
|
this.setState({
|
|
|
|
|
pos: {x: x, y: y}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPos(){
|
|
|
|
|
return this.state.pos
|
2024-08-08 16:21:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getProps(){
|
2024-09-09 19:06:03 +05:30
|
|
|
return this.attrs
|
2024-08-08 16:21:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getWidgetFunctions(){
|
|
|
|
|
return this.functions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getId(){
|
|
|
|
|
return this.__id
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
getElement(){
|
|
|
|
|
return this.elementRef.current
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 16:21:19 +05:30
|
|
|
renderContent(){
|
|
|
|
|
// throw new NotImplementedError("render method has to be implemented")
|
|
|
|
|
return (
|
|
|
|
|
<div className="tw-w-full tw-h-full tw-bg-red-400">
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is an internal methods don't override
|
|
|
|
|
* @returns {HTMLElement}
|
|
|
|
|
*/
|
|
|
|
|
render(){
|
|
|
|
|
|
|
|
|
|
let style = {
|
|
|
|
|
cursor: this.cursor,
|
2024-09-09 19:06:03 +05:30
|
|
|
top: `${this.state.pos.y}px`,
|
|
|
|
|
left: `${this.state.pos.x}px`,
|
2024-08-08 16:21:19 +05:30
|
|
|
width: this.boundingRect.width,
|
|
|
|
|
height: this.boundingRect.height
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let selectionStyle = {
|
|
|
|
|
x: "-5px",
|
|
|
|
|
y: "-5px",
|
|
|
|
|
width: this.boundingRect.width + 5,
|
|
|
|
|
height: this.boundingRect.height + 5
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
const onWidgetNameChange = (value) => {
|
|
|
|
|
|
|
|
|
|
this.setState((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
widgetName: value.length > 0 ? value : prev.widgetName
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 16:21:19 +05:30
|
|
|
return (
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
<div data-id={this.__id} ref={this.elementRef} className="tw-relative tw-w-fit tw-h-fit"
|
|
|
|
|
style={style}
|
2024-08-08 16:21:19 +05:30
|
|
|
>
|
|
|
|
|
|
2024-08-08 22:49:14 +05:30
|
|
|
{this.renderContent()}
|
2024-09-09 19:06:03 +05:30
|
|
|
<div className={`tw-absolute tw-bg-transparent tw-scale-[1.1] tw-opacity-100
|
|
|
|
|
tw-w-full tw-h-full tw-top-0
|
|
|
|
|
${this.state.selected ? 'tw-border-2 tw-border-solid tw-border-blue-500' : 'tw-border-none'}`}>
|
2024-08-08 16:21:19 +05:30
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
<div className="tw-relative tw-w-full tw-h-full">
|
|
|
|
|
|
|
|
|
|
{/* <div contentEditable="true" onClick={(e) => e.preventDefault()} className="tw-text-sm tw-w-fit tw-min-w-[100px] tw-absolute tw--top-2">
|
|
|
|
|
{this._widgetName}
|
|
|
|
|
</div> */}
|
|
|
|
|
{ this.state.selected &&
|
|
|
|
|
<EditableDiv value={this.state.widgetName} onChange={onWidgetNameChange}
|
|
|
|
|
maxLength={40}
|
|
|
|
|
className="tw-text-sm tw-w-fit tw-max-w-[160px] tw-text-clip tw-min-w-[150px]
|
2024-09-09 19:24:43 +05:30
|
|
|
tw-overflow-hidden tw-absolute tw--top-4 tw-h-6"
|
2024-09-09 19:06:03 +05:30
|
|
|
/>
|
|
|
|
|
}
|
2024-08-08 16:21:19 +05:30
|
|
|
</div>
|
|
|
|
|
|
2024-09-09 19:06:03 +05:30
|
|
|
|
|
|
|
|
|
2024-08-08 16:21:19 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default Widget
|