@@ -37,6 +37,8 @@ style.innerHTML = `[data-click-to-component-target] {
37
37
document . head . appendChild ( style ) ;
38
38
39
39
const root = "__ROOT__" ;
40
+ const base = "__BASE__" ;
41
+ const originWithBase = window . location . origin + base . slice ( 0 , - 1 ) ;
40
42
let currentTarget : HTMLElement | undefined ;
41
43
let hasMenu = false ;
42
44
const menuElement = document . createElement ( "div" ) ;
@@ -123,7 +125,9 @@ window.addEventListener("contextmenu", (event) => {
123
125
spanR . textContent = layer . path . replace ( `${ root } /` , "" ) ;
124
126
item . appendChild ( spanR ) ;
125
127
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
+ ) ;
127
131
cleanUp ( ) ;
128
132
} ) ;
129
133
menuElement . appendChild ( item ) ;
@@ -160,6 +164,7 @@ const getLayersForElement = (element: Element) => {
160
164
const layers : { name : string ; path : string } [ ] = [ ] ;
161
165
while ( instance ) {
162
166
const path = getPath ( instance ) ;
167
+ console . log ( path ) ;
163
168
if ( path ) {
164
169
const name =
165
170
typeof instance . type === "string"
@@ -169,21 +174,26 @@ const getLayersForElement = (element: Element) => {
169
174
?? instance . type . render ?. name
170
175
?? "undefined" ) ;
171
176
layers . push ( { name, path } ) ;
177
+ } else {
178
+ console . debug ( "Couldn't find a React instance for the element" , instance ) ;
172
179
}
173
180
instance = instance . _debugOwner ;
174
181
}
175
-
176
182
return layers ;
177
183
} ;
178
184
179
185
const 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 ) ;
184
196
}
185
- const { columnNumber = 1 , fileName, lineNumber = 1 } = source ;
186
- return `${ fileName } :${ lineNumber } :${ columnNumber } ` ;
187
197
} ;
188
198
189
199
type Source = {
@@ -193,8 +203,8 @@ type Source = {
193
203
} ;
194
204
type Fiber = {
195
205
_debugSource ?: Source ;
196
- _debugInfo ?: Source ; // Injected by React jsxDev patch for React 19
197
206
_debugOwner ?: Fiber ;
207
+ _debugStack ?: Error ;
198
208
type :
199
209
| string
200
210
| { displayName ?: string ; name ?: string ; render ?: ( ) => unknown } ;
0 commit comments