11import React from 'react' ;
22import PropTypes from 'prop-types' ;
3- import { Provider } from 'react-redux' ;
4- import { createStore , combineReducers , compose } from 'redux' ;
5- import ConnectedIntlProvider from './connected-intl-provider.jsx' ;
63
7- import localesReducer , { initLocale , localesInitialState } from '../reducers/locales' ;
8-
9- import { setPlayer , setFullScreen } from '../reducers/mode.js' ;
10-
11- import locales from 'scratch-l10n' ;
12- import { detectLocale } from './detect-locale' ;
13-
14- const composeEnhancers = window . __REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose ;
4+ import { EditorState } from './editor-state' ;
5+ import { AppStateProviderHOC } from './app-state-provider-hoc' ;
156
167/**
178 * Higher Order Component to provide redux state. If an `intl` prop is provided
@@ -26,95 +17,27 @@ const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
2617 * @returns {React.Component } component with redux and intl state provided
2718 */
2819const AppStateHOC = function ( WrappedComponent , localesOnly , configFactory ) {
20+ const AppStateProvider = AppStateProviderHOC ( WrappedComponent ) ;
21+
2922 class AppStateWrapper extends React . Component {
3023 constructor ( props ) {
3124 super ( props ) ;
32- let initialState = { } ;
33- let reducers = { } ;
34- let enhancer ;
35-
36- let initializedLocales = localesInitialState ;
37- const locale = detectLocale ( Object . keys ( locales ) ) ;
38- if ( locale !== 'en' ) {
39- initializedLocales = initLocale ( initializedLocales , locale ) ;
40- }
41- if ( localesOnly ) {
42- // Used for instantiating minimal state for the unsupported
43- // browser modal
44- reducers = { locales : localesReducer } ;
45- initialState = { locales : initializedLocales } ;
46- enhancer = composeEnhancers ( ) ;
47- } else {
48- // You are right, this is gross. But it's necessary to avoid
49- // importing unneeded code that will crash unsupported browsers.
50- const guiRedux = require ( '../reducers/gui' ) ;
51- const guiReducer = guiRedux . default ;
52- const {
53- buildInitialState,
54- guiMiddleware,
55- initFullScreen,
56- initPlayer,
57- initTelemetryModal
58- } = guiRedux ;
59- const { ScratchPaintReducer} = require ( 'scratch-paint' ) ;
6025
61- const configOrLegacy = configFactory ?
62- configFactory ( ) :
63- require ( '../legacy-config' ) . legacyConfig ;
64-
65- let initializedGui = buildInitialState ( configOrLegacy ) ;
66- if ( props . isFullScreen || props . isPlayerOnly ) {
67- if ( props . isFullScreen ) {
68- initializedGui = initFullScreen ( initializedGui ) ;
69- }
70- if ( props . isPlayerOnly ) {
71- initializedGui = initPlayer ( initializedGui ) ;
72- }
73- } else if ( props . showTelemetryModal ) {
74- initializedGui = initTelemetryModal ( initializedGui ) ;
75- }
76- reducers = {
77- locales : localesReducer ,
78- scratchGui : guiReducer ,
79- scratchPaint : ScratchPaintReducer
80- } ;
81- initialState = {
82- locales : initializedLocales ,
83- scratchGui : initializedGui
84- } ;
85- enhancer = composeEnhancers ( guiMiddleware ) ;
86- }
87- const reducer = combineReducers ( reducers ) ;
88- this . store = createStore (
89- reducer ,
90- initialState ,
91- enhancer
92- ) ;
93- }
94- componentDidUpdate ( prevProps ) {
95- if ( localesOnly ) return ;
96- if ( prevProps . isPlayerOnly !== this . props . isPlayerOnly ) {
97- this . store . dispatch ( setPlayer ( this . props . isPlayerOnly ) ) ;
98- }
99- if ( prevProps . isFullScreen !== this . props . isFullScreen ) {
100- this . store . dispatch ( setFullScreen ( this . props . isFullScreen ) ) ;
101- }
26+ this . appState = new EditorState ( {
27+ localesOnly,
28+ isFullScreen : props . isFullScreen ,
29+ isPlayerOnly : props . isPlayerOnly ,
30+ showTelemetryModal : props . showTelemetryModal
31+ } , configFactory ) ;
10232 }
33+
10334 render ( ) {
104- const {
105- isFullScreen, // eslint-disable-line no-unused-vars
106- isPlayerOnly, // eslint-disable-line no-unused-vars
107- showTelemetryModal, // eslint-disable-line no-unused-vars
108- ...componentProps
109- } = this . props ;
11035 return (
111- < Provider store = { this . store } >
112- < ConnectedIntlProvider >
113- < WrappedComponent
114- { ...componentProps }
115- />
116- </ ConnectedIntlProvider >
117- </ Provider >
36+ < AppStateProvider
37+ appState = { this . appState }
38+ localesOnly = { localesOnly }
39+ { ...this . props }
40+ />
11841 ) ;
11942 }
12043 }
0 commit comments