@@ -39,8 +39,8 @@ export interface IRouter {
3939}
4040
4141export enum EditMode {
42- Normal = 'normal ' ,
43- MapEditor = 'mapEditor '
42+ Default = 'default ' ,
43+ Map = 'map '
4444}
4545
4646export class EditorState extends Disposable {
@@ -80,7 +80,7 @@ export class EditorState extends Disposable {
8080 editing : editing . Editing
8181 stageState : StageEditorState
8282 spriteState : SpriteEditorState | null = null
83- private selectedEditModeRef = ref < EditMode > ( EditMode . Normal )
83+ private selectedEditModeRef = ref < EditMode > ( EditMode . Default )
8484 private selectedTypeRef = ref < SelectedType > ( 'sprite' )
8585 private selectedSpriteIdRef = ref < string | null > ( null )
8686 private selectedSoundIdRef = ref < string | null > ( null )
@@ -242,7 +242,27 @@ export class EditorState extends Disposable {
242242 }
243243
244244 private selectByRoute ( path : PathSegments ) {
245- const [ segment , extra ] = shiftPath ( path )
245+ let [ segment , extra ] = shiftPath ( path )
246+
247+ switch ( segment ) {
248+ // Temporarily retain `sounds` and `stage` routes for `map` mode.
249+ // Although this deviates from the plan (where `map` mode only supports `sprites`),
250+ // it is done to minimize the complexity of handling additional route redirects.
251+ // For instance, navigating from `Sound` to `Map` might otherwise cause the history stack to shift from `map/sound` to `map/sprites/default/code`,
252+ // increasing the cost of stack maintenance.
253+ case EditMode . Map :
254+ this . selectEditMode ( EditMode . Map )
255+ break
256+ case EditMode . Default :
257+ default :
258+ this . selectEditMode ( EditMode . Default )
259+ break
260+ }
261+
262+ if ( this . selectedEditMode !== EditMode . Default ) {
263+ ; [ segment , extra ] = shiftPath ( extra )
264+ }
265+
246266 switch ( segment ) {
247267 case 'stage' :
248268 this . select ( { type : 'stage' } )
@@ -264,21 +284,37 @@ export class EditorState extends Disposable {
264284 }
265285
266286 private getRoute ( ) : PathSegments {
287+ const pathSegments = [ ]
267288 const selected = this . selected
289+ const editMode = this . selectedEditMode
290+
291+ if ( editMode !== EditMode . Default ) {
292+ pathSegments . push ( editMode )
293+ }
294+
268295 switch ( selected . type ) {
269296 case 'stage' :
270- return [ 'stage' , ...this . stageState . getRoute ( ) ]
297+ pathSegments . push ( 'stage' , ...this . stageState . getRoute ( ) )
298+ break
271299 case 'sprite' : {
272300 const sprite = selected . sprite
273- if ( sprite == null || this . spriteState == null ) return [ 'sprites' ]
274- return [ 'sprites' , sprite . name , ...this . spriteState . getRoute ( ) ]
301+ pathSegments . push ( 'sprites' )
302+ if ( sprite != null && this . spriteState != null ) {
303+ pathSegments . push ( sprite . name , ...this . spriteState . getRoute ( ) )
304+ }
305+ break
275306 }
276307 case 'sound' : {
277308 const sound = selected . sound
278- if ( sound == null ) return [ 'sounds' ]
279- return [ 'sounds' , sound . name ]
309+ pathSegments . push ( 'sounds' )
310+ if ( sound != null ) {
311+ pathSegments . push ( sound . name )
312+ }
313+ break
280314 }
281315 }
316+
317+ return pathSegments
282318 }
283319
284320 private updateRouter ( router : IRouter , replace : boolean ) {
@@ -320,7 +356,12 @@ export class EditorState extends Disposable {
320356 // Sync from selected state to router
321357 this . addDisposer (
322358 watch (
323- ( ) => this . selected ,
359+ // Why watch?
360+ // We can switch to map without watching, but we need the map route in the history stack.
361+ // This requires responding to changes in `selectedEditMode`.
362+ // Scenario: Sound -> Stage -> Map -> Click Back.
363+ // With watch: Back to Stage. Without watch: Back to Sound.
364+ ( ) => [ this . selected , this . selectedEditMode ] ,
324365 ( _ , __ , onCleanup ) => {
325366 // If `selected` changes, push new route
326367 this . updateRouter ( router , false )
0 commit comments