@@ -122,7 +122,8 @@ const blocklyMpdernTheme = Blockly.Theme.defineTheme('modern', {
122122} ) ;
123123
124124interface BlocklyEditorProps {
125- tabname : string ;
125+ tabId : string ;
126+ tabName : string ;
126127}
127128
128129/**
@@ -146,19 +147,19 @@ function blocklyToPython(ws: Workspace) {
146147 * BlocklyEditor component
147148 * @returns
148149 */
149- function BlocklyEditor ( { tabname } : BlocklyEditorProps ) {
150+ function BlocklyEditor ( { tabId , tabName } : BlocklyEditorProps ) {
150151 const [ toolboxKey , setToolboxKey ] = useState ( 0 ) ; // Force re-render when toolbox updates
151152 const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
152153 const [ isListenerSet , setIsListenerSet ] = useState ( false ) ;
153- const [ name , setName ] = useState < string > ( tabname ) ;
154+ const [ name , setName ] = useState < string > ( tabName ) ;
154155 const nameRef = useRef ( name ) ;
155156
156157 /**
157158 * handleOnInject
158159 */
159160 function handleOnInject ( ws : Workspace ) {
160161 // save the ws for this editor session
161- const editorSession = EditorMgr . getInstance ( ) . getEditorSession ( name ) ;
162+ const editorSession = EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ;
162163 if ( editorSession ) {
163164 editorSession . workspace = ws ;
164165 }
@@ -168,16 +169,15 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
168169 * saveEditor
169170 */
170171 const saveEditor = useCallback ( async ( ) => {
171- const currentName = nameRef . current ;
172- const ws = EditorMgr . getInstance ( ) . getEditorSession ( currentName ) ?. workspace ;
172+ const ws = EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ?. workspace ;
173173 if ( ws ) {
174174 const activeTab = localStorage . getItem ( StorageKeys . ACTIVETAB ) ?. replace ( / ^ " | " $ / g, '' ) ;
175- if ( activeTab === currentName ) {
175+ if ( activeTab === tabId ) {
176176 const code = blocklyToPython ( ws ) ;
177177 console . log ( 'Saving blockly' , activeTab , code ) ;
178- await EditorMgr . getInstance ( ) . saveEditor ( currentName , code ) ;
178+ await EditorMgr . getInstance ( ) . saveEditor ( tabId , code ) ;
179179 EditorMgr . getInstance ( ) . SaveToLocalStorage (
180- EditorMgr . getInstance ( ) . getEditorSession ( currentName ) as EditorSession ,
180+ EditorMgr . getInstance ( ) . getEditorSession ( tabId ) as EditorSession ,
181181 code
182182 ) ;
183183 }
@@ -198,11 +198,11 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
198198
199199 useEffect ( ( ) => {
200200 if (
201- EditorMgr . getInstance ( ) . hasEditorSession ( nameRef . current ) &&
202- ! EditorMgr . getInstance ( ) . hasSubscription ( nameRef . current )
201+ EditorMgr . getInstance ( ) . hasEditorSession ( tabId ) &&
202+ ! EditorMgr . getInstance ( ) . hasSubscription ( tabId )
203203 ) {
204204 AppMgr . getInstance ( ) . on ( EventType . EVENT_THEME , ( theme ) => {
205- const ws = EditorMgr . getInstance ( ) . getEditorSession ( nameRef . current ) ?. workspace ;
205+ const ws = EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ?. workspace ;
206206
207207 if ( ws ) {
208208 // Not sure why the compiler complain but it works at runtime
@@ -214,26 +214,26 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
214214
215215 AppMgr . getInstance ( ) . on ( EventType . EVENT_EDITOR_LOAD , ( content ) => {
216216 const loadContent = JSON . parse ( content ) ;
217- if ( loadContent . name !== nameRef . current ) return ;
217+ const session : EditorSession | undefined =
218+ EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ;
219+ if ( loadContent . name !== nameRef . current || loadContent . path !== session ?. path ) return ;
218220
219- const ws = EditorMgr . getInstance ( ) . getEditorSession ( nameRef . current ) ?. workspace ;
221+ const ws = EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ?. workspace ;
220222 if ( ws ) {
221223 Blockly . serialization . workspaces . load ( JSON . parse ( loadContent . content ) , ws ) ;
222224 // @ts -expect-error - it is a valid function
223225 ws . scrollCenter ( ) ;
224226 // @ts -expect-error - it is a valid function
225227 ws . zoomToFit ( ) ;
226228 }
227- const session : EditorSession | undefined =
228- EditorMgr . getInstance ( ) . getEditorSession ( loadContent . name ) ;
229229 if ( session ) {
230230 EditorMgr . getInstance ( ) . SaveToLocalStorage ( session , loadContent . content ) ;
231231 }
232232 } ) ;
233233
234234 AppMgr . getInstance ( ) . on ( EventType . EVENT_EDITOR , ( type ) => {
235235 if ( type === EditorType . BLOCKLY ) {
236- const ws = EditorMgr . getInstance ( ) . getEditorSession ( nameRef . current ) ?. workspace ;
236+ const ws = EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ?. workspace ;
237237 if ( ws ) {
238238 console . log ( 'rescrolling to center!' )
239239 // @ts -expect-error - it is a valid function
@@ -248,7 +248,7 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
248248
249249 AppMgr . getInstance ( ) . on ( EventType . EVENT_BLOCKLY_TOOLBOX_UPDATED , ( ) => {
250250 // Force a re-render of the Blockly workspace to show new blocks
251- const ws = EditorMgr . getInstance ( ) . getEditorSession ( nameRef . current ) ?. workspace ;
251+ const ws = EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ?. workspace ;
252252 if ( ws ) {
253253 // Save current workspace content
254254 const content = Blockly . serialization . workspaces . save ( ws ) ;
@@ -271,7 +271,7 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
271271 } ) ;
272272
273273 AppMgr . getInstance ( ) . on ( EventType . EVENT_GENPYTHON , ( activeTab ) => {
274- if ( nameRef . current === activeTab ) {
274+ if ( tabId === activeTab ) {
275275 const session : EditorSession | undefined =
276276 EditorMgr . getInstance ( ) . getEditorSession ( activeTab ) ;
277277 if ( session ) {
@@ -288,21 +288,23 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
288288 saveEditor ( ) ;
289289 } ) ;
290290
291- AppMgr . getInstance ( ) . on ( EventType . EVENT_EDITOR_NAME_CHANGED , ( newName ) => {
292- setName ( newName ) ;
291+ AppMgr . getInstance ( ) . on ( EventType . EVENT_EDITOR_NAME_CHANGED , ( names ) => {
292+ const newNames = JSON . parse ( names ) ;
293+ if ( newNames . oldId !== nameRef . current ) return ;
294+ setName ( newNames . newId ) ;
293295 } ) ;
294296
295- EditorMgr . getInstance ( ) . setSubscription ( nameRef . current ) ;
297+ EditorMgr . getInstance ( ) . setSubscription ( tabId ) ;
296298
297299 } else {
298- const editorSession = EditorMgr . getInstance ( ) . getEditorSession ( nameRef . current ) ;
300+ const editorSession = EditorMgr . getInstance ( ) . getEditorSession ( tabId ) ;
299301 if ( editorSession && editorSession . content ) {
300302 // There appears to be some timing issues in loading the content into the workspace
301303 // Set 100 ms delay to accommendate the timing issue
302304 const loadEditor = ( name : string , content : string ) => {
303305 const lines : string [ ] | undefined = content . split ( '##XRPBLOCKS ' ) ;
304306 const blockContent = lines . length > 1 ? lines [ 1 ] : lines [ 0 ] ;
305- const loadContent = { name : name , content : blockContent } ;
307+ const loadContent = { name : name , path : editorSession . path , content : blockContent } ;
306308 AppMgr . getInstance ( ) . emit ( EventType . EVENT_EDITOR_LOAD , JSON . stringify ( loadContent ) ) ;
307309 } ;
308310 setTimeout ( loadEditor , 100 , nameRef . current , editorSession . content ) ;
@@ -336,9 +338,9 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
336338 if ( event . type === Blockly . Events . VIEWPORT_CHANGE || event . isUiEvent ) { return ; }
337339 try {
338340 console . log ( 'Workspace changed, saving session:' , nameRef . current ) ;
339- EditorMgr . getInstance ( ) . updateEditorSessionChange ( nameRef . current , true ) ;
341+ EditorMgr . getInstance ( ) . updateEditorSessionChange ( tabId , true ) ;
340342 const code = blocklyToPython ( ws ) ;
341- EditorMgr . getInstance ( ) . SaveToLocalStorage ( EditorMgr . getInstance ( ) . getEditorSession ( nameRef . current ) as EditorSession , code ) ;
343+ EditorMgr . getInstance ( ) . SaveToLocalStorage ( EditorMgr . getInstance ( ) . getEditorSession ( tabId ) as EditorSession , code ) ;
342344 } catch ( e ) {
343345 console . warn ( 'Failed to serialize Blockly workspace:' , e ) ;
344346 }
@@ -366,7 +368,7 @@ function BlocklyEditor({ tabname }: BlocklyEditorProps) {
366368 ws . removeChangeListener ( listener ) ;
367369 }
368370 }
369- } , [ isListenerSet , isLoading , saveEditor ] ) ;
371+ } , [ isListenerSet , isLoading , saveEditor , tabId ] ) ;
370372
371373 return (
372374 < BlocklyWorkspace
0 commit comments