11import React from 'react' ;
2-
32import * as Antd from 'antd' ;
4- import * as I18Next from "react-i18next" ;
53
6- import * as Blockly from 'blockly/core' ;
4+ import {
5+ SettingOutlined ,
6+ } from '@ant-design/icons' ;
77
8- import { createGeneratorContext , GeneratorContext } from './editor/generator_context' ;
8+
9+ import * as Blockly from 'blockly/core' ;
10+ import { pythonGenerator } from 'blockly/python' ;
911
1012import Header from './reactComponents/Header' ;
1113import Footer from './reactComponents/Footer' ;
@@ -14,38 +16,30 @@ import CodeDisplay from './reactComponents/CodeDisplay';
1416import BlocklyComponent , { BlocklyComponentType } from './reactComponents/BlocklyComponent' ;
1517import ToolboxSettingsModal from './reactComponents/ToolboxSettings' ;
1618
19+ import { createGeneratorContext , GeneratorContext } from './editor/generator_context' ;
20+ import * as editor from './editor/editor' ;
21+ import { extendedPythonGenerator } from './editor/extended_python_generator'
22+
1723import * as commonStorage from './storage/common_storage' ;
1824import * as clientSideStorage from './storage/client_side_storage' ;
19- import * as editor from './editor/editor' ;
2025
2126import * as CustomBlocks from './blocks/setup_custom_blocks' ;
2227import { initialize as initializeGeneratedBlocks } from './blocks/utils/generated/initialize' ;
23-
24- import { pythonGenerator } from 'blockly/python' ;
25- import { extendedPythonGenerator } from './editor/extended_python_generator'
26-
2728import * as ChangeFramework from './blocks/utils/change_framework'
2829import { mutatorOpenListener } from './blocks/mrc_class_method_def'
2930
30- import {
31- SettingOutlined ,
32- } from '@ant-design/icons' ;
33-
3431const App : React . FC = ( ) => {
35- const { t } = I18Next . useTranslation ( ) ;
3632 const [ alertErrorMessage , setAlertErrorMessage ] = React . useState ( '' ) ;
3733 const [ storage , setStorage ] = React . useState < commonStorage . Storage | null > ( null ) ;
3834 const [ currentModule , setCurrentModule ] = React . useState < commonStorage . Module | null > ( null ) ;
39-
4035 const [ messageApi , contextHolder ] = Antd . message . useMessage ( ) ;
4136 const [ generatedCode , setGeneratedCode ] = React . useState < string > ( '' ) ;
4237 const [ toolboxSettingsModalIsOpen , setToolboxSettingsModalIsOpen ] = React . useState ( false ) ;
38+
4339 const [ shownPythonToolboxCategories , setShownPythonToolboxCategories ] = React . useState < Set < string > > ( new Set ( ) ) ;
4440 const blocksEditor = React . useRef < editor . Editor | null > ( null ) ;
4541 const [ triggerPythonRegeneration , setTriggerPythonRegeneration ] = React . useState ( 0 ) ;
4642 const generatorContext = React . useRef < GeneratorContext | null > ( null ) ;
47-
48-
4943 const blocklyComponent = React . useRef < BlocklyComponentType | null > ( null ) ;
5044
5145 // When the app is loaded, open storage and initialize the blocks we provide.
@@ -61,39 +55,6 @@ const App: React.FC = () => {
6155 }
6256 } , [ currentModule ] ) ;
6357
64- const openStorage = async ( ) => {
65- try {
66- const c = await clientSideStorage . openClientSideStorage ( ) ;
67- setStorage ( c ) ;
68- } catch ( e ) {
69- console . log ( 'Failed to open client side storage. Caught the following error...' ) ;
70- console . log ( e ) ;
71- }
72- } ;
73-
74- const initializeBlocks = ( ) => {
75- // Initialize blocks and extended python generator.
76- const forBlock = Object . create ( null ) ;
77- CustomBlocks . setup ( forBlock ) ;
78- Object . assign ( pythonGenerator . forBlock , forBlock ) ;
79- Object . assign ( extendedPythonGenerator . forBlock , pythonGenerator . forBlock ) ;
80- initializeGeneratedBlocks ( ) ;
81- } ;
82-
83- const initializeShownPythonToolboxCategories = async ( ) => {
84- if ( ! storage ) {
85- return ;
86- }
87- try {
88- const value = await storage . fetchEntry ( 'shownPythonToolboxCategories' , '[]' ) ;
89- const shownCategories : string [ ] = JSON . parse ( value ) ;
90- setShownPythonToolboxCategories ( new Set ( shownCategories ) ) ;
91- } catch ( e ) {
92- console . log ( 'Failed to fetch shownPythonToolboxCategories. Caught the following error...' ) ;
93- console . log ( e ) ;
94- }
95- } ;
96-
9758 React . useEffect ( ( ) => {
9859 if ( blocksEditor . current ) {
9960 blocksEditor . current . updateToolbox ( shownPythonToolboxCategories ) ;
@@ -127,6 +88,45 @@ const App: React.FC = () => {
12788 }
12889 } , [ currentModule , triggerPythonRegeneration , blocklyComponent ] ) ;
12990
91+ React . useEffect ( ( ) => {
92+ if ( blocksEditor . current ) {
93+ blocksEditor . current . updateToolbox ( shownPythonToolboxCategories ) ;
94+ }
95+ } , [ currentModule , shownPythonToolboxCategories ] ) ;
96+
97+ const openStorage = async ( ) => {
98+ try {
99+ const c = await clientSideStorage . openClientSideStorage ( ) ;
100+ setStorage ( c ) ;
101+ } catch ( e ) {
102+ console . log ( 'Failed to open client side storage. Caught the following error...' ) ;
103+ console . log ( e ) ;
104+ }
105+ } ;
106+
107+ const initializeBlocks = ( ) => {
108+ // Initialize blocks and extended python generator.
109+ const forBlock = Object . create ( null ) ;
110+ CustomBlocks . setup ( forBlock ) ;
111+ Object . assign ( pythonGenerator . forBlock , forBlock ) ;
112+ Object . assign ( extendedPythonGenerator . forBlock , pythonGenerator . forBlock ) ;
113+ initializeGeneratedBlocks ( ) ;
114+ } ;
115+
116+ const initializeShownPythonToolboxCategories = async ( ) => {
117+ if ( ! storage ) {
118+ return ;
119+ }
120+ try {
121+ const value = await storage . fetchEntry ( 'shownPythonToolboxCategories' , '[]' ) ;
122+ const shownCategories : string [ ] = JSON . parse ( value ) ;
123+ setShownPythonToolboxCategories ( new Set ( shownCategories ) ) ;
124+ } catch ( e ) {
125+ console . log ( 'Failed to fetch shownPythonToolboxCategories. Caught the following error...' ) ;
126+ console . log ( e ) ;
127+ }
128+ } ;
129+
130130 const handleBlocksChanged = ( event : Blockly . Events . Abstract ) => {
131131 if ( event . isUiEvent ) {
132132 // UI events are things like scrolling, zooming, etc.
@@ -146,12 +146,6 @@ const App: React.FC = () => {
146146 }
147147 setTriggerPythonRegeneration ( Date . now ( ) ) ;
148148 } ;
149-
150- React . useEffect ( ( ) => {
151- if ( blocksEditor . current ) {
152- blocksEditor . current . updateToolbox ( shownPythonToolboxCategories ) ;
153- }
154- } , [ currentModule , shownPythonToolboxCategories ] ) ;
155149
156150 const handleToolboxSettingsClicked = ( ) => {
157151 setToolboxSettingsModalIsOpen ( true ) ;
@@ -214,16 +208,16 @@ const App: React.FC = () => {
214208 </ Antd . Splitter . Panel >
215209 < Antd . Splitter . Panel min = '2%' defaultSize = '50%' >
216210 < Antd . Space >
217- < Antd . Tooltip title = "Toolbox Settings" >
218- < Antd . Button className = "smallButton"
219- icon = { < SettingOutlined /> }
220- size = "small"
221- onClick = { handleToolboxSettingsClicked }
222- >
223- </ Antd . Button >
224- </ Antd . Tooltip >
225- </ Antd . Space >
226- < BlocklyComponent ref = { blocklyComponent } />
211+ < Antd . Tooltip title = "Toolbox Settings" >
212+ < Antd . Button className = "smallButton"
213+ icon = { < SettingOutlined /> }
214+ size = "small"
215+ onClick = { handleToolboxSettingsClicked }
216+ >
217+ </ Antd . Button >
218+ </ Antd . Tooltip >
219+ </ Antd . Space >
220+ < BlocklyComponent ref = { blocklyComponent } />
227221 </ Antd . Splitter . Panel >
228222 < Antd . Splitter . Panel min = '2%' >
229223 < CodeDisplay generatedCode = { generatedCode }
0 commit comments