Spaces:
Running
Running
File size: 11,685 Bytes
f2bee8a |
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 296 297 298 299 |
import bindAll from 'lodash.bindall';
import React from 'react';
import PropTypes from 'prop-types';
import {injectIntl, intlShape, defineMessages} from 'react-intl';
import monitorAdapter from '../lib/monitor-adapter.js';
import MonitorComponent, {monitorModes} from '../components/monitor/monitor.jsx';
import {addMonitorRect, getInitialPosition, resizeMonitorRect, removeMonitorRect} from '../reducers/monitor-layout';
import {getVariable, setVariableValue} from '../lib/variable-utils';
import importCSV from '../lib/import-csv';
import downloadBlob from '../lib/download-blob';
import SliderPrompt from './slider-prompt.jsx';
import {connect} from 'react-redux';
import {Map} from 'immutable';
import VM from 'scratch-vm';
const availableModes = opcode => (
monitorModes.filter(mode => {
if (opcode === 'data_variable') {
return mode !== 'list';
} else if (opcode === 'data_listcontents') {
return mode === 'list';
} else if (opcode === 'canvas_canvasGetter') {
return mode === 'image';
}
return mode !== 'slider' && mode !== 'list';
})
);
const messages = defineMessages({
columnPrompt: {
defaultMessage: 'Which column should be used (1-{numberOfColumns})?',
description: 'Prompt for which column should be used',
id: 'gui.monitors.importListColumnPrompt'
}
});
class Monitor extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleDragEnd',
'handleHide',
'handleNextMode',
'handleSetModeToDefault',
'handleSetModeToLarge',
'handleSetModeToSlider',
'handleSliderPromptClose',
'handleSliderPromptOk',
'handleSliderPromptOpen',
'handleImport',
'handleExport',
'setElement'
]);
this.state = {
sliderPrompt: false
};
}
componentDidMount () {
let rect;
const isNum = num => typeof num === 'number' && !isNaN(num);
// Load the VM provided position if not loaded already
// If a monitor has numbers for the x and y positions, load the saved position.
// Otherwise, auto-position the monitor.
if (isNum(this.props.x) && isNum(this.props.y) &&
!this.props.monitorLayout.savedMonitorPositions[this.props.id]) {
rect = {
upperStart: {x: this.props.x, y: this.props.y},
lowerEnd: {x: this.props.x + this.element.offsetWidth, y: this.props.y + this.element.offsetHeight}
};
this.props.addMonitorRect(this.props.id, rect, true /* savePosition */);
} else { // Newly created user monitor
rect = getInitialPosition(
this.props.monitorLayout, this.props.id, this.element.offsetWidth, this.element.offsetHeight);
this.props.addMonitorRect(this.props.id, rect);
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
x: rect.upperStart.x,
y: rect.upperStart.y
}));
}
this.element.style.top = `${rect.upperStart.y}px`;
this.element.style.left = `${rect.upperStart.x}px`;
}
shouldComponentUpdate (nextProps, nextState) {
if (nextState !== this.state) {
return true;
}
for (const key of Object.getOwnPropertyNames(nextProps)) {
// skip all the other things to check custom monitors and see if they need an update
if (key === 'value' && typeof nextProps[key] === 'object') {
return !nextProps[key]._monitorUpToDate;
}
// Don't need to rerender when other monitors are moved.
// monitorLayout is only used during initial layout.
if (key !== 'monitorLayout' && nextProps[key] !== this.props[key]) {
return true;
}
}
return false;
}
componentDidUpdate () {
// tw: if monitor is not draggable (ie. not in editor), do not calculate size of monitor for performance
if (!this.props.draggable) {
return;
}
this.props.resizeMonitorRect(this.props.id, this.element.offsetWidth, this.element.offsetHeight);
}
componentWillUnmount () {
this.props.removeMonitorRect(this.props.id);
}
handleDragEnd (e, {x, y}) {
const newX = parseInt(this.element.style.left, 10) + x;
const newY = parseInt(this.element.style.top, 10) + y;
this.props.onDragEnd(
this.props.id,
newX,
newY
);
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
x: newX,
y: newY
}));
}
handleHide () {
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
visible: false
}));
}
handleNextMode () {
const modes = availableModes(this.props.opcode);
const modeIndex = modes.indexOf(this.props.mode);
const newMode = modes[(modeIndex + 1) % modes.length];
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
mode: newMode
}));
}
handleSetModeToDefault () {
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
mode: 'default'
}));
}
handleSetModeToLarge () {
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
mode: 'large'
}));
}
handleSetModeToSlider () {
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
mode: 'slider'
}));
}
handleSliderPromptClose () {
this.setState({sliderPrompt: false});
}
handleSliderPromptOpen () {
this.setState({sliderPrompt: true});
}
handleSliderPromptOk (min, max, isDiscrete) {
const realMin = Math.min(min, max);
const realMax = Math.max(min, max);
this.props.vm.runtime.requestUpdateMonitor(Map({
id: this.props.id,
sliderMin: realMin,
sliderMax: realMax,
isDiscrete: isDiscrete
}));
this.handleSliderPromptClose();
}
setElement (monitorElt) {
this.element = monitorElt;
}
handleImport () {
importCSV().then(async ({rows, text}) => {
const numberOfColumns = rows[0].length;
let columnNumber = 1;
if (numberOfColumns > 1) {
const msg = this.props.intl.formatMessage(messages.columnPrompt, {numberOfColumns});
// prompt() returns Promise in desktop app
columnNumber = parseInt(await prompt(msg), 10); // eslint-disable-line no-alert
}
let newListValue;
if (isNaN(columnNumber) || numberOfColumns === 1) {
newListValue = text.replace(/\r/g, '').split('\n');
} else {
newListValue = rows.map(row => row[columnNumber - 1])
.filter(item => typeof item === 'string'); // CSV importer can leave undefineds
}
const {vm, targetId, id: variableId} = this.props;
setVariableValue(vm, targetId, variableId, newListValue);
});
}
handleExport () {
const {vm, targetId, id: variableId} = this.props;
const variable = getVariable(vm, targetId, variableId);
const text = variable.value.join('\r\n');
const blob = new Blob([text], {type: 'text/plain;charset=utf-8'});
downloadBlob(`${variable.name}.txt`, blob);
}
render () {
const monitorProps = monitorAdapter(this.props);
const showSliderOption = availableModes(this.props.opcode).indexOf('slider') !== -1;
const isList = this.props.mode === 'list';
const isImage = this.props.mode === 'image';
return (
<React.Fragment>
{this.state.sliderPrompt && <SliderPrompt
isDiscrete={this.props.isDiscrete}
maxValue={parseFloat(this.props.max)}
minValue={parseFloat(this.props.min)}
onCancel={this.handleSliderPromptClose}
onOk={this.handleSliderPromptOk}
/>}
<MonitorComponent
componentRef={this.setElement}
{...monitorProps}
opcode={this.props.opcode}
draggable={this.props.draggable}
height={this.props.height}
isDiscrete={this.props.isDiscrete}
max={this.props.max}
min={this.props.min}
mode={this.props.mode}
targetId={this.props.targetId}
width={this.props.width}
onDragEnd={this.handleDragEnd}
onExport={isList || isImage ? this.handleExport : null}
onImport={isList || isImage ? this.handleImport : null}
onHide={this.handleHide}
onNextMode={this.handleNextMode}
onSetModeToDefault={isList || isImage ? null : this.handleSetModeToDefault}
onSetModeToLarge={isList || isImage ? null : this.handleSetModeToLarge}
onSetModeToSlider={showSliderOption ? this.handleSetModeToSlider : null}
onSliderPromptOpen={this.handleSliderPromptOpen}
/>
</React.Fragment>
);
}
}
Monitor.propTypes = {
addMonitorRect: PropTypes.func.isRequired,
draggable: PropTypes.bool,
height: PropTypes.number,
id: PropTypes.string.isRequired,
intl: intlShape,
isDiscrete: PropTypes.bool,
max: PropTypes.number,
min: PropTypes.number,
mode: PropTypes.oneOf(['default', 'slider', 'large', 'list']),
monitorLayout: PropTypes.shape({
monitors: PropTypes.object, // eslint-disable-line react/forbid-prop-types
savedMonitorPositions: PropTypes.object // eslint-disable-line react/forbid-prop-types
}).isRequired,
onDragEnd: PropTypes.func.isRequired,
opcode: PropTypes.string.isRequired, // eslint-disable-line react/no-unused-prop-types
params: PropTypes.object, // eslint-disable-line react/no-unused-prop-types, react/forbid-prop-types
removeMonitorRect: PropTypes.func.isRequired,
resizeMonitorRect: PropTypes.func.isRequired,
spriteName: PropTypes.string, // eslint-disable-line react/no-unused-prop-types
targetId: PropTypes.string,
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.arrayOf(PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]))
]), // eslint-disable-line react/no-unused-prop-types
vm: PropTypes.instanceOf(VM),
width: PropTypes.number,
x: PropTypes.number,
y: PropTypes.number
};
const mapStateToProps = state => ({
monitorLayout: state.scratchGui.monitorLayout,
vm: state.scratchGui.vm
});
const mapDispatchToProps = dispatch => ({
addMonitorRect: (id, rect, savePosition) =>
dispatch(addMonitorRect(id, rect.upperStart, rect.lowerEnd, savePosition)),
resizeMonitorRect: (id, newWidth, newHeight) => dispatch(resizeMonitorRect(id, newWidth, newHeight)),
removeMonitorRect: id => dispatch(removeMonitorRect(id))
});
export default injectIntl(connect(
mapStateToProps,
mapDispatchToProps
)(Monitor));
|