import React, { Component, DragEventHandler } from 'react'; import { GlobalDataProp, GlobalItemProp } from '../../../App'; import { ChartConfigProp, ConfigItemProp, ConfigListProp, SegmentProp } from '../../../component/chart.d'; import ChartsFactory from '../../../component/chartsFactory'; import styles from "./dragContext.module.css"; import { DragDropContext, DragUpdate, Droppable, DroppableProvided, DroppableStateSnapshot, DropResult, ResponderProvided } from "react-beautiful-dnd"; import Draggable, { DraggableEvent } from 'react-draggable'; import {DraggableData, Position, ResizableDelta, Rnd} from "react-rnd"; import { PositionProp } from '../../../component/ComponentHOC.d'; import { Menu, Popover } from 'antd'; import DragItem from './dragItem'; interface Prop { width : number ; height : number ; data ?: GlobalDataProp; style ?: React.CSSProperties ; selectKey ?: string ; onClickItem:(key:string,config:ChartConfigProp)=>void ; // onDragEnd ?: (result:DropResult,provide:ResponderProvided)=>void ; onChange : (data:GlobalDataProp)=>void ; times : number; // 缩放倍率 = 实际尺寸 / 当前尺寸 debug ?: boolean ; onCancel ? :()=>void ; onCopy ? : (data:GlobalItemProp,key:string)=>void; onDelete ?:(data:GlobalItemProp,key:string)=>void ; onChartClick ?: (res:any)=>void ; } interface State { } export default class DragContext extends Component { onClick = (key:string,config:ChartConfigProp,e:any)=>{ this.props.onClickItem && this.props.onClickItem(key,config); e.stopPropagation() } onClickBack = (e:any)=>{ e.stopPropagation() } onResize = (e: MouseEvent | TouchEvent, dir: any, elementRef: HTMLElement, delta: ResizableDelta, position: Position,key:string)=>{ const {data} = this.props; if(!data){ return } let tmp:GlobalDataProp=this.changePosition(data,key,"宽度",data[key].data.param?.config['宽度'].value+(delta.width*this.props.times)); tmp = this.changePosition(tmp,key,"高度",data[key].data.param?.config['高度'].value+(delta.height*this.props.times)); tmp = this.changePosition(tmp,key,"X位置",(position.x*this.props.times)); tmp = this.changePosition(tmp,key,"Y位置",(position.y*this.props.times)); this.onChange(tmp); this.onClick(key,data[key].data,e); } /** * 根据主键修改数值大小 * @param componentKey * @param valueKey * @param value */ changePosition = (originData:GlobalDataProp,componentKey:string,valueKey:string,value:number):GlobalDataProp=>{ let data : GlobalDataProp|undefined = {...originData}; if(!data)return data; let param : SegmentProp|undefined = data[componentKey].data.param; if(param){ param = { ...param, config:{ ...param.config, [valueKey]:{ ...param.config[valueKey], value : value } } } }else{ return data; } data = { ...data, [componentKey]:{ ...data[componentKey], data : { ...data[componentKey].data, param : {...param} } } } return data; } onChange = (data:GlobalDataProp)=>{ this.props.onChange && this.props.onChange(data); } onDragEnd = (e:DraggableEvent,data:DraggableData,key:string)=>{ let tmpData:GlobalDataProp = {...this.props.data}; tmpData = this.changePosition(tmpData,key,"X位置",data.lastX*this.props.times) tmpData = this.changePosition(tmpData,key,"Y位置",data.lastY*this.props.times) this.onChange(tmpData); this.onClick(key,tmpData[key].data,e) } /** * * @param data 从全局数据中解析出尺寸数据 */ static getSizeFromGlobal = (data:GlobalItemProp,times:number):PositionProp=>{ const param : SegmentProp|undefined = data.data.param; const scale: number = times; if(param){ return { x : param.config['X位置'].value/scale, y : param.config['Y位置'].value/scale, w : param.config['宽度'].value/scale, h : param.config['高度'].value/scale, } }else{ return { x:0,y:0,w:0,h:0 } } } onMouseOut = (e:any)=>{ this.onClickCancel() } onClickCancel = ()=>{ this.props.onCancel && this.props.onCancel() } changeLayer = (key:string,item:GlobalItemProp)=>{ const {data} =this.props; const newGlobalData:GlobalDataProp = { ...data, [key] : item } this.onChange(newGlobalData); } downLayer = (e:GlobalItemProp,key:string,)=>{ const newData:GlobalItemProp = { ...e, index : e.index-1>=0?e.index-1:0, }; this.changeLayer(key,newData); } copyLayer = (e:GlobalItemProp,key:string,)=>{ this.props.onCopy && this.props.onCopy(e,key); } deleteLayer = (e:GlobalItemProp,key:string,)=>{ this.props.onDelete && this.props.onDelete(e,key); } renderMenu = (data:GlobalItemProp,key:string)=>{ return ( this.deleteLayer(data,key)}>删除图层 this.copyLayer(data,key)}>复制图层 this.downLayer(data,key)}>下移图层 ) } ondrag = (e:any,data:any)=>{ console.log(e); console.log(data); } onChartClick = (res:any)=>{ this.props.onChartClick && this.props.onChartClick(res); } renderComponent = (data:GlobalDataProp)=>{ const keys: string[] = Object.keys(data); const {selectKey,debug} = this.props; const isDebug : boolean = debug!=undefined ? debug : true ; return keys.map((item,index)=>{ const times : number = this.props.times; // const p : PositionProp = DragContext.getSizeFromGlobal(data[item],times) // const p1 : PositionProp = DragContext.getSizeFromGlobal(data[item],1) // const zIndex : number = data[item].index; return ( this.onResize(e.e,e.dir,e.elementRef,e.delta,e.position,e.key)} onDragEnd={(e)=>this.onDragEnd(e.de,e.dd,e.key)} onClick={(d,k,e)=>this.onClick(k,d.data,e)} onDelete={(d,k)=>this.deleteLayer(d,k)} onCopy={(d,k)=>this.copyLayer(d,k)} onDownLayer={(d,k)=>this.downLayer(d,k)} /> ) // return ( // this.renderMenu(data[item],item)} trigger="contextMenu"> // this.onResize(e,dir,ele,delta,p,item)} // onDragStop={(de:DraggableEvent,dd:DraggableData)=>this.onDragEnd(de,dd,item)} // style={{backgroundColor:selectKey==item?'rgba(19,152,232,0.4)':'',zIndex : zIndex,}} // position={{x:p.x,y:p.y}}onChartClick // size={{width:p1.w/times,height:p1.h/times}} // disableDragging={!debug} // enableResizing={debug && item==selectKey} // onDrag={this.ondrag} // > //
this.onClick(item,data[item].data,e)}> // //
//
//
// ) }) } render(){ const {width,height,data,style,times} = this.props; return (
{this.renderComponent(data||{})}
) } }