-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage-header.jsx
163 lines (138 loc) · 4.95 KB
/
stage-header.jsx
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
153
154
155
156
157
158
159
160
161
162
163
/*
* @Author: wuqinfa
* @Date: 2022-06-20 10:26:16
* @LastEditTime: 2022-09-29 15:50:59
* @LastEditors: wuqinfa
* @Description:
*/
import PropTypes from 'prop-types';
import React from 'react';
import bindAll from 'lodash.bindall';
import VM from 'scratch-vm';
import {STAGE_SIZE_MODES} from '../lib/layout-constants';
import {setStageSize} from '../reducers/stage-size';
import {setFullScreen} from '../reducers/mode';
import {
setStageNativeSize,
} from '../reducers/stage-size';
import {connect} from 'react-redux';
import StageHeaderComponent from '../components/stage-header/stage-header.jsx';
// eslint-disable-next-line react/prefer-stateless-function
class StageHeader extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleKeyPress',
'handleTriggerCoordinate',
'handleZoomOutCoordinateFontSize',
'handleZoomInCoordinateFontSize',
'handleOpenStageNativeSizePopover',
'handleCloseStageNativeSizePopover',
]);
this.minCoordinateFontSize = 10;
this.maxCoordinateFontSize = 18;
this.coordinateFontSize = 14;
this.state = {
isShowCoordinate: false, // 是否显示坐标网格
stageNativeSizePopoverOpen: false,
};
}
componentDidMount () {
document.addEventListener('keydown', this.handleKeyPress);
}
componentWillUnmount () {
document.removeEventListener('keydown', this.handleKeyPress);
}
handleKeyPress (event) {
if (event.key === 'Escape' && this.props.isFullScreen) {
this.props.onSetStageUnFull(false);
}
}
/**
* 触发坐标网格的显示 or 隐藏
*/
handleTriggerCoordinate () {
const visible = !this.state.isShowCoordinate;
this.props.vm.runtime.triggerCoordinate(visible);
this.setState({
isShowCoordinate: visible
});
}
/**
* 缩小坐标系的字体
*/
handleZoomOutCoordinateFontSize () {
let temp = this.coordinateFontSize - 1;
temp = temp >= this.minCoordinateFontSize ? temp : this.minCoordinateFontSize;
console.log('temp :>> ', temp);
this.coordinateFontSize = temp;
this.props.vm.runtime.setCoordinateFontSize(temp);
}
/**
* 放大坐标系的字体
*/
handleZoomInCoordinateFontSize () {
let temp = this.coordinateFontSize + 1;
temp = temp <= this.maxCoordinateFontSize ? temp : this.maxCoordinateFontSize;
this.coordinateFontSize = temp;
this.props.vm.runtime.setCoordinateFontSize(temp);
}
/**
* 打开 stageNativeSize 的 Popover 组件
*/
handleOpenStageNativeSizePopover () {
this.setState({stageNativeSizePopoverOpen: true});
}
/**
* 关闭 stageNativeSize 的 Popover 组件
*/
handleCloseStageNativeSizePopover () {
this.setState({stageNativeSizePopoverOpen: false});
}
render () {
const {
...props
} = this.props;
return (
<StageHeaderComponent
{...props}
isShowCoordinate={this.state.isShowCoordinate}
onKeyPress={this.handleKeyPress}
onTriggerCoordinate={this.handleTriggerCoordinate}
onZoomOutCoordinateFontSize={this.handleZoomOutCoordinateFontSize}
onZoomInCoordinateFontSize={this.handleZoomInCoordinateFontSize}
stageNativeSizePopoverOpen={this.state.stageNativeSizePopoverOpen}
onOpenStageNativeSizePopover={this.handleOpenStageNativeSizePopover}
onCloseStageNativeSizePopover={this.handleCloseStageNativeSizePopover}
/>
);
}
}
StageHeader.propTypes = {
isFullScreen: PropTypes.bool,
isPlayerOnly: PropTypes.bool,
onSetStageUnFull: PropTypes.func.isRequired,
showBranding: PropTypes.bool,
stageSizeMode: PropTypes.oneOf(Object.keys(STAGE_SIZE_MODES)),
vm: PropTypes.instanceOf(VM).isRequired,
stageNativeSize: PropTypes.array.isRequired,
onSetStageNativeSize: PropTypes.func.isRequired,
};
const mapStateToProps = state => ({
stageSizeMode: state.scratchGui.stageSize.stageSize,
showBranding: state.scratchGui.mode.showBranding,
isFullScreen: state.scratchGui.mode.isFullScreen,
isPlayerOnly: state.scratchGui.mode.isPlayerOnly,
stageNativeSize: state.scratchGui.stageSize.stageNativeSize, // 当前舞台尺寸
});
const mapDispatchToProps = dispatch => ({
onSetStageLarge: () => dispatch(setStageSize(STAGE_SIZE_MODES.large)),
onSetStageSmall: () => dispatch(setStageSize(STAGE_SIZE_MODES.small)),
onSetStageFull: () => dispatch(setFullScreen(true)),
onSetStageUnFull: () => dispatch(setFullScreen(false)),
onSetStageNativeSize: (stageNativeSize) => dispatch(setStageNativeSize(stageNativeSize)),
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(StageHeader);