1+ import React from 'react' ;
2+ import { NumberPicker , Icon } from '@alifd/next' ;
3+ import { ILowCodePluginContext , project , material } from '@alilc/lowcode-engine' ;
4+
5+ import './index.scss' ;
6+
7+ const isNewEngineVersion = ! ! material ;
8+
9+ const devices = [
10+ { key : 'desktop' } ,
11+ { key : 'tablet' } ,
12+ { key : 'phone' } ,
13+ ] ;
14+
15+ const CustomIcon = Icon . createFromIconfontCN ( {
16+ scriptUrl : 'https://at.alicdn.com/t/font_2896595_33xhsbg9ux5.js' ,
17+ } ) ;
18+
19+ export class SimulatorPane extends React . Component {
20+ static displayName = 'SimulatorPane' ;
21+
22+ state = {
23+ actived : 'desktop' ,
24+ currentWidth : null
25+ } ;
26+
27+ componentDidMount ( ) {
28+ if ( isNewEngineVersion ) {
29+ project . onSimulatorRendererReady ( ( ) => {
30+ const currentWidth = document . querySelector ( '.lc-simulator-canvas' ) ?. clientWidth || this . state . currentWidth || 0 ;
31+ this . setState ( {
32+ currentWidth
33+ } ) ;
34+ } ) ;
35+ } else {
36+ // 兼容老版本引擎
37+ // @ts -ignore
38+ project . onRendererReady ( ( ) => {
39+ const currentWidth = document . querySelector ( '.lc-simulator-canvas' ) ?. clientWidth || this . state . currentWidth || 0 ;
40+ this . setState ( {
41+ currentWidth
42+ } ) ;
43+ } ) ;
44+ }
45+ }
46+
47+ change = ( device : string ) => {
48+ const simulator = project . simulator ;
49+ // 切换画布
50+ simulator ?. set ( 'device' , device ) ;
51+ if ( document . querySelector ( '.lc-simulator-canvas' ) ?. style ) {
52+ document . querySelector ( '.lc-simulator-canvas' ) . style . width = null ;
53+ }
54+ setTimeout ( ( ) => {
55+ const currentWidth = document . querySelector ( '.lc-simulator-canvas' ) ?. clientWidth || this . state . currentWidth || 0 ;
56+ this . setState ( {
57+ actived : device ,
58+ currentWidth
59+ } ) ;
60+ } , 0 ) ;
61+ }
62+
63+ renderItemSVG ( device : string ) {
64+ switch ( device ) {
65+ case 'desktop' :
66+ return < CustomIcon size = "large" type = "iconic_PC_Select" /> ;
67+ case 'tablet' :
68+ return < CustomIcon size = "large" type = "iconic_Tablet_Select" /> ;
69+ case 'phone' :
70+ return < CustomIcon size = "large" type = "iconic_smartphone" /> ;
71+ default :
72+ return < CustomIcon size = "large" type = "iconic_PC_Select" /> ;
73+ }
74+ }
75+
76+ render ( ) {
77+ const currentWidth = this . state . currentWidth || 0 ;
78+ return (
79+ < div className = "lp-simulator-pane" >
80+ {
81+ devices . map ( ( item , index ) => {
82+ return (
83+ < span
84+ key = { item . key }
85+ className = { `lp-simulator-pane-item ${ this . state . actived === item . key ? 'actived' : '' } ` }
86+ onClick = { this . change . bind ( this , item . key ) }
87+ >
88+ { this . renderItemSVG ( item . key ) }
89+ </ span >
90+ )
91+ } )
92+ }
93+ < div className = 'lp-simulator-width-setter' >
94+ < NumberPicker className = "lp-simulator-width-input" addonTextAfter = "px" value = { currentWidth } placeholder = "请输入" onChange = { ( value ) => {
95+ this . setState ( {
96+ currentWidth : value
97+ } ) ;
98+ } } onPressEnter = { ( event : any ) => {
99+ const value = event ?. target ?. value ;
100+ if ( document . querySelector ( '.lc-simulator-canvas' ) ?. style ) {
101+ document . querySelector ( '.lc-simulator-canvas' ) . style . width = `${ value } px`
102+ }
103+ this . setState ( {
104+ currentWidth : value
105+ } ) ;
106+ } } />
107+ </ div >
108+ </ div >
109+ ) ;
110+ }
111+ }
112+ export default ( ctx : ILowCodePluginContext ) => {
113+ const simulatorPaneRef = React . createRef < SimulatorPane > ( ) ;
114+
115+ return {
116+ name : 'SimulatorPane' ,
117+ // 插件的初始化函数,在引擎初始化之后会立刻调用
118+ init ( ) {
119+ // 往引擎增加工具条
120+ ctx . skeleton . add ( {
121+ area : 'top' ,
122+ name : 'SimulatorPane' ,
123+ type : 'Widget' ,
124+ props : {
125+ description : '切换画布尺寸' ,
126+ align : "center" ,
127+ } ,
128+ content : (
129+ < SimulatorPane
130+ ref = { simulatorPaneRef }
131+ />
132+ ) ,
133+ } ) ;
134+ }
135+ } ;
136+ } ;
0 commit comments