forked from alibaba/lowcode-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.tsx
More file actions
152 lines (139 loc) · 4.19 KB
/
index.tsx
File metadata and controls
152 lines (139 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React from 'react';
import { NumberPicker, Icon } from '@alifd/next';
import { IPublicModelPluginContext } from '@alilc/lowcode-types';
import './index.scss';
const devices = [
{ key: 'default' },
{ key: 'tablet' },
{ key: 'phone' },
];
const CustomIcon = Icon.createFromIconfontCN({
scriptUrl: 'https://at.alicdn.com/t/font_2896595_33xhsbg9ux5.js',
});
export class SimulatorResizePane extends React.Component<{
pluginContext: IPublicModelPluginContext
}> {
static displayName = 'SimulatorResizePane';
state = {
active: 'default',
currentWidth: null
};
componentDidMount() {
const { project } = this.props.pluginContext;
// @ts-ignore
const onSimulatorRendererReady = (project.onSimulatorRendererReady || project.onRendererReady).bind(project);
onSimulatorRendererReady(() => {
const currentWidth = document.querySelector('.lc-simulator-canvas')?.clientWidth || this.state.currentWidth || 0;
this.setState({
currentWidth
});
});
project.onSimulatorHostReady?.((simulator) => {
if (simulator.get('device')) {
this.setState({
active: simulator.get('device'),
});
}
});
}
change = (device: string) => {
const { project } = this.props.pluginContext;
const simulator = project.simulatorHost;
// 切换画布
simulator?.set('device', device);
if (document.querySelector('.lc-simulator-canvas')?.style) {
document.querySelector('.lc-simulator-canvas').style.width = null;
}
setTimeout(() => {
const currentWidth = document.querySelector('.lc-simulator-canvas')?.clientWidth || this.state.currentWidth || 0;
this.setState({
active: device,
currentWidth
});
}, 0);
};
renderItemSVG(device: string) {
switch (device) {
case 'default':
return <CustomIcon size="large" type="iconic_PC_Select" />;
case 'tablet':
return <CustomIcon size="large" type="iconic_Tablet_Select" />;
case 'phone':
return <CustomIcon size="large" type="iconic_smartphone" />;
default:
return <CustomIcon size="large" type="iconic_PC_Select" />;
}
}
render() {
const currentWidth = this.state.currentWidth || 0;
const { project } = this.props.pluginContext;
return (
<div className="lp-simulator-pane">
{
devices.map((item, index) => {
return (
<span
key={item.key}
className={`lp-simulator-pane-item ${this.state.active === item.key ? 'active' : ''}`}
onClick={this.change.bind(this, item.key)}
>
{this.renderItemSVG(item.key)}
</span>
);
})
}
<div className="lp-simulator-width-setter">
<NumberPicker
className="lp-simulator-width-input"
addonTextAfter="px"
value={currentWidth}
placeholder="请输入"
onChange={(value) => {
this.setState({
currentWidth: value
});
}}
onPressEnter={(event: any) => {
const value = event?.target?.value;
const simulator = project.simulatorHost;
simulator?.set('deviceStyle', {
canvas: {
width: `${value}px`,
},
});
this.setState({
currentWidth: value
});
}}
/>
</div>
</div>
);
}
}
const plugin = (ctx: IPublicModelPluginContext) => {
const SimulatorResizePaneRef = React.createRef<SimulatorResizePane>();
return {
// 插件的初始化函数,在引擎初始化之后会立刻调用
init() {
// 往引擎增加工具条
ctx.skeleton.add({
area: 'topArea',
name: 'SimulatorResizePane',
type: 'Widget',
props: {
description: '切换画布尺寸',
align: 'center',
},
content: (
<SimulatorResizePane
ref={SimulatorResizePaneRef}
pluginContext={ctx}
/>
),
});
}
};
};
plugin.pluginName = 'SimulatorResizePane';
export default plugin;