Skip to content

Commit cca0ed2

Browse files
author
Timothy Johnson
committed
Highlight bounding rects on node hover
1 parent 5833c59 commit cca0ed2

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/client/index.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,85 @@ window.__svelte_devtools_select_element = function(element) {
1010
if (node) window.postMessage({ type: 'inspect', node: serializeNode(node) })
1111
}
1212

13+
const hoverArea = document.createElement('div')
14+
hoverArea.style.position = 'fixed'
15+
hoverArea.style.backgroundColor = 'rgba(0, 136, 204, 0.2)'
16+
hoverArea.style.zIndex = '2147483647'
17+
18+
const hoverX = document.createElement('div')
19+
hoverX.style.position = 'fixed'
20+
hoverX.style.borderStyle = 'dashed'
21+
hoverX.style.borderColor = 'rgb(0, 136, 204)'
22+
hoverX.style.borderWidth = '1px 0'
23+
hoverX.style.zIndex = '2147483647'
24+
hoverX.style.left = '0'
25+
hoverX.style.width = '100vw'
26+
27+
const hoverY = document.createElement('div')
28+
hoverY.style.position = 'fixed'
29+
hoverY.style.borderStyle = 'dashed'
30+
hoverY.style.borderColor = 'rgb(0, 136, 204)'
31+
hoverY.style.borderWidth = '0 1px'
32+
hoverY.style.zIndex = '2147483647'
33+
hoverY.style.top = '0'
34+
hoverY.style.height = '100vh'
35+
36+
function getBoundingRect(node) {
37+
if (node.type == 'element') return node.detail.getBoundingClientRect()
38+
39+
const maxRect = {
40+
top: Infinity,
41+
left: Infinity,
42+
bottom: -Infinity,
43+
right: -Infinity
44+
}
45+
46+
for (const child of node.children) {
47+
const rect = getBoundingRect(child)
48+
if (rect.top < maxRect.top) maxRect.top = rect.top
49+
if (rect.left < maxRect.left) maxRect.left = rect.left
50+
if (rect.bottom > maxRect.bottom) maxRect.bottom = rect.bottom
51+
if (rect.right > maxRect.right) maxRect.right = rect.right
52+
}
53+
54+
maxRect.width = maxRect.right - maxRect.left
55+
maxRect.height = maxRect.bottom - maxRect.top
56+
57+
return maxRect
58+
}
59+
1360
window.addEventListener('message', e => handleMessage(e.data), false)
1461

1562
function handleMessage(msg) {
63+
const node = getNode(msg.nodeId)
64+
1665
switch (msg.type) {
1766
case 'setSelected':
18-
const node = getNode(msg.nodeId)
1967
if (node) window.$s = node.detail
68+
break
69+
70+
case 'setHover':
71+
if (!node) {
72+
hoverArea.remove()
73+
hoverX.remove()
74+
hoverY.remove()
75+
break
76+
}
77+
78+
const box = getBoundingRect(node)
79+
hoverArea.style.top = box.top + 'px'
80+
hoverArea.style.left = box.left + 'px'
81+
hoverArea.style.width = box.width + 'px'
82+
hoverArea.style.height = box.height + 'px'
83+
document.body.append(hoverArea)
84+
85+
hoverX.style.top = box.top + 'px'
86+
hoverX.style.height = box.height - 2 + 'px'
87+
document.body.append(hoverX)
88+
89+
hoverY.style.left = box.left + 'px'
90+
hoverY.style.width = box.width - 2 + 'px'
91+
document.body.append(hoverY)
2092

2193
break
2294
}

src/store.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ selectedNode.subscribe(node => {
4848
if (invalid) invalid.invalidate()
4949
})
5050

51+
hoveredNodeId.subscribe(nodeId =>
52+
port.postMessage({
53+
type: 'setHover',
54+
tabId: chrome.devtools.inspectedWindow.tabId,
55+
nodeId
56+
})
57+
)
58+
5159
function noop() {}
5260

5361
function insertNode(node, target, anchorId) {

0 commit comments

Comments
 (0)