|
1 |
| -import React, { useCallback, useRef } from 'react'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { useAppContext } from './Context.js'; |
| 3 | +import { getQueryAdvise } from '../lib'; |
2 | 4 |
|
3 | 5 | function HtmlPreview({ html }, forwardRef) {
|
| 6 | + // Okay, listen up. `highlighted` can be a number of things, as I wanted to |
| 7 | + // keep a single variable to represent the state. This to reduce bug count |
| 8 | + // by creating out-of-sync states. |
| 9 | + // |
| 10 | + // 1. When the mouse pointer enters the preview area, `highlighted` changes |
| 11 | + // to true. True indicates that the highlight no longer indicates the parsed |
| 12 | + // element. |
| 13 | + // 2. When the mouse pointer is pointing at an element, `highlighted` changes |
| 14 | + // to the target element. A dom node. |
| 15 | + // 3. When the mouse pointer leaves that element again, `highlighted` changse |
| 16 | + // back to... true. Not to false! To indicate that we still want to use |
| 17 | + // the mouse position to control the highlight. |
| 18 | + // 4. Once the mouse leaves the preview area, `highlighted` switches to false. |
| 19 | + // Indiating that the `parsed` element can be highlighted again. |
| 20 | + const [highlighted, setHighlighted] = useState(false); |
| 21 | + const { parsed, jsEditorRef } = useAppContext(); |
| 22 | + |
| 23 | + const { advise } = getQueryAdvise({ |
| 24 | + root: forwardRef.current, |
| 25 | + element: highlighted, |
| 26 | + }); |
| 27 | + |
| 28 | + const handleClick = (event) => { |
| 29 | + if (event.target === forwardRef.current) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + event.preventDefault(); |
| 34 | + const expression = |
| 35 | + advise.expression || |
| 36 | + '// No recommendation available.\n// Add some html attributes, or\n// use container.querySelector(…)'; |
| 37 | + jsEditorRef.current.setValue(expression); |
| 38 | + }; |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (highlighted) { |
| 42 | + parsed.targets?.forEach((el) => el.classList.remove('highlight')); |
| 43 | + highlighted.classList?.add('highlight'); |
| 44 | + } else { |
| 45 | + highlighted?.classList?.remove('highlight'); |
| 46 | + |
| 47 | + if (highlighted === false) { |
| 48 | + parsed.targets?.forEach((el) => el.classList.add('highlight')); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return () => highlighted?.classList?.remove('highlight'); |
| 53 | + }, [highlighted, parsed.targets]); |
| 54 | + |
| 55 | + const handleMove = (event) => { |
| 56 | + const target = document.elementFromPoint(event.clientX, event.clientY); |
| 57 | + if (target === highlighted) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (target === forwardRef.current) { |
| 62 | + setHighlighted(true); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + setHighlighted(target); |
| 67 | + }; |
| 68 | + |
4 | 69 | return (
|
5 | 70 | <div
|
6 |
| - className="preview" |
7 |
| - ref={forwardRef} |
8 |
| - dangerouslySetInnerHTML={{ __html: html }} |
9 |
| - /> |
| 71 | + className="relative flex flex-col" |
| 72 | + onMouseEnter={() => setHighlighted(true)} |
| 73 | + onMouseLeave={() => setHighlighted(false)} |
| 74 | + > |
| 75 | + <div |
| 76 | + className="preview flex-auto" |
| 77 | + ref={forwardRef} |
| 78 | + dangerouslySetInnerHTML={{ __html: html }} |
| 79 | + onClick={handleClick} |
| 80 | + onMouseMove={handleMove} |
| 81 | + /> |
| 82 | + <div className="p-2 bg-gray-200 rounded text-gray-800 font-mono text-xs"> |
| 83 | + {advise.expression && `> ${advise.expression}`} |
| 84 | + |
| 85 | + {!advise.expression && forwardRef.current && ( |
| 86 | + <> |
| 87 | + <span className="font-bold">roles: </span> |
| 88 | + {Object.keys(TestingLibraryDom.getRoles(forwardRef.current)) |
| 89 | + .sort() |
| 90 | + .join(', ')} |
| 91 | + </> |
| 92 | + )} |
| 93 | + </div> |
| 94 | + </div> |
10 | 95 | );
|
11 | 96 | }
|
12 | 97 |
|
|
0 commit comments