@@ -37,6 +37,8 @@ style.innerHTML = `[data-click-to-component-target] {
3737document . head . appendChild ( style ) ;
3838
3939const root = "__ROOT__" ;
40+ const base = "__BASE__" ;
41+ const originWithBase = window . location . origin + base . slice ( 0 , - 1 ) ;
4042let currentTarget : HTMLElement | undefined ;
4143let hasMenu = false ;
4244const menuElement = document . createElement ( "div" ) ;
@@ -123,7 +125,9 @@ window.addEventListener("contextmenu", (event) => {
123125 spanR . textContent = layer . path . replace ( `${ root } /` , "" ) ;
124126 item . appendChild ( spanR ) ;
125127 item . addEventListener ( "click" , ( ) => {
126- void fetch ( `/__open-in-editor?file=${ encodeURIComponent ( layer . path ) } ` ) ;
128+ void fetch (
129+ `${ base } __open-in-editor?file=${ encodeURIComponent ( layer . path ) } ` ,
130+ ) ;
127131 cleanUp ( ) ;
128132 } ) ;
129133 menuElement . appendChild ( item ) ;
@@ -160,6 +164,7 @@ const getLayersForElement = (element: Element) => {
160164 const layers : { name : string ; path : string } [ ] = [ ] ;
161165 while ( instance ) {
162166 const path = getPath ( instance ) ;
167+ console . log ( path ) ;
163168 if ( path ) {
164169 const name =
165170 typeof instance . type === "string"
@@ -169,21 +174,26 @@ const getLayersForElement = (element: Element) => {
169174 ?? instance . type . render ?. name
170175 ?? "undefined" ) ;
171176 layers . push ( { name, path } ) ;
177+ } else {
178+ console . debug ( "Couldn't find a React instance for the element" , instance ) ;
172179 }
173180 instance = instance . _debugOwner ;
174181 }
175-
176182 return layers ;
177183} ;
178184
179185const getPath = ( fiber : Fiber ) => {
180- const source = fiber . _debugSource ?? fiber . _debugInfo ;
181- if ( ! source ) {
182- console . debug ( "Couldn't find a React instance for the element" , fiber ) ;
183- return ;
186+ if ( fiber . _debugSource ) {
187+ const { columnNumber = 1 , fileName, lineNumber = 1 } = fiber . _debugSource ;
188+ return `${ fileName } :${ lineNumber } :${ columnNumber } ` ;
189+ } else if ( fiber . _debugStack ) {
190+ const componentLine = fiber . _debugStack . stack ?. split ( "\n" ) [ 2 ] ;
191+ if ( ! componentLine ) return ;
192+ if ( ! componentLine . endsWith ( ")" ) ) return ;
193+ const startBracketIndex = componentLine . indexOf ( "(" ) ;
194+ const url = componentLine . slice ( startBracketIndex + 1 , - 1 ) ;
195+ return url . replace ( originWithBase , root ) ;
184196 }
185- const { columnNumber = 1 , fileName, lineNumber = 1 } = source ;
186- return `${ fileName } :${ lineNumber } :${ columnNumber } ` ;
187197} ;
188198
189199type Source = {
@@ -193,8 +203,8 @@ type Source = {
193203} ;
194204type Fiber = {
195205 _debugSource ?: Source ;
196- _debugInfo ?: Source ; // Injected by React jsxDev patch for React 19
197206 _debugOwner ?: Fiber ;
207+ _debugStack ?: Error ;
198208 type :
199209 | string
200210 | { displayName ?: string ; name ?: string ; render ?: ( ) => unknown } ;
0 commit comments