-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage-header.jsx
295 lines (275 loc) · 11.4 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import classNames from 'classnames';
import {defineMessages, injectIntl, intlShape} from 'react-intl';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import VM from 'scratch-vm';
import Popover from 'react-popover';
import isEqual from 'lodash.isequal';
import Box from '../box/box.jsx';
import Button from '../button/button.jsx';
import Controls from '../../containers/controls.jsx';
import {getStageDimensions} from '../../lib/screen-utils';
import { STAGE_SIZE_MODES, STAGE_NATIVE_SIZES } from '../../lib/layout-constants';
import fullScreenIcon from './icon--fullscreen.svg';
// import largeStageIcon from './icon--large-stage.svg';
// import smallStageIcon from './icon--small-stage.svg';
import unFullScreenIcon from './icon--unfullscreen.svg';
import stageSizeIcon from './stageSize.svg';
import scratchLogo from '../menu-bar/scratch-logo.svg';
import styles from './stage-header.css';
const messages = defineMessages({
largeStageSizeMessage: {
defaultMessage: 'Switch to large stage',
description: 'Button to change stage size to large',
id: 'gui.stageHeader.stageSizeLarge'
},
smallStageSizeMessage: {
defaultMessage: 'Switch to small stage',
description: 'Button to change stage size to small',
id: 'gui.stageHeader.stageSizeSmall'
},
fullStageSizeMessage: {
defaultMessage: 'Enter full screen mode',
description: 'Button to change stage size to full screen',
id: 'gui.stageHeader.stageSizeFull'
},
unFullStageSizeMessage: {
defaultMessage: 'Exit full screen mode',
description: 'Button to get out of full screen mode',
id: 'gui.stageHeader.stageSizeUnFull'
},
fullscreenControl: {
defaultMessage: 'Full Screen Control',
description: 'Button to enter/exit full screen mode',
id: 'gui.stageHeader.fullscreenControl'
}
});
const StageHeaderComponent = function (props) {
const {
isFullScreen,
isPlayerOnly,
onKeyPress,
onSetStageLarge,
onSetStageSmall,
onSetStageFull,
onSetStageUnFull,
showBranding,
stageSizeMode,
vm,
isShowCoordinate,
onTriggerCoordinate,
onZoomOutCoordinateFontSize,
onZoomInCoordinateFontSize,
stageNativeSize,
onSetStageNativeSize,
stageNativeSizePopoverOpen,
onOpenStageNativeSizePopover,
onCloseStageNativeSizePopover,
} = props;
let header = null;
const stageNativeSizePopoverBody = (
<div className={styles.stageNativeSize}>
{
STAGE_NATIVE_SIZES.map((item) => {
const {
width,
height,
title,
} = item;
const isCurSel = isEqual(stageNativeSize, [width, height]);
return (
<div
key={`key_${width}_${height}`}
className={classNames(
styles.stageNativeSizeButton,
{
[styles['stageNativeSizeButton--sel']]: isCurSel,
}
)}
onClick={() => {
onSetStageNativeSize([width, height]);
}}
>
{title}
</div>
);
})
}
</div>
);
if (isFullScreen) {
const stageDimensions = getStageDimensions(null, true);
const stageButton = showBranding ? (
<div className={styles.embedScratchLogo}>
<a
href="https://scratch.mit.edu"
rel="noopener noreferrer"
target="_blank"
>
<img
alt="Scratch"
src={scratchLogo}
/>
</a>
</div>
) : (
<Button
className={styles.stageButton}
onClick={onSetStageUnFull}
onKeyPress={onKeyPress}
>
<img
alt={props.intl.formatMessage(messages.unFullStageSizeMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={unFullScreenIcon}
title={props.intl.formatMessage(messages.fullscreenControl)}
/>
</Button>
);
header = (
<Box className={styles.stageHeaderWrapperOverlay}>
<Box
className={styles.stageMenuWrapper}
style={{width: stageDimensions.width}}
>
<Controls vm={vm} />
{stageButton}
</Box>
</Box>
);
} else {
const stageControls =
isPlayerOnly ? (
[]
) : (
<div className={styles.stageSizeToggleGroup}>
<div>
<span className={styles.btn} onClick={onTriggerCoordinate}>{isShowCoordinate ? '关闭坐标' : '开启坐标'}</span>
{
isShowCoordinate ? (
<>
<span className={styles.btn} onClick={onZoomOutCoordinateFontSize}>缩小字体</span>
<span className={styles.btn} onClick={onZoomInCoordinateFontSize}>放大字体</span>
</>
) : null
}
</div>
{/* 新增 stageNativeSize 的 Popover 组件,用于选择舞台尺寸,如图: ![](http://res.watermcc.top/blog/2022/20220929-1664437753.png) */}
<Popover
className="custom-popover"
body={stageNativeSizePopoverBody}
isOpen={stageNativeSizePopoverOpen}
preferPlace="below"
onOuterAction={onCloseStageNativeSizePopover}
>
<div>
<Button
className={styles.stageButton}
onClick={onOpenStageNativeSizePopover}
>
<img
className={styles.stageButtonIcon}
draggable={false}
src={stageSizeIcon}
/>
</Button>
</div>
</Popover>
{/* FIXME: 原本用于等比例放大缩小舞台尺寸的功能就先注释掉 */}
{/* <div>
<Button
className={classNames(
styles.stageButton,
styles.stageButtonFirst,
(stageSizeMode === STAGE_SIZE_MODES.small) ? null : styles.stageButtonToggledOff
)}
onClick={onSetStageSmall}
>
<img
alt={props.intl.formatMessage(messages.smallStageSizeMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={smallStageIcon}
/>
</Button>
</div>
<div>
<Button
className={classNames(
styles.stageButton,
styles.stageButtonLast,
(stageSizeMode === STAGE_SIZE_MODES.large) ? null : styles.stageButtonToggledOff
)}
onClick={onSetStageLarge}
>
<img
alt={props.intl.formatMessage(messages.largeStageSizeMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={largeStageIcon}
/>
</Button>
</div> */}
</div>
);
header = (
<Box className={styles.stageHeaderWrapper}>
<Box className={styles.stageMenuWrapper}>
<Controls vm={vm} />
<div className={styles.stageSizeRow}>
{stageControls}
<div>
<Button
className={styles.stageButton}
onClick={onSetStageFull}
>
<img
alt={props.intl.formatMessage(messages.fullStageSizeMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={fullScreenIcon}
title={props.intl.formatMessage(messages.fullscreenControl)}
/>
</Button>
</div>
</div>
</Box>
</Box>
);
}
return header;
};
const mapStateToProps = state => ({
// This is the button's mode, as opposed to the actual current state
stageSizeMode: state.scratchGui.stageSize.stageSize
});
StageHeaderComponent.propTypes = {
intl: intlShape,
isFullScreen: PropTypes.bool.isRequired,
isPlayerOnly: PropTypes.bool.isRequired,
onKeyPress: PropTypes.func.isRequired,
onSetStageFull: PropTypes.func.isRequired,
onSetStageLarge: PropTypes.func.isRequired,
onSetStageSmall: PropTypes.func.isRequired,
onSetStageUnFull: PropTypes.func.isRequired,
showBranding: PropTypes.bool.isRequired,
stageSizeMode: PropTypes.oneOf(Object.keys(STAGE_SIZE_MODES)),
vm: PropTypes.instanceOf(VM).isRequired,
isShowCoordinate: PropTypes.bool.isRequired, // 标识是否显示网格坐标
onTriggerCoordinate: PropTypes.func.isRequired, // 控制是否显示网格坐标
onZoomOutCoordinateFontSize: PropTypes.func.isRequired, // 缩小网格坐标系的字体大小
onZoomInCoordinateFontSize: PropTypes.func.isRequired, // 放大网格坐标系的字体大小
stageNativeSize: PropTypes.array.isRequired, // 当前舞台尺寸
onSetStageNativeSize: PropTypes.func.isRequired, // 设置当前舞台尺寸
stageNativeSizePopoverOpen: PropTypes.bool.isRequired, // 用来控制 stageNativeSize 的 Popover 组件是否显示
onOpenStageNativeSizePopover: PropTypes.func.isRequired, // 打开 stageNativeSize 的 Popover 组件
onCloseStageNativeSizePopover: PropTypes.func.isRequired, // 关闭 stageNativeSize 的 Popover 组件
};
StageHeaderComponent.defaultProps = {
stageSizeMode: STAGE_SIZE_MODES.large
};
export default injectIntl(connect(
mapStateToProps
)(StageHeaderComponent));