Skip to content

Commit 39cbbfc

Browse files
authored
feat: add support for user-event (#209)
1 parent f45d6e4 commit 39cbbfc

36 files changed

+788
-459
lines changed

devtools/src/content-script/highlighter/Overlay.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export default class Overlay {
228228
const name = elements[0].nodeName.toLowerCase();
229229

230230
const node = elements[0];
231-
const hook = node.ownerDocument.defaultView.__TESTING_PLAYGROUND__;
231+
const hook = node.ownerDocument.defaultView.__TESTING_PLAYGROUND__ || {};
232232

233233
let tipData = {
234234
target: node,

devtools/src/content-script/highlighter/index.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,23 @@ export default function setupHighlighter({
2626
onSelectNode = () => {},
2727
} = {}) {
2828
let isInspecting = false;
29+
let stopOnClick = true;
30+
let blockEvents = true;
2931

3032
Bridge.onMessage('CLEAR_HIGHLIGHTS', withMessageData(clearHighlights));
3133
Bridge.onMessage('HIGHLIGHT_ELEMENTS', withMessageData(highlightElements));
3234
Bridge.onMessage('SHUTDOWN', withMessageData(stopInspecting));
3335
Bridge.onMessage('START_INSPECTING', withMessageData(startInspecting));
3436
Bridge.onMessage('STOP_INSPECTING', withMessageData(stopInspecting));
3537

36-
function startInspecting() {
38+
function startInspecting(options) {
3739
isInspecting = true;
40+
41+
if (options) {
42+
stopOnClick = options.stopOnClick ?? true;
43+
blockEvents = options.blockEvents ?? true;
44+
}
45+
3846
addEventListeners(view);
3947
}
4048

@@ -51,6 +59,13 @@ export default function setupHighlighter({
5159
}
5260
}
5361

62+
function stopPropagation(event) {
63+
if (blockEvents) {
64+
event.preventDefault();
65+
event.stopPropagation();
66+
}
67+
}
68+
5469
function stopInspecting() {
5570
hideOverlay();
5671
removeEventListeners(view);
@@ -99,30 +114,27 @@ export default function setupHighlighter({
99114
}
100115

101116
function onClick(event) {
102-
event.preventDefault();
103-
event.stopPropagation();
117+
stopPropagation(event);
104118

105-
stopInspecting();
119+
if (isInspecting && stopOnClick) {
120+
stopInspecting();
121+
}
106122
}
107123

108124
function onMouseEvent(event) {
109-
event.preventDefault();
110-
event.stopPropagation();
125+
stopPropagation(event);
111126
}
112127

113128
function onPointerDown(event) {
114-
event.preventDefault();
115-
event.stopPropagation();
129+
stopPropagation(event);
116130

117-
selectNode(event.target);
131+
selectNode(event.target, { origin: event.isTrusted ? 'click' : 'script' });
118132
}
119133

120134
function onPointerOver(event) {
121-
event.preventDefault();
122-
event.stopPropagation();
135+
stopPropagation(event);
123136

124137
const target = event.target;
125-
126138
if (target.tagName === 'IFRAME') {
127139
try {
128140
if (!iframesListeningTo.has(target)) {
@@ -136,12 +148,11 @@ export default function setupHighlighter({
136148
}
137149

138150
showOverlay([target], false);
139-
selectNode(target);
151+
selectNode(target, { origin: event.isTrusted ? 'hover' : 'script' });
140152
}
141153

142154
function onPointerUp(event) {
143-
event.preventDefault();
144-
event.stopPropagation();
155+
stopPropagation(event);
145156
}
146157

147158
const selectNode = throttle(

devtools/src/content-script/highlighter/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export function mergeRectOffsets(rects) {
6464
// taking into account any offsets caused by intermediate iframes.
6565
export function getNestedBoundingClientRect(node, boundaryWindow) {
6666
const ownerIframe = getOwnerIframe(node);
67-
if (ownerIframe && ownerIframe !== boundaryWindow) {
67+
68+
if (ownerIframe && ownerIframe.contentWindow !== boundaryWindow) {
6869
const rects = [node.getBoundingClientRect()];
6970
let currentIframe = ownerIframe;
7071
let onlyOneMore = false;

devtools/src/devtools/panel.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ function Panel() {
5656
</div>
5757
<div className="grid relative p-2 gap-4 flex-auto grid-cols-1 md:grid-cols-2 grid-equal-cells overflow-hidden">
5858
<div className="relative">
59-
<Query query={''} result={result} dispatch={dispatch} />
59+
<Query
60+
query={''}
61+
result={result}
62+
dispatch={dispatch}
63+
variant="minimal"
64+
/>
6065
</div>
6166

6267
<div className="overflow-hidden">

package-lock.json

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"postversion": "git push && git push --tags && git checkout master && git merge develop --ff && git push && git checkout -"
3939
},
4040
"dependencies": {
41+
"@primer/octicons-react": "^10.0.0",
4142
"@testing-library/dom": "^7.15.0",
43+
"@testing-library/user-event": "^12.0.2",
4244
"codemirror": "5.54.0",
4345
"crx-bridge": "^2.1.0",
4446
"deep-diff": "^1.0.2",
@@ -65,7 +67,6 @@
6567
"@babel/preset-react": "^7.10.1",
6668
"@testing-library/jest-dom": "^5.9.0",
6769
"@testing-library/react": "^10.2.0",
68-
"@testing-library/user-event": "^11.4.2",
6970
"@types/fs-extra": "^9.0.1",
7071
"babel-eslint": "^10.1.0",
7172
"chrome-launch": "^1.1.4",

scripts/build-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async function main() {
6767
const dest = resolve('dist/client');
6868
await remove(dest);
6969

70-
const entries = ['src/index.html', 'src/embed.js'];
70+
const entries = ['src/index.html', 'src/embed.js', 'src/sandbox.html'];
7171

7272
if (process.env.NODE_ENV === 'development') {
7373
entries.push('src/embed.html');

src/components/CopyButton.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* global chrome */
22
import React, { useState, useEffect } from 'react';
33
import IconButton from './IconButton';
4-
import SuccessIcon from './icons/SuccessIcon';
5-
import CopyIcon from './icons/CopyIcon';
4+
import { CopyIcon, CheckIcon } from '@primer/octicons-react';
65

76
const IS_DEVTOOL = !!(window.chrome && chrome.runtime && chrome.runtime.id);
87

@@ -75,7 +74,7 @@ function CopyButton({ text, title, className, variant }) {
7574
title={title}
7675
className={className}
7776
>
78-
{copied ? <SuccessIcon /> : <CopyIcon />}
77+
{copied ? <CheckIcon /> : <CopyIcon />}
7978
</IconButton>
8079
);
8180
}

src/components/DomEvents.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React, { useRef, useCallback, useState } from 'react';
22

3+
import { eventMap } from '@testing-library/dom/dist/event-map';
4+
import throttle from 'lodash.throttle';
5+
import AutoSizer from 'react-virtualized-auto-sizer';
6+
import { TrashcanIcon } from '@primer/octicons-react';
7+
38
import Preview from './Preview';
49
import MarkupEditor from './MarkupEditor';
510
import usePlayground from '../hooks/usePlayground';
611
import state from '../lib/state';
7-
import { eventMap } from '@testing-library/dom/dist/event-map';
812
import { VirtualScrollable } from './Scrollable';
9-
import throttle from 'lodash.throttle';
10-
import AutoSizer from 'react-virtualized-auto-sizer';
1113
import IconButton from './IconButton';
12-
import TrashcanIcon from './icons/TrashcanIcon';
1314
import CopyButton from './CopyButton';
1415
import EmptyStreetImg from '../images/EmptyStreetImg';
1516
import StickyList from './StickyList';
@@ -158,7 +159,7 @@ function DomEvents() {
158159

159160
return (
160161
<div className="flex flex-col h-auto md:h-full w-full">
161-
<div className="editor markup-editor gap-4 md:gap-8 md:h-56 flex-auto grid-cols-1 md:grid-cols-2">
162+
<div className="editor p-4 markup-editor gap-4 md:gap-8 md:h-56 flex-auto grid-cols-1 md:grid-cols-2">
162163
<div className="flex-auto relative h-56 md:h-full">
163164
<MarkupEditor markup={markup} dispatch={dispatch} />
164165
</div>
@@ -177,7 +178,7 @@ function DomEvents() {
177178

178179
<div className="flex-none h-8" />
179180

180-
<div className="editor md:h-56 flex-auto overflow-hidden">
181+
<div className="editor p-4 md:h-56 flex-auto overflow-hidden">
181182
<div className="h-56 md:h-full w-full flex flex-col">
182183
<div className="h-8 flex items-center w-full text-sm font-bold">
183184
<div className="p-2 w-16">#</div>

0 commit comments

Comments
 (0)