1- import { useEffect , useLayoutEffect , useState } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22import { useNavigate } from 'react-router-dom' ;
33import { useSelector , useDispatch } from 'react-redux' ;
44import { useIntl } from 'react-intl' ;
@@ -15,19 +15,9 @@ import {
1515import { useSandboxOAuthCallback } from '../hooks/UseSandboxOAuthCallback' ;
1616import { getValidTokenOrInitiateAuth , fetchSandboxLicense } from '../utils/sandboxUtils' ;
1717import { useOsmFeaturesQuery } from '../api/projects' ;
18- import {
19- registerIdEditorStylesheet ,
20- activateIdEditorStylesheet ,
21- } from '../utils/idEditorStylesheets' ;
22-
23- // @osm -sandbox/sandbox-id has no real ESM/CJS exports — it only assigns
24- // itself to window.iD as a side effect on import. Capture it here, right
25- // after the imports above force that assignment, so this module keeps its
26- // own private reference instead of the shared global, which
27- // @openstreetmap /id (imported by editor.js) later overwrites.
28- const sandboxID = window . iD ;
18+ import { captureIdEditorPackage , resolveIdEditorContext } from '../utils/idEditorContext' ;
2919
30- registerIdEditorStylesheet ( 'sandbox' ) ;
20+ const sandboxID = captureIdEditorPackage ( ) ;
3121
3222export default function SandboxEditor ( {
3323 setDisable,
@@ -62,14 +52,6 @@ export default function SandboxEditor({
6252
6353 useSandboxOAuthCallback ( sandboxId ) ;
6454
65- // Only one of the OSM iD editor / Sandbox editor is ever mounted at a
66- // time, but both of their stylesheets stay loaded for the whole page
67- // session once visited. Disable the other one's so its rules can't leak
68- // into this editor via their shared ".ideditor" root class.
69- useLayoutEffect ( ( ) => {
70- activateIdEditorStylesheet ( 'sandbox' ) ;
71- } , [ ] ) ;
72-
7355 const customSource =
7456 iDContext && iDContext . background ( ) && iDContext . background ( ) . findSource ( 'custom' ) ;
7557
@@ -90,10 +72,9 @@ export default function SandboxEditor({
9072 } , [ customImageryIsSet , imagery , iDContext , customSource ] ) ;
9173
9274 useEffect ( ( ) => {
93- if ( iDContext === null ) {
94- // we need to keep iD context on redux store because iD works better if
95- // the context is not restarted while running in the same browser session
96- dispatch ( { type : 'SET_EDITOR' , context : sandboxID . coreContext ( ) } ) ;
75+ const context = resolveIdEditorContext ( iDContext , 'sandbox' , ( ) => sandboxID . coreContext ( ) ) ;
76+ if ( context && context !== iDContext ) {
77+ dispatch ( { type : 'SET_EDITOR' , context } ) ;
9778 }
9879 } , [ iDContext , dispatch ] ) ;
9980
@@ -106,7 +87,14 @@ export default function SandboxEditor({
10687 // Initialize sandbox editor
10788 useEffect ( ( ) => {
10889 const initializeSandbox = async ( ) => {
109- if ( ! session || ! locale || ! iD || ! iDContext || isInitialized ) {
90+ if (
91+ ! session ||
92+ ! locale ||
93+ ! iD ||
94+ ! iDContext ||
95+ iDContext . __idEditorType !== 'sandbox' ||
96+ isInitialized
97+ ) {
11098 return ;
11199 }
112100 const authStatus = sandboxAuthStatus ?. [ sandboxId ] ;
@@ -151,7 +139,16 @@ export default function SandboxEditor({
151139 . setsDocumentTitle ( false )
152140 . containerNode ( document . getElementById ( 'id-container' ) ) ;
153141
154- // init the ui or restart if it was loaded previously
142+ // @osm -sandbox/sandbox-id has no dark theme of its own, but it shares the
143+ // "ideditor" root class with @openstreetmap/id, which does have one keyed
144+ // off the OS/browser's prefers-color-scheme. Force light explicitly so
145+ // this editor doesn't pick up a dark theme it was never styled for.
146+ iDContext . container ( ) . classed ( 'theme-light' , true ) ;
147+
148+ // init the ui or restart if it was loaded previously. Either path ends
149+ // with connection().switch() below, which resets the (module-scoped,
150+ // session-wide) tile cache and triggers a fresh load from the current
151+ // view — so a reused/fresh context never serves stale, pre-edit data.
155152 if ( iDContext . ui ( ) !== undefined ) {
156153 iDContext . reset ( ) ;
157154 iDContext . ui ( ) . restart ( ) ;
@@ -288,9 +285,6 @@ export default function SandboxEditor({
288285 return ( ) => {
289286 // Reset auth status for this sandbox on unmount
290287 dispatch ( setSandboxAuthStatus ( sandboxId , 'idle' ) ) ;
291- // Reset context on unmount so the OSM iD editor always gets a fresh context
292- // from its own iD module (@openstreetmap/id), preventing cross-editor context bleed.
293- dispatch ( { type : 'SET_EDITOR' , context : null } ) ;
294288 } ;
295289 } , [ dispatch , sandboxId ] ) ;
296290
0 commit comments