55 Button ,
66 ConfigProvider ,
77 Flex ,
8- Input ,
98 message ,
109 Popconfirm ,
1110 Space ,
@@ -16,7 +15,6 @@ import {
1615 theme ,
1716 Upload
1817} from 'antd' ;
19- import type { InputRef } from 'antd' ;
2018import type { TreeDataNode , TreeProps } from 'antd' ;
2119import type { UploadProps } from 'antd' ;
2220import {
@@ -55,8 +53,6 @@ import * as editor from './editor/editor';
5553import { extendedPythonGenerator } from './editor/extended_python_generator' ;
5654import { createGeneratorContext , GeneratorContext } from './editor/generator_context' ;
5755
58- import * as toolboxItems from './toolbox/items' ;
59- import * as toolbox from './toolbox/toolbox' ;
6056//import { testAllBlocksInToolbox } from './toolbox/toolbox_tests';
6157import ToolboxSettingsModal from './toolbox/settings' ;
6258
@@ -173,6 +169,9 @@ const App: React.FC = () => {
173169 } , [ storage ] ) ;
174170
175171 const fetchMostRecentModulePath = async ( ) => {
172+ if ( ! storage ) {
173+ return ;
174+ }
176175 try {
177176 const value = await storage . fetchEntry ( 'mostRecentModulePath' , '' ) ;
178177 setMostRecentModulePath ( value ) ;
@@ -183,6 +182,9 @@ const App: React.FC = () => {
183182 } ;
184183
185184 const initializeShownPythonToolboxCategories = async ( ) => {
185+ if ( ! storage ) {
186+ return ;
187+ }
186188 try {
187189 const value = await storage . fetchEntry ( 'shownPythonToolboxCategories' , '[]' ) ;
188190 const shownCategories : string [ ] = JSON . parse ( value ) ;
@@ -206,6 +208,10 @@ const App: React.FC = () => {
206208
207209 const fetchListOfModules = async ( ) : Promise < commonStorage . Project [ ] > => {
208210 return new Promise ( async ( resolve , reject ) => {
211+ if ( ! storage ) {
212+ reject ( new Error ( ) ) ;
213+ return ;
214+ }
209215 try {
210216 const array = await storage . listModules ( ) ;
211217 setModules ( array )
@@ -348,7 +354,7 @@ const App: React.FC = () => {
348354 }
349355 if ( currentModule && blocklyComponent . current && generatorContext . current ) {
350356 const blocklyWorkspace = blocklyComponent . current . getBlocklyWorkspace ( ) ;
351- setGeneratedCode ( extendedPythonGenerator . workspaceToCode (
357+ setGeneratedCode ( extendedPythonGenerator . mrcWorkspaceToCode (
352358 blocklyWorkspace , generatorContext . current ) ) ;
353359 } else {
354360 setGeneratedCode ( '' ) ;
@@ -462,6 +468,9 @@ const App: React.FC = () => {
462468 } ;
463469
464470 const handleNewProjectNameOk = async ( newProjectClassName : string ) => {
471+ if ( ! storage || ! currentModule ) {
472+ return ;
473+ }
465474 const newProjectName = commonStorage . classNameToModuleName ( newProjectClassName ) ;
466475 const newProjectPath = commonStorage . makeProjectPath ( newProjectName ) ;
467476 if ( newProjectNameModalPurpose === PURPOSE_NEW_PROJECT ) {
@@ -553,7 +562,10 @@ const App: React.FC = () => {
553562 } ;
554563
555564 const handleNewModuleNameOk = async ( newModuleClassName : string ) => {
556- const newModuleName = commonStorage . classNameToModuleName ( newModuleClassName ) ;
565+ if ( ! storage || ! currentModule ) {
566+ return ;
567+ }
568+ const newModuleName = commonStorage . classNameToModuleName ( newModuleClassName ) ;
557569 const newModulePath = commonStorage . makeModulePath ( currentModule . projectName , newModuleName ) ;
558570 if ( newModuleNameModalPurpose === PURPOSE_NEW_MECHANISM ) {
559571 const mechanismContent = commonStorage . newMechanismContent (
@@ -612,26 +624,30 @@ const App: React.FC = () => {
612624 } ;
613625
614626 const handleSaveClicked = async ( ) => {
615- saveBlocks ( ( success ) => { } ) ;
627+ saveBlocks ( ) ;
616628 } ;
617629
618- const saveBlocks = async ( ) : boolean => {
619- if ( blocksEditor . current && currentModulePath ) {
630+ const saveBlocks = async ( ) : Promise < boolean > => {
631+ return new Promise ( async ( resolve , reject ) => {
632+ if ( ! blocksEditor . current || ! currentModulePath ) {
633+ reject ( new Error ( ) ) ;
634+ return ;
635+ }
620636 try {
621637 await blocksEditor . current . saveBlocks ( ) ;
622638 messageApi . open ( {
623639 type : 'success' ,
624640 content : 'Save completed successfully.' ,
625641 } ) ;
626- return true ;
627- } catch ( e : Error ) {
642+ resolve ( true ) ;
643+ } catch ( e ) {
628644 console . log ( 'Failed to save the blocks. Caught the following error...' ) ;
629645 console . log ( e ) ;
630646 setAlertErrorMessage ( 'Failed to save the blocks.' ) ;
631647 setAlertErrorVisible ( true ) ;
648+ reject ( new Error ( 'Failed to save the blocks.' ) ) ;
632649 }
633- }
634- return false ;
650+ } ) ;
635651 } ;
636652
637653 const handleRenameClicked = ( ) => {
@@ -715,7 +731,7 @@ const App: React.FC = () => {
715731 afterPopconfirmOk . current = ( ) => {
716732 setOpenPopconfirm ( false ) ;
717733 checkIfBlocksWereModified ( async ( ) => {
718- if ( ! currentModule ) {
734+ if ( ! storage || ! currentModule ) {
719735 return ;
720736 }
721737 if ( currentModule . moduleType == commonStorage . MODULE_TYPE_PROJECT ) {
@@ -779,40 +795,48 @@ const App: React.FC = () => {
779795 }
780796 return isBlocks || Upload . LIST_IGNORE ;
781797 } ,
782- onChange : ( info ) => {
783- } ,
784798 customRequest : ( { file, onSuccess, onError } ) => {
799+ if ( ! onSuccess || ! onError ) {
800+ return ;
801+ }
802+ const fileObject = file as File ;
785803 const reader = new FileReader ( ) ;
786804 reader . onload = async ( event ) => {
787- const dataUrl = event . target . result ;
788- const uploadProjectName = commonStorage . makeUploadProjectName ( file . name , getProjectNames ( ) ) ;
805+ const dataUrl = event . target ?. result ;
806+ if ( ! storage || ! dataUrl ) {
807+ return ;
808+ }
809+ const uploadProjectName = commonStorage . makeUploadProjectName ( fileObject . name , getProjectNames ( ) ) ;
789810 try {
790- await storage . uploadProject ( uploadProjectName , dataUrl ) ;
811+ await storage . uploadProject ( uploadProjectName , dataUrl as string ) ;
791812 onSuccess ( 'Upload successful' ) ;
792813 await fetchListOfModules ( ) ;
793814 const uploadProjectPath = commonStorage . makeProjectPath ( uploadProjectName ) ;
794815 setCurrentModulePath ( uploadProjectPath ) ;
795816 } catch ( e ) {
796817 console . log ( 'Failed to upload the project. Caught the following error...' ) ;
797818 console . log ( e ) ;
798- onError ( 'Failed to upload the project.' ) ;
819+ onError ( new Error ( 'Failed to upload the project.' ) ) ;
799820 setAlertErrorMessage ( 'Failed to upload the project' ) ;
800821 setAlertErrorVisible ( true ) ;
801822 }
802823 } ;
803824 reader . onerror = ( error ) => {
804825 console . log ( 'Failed to upload the project. reader.onerror called with the following error...' ) ;
805826 console . log ( error ) ;
806- onError ( 'Failed to upload the project.' ) ;
827+ onError ( new Error ( 'Failed to upload the project.' ) ) ;
807828 setAlertErrorMessage ( 'Failed to upload the project' ) ;
808829 setAlertErrorVisible ( true ) ;
809830 } ;
810- reader . readAsDataURL ( file ) ;
831+ reader . readAsDataURL ( fileObject ) ;
811832 } ,
812833 } ;
813834
814835 const handleDownloadClicked = ( ) => {
815836 checkIfBlocksWereModified ( async ( ) => {
837+ if ( ! storage || ! currentModule ) {
838+ return ;
839+ }
816840 try {
817841 const url = await storage . downloadProject ( currentModule . projectName ) ;
818842 const link = document . createElement ( 'a' ) ;
@@ -833,13 +857,16 @@ const App: React.FC = () => {
833857 } ;
834858
835859 const handleToolboxSettingsOk = async ( updatedShownCategories : Set < string > ) => {
860+ if ( ! storage ) {
861+ return ;
862+ }
836863 setShownPythonToolboxCategories ( updatedShownCategories ) ;
837864 const array = Array . from ( updatedShownCategories ) ;
838865 array . sort ( ) ;
839866 storage . saveEntry ( 'shownPythonToolboxCategories' , JSON . stringify ( array ) ) ;
840867 } ;
841868
842- const handleModuleSelected : TreeProps [ 'onSelect' ] = ( a : React . Key [ ] , e ) => {
869+ const handleModuleSelected : TreeProps [ 'onSelect' ] = ( a : React . Key [ ] ) => {
843870 if ( a . length === 1 ) {
844871 checkIfBlocksWereModified ( ( ) => {
845872 setTreeSelectedKey ( a [ 0 ] ) ;
0 commit comments