Navigation Menu

Skip to content

Commit

Permalink
platform setting cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Carlier committed Sep 1, 2018
1 parent 3d4f369 commit 6f479e1
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 60 deletions.
14 changes: 12 additions & 2 deletions backend/options.js
Expand Up @@ -7,7 +7,8 @@ export function buildCommonOptions(start, query) {
showStack: true,
showViews: true,
showIO: true,
mode: 'plain',
platform: 'unix',
canChangePlatform: /sandbox|recorder/.test(start),
controls: {},
};

Expand Down Expand Up @@ -38,7 +39,16 @@ export function buildCommonOptions(start, query) {
options.showIO = false;
}
if ('mode' in query) {
options.mode = query.mode; // 'plain'|'arduino'
/* Deprecated */
switch (query.mode) {
case 'plain': options.platform = 'unix';
case 'arduino': options.platform = 'arduino';
}
options.canChangePlatform = false;
}
if ('platform' in query) {
options.platform = query.platform;
options.canChangePlatform = false;
}

if ('source' in query) {
Expand Down
6 changes: 3 additions & 3 deletions backend/server.js
Expand Up @@ -187,8 +187,8 @@ function addBackendRoutes (app, config, store) {
app.post('/translate', function (req, res) {
const env = {LANGUAGE: 'c'};
env.SYSROOT = path.join(config.rootDir, 'sysroot');
const {source, mode} = req.body;
if (mode === 'arduino') {
const {source, platform} = req.body;
if (platform === 'arduino') {
env.SOURCE_WRAPPER = "wrappers/Arduino";
env.LANGUAGE = 'c++';
}
Expand Down Expand Up @@ -222,7 +222,7 @@ function addBackendRoutes (app, config, store) {
try {
let ast = JSON.parse(chunks.join(''));
const convert = new AnsiToHtml();
if (mode === 'arduino') {
if (platform === 'arduino') {
ast = Arduino.transform(ast);
}
directives.enrichSyntaxTree(source, ast);
Expand Down
10 changes: 5 additions & 5 deletions frontend/common/examples.js
Expand Up @@ -21,17 +21,17 @@ function updateExamplesState (state, examples) {
const {callbackUrl, examplesUrl} = examples;
if (!examplesUrl) return false;
/* Clean up the callback URL to avoid passing the current source to the
examples selector. Also clear 'mode' in case the user changes it in
the selector. */
examples selector. Also clear 'platform' in case the user changes it
in the selector. */
let fullCallbackUrl = url.parse(callbackUrl, true);
delete fullCallbackUrl.search; // force url.format to rebuild the search string
delete fullCallbackUrl.query.source;
delete fullCallbackUrl.query.mode;
delete fullCallbackUrl.query.platform;
fullCallbackUrl = url.format(fullCallbackUrl);
const {mode, language} = state.get('options');
const {platform, language} = state.get('options');
let fullExamplesUrl = url.parse(examplesUrl, true);
fullExamplesUrl.query.target = '_self';
fullExamplesUrl.query.tags = mode;
fullExamplesUrl.query.tags = platform;
/* XXX better to pass language unchanged and have the examples app drop the country code */
fullExamplesUrl.query.lang = language.replace(/_.*$/, '');
fullExamplesUrl.query.callback = fullCallbackUrl;
Expand Down
38 changes: 21 additions & 17 deletions frontend/common/menu.js
@@ -1,14 +1,14 @@

import React from 'react';
import {Button, ButtonGroup, Dialog} from '@blueprintjs/core';
import {Button, ButtonGroup, Dialog, Label} from '@blueprintjs/core';

export default function (bundle) {
bundle.defineView('Menu', MenuSelector, Menu);
};

class Menu extends React.PureComponent {
render () {
const {getMessage, SubtitlesMenu, FullscreenButton, LanguageSelection, ExamplePicker, platform} = this.props;
const {getMessage, SubtitlesMenu, FullscreenButton, LanguageSelection, ExamplePicker, platform, canChangePlatform} = this.props;
const {isOpen} = this.state;
return (
<div id='menu'>
Expand All @@ -22,18 +22,19 @@ class Menu extends React.PureComponent {
<div style={{marginBottom: '10px'}}>
<LanguageSelection closeMenu={this.closeMenu} />
</div>
<div className='bp3-select'>
<label className='bp3-label'>
{getMessage('PLATFORM_SETTING')}
<select onChange={this.setPlatform} value={platform}>
<option value='plain'>{getMessage('PLATFORM_UNIX')}</option>
<option value='arduino'>{getMessage('PLATFORM_ARDUINO')}</option>
</select>
</label>
</div>
<div>
<ExamplePicker />
</div>
{canChangePlatform &&
<div>
<label className='bp3-label'>
{getMessage('PLATFORM_SETTING')}
<div className='bp3-select'>
<select onChange={this.setPlatform} value={platform}>
<option value='unix'>{getMessage('PLATFORM_UNIX')}</option>
<option value='arduino'>{getMessage('PLATFORM_ARDUINO')}</option>
</select>
</div>
</label>
</div>}
<ExamplePicker />
</div>
</Dialog>
</div>
Expand All @@ -44,14 +45,17 @@ class Menu extends React.PureComponent {
closeMenu = () => { this.setState({isOpen: false}); };
setPlatform = (event) => {
const platform = event.target.value;
this.props.dispatch({type: this.props.optionsChanged, payload: {mode: {$set: platform}}});
this.props.dispatch({type: this.props.optionsChanged, payload: {platform: {$set: platform}}});
};
}

function MenuSelector (state, props) {
const {FullscreenButton, SubtitlesMenu, LanguageSelection, ExamplePicker} = state.get('scope');
const {optionsChanged} = state.get('actionTypes');
const {mode} = state.get('options');
const {platform, canChangePlatform} = state.get('options');
const getMessage = state.get('getMessage');
return {getMessage, FullscreenButton, SubtitlesMenu, LanguageSelection, ExamplePicker, platform: mode, optionsChanged};
return {
getMessage, FullscreenButton, SubtitlesMenu, LanguageSelection, ExamplePicker,
platform, canChangePlatform, optionsChanged
};
}
5 changes: 5 additions & 0 deletions frontend/common/options.js
Expand Up @@ -3,9 +3,14 @@ import update from 'immutability-helper';

export default function (bundle) {
bundle.defineAction('optionsChanged', 'Options.Changed');
bundle.addReducer('init', initReducer);
bundle.addReducer('optionsChanged', optionsChangedReducer);
}

function initReducer (state, {payload: {options}}) {
return state.set('options', options);
}

function optionsChangedReducer (state, {payload: changes}) {
return state.update('options', options => update(options, changes));
}
8 changes: 4 additions & 4 deletions frontend/index.js
Expand Up @@ -26,8 +26,8 @@ import editorBundle from './editor/index';
const {store, scope, actionTypes, views, finalize, start} = link(function (bundle, deps) {

bundle.defineAction('init', 'System.Init');
bundle.addReducer('init', (_state, {payload: options}) =>
Immutable.Map({scope, actionTypes, views, options}));
bundle.addReducer('init', (_state, _action) =>
Immutable.Map({scope, actionTypes, views}));

bundle.include(commonBundle);
bundle.include(sandboxBundle);
Expand Down Expand Up @@ -69,7 +69,7 @@ const Codecast = window.Codecast = {store, scope, restart};
examplesUrl: url,
baseDataUrl: url,
user: {…},
mode: 'plain'|'arduino',
platform: 'unix'|'arduino',
controls: {…},
showStepper: boolean,
showStack: boolean,
Expand All @@ -82,7 +82,7 @@ const Codecast = window.Codecast = {store, scope, restart};
*/
Codecast.start = function (options) {

store.dispatch({type: scope.init, payload: options});
store.dispatch({type: scope.init, payload: {options}});
// XXX store.dispatch({type: scope.stepperConfigure, options: stepperOptions});

/* Run the sagas (must be done before calling autoLogin) */
Expand Down
40 changes: 21 additions & 19 deletions frontend/lang/index.js
Expand Up @@ -34,25 +34,22 @@ const Message = {
};
Object.defineProperty(Message, 's', { get() { return this.toString(); } });

function initReducer (state, _action) {
let language = navigator.language;
try {
if (window.localStorage.language) {
language = window.localStorage.language;
}
} catch (ex) {
// No local storage access.
function initReducer (state, {payload: {options}}) {
let language = 'en-US';
if (navigator.language in Languages) {
language = navigator.language;
}
if (window.localStorage.language && window.localStorage.language in Languages) {
language = window.localStorage.language;
}
if (language in options && options.language in Languages) {
language = options.language;
}
return setLanguageReducer(state, {payload: {language}});
}

function setLanguageReducer (state, {payload: {language}}) {
if (!Languages[language]) language = 'en-US';
try {
window.localStorage.language = language;
} catch (ex) {
// No local storage access.
}
const localizedMessage = Object.create(Message,
{_l: {writable: false, configurable: false, value: language}});
const getMessage = memoize(function (message, defaultText) {
Expand All @@ -75,30 +72,35 @@ class LanguageSelection extends React.PureComponent {
render() {
const {language, getMessage} = this.props;
return (
<div className='bp3-select'>
<label className='bp3-label'>
{getMessage('LANGUAGE:')}
<label className='bp3-label'>
{getMessage('LANGUAGE:')}
<div className='bp3-select'>
<select onChange={this.setLanguage} value={language}>
{languageKeys.map(lang => {
const label = Languages[lang].language;
return <option key={lang} value={lang}>{label}</option>;
})}
</select>
</label>
</div>
</div>
</label>
);
}
setLanguage = (event) => {
const language = event.target.value;
const {closeMenu, dispatch, setLanguage} = this.props;
closeMenu();
try {
window.localStorage.language = language;
} catch (ex) {
// No local storage access.
}
setTimeout(() => dispatch({type: setLanguage, payload: {language}}), 0);
};
}

function LanguageSelectionSelector (state) {
const {setLanguage} = state.get('actionTypes');
const {language} = state.get('options');
const language = state.get('language');
const getMessage = state.get('getMessage');
return {setLanguage, language, getMessage};
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/stepper/arduino/index.js
Expand Up @@ -325,8 +325,8 @@ export default function (bundle, deps) {
bundle.defer(function ({recordApi, replayApi, stepperApi}) {

recordApi.onStart(function* (init) {
const {mode} = yield select(state => state.get('options'));
if (mode === 'arduino') {
const {platform} = yield select(state => state.get('options'));
if (platform === 'arduino') {
init.arduino = yield select(state => state.get('arduino'));
}
});
Expand Down
6 changes: 3 additions & 3 deletions frontend/stepper/io/index.js
Expand Up @@ -32,9 +32,9 @@ export default function (bundle, deps) {
});

function updateIoPaneState (state, ioPane) {
const {mode} = state.get('options');
if (mode === 'arduino') {
/* Arduino mode is forced to terminal mode. */
const {platform} = state.get('options');
if (platform === 'arduino') {
/* Arduino is forced to terminal mode. */
return {mode: 'terminal', modeSelect: false};
}
return {mode: ioPane.mode || 'terminal', modeSelect: true};
Expand Down
6 changes: 3 additions & 3 deletions frontend/stepper/translate.js
Expand Up @@ -89,12 +89,12 @@ export default function (bundle, deps) {
yield takeLatest(deps.translate, function* (action) {
const sourceModel = yield select(deps.getBufferModel, 'source');
const source = sourceModel.get('document').toString();
const {mode} = yield select(state => state.get('options'));
const {platform} = yield select(state => state.get('options'));
yield put({type: deps.translateStarted, source});
let response, syntaxTree;
try {
/* XXX replace 'translate' with a computed absolute path */
response = yield call(asyncRequestJson, 'translate', {source, mode});
response = yield call(asyncRequestJson, 'translate', {source, platform});
} catch (ex) {
response = {error: ex.toString()};
}
Expand All @@ -116,7 +116,7 @@ export default function (bundle, deps) {

recordApi.on(deps.translateStarted, function* (addEvent, action) {
const {source} = action;
yield call(addEvent, 'translate.start', source);
yield call(addEvent, 'translate.start', source); // XXX should also have platform
});
replayApi.on(['stepper.translate', 'translate.start'], function (replayContext, event) {
const action = {source: event[2]};
Expand Down
4 changes: 2 additions & 2 deletions frontend/stepper/views/main.js
Expand Up @@ -167,8 +167,8 @@ function StepperViewSelector (state, props) {
const haveStepper = !!stepperDisplay;
const error = haveStepper && stepperDisplay.error;
const readOnly = haveStepper || props.preventInput;
const {showIO, showViews, showStack, mode} = state.get('options');
const arduinoEnabled = mode === 'arduino';
const {showIO, showViews, showStack, platform} = state.get('options');
const arduinoEnabled = platform === 'arduino';
/* TODO: make number of visible rows in source editor configurable. */
const sourceRowHeight = `${Math.ceil(16 * 25)}px`; // 12*25 for /next
const sourceMode = arduinoEnabled ? 'arduino' : 'c_cpp';
Expand Down

0 comments on commit 6f479e1

Please sign in to comment.