Skip to content

Commit bf7e4f0

Browse files
committed
Add listener in capture phase and locate nearest source if needed
1. Capture Phase listener allows the plugin to work for the cases where stopPropagation is done in bubble phase. 2. Looking for nearest parent element with dataset source handles the case when the element directly doesn't have dataset source. This hhappens if the element is not created through JSX e.g. Chakra UI ModalCloseButton. Basically any component coming through an npm dependency should have this problem. See https://codesandbox.io/s/suspicious-breeze-smdrq?file=/src/index.js
1 parent 652a9db commit bf7e4f0

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed
+24-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
if (typeof document !== 'undefined') {
2-
document.addEventListener('click', (event) => {
3-
if (!event.target.dataset.source) return;
4-
if (!event.altKey) return;
1+
(function () {
2+
3+
// Element being clicked might not have the dataset source on it directly.
4+
// Get the nearest available in the tree
5+
// e.g. this happens for Chakra UI Components.
6+
function getSource(element) {
7+
while (element && !element.dataset.source) {
8+
element = element.parentElement;
9+
}
510

6-
event.preventDefault();
7-
const { filename, start } = JSON.parse(event.target.dataset.source)
8-
window.open('vscode://file/' + filename + ':' + start)
9-
})
10-
}
11+
return element ? element.dataset.source : null;
12+
}
13+
14+
if (typeof document !== 'undefined') {
15+
document.addEventListener('click', (event) => {
16+
if (!event.altKey) return;
17+
let source = getSource(event.target)
18+
if (!source) return;
19+
20+
event.preventDefault();
21+
const { filename, start } = JSON.parse(source)
22+
window.open('vscode://file/' + filename + ':' + start)
23+
}, true)
24+
}
25+
})();

0 commit comments

Comments
 (0)