import {Button, Spin, message, Modal} from 'antd'; import React, { Component } from 'react'; import { GlobalDataProp, GlobalItemProp, HeaderItemProp, HeaderItems, PageConfig } from "./App"; import HeaderBar from './pane/headerBar'; import './App.less'; import { ChartConfigProp, SegmentProp } from "./component/chart.d"; import { ChartKeyMap, getConfigByKey } from './component/chart'; import LayerBar, { DataProp } from './pane/layerBar'; import ConfigBar from './pane/configBar'; import RenderBar from './pane/renderBar'; import { DesignerDataProp } from "./designer.d"; import ClientBoard from "./clientBoard"; interface Prop { onSave : (pageConfig?:ChartConfigProp,data?:GlobalDataProp)=>void; pageConfig ?: ChartConfigProp , data ?: GlobalDataProp , debug ? : boolean; } interface State { // 全局数据 data ?: GlobalDataProp ; // 选中组件 selectedItemKey ?: string ; // 是否加载loading loading ?: boolean ; // 页面配置 pageConfig ?: ChartConfigProp ; // 操作记录 operatorRecords ?: DesignerDataProp[] ; // 操作步骤游标;指向当前步骤 operatorIndex ?: number ; // 显示预览面板 showClientBoard ?: boolean ; // 当前是否有组件属性正在被编辑 onTargetEditing ?: boolean ; } // 工具栏数据,因为数据是固定的所以直接存放成常量 const HeaderBarData = HeaderItems ; // 保存步骤最多 const BACK_STEP_MAX :number = 10; export default class Designer extends Component{ static defaultProps : Prop = { debug : true, onSave : (p,d)=>console.log(p), } // 初始化state state : State = { data : this.props.data , selectedItemKey : undefined , loading : false, pageConfig : this.props.pageConfig||PageConfig , operatorRecords: [] , operatorIndex : undefined , showClientBoard : false, onTargetEditing : false, } // static getDerivedStateFromProps(nextProp:Prop,preState:State):State{ // return { // ...preState, // data : {...preState.data,...nextProp.data}, // pageConfig : {...preState.pageConfig,...nextProp.pageConfig} // } // } resetData = (extraData:State)=>{ this.setState({ ...extraData }) } componentDidMount(){ this.addListener() } addListener = ()=>{ document.onkeydown = (k)=>{ const key : string = k.key.toLowerCase(); switch (key) { case "arrowup":this.director("y",-1);this.stopContinue(k);break; case "arrowdown":this.director("y",1);this.stopContinue(k);break; case "arrowleft":this.director("x",-1);this.stopContinue(k);break; case "arrowright":this.director("x",1);this.stopContinue(k);break; default: } } // 监听编辑栏是否有输入控件处于编辑状态 window.addEventListener("focus",(e)=>{ console.log(`true focus`) this.setState({onTargetEditing : true}) }) // 监听编辑栏是否有输入控件处于编辑状态 window.addEventListener("blur",(e)=>{ this.setState({onTargetEditing : false}) }) } stopContinue = (k:KeyboardEvent)=>{ k.stopPropagation(); k.preventDefault(); } director = (type:"x"|"y",delta:number)=>{ const {selectedItemKey,data,onTargetEditing} = this.state; console.log(onTargetEditing) if(!selectedItemKey || !data || onTargetEditing){ return ; } const key = type == "x" ? "X位置" : "Y位置"; const tmp:any = { ...data[selectedItemKey] }; const res = { ...tmp, data: { ...tmp.data, param:{ ...tmp.data.param, config : { ...tmp.data.param.config, [key] : { ...tmp.data.param.config[key], value : tmp.data.param.config[key].value + delta } } } } } const result = { ...data, [selectedItemKey] : res } this.setState({data : result}) } // 新增步骤 addStep = (extraData:State)=>{ let {operatorIndex,operatorRecords} = this.state; const {pageConfig,data} = extraData; if(!operatorRecords){ operatorRecords = [] } if(!operatorIndex){ operatorIndex = 0; } // 如果步骤已经最多了,先去除第一步,然后往最后添加数据 if(operatorRecords?.length>=BACK_STEP_MAX){ operatorRecords.splice(0,1); } // 如果步骤之前是回退,则需要删除往后的步骤 if(operatorIndex>0 && operatorIndex{ const {operatorIndex} = this.state; if(operatorIndex && operatorIndex>=1){ const {operatorRecords} = this.state; if(operatorRecords ){ const index : number = operatorIndex-backDelta; const data : DesignerDataProp = operatorRecords[index]; this.resetData({ pageConfig : data.pageConfig, data : data.data, operatorIndex : index, selectedItemKey:undefined, }) } }else{ message.info("最多只能记录"+BACK_STEP_MAX+"步呢"); } } // 选中组件 onSelected = (key:string)=>{ // return const keys : string = key; console.log('selected key :'+keys) const {debug} = this.props; if(debug){ this.setState({ selectedItemKey :keys }) } } // 删除组件 onDelete = (key:string)=>{ let data : GlobalDataProp|undefined = this.state.data; if(!data)return; const {pageConfig} = this.state; delete data[key] this.setState({selectedItemKey : undefined},()=>{ this.onChangeGlobal(pageConfig,data) }) } // 添加图层 onAddLayer = (componentType:HeaderItemProp)=>{ if(componentType.key.indexOf("huanyuan")>=0){ console.log(componentType) // onClickBack this.onClickBack(componentType) return ; } if(componentType.key.indexOf("baocun")>=0){ this.onSave(); return ; } if(componentType.key.indexOf("yulan")>=0){ this.setState({showClientBoard : true}) return ; } const keys : string = componentType.title+"_"+new Date().getTime(); console.log(`新组件key:${keys}`) this.addLayer(keys,getConfigByKey(componentType.key)); } // 添加图层方法 addLayer = (keys:string,data:ChartConfigProp)=>{ const key : string[] = Object.keys(this.state.data||{}); const d : GlobalDataProp = { ...this.state.data, [keys]:{ index : 300, data : data } } this.setState({ selectedItemKey:keys }) this.addStep({ pageConfig:undefined, data : d }); } // 复制图层 onCopy = (data:DataProp)=>{ // alert(JSON.stringify(data)) const title : string = data.title ; const componentName : string = title.split("_")[0]; const time : string = new Date().getTime().toString(); const key : string = `${componentName}_${time}`; this.addLayer(key,data); } onSave = ()=>{ const {pageConfig,data} = this.state; // alert(data); const {onSave} = this.props; if(!onSave){ const res = { pageConfig, data } localStorage.setItem("data",JSON.stringify(res)); }else{ onSave(pageConfig,data||{}); } } /** * 修改配置 */ onChangeConfig = (config:ChartConfigProp)=>{ let selectedKey:string = this.state.selectedItemKey||''; let data:GlobalDataProp = {...this.state.data}; if(selectedKey=='' || !data){ // 表示修改全局数据 const segment : any = { ...this.state.pageConfig?.config, config : {...config.config.config} } const pc : ChartConfigProp = { ...this.state.pageConfig, config :segment } // this.resetData(); this.addStep({pageConfig:pc}) return ; } let p : GlobalItemProp = data[selectedKey]; p = { ...p, data : { ...p.data, ...config, } } data = { ...data, [selectedKey]:p } this.addStep({data : data}) } onCancelSelected = ()=>{ console.log("cancel") this.setState({selectedItemKey : undefined}) } onChangeGlobal = (pageConfig?:ChartConfigProp,globalData?:GlobalDataProp)=>{ console.log(globalData) // this.setState({ // pageConfig , // data:globalData, // }) // return ; this.addStep({pageConfig:pageConfig,data:globalData}) } contextCopy = (e:GlobalItemProp,key:string)=>{ const data : any = {...e.data,title:key}; this.onCopy(data); } contextDelete = (e:GlobalItemProp,key:string)=>{ this.onDelete(key); } onClickBack = (componentType:HeaderItemProp)=>{ const key:string = componentType.key; let index : string = key.split("_")[1]; try { this.goBack(parseInt(index)); } catch (error) { console.log(error) } } renderStepRecords = (records : DesignerDataProp[])=>{ let subItems: {[key:string]:HeaderItemProp}={}; let r : DesignerDataProp[] = [...records]; let {operatorIndex} = this.state; if(!operatorIndex){ operatorIndex = 0; } r.splice(operatorIndex,r.length-operatorIndex); const RecordItems : HeaderItemProp = { title : "还原", icon : undefined, key : "还原", subItems: r.map((item,index,a)=>{ const i: number = a.length-index; return { title : "返回上"+i +"步", icon : undefined, key : "huanyuan_"+i, subItems:undefined, } }) } return RecordItems; } onChartClick = (res:any)=>{ const data:any = res.data; let paramStr : string = ""; if(data){ // 拼接参数 paramStr = data.paramKey; }else{ paramStr = "keyword=12313"; } // console.log(res); const componentKey : string = res.key; // console.log(componentKey); const item : GlobalItemProp|undefined = this.state.data&&this.state.data[componentKey]; console.log(item) if(item){ const obj : any = item.data.config.config["超链设置"].subItem; const url : string = obj && obj['链接地址']['value']; const newWindow : boolean = obj && obj['打开方式']['value'].value == '新窗口'; const enable : boolean = obj && obj['启用']['value']; console.log(url); if(enable){ const link : string = url + "?" +paramStr ; if(newWindow){ window.open(link); }else{ window.location.href=link; } } } // const url : string = this.props.data[componentKey].data.config } // 新版本更新新属性时,兼容处理旧版本 的数据 handleForOldVersion = (oldData?:GlobalDataProp):GlobalDataProp=>{ if(!oldData){ return {}; } for(let key in oldData){ const componentType : string = key.split("_")[0]; let componentKey : string = this.getComponentKeyWithChineseName(componentType,HeaderItems); let componentConfig : ChartConfigProp = ChartKeyMap[componentKey]; // oldData[key].data = {...componentConfig,...oldData[key].data}; const config :any = { ...oldData[key].data.config, config : { ...componentConfig.config.config, ...oldData[key].data.config.config } } const param : any = { ...componentConfig.param, config:{ ...componentConfig.param?.config, ...oldData[key].data.param?.config, } } const data : any = { ...componentConfig.data, config:{ ...componentConfig.data?.config, ...oldData[key].data.data?.config, } } oldData[key].data = { ...componentConfig, config : config, param : param, data : data, } } return oldData ; } getComponentKeyWithChineseName = (name:string,items:HeaderItemProp[]):string=>{ for(let headerItem of items){ if(headerItem.subItems && headerItem.subItems.length>0){ let val: string = this.getComponentKeyWithChineseName(name,headerItem.subItems); if(val!=''){ return val ; } } if(headerItem.title == name){ return headerItem.key ; } } return ''; } render(){ const {debug} =this.props; const {selectedItemKey,loading,pageConfig,operatorRecords,showClientBoard} = this.state; let data : GlobalDataProp = this.handleForOldVersion(this.state.data) const pc : ChartConfigProp = pageConfig || PageConfig; return (
{/* 头部工具栏 */}
{/* 侧边图层栏 */} this.onSelected(d.key)} onDelete={this.onDelete} onCopy={this.onCopy} /> {/* 内容渲染栏 */} this.onSelected(k)} onCancel={this.onCancelSelected} onCopy={this.contextCopy} onChartClick={this.onChartClick} onDelete={this.contextDelete} /> {/* 配置栏 */}
this.setState({showClientBoard : false})} width={1500} >
{data&&pageConfig?:null}
) } }