p5.js-web-editor/client/modules/Mobile/MobilePreferences.jsx

175 lines
5.0 KiB
React
Raw Normal View History

import React from 'react';
2020-06-23 02:06:40 +02:00
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { withRouter } from 'react-router';
2020-06-23 02:06:40 +02:00
import PropTypes from 'prop-types';
import styled from 'styled-components';
import * as PreferencesActions from '../IDE/actions/preferences';
import * as IdeActions from '../IDE/actions/ide';
import IconButton from '../../components/mobile/IconButton';
2020-06-23 02:06:40 +02:00
import Screen from '../../components/mobile/MobileScreen';
import Header from '../../components/mobile/Header';
import PreferencePicker from '../../components/mobile/PreferencePicker';
2020-06-23 02:06:40 +02:00
import { ExitIcon } from '../../common/icons';
import { remSize, prop } from '../../theme';
2020-06-23 02:06:40 +02:00
const Content = styled.div`
z-index: 0;
margin-top: ${remSize(68)};
`;
const SectionHeader = styled.h2`
color: ${prop('primaryTextColor')};
padding-top: ${remSize(32)};
`;
const SectionSubeader = styled.h3`
color: ${prop('primaryTextColor')};
`;
const optionsOnOff = (name, onLabel = 'On', offLabel = 'Off') => [
{
value: true, label: onLabel, ariaLabel: `${name} on`, name: `${name}`, id: `${name}-on`.replace(' ', '-')
},
{
value: false, label: offLabel, ariaLabel: `${name} off`, name: `${name}`, id: `${name}-off`.replace(' ', '-')
},
];
const optionsPickOne = (name, ...options) => options.map(option => ({
value: option,
label: option,
ariaLabel: `${option} ${name} on`,
name: `${option} ${name}`,
id: `${option}-${name}-on`.replace(' ', '-')
}));
2020-06-23 02:06:40 +02:00
const MobilePreferences = (props) => {
const {
2020-06-23 22:27:43 +02:00
setTheme, setAutosave, setLinewrap, setTextOutput, setGridOutput, setSoundOutput, lineNumbers, lintWarning
} = props;
const {
2020-06-23 22:27:43 +02:00
theme, autosave, linewrap, textOutput, gridOutput, soundOutput, setLineNumbers, setLintWarning
} = props;
const generalSettings = [
2020-06-23 02:06:40 +02:00
{
title: 'Theme',
value: theme,
options: optionsPickOne('theme', 'light', 'dark', 'contrast'),
onSelect: x => setTheme(x) // setTheme
2020-06-23 02:06:40 +02:00
},
{
title: 'Autosave',
value: autosave,
options: optionsOnOff('autosave'),
onSelect: x => setAutosave(x) // setAutosave
},
{
title: 'Word Wrap',
value: linewrap,
options: optionsOnOff('linewrap'),
onSelect: x => setLinewrap(x)
2020-06-23 02:06:40 +02:00
}
];
2020-06-23 22:27:43 +02:00
const outputSettings = [
{
title: 'Plain-text',
value: textOutput,
options: optionsOnOff('text output'),
onSelect: x => setTextOutput(x)
},
{
title: 'Table-text',
value: gridOutput,
options: optionsOnOff('table output'),
onSelect: x => setGridOutput(x)
},
{
title: 'Sound',
value: soundOutput,
options: optionsOnOff('sound output'),
onSelect: x => setSoundOutput(x)
},
];
2020-06-23 22:27:43 +02:00
const accessibilitySettings = [
{
title: 'Line Numbers',
value: lineNumbers,
options: optionsOnOff('line numbers'),
2020-06-23 22:27:43 +02:00
onSelect: x => setLineNumbers(x)
},
{
title: 'Lint Warning Sound',
value: lintWarning,
options: optionsOnOff('lint warning'),
2020-06-23 22:27:43 +02:00
onSelect: x => setLintWarning(x)
},
];
2020-06-23 02:06:40 +02:00
return (
2020-06-23 21:18:34 +02:00
<Screen fullscreen>
<section>
<Header transparent title="Preferences">
<IconButton to="/mobile" icon={ExitIcon} aria-label="Return to ide view" />
</Header>
2020-06-23 21:18:34 +02:00
<section className="preferences">
<Content>
<SectionHeader>General Settings</SectionHeader>
{ generalSettings.map(option => <PreferencePicker key={`${option.title}wrapper`} {...option} />) }
<SectionHeader>Accessibility</SectionHeader>
{ accessibilitySettings.map(option => <PreferencePicker key={`${option.title}wrapper`} {...option} />) }
2020-06-23 22:27:43 +02:00
<SectionHeader>Accessible Output</SectionHeader>
<SectionSubeader>Used with screen reader</SectionSubeader>
{ outputSettings.map(option => <PreferencePicker key={`${option.title}wrapper`} {...option} />) }
2020-06-23 22:27:43 +02:00
2020-06-23 21:18:34 +02:00
</Content>
</section>
2020-06-23 02:06:40 +02:00
</section>
</Screen>);
};
2020-06-23 22:27:43 +02:00
2020-06-23 02:06:40 +02:00
MobilePreferences.propTypes = {
fontSize: PropTypes.number.isRequired,
lineNumbers: PropTypes.bool.isRequired,
autosave: PropTypes.bool.isRequired,
linewrap: PropTypes.bool.isRequired,
textOutput: PropTypes.bool.isRequired,
gridOutput: PropTypes.bool.isRequired,
soundOutput: PropTypes.bool.isRequired,
lintWarning: PropTypes.bool.isRequired,
theme: PropTypes.string.isRequired,
setLinewrap: PropTypes.func.isRequired,
setLintWarning: PropTypes.func.isRequired,
setTheme: PropTypes.func.isRequired,
setFontSize: PropTypes.func.isRequired,
setLineNumbers: PropTypes.func.isRequired,
setAutosave: PropTypes.func.isRequired,
setTextOutput: PropTypes.func.isRequired,
setGridOutput: PropTypes.func.isRequired,
setSoundOutput: PropTypes.func.isRequired,
};
const mapStateToProps = state => ({
...state.preferences,
2020-06-23 02:06:40 +02:00
});
const mapDispatchToProps = dispatch => bindActionCreators({
...PreferencesActions,
...IdeActions
}, dispatch);
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(MobilePreferences));