forked from testing-library/testing-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreview.js
147 lines (128 loc) · 4.38 KB
/
Preview.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React, { useState, useEffect, useRef, useMemo } from 'react';
import Scrollable from './Scrollable';
import PreviewHint from './PreviewHint';
import AddHtml from './AddHtml';
import { getQueryAdvise } from '../lib';
function selectByCssPath(rootNode, cssPath) {
return rootNode?.querySelector(cssPath.replace(/^body > /, ''));
}
function Preview({ markup, accessibleRoles, elements, dispatch }) {
// Okay, listen up. `highlighted` can be a number of things, as I wanted to
// keep a single variable to represent the state. This to reduce bug count
// by creating out-of-sync states.
//
// 1. When the mouse pointer enters the preview area, `highlighted` changes
// to true. True indicates that the highlight no longer indicates the parsed
// element.
// 2. When the mouse pointer is pointing at an element, `highlighted` changes
// to the target element. A dom node.
// 3. When the mouse pointer leaves that element again, `highlighted` changse
// back to... true. Not to false! To indicate that we still want to use
// the mouse position to control the highlight.
// 4. Once the mouse leaves the preview area, `highlighted` switches to false.
// Indicating that the `parsed` element can be highlighted again.
const [highlighted, setHighlighted] = useState(false);
const [roles, setRoles] = useState([]);
const htmlRoot = useRef();
const { suggestion } = getQueryAdvise({
rootNode: htmlRoot?.current,
element: highlighted,
});
// TestingLibraryDom?.getSuggestedQuery(highlighted, 'get').toString() : null
const scripts = useMemo(() => {
const container = document.createElement('div');
container.innerHTML = markup;
const scriptsCollections = container.getElementsByTagName('script');
return Array.from(scriptsCollections).map((script) => script.innerHTML);
}, [markup]);
const actualMarkup = useMemo(
() =>
scripts.length
? scripts.reduce(
(html, script) => html.replace(`<script>${script}</script>`, ''),
markup,
)
: markup,
[scripts, markup],
);
useEffect(() => {
if (scripts.length) {
try {
scripts.forEach((script) => {
window.eval(script);
});
} catch {
return;
}
}
}, [scripts]);
useEffect(() => {
setRoles(Object.keys(accessibleRoles || {}).sort());
}, [accessibleRoles]);
useEffect(() => {
if (highlighted) {
elements?.forEach((el) => {
const target = selectByCssPath(htmlRoot.current, el.cssPath);
target?.classList.remove('highlight');
});
highlighted.classList?.add('highlight');
} else {
highlighted?.classList?.remove('highlight');
if (highlighted === false) {
elements?.forEach((el) => {
const target = selectByCssPath(htmlRoot.current, el.cssPath);
target?.classList.add('highlight');
});
}
}
return () => highlighted?.classList?.remove('highlight');
}, [highlighted, elements]);
const handleClick = (event) => {
if (event.target === htmlRoot.current) {
return;
}
event.preventDefault();
const expression =
suggestion.expression ||
'// No recommendation available.\n// Add some html attributes, or\n// use container.querySelector(…)';
dispatch({ type: 'SET_QUERY', query: expression });
};
const handleMove = (event) => {
const target = document.elementFromPoint(event.clientX, event.clientY);
if (target === highlighted) {
return;
}
if (target === htmlRoot) {
setHighlighted(true);
return;
}
setHighlighted(target);
};
return markup ? (
<div
className="w-full h-full flex flex-col relative overflow-hidden"
onMouseEnter={() => setHighlighted(true)}
onMouseLeave={() => setHighlighted(false)}
>
<div className="flex-auto relative overflow-hidden h-1">
<Scrollable>
<div
className="preview"
onClick={handleClick}
onMouseMove={handleMove}
ref={htmlRoot}
dangerouslySetInnerHTML={{
__html: actualMarkup,
}}
/>
</Scrollable>
</div>
<PreviewHint roles={roles} suggestion={suggestion} />
</div>
) : (
<div className="w-full h-full flex flex-col relative overflow-hidden">
<AddHtml />
</div>
);
}
export default React.memo(Preview);