Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
import { useTheme } from '@mui/material';
import PhotosCarousel, { PhotoItem } from '../PhotosCarousel';
import type { AnnotationProps } from './Annotation';
interface AnnotationPanelProps {
annotation: AnnotationProps | null;
// Screen position (and rendered diameter) of the associated hotspot, relative
// to the viewer, used to draw the connector line. Null until the hotspot's
// position is known.
hotspotPosition?: { x: number; y: number; size?: number } | null;
onClose: () => void;
}
// Fallback hotspot diameter (px) when the rendered size isn't reported yet.
const DEFAULT_HOTSPOT_DIAMETER = 35;
// Horizontal gap (px) kept between the panel's right edge and the hotspot so
// the connector line stays visible.
const PANEL_HOTSPOT_GAP = 48;
// Minimum distance from the left edge of the viewer, as a fraction of its
// width, so the panel never drifts further left than this.
const MIN_LEFT_FRACTION = 0.02;
const CONNECTOR_STYLE: React.CSSProperties = {
position: 'absolute',
inset: 0,
width: '100%',
height: '100%',
pointerEvents: 'none',
overflow: 'visible',
zIndex: 5002,
};
const PANEL_STYLE: React.CSSProperties = {
position: 'absolute',
left: '2%',
top: '50%',
transform: 'translateY(-50%)',
maxHeight: '80vh',
zIndex: 5003,
backgroundColor: '#ffffff',
borderRadius: 12,
boxShadow: '0 8px 32px rgba(0,0,0,0.32)',
overflowY: 'auto',
display: 'flex',
flexDirection: 'column',
};
const CAROUSEL_STYLE: React.CSSProperties = {
width: 'min(60vw, 640px)',
borderRadius: '12px 12px 0 0',
overflow: 'hidden',
flexShrink: 0,
};
const TEXT_BLOCK_STYLE: React.CSSProperties = {
padding: '24px',
display: 'flex',
flexDirection: 'column',
gap: 8,
};
const AnnotationPanel = ({ annotation, hotspotPosition, onClose }: AnnotationPanelProps) => {
const theme = useTheme();
const panelRef = useRef<HTMLDivElement>(null);
const connectorRef = useRef<SVGSVGElement>(null);
const [lineStart, setLineStart] = useState<{ x: number; y: number } | null>(null);
const [panelLeft, setPanelLeft] = useState<number | null>(null);
useEffect(() => {
if (!annotation) {
return;
}
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
onClose();
}
};
const handlePointerDown = (e: PointerEvent) => {
if (panelRef.current && !panelRef.current.contains(e.target as Node)) {
onClose();
}
};
document.addEventListener('keydown', handleKeyDown);
document.addEventListener('pointerdown', handlePointerDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
document.removeEventListener('pointerdown', handlePointerDown);
};
}, [annotation, onClose]);
// Position the panel just to the left of the hotspot (clamped so it never
// goes further left than MIN_LEFT_FRACTION of the viewer) and anchor the
// connector line at the vertical center of the panel's right edge, in the
// connector SVG's coordinate space.
useLayoutEffect(() => {
if (!annotation || !hotspotPosition || !panelRef.current || !connectorRef.current) {
// eslint-disable-next-line react-hooks/set-state-in-effect
setPanelLeft(null);
setLineStart(null);
return;
}
const panelWidth = panelRef.current.getBoundingClientRect().width;
const connectorRect = connectorRef.current.getBoundingClientRect();
const minLeft = connectorRect.width * MIN_LEFT_FRACTION;
const desiredLeft = hotspotPosition.x - PANEL_HOTSPOT_GAP - panelWidth;
const left = Math.max(desiredLeft, minLeft);
setPanelLeft(left);
setLineStart({
x: left + panelWidth,
y: connectorRect.height / 2,
});
}, [annotation, hotspotPosition]);
if (!annotation) {
return null;
}
const panelStyle: React.CSSProperties = {
...PANEL_STYLE,
...(annotation.imageUrls?.[0]
? { width: 'min(60vw, 640px)', maxWidth: '60vw' }
: { width: 'fit-content', maxWidth: '50vw' }),
...(panelLeft !== null ? { left: `${panelLeft}px` } : {}),
};
// Stop the line at the edge of the hotspot circle
let lineEnd: { x: number; y: number } | null = null;
if (lineStart && hotspotPosition) {
const dx = hotspotPosition.x - lineStart.x;
const dy = hotspotPosition.y - lineStart.y;
const dist = Math.hypot(dx, dy);
const radius = (hotspotPosition.size ?? DEFAULT_HOTSPOT_DIAMETER) / 2;
const t = dist > radius ? (dist - radius) / dist : 0;
lineEnd = { x: lineStart.x + dx * t, y: lineStart.y + dy * t };
}
return (
<>
<svg ref={connectorRef} data-testid='annotation-panel-connector' style={CONNECTOR_STYLE}>
{lineStart && lineEnd && (
<line x1={lineStart.x} y1={lineStart.y} x2={lineEnd.x} y2={lineEnd.y} stroke='#ffffff' strokeWidth={2} />
)}
</svg>
<div ref={panelRef} data-testid='annotation-panel' style={panelStyle}>
{annotation.imageUrls && annotation.imageUrls.length > 0 && (
<div style={CAROUSEL_STYLE}>
<PhotosCarousel
photos={annotation.imageUrls.map((url): PhotoItem => ({ url, alt: annotation.title }))}
showArrows={true}
dots={false}
/>
</div>
)}
<div style={TEXT_BLOCK_STYLE}>
{annotation.label && (
<span
data-testid='annotation-panel-label'
style={{
alignSelf: 'flex-start',
fontSize: 14,
fontWeight: 500,
color: theme.palette.TwClrTxtSuccess,
margin: 0,
padding: theme.spacing(0.5, 1),
backgroundColor: theme.palette.TwClrBgSuccessTertiary,
border: `1px solid ${theme.palette.TwClrBrdrSuccess}`,
borderRadius: theme.spacing(1),
}}
>
{annotation.label}
</span>
)}
<h2
data-testid='annotation-panel-title'
style={{
fontSize: 32,
fontWeight: 600,
color: theme.palette.TwClrTxt,
margin: 0,
}}
>
{annotation.title}
</h2>
{annotation.bodyText && (
<p
data-testid='annotation-panel-description'
style={{
fontSize: 16,
color: theme.palette.TwClrTxt,
lineHeight: '24px',
margin: 0,
}}
>
{annotation.bodyText}
</p>
)}
</div>
</div>
</>
);
};
export default AnnotationPanel;
|