working on canvas

This commit is contained in:
paul
2024-08-08 22:49:14 +05:30
parent 35cc57c453
commit e9f44bfbee
3 changed files with 61 additions and 20 deletions

View File

@@ -5,6 +5,7 @@ import { Button, Tooltip } from "antd"
import Widget from "./widgets/base" import Widget from "./widgets/base"
import Cursor from "./constants/cursor" import Cursor from "./constants/cursor"
import { UID } from "../utils/uid"
class Canvas extends React.Component { class Canvas extends React.Component {
@@ -15,10 +16,7 @@ class Canvas extends React.Component {
this.canvasRef = React.createRef() this.canvasRef = React.createRef()
this.canvasContainerRef = React.createRef() this.canvasContainerRef = React.createRef()
/** this.widgetRefs = {}
* @type {Widget[]}
*/
this.widgets = []
this.modes = { this.modes = {
DEFAULT: '', DEFAULT: '',
@@ -40,6 +38,7 @@ class Canvas extends React.Component {
} }
this.resetTransforms = this.resetTransforms.bind(this) this.resetTransforms = this.resetTransforms.bind(this)
this.renderWidget = this.renderWidget.bind(this)
// this.updateCanvasDimensions = this.updateCanvasDimensions.bind(this) // this.updateCanvasDimensions = this.updateCanvasDimensions.bind(this)
} }
@@ -49,7 +48,16 @@ class Canvas extends React.Component {
// this.widgets.push(new Widget()) // this.widgets.push(new Widget())
this.setState({widgets: [new Widget()]}) // let widgetRef = React.createRef()
// this.widgetRefs[widgetRef.current.__id] = widgetRef
// // Update the state to include the new widget's ID
// this.setState((prevState) => ({
// widgetIds: [...prevState.widgetIds, widgetRef.current.__id]
// }))
this.addWidget(Widget)
} }
@@ -234,6 +242,31 @@ class Canvas extends React.Component {
}, this.applyTransform) }, this.applyTransform)
} }
/**
*
* @param {Widget} widgetComponentType - don't pass <Widget /> instead pass Widget
*/
addWidget(widgetComponentType){
const widgetRef = React.createRef()
const id = `${widgetComponentType.widgetType}_${UID()}`
// Store the ref in the instance variable
this.widgetRefs[id] = widgetRef
console.log("widget ref: ", this.widgetRefs)
// Update the state to include the new widget's type and ID
this.setState((prevState) => ({
widgets: [...prevState.widgets, { id, type: widgetComponentType }]
}))
}
renderWidget(widget){
const { id, type: ComponentType } = widget
console.log("widet: ", this.widgetRefs, id)
return <ComponentType key={id} id={id} ref={this.widgetRefs[id]} />
}
render() { render() {
return ( return (
<div className="tw-relative tw-flex tw-w-full tw-h-full tw-max-h-[100vh]"> <div className="tw-relative tw-flex tw-w-full tw-h-full tw-max-h-[100vh]">
@@ -254,9 +287,7 @@ class Canvas extends React.Component {
ref={this.canvasRef}> ref={this.canvasRef}>
<div className="tw-relative tw-w-full tw-h-full"> <div className="tw-relative tw-w-full tw-h-full">
{ {
this.state.widgets.map((wid, index) => { this.state.widgets.map(this.renderWidget)
return <React.Fragment key={index}>{React.cloneElement(wid, {ref:wid.elementRef})}</React.Fragment>
})
} }
</div> </div>
</div> </div>

View File

@@ -6,23 +6,21 @@ import Layouts from "../constants/layouts"
import Cursor from "../constants/cursor" import Cursor from "../constants/cursor"
function UID(){
return Date.now().toString(36) + Math.random().toString(36).substr(2)
}
/** /**
* Base class to be extended * Base class to be extended
*/ */
class Widget extends React.Component{ class Widget extends React.Component{
constructor(props, _type="widget"){ static widgetType = "widget"
constructor(props){
super(props) super(props)
// const _type="widget" const {id} = props
this.type = _type 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 has to be unique inside the canvas, it will be set automatically and should never be changed
this.__id = `${_type}_${UID()}` this.__id = id
this._zIndex = 0 this._zIndex = 0
this._selected = false this._selected = false
@@ -68,6 +66,13 @@ class Widget extends React.Component{
width: 100 width: 100
} }
this.state = {
attrs: { // attributes
// replace this with this.props
},
zIndex: 0
}
} }
@@ -79,11 +84,12 @@ class Widget extends React.Component{
} }
componentDidMount(){ componentDidMount(){
this.elementRef?.addEventListener("click", this.mousePress) console.log("mounted: ")
this.elementRef.current?.addEventListener("click", this.mousePress)
} }
componentWillUnmount(){ componentWillUnmount(){
this.elementRef?.removeEventListener("click", this.mousePress) this.elementRef.current?.removeEventListener("click", this.mousePress)
} }
mousePress(event){ mousePress(event){
@@ -155,10 +161,10 @@ class Widget extends React.Component{
return ( return (
<div data-id={this.__id} className="tw-relative tw-w-fit tw-h-fit" style={style} <div data-id={this.__id} ref={this.elementRef} className="tw-relative tw-w-fit tw-h-fit" style={style}
> >
{this.render()} {this.renderContent()}
<div className="tw-absolute tw-bg-transparent tw-scale-[1.1] tw-opacity-35 <div className="tw-absolute tw-bg-transparent tw-scale-[1.1] tw-opacity-35
tw-w-full tw-h-full tw-top-0 tw-border-2 tw-border-solid tw-border-black"> tw-w-full tw-h-full tw-top-0 tw-border-2 tw-border-solid tw-border-black">

4
src/utils/uid.js Normal file
View File

@@ -0,0 +1,4 @@
export function UID(){
return Date.now().toString(36) + Math.random().toString(36).substr(2)
}