From 6f479e1b3e62f620434e9a3824ef68452585bbcc Mon Sep 17 00:00:00 2001 From: Sebastien Carlier Date: Sat, 1 Sep 2018 18:55:26 +0000 Subject: [PATCH] platform setting cleanup --- backend/options.js | 14 +++++++++-- backend/server.js | 6 ++--- frontend/common/examples.js | 10 ++++---- frontend/common/menu.js | 38 ++++++++++++++++------------- frontend/common/options.js | 5 ++++ frontend/index.js | 8 +++---- frontend/lang/index.js | 40 ++++++++++++++++--------------- frontend/stepper/arduino/index.js | 4 ++-- frontend/stepper/io/index.js | 6 ++--- frontend/stepper/translate.js | 6 ++--- frontend/stepper/views/main.js | 4 ++-- 11 files changed, 81 insertions(+), 60 deletions(-) diff --git a/backend/options.js b/backend/options.js index 149d510f1..299bbbb58 100644 --- a/backend/options.js +++ b/backend/options.js @@ -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: {}, }; @@ -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) { diff --git a/backend/server.js b/backend/server.js index ed04b1c0d..16035740b 100644 --- a/backend/server.js +++ b/backend/server.js @@ -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++'; } @@ -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); diff --git a/frontend/common/examples.js b/frontend/common/examples.js index 16c0b89a4..73c527051 100644 --- a/frontend/common/examples.js +++ b/frontend/common/examples.js @@ -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; diff --git a/frontend/common/menu.js b/frontend/common/menu.js index dc7475a19..598da397c 100644 --- a/frontend/common/menu.js +++ b/frontend/common/menu.js @@ -1,6 +1,6 @@ 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); @@ -8,7 +8,7 @@ export default function (bundle) { 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 ( @@ -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 + }; } diff --git a/frontend/common/options.js b/frontend/common/options.js index 2ffd133ba..a88636a62 100644 --- a/frontend/common/options.js +++ b/frontend/common/options.js @@ -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)); } diff --git a/frontend/index.js b/frontend/index.js index c24bb3435..d2bb1db4b 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -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); @@ -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, @@ -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) */ diff --git a/frontend/lang/index.js b/frontend/lang/index.js index 5c535a864..af45d3856 100644 --- a/frontend/lang/index.js +++ b/frontend/lang/index.js @@ -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) { @@ -75,30 +72,35 @@ class LanguageSelection extends React.PureComponent { render() { const {language, getMessage} = this.props; return ( -
-
+ ); } 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}; } diff --git a/frontend/stepper/arduino/index.js b/frontend/stepper/arduino/index.js index 7f1654ccf..5bc38d847 100644 --- a/frontend/stepper/arduino/index.js +++ b/frontend/stepper/arduino/index.js @@ -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')); } }); diff --git a/frontend/stepper/io/index.js b/frontend/stepper/io/index.js index 72a64f3af..e78d4c6a5 100644 --- a/frontend/stepper/io/index.js +++ b/frontend/stepper/io/index.js @@ -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}; diff --git a/frontend/stepper/translate.js b/frontend/stepper/translate.js index c830918bc..992c4ae4e 100644 --- a/frontend/stepper/translate.js +++ b/frontend/stepper/translate.js @@ -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()}; } @@ -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]}; diff --git a/frontend/stepper/views/main.js b/frontend/stepper/views/main.js index d2b66f397..cd0decfaf 100644 --- a/frontend/stepper/views/main.js +++ b/frontend/stepper/views/main.js @@ -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';