-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathuseOpenElementSource.ts
More file actions
43 lines (37 loc) · 1.27 KB
/
useOpenElementSource.ts
File metadata and controls
43 lines (37 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useAttachDocumentListener } from "./useAttachListener.js"
import { useDevServerConnection } from "./useDevServerConnection.js"
const useOpenElementSource = () => {
const { sendJsonMessage } = useDevServerConnection()
useAttachDocumentListener("contextmenu", (e: any) => {
if (!e.shiftKey || !e) {
return
}
e.stopPropagation()
e.preventDefault()
const target = e.target as HTMLElement
const rdtSource = target?.getAttribute("data-rrdt-source")
if (rdtSource) {
const [source, line, column] = rdtSource.split(":")
return sendJsonMessage({
type: "open-source",
data: { source, line, column },
})
}
for (const key in e.target) {
if (key.startsWith("__reactFiber")) {
const fiberNode = (e.target as any)[key]
const originalSource = fiberNode?._debugSource
const source = fiberNode?._debugOwner?._debugSource ?? fiberNode?._debugSource
const line = source?.fileName?.startsWith("/") ? originalSource?.lineNumber : source?.lineNumber
const fileName = source?.fileName?.startsWith("/") ? originalSource?.fileName : source?.fileName
if (fileName && line) {
return sendJsonMessage({
type: "open-source",
data: { source: fileName, line, column: 0 },
})
}
}
}
})
}
export { useOpenElementSource }