|
1 | | -import { useState, useRef, useEffect } from 'react'; |
| 1 | +import { useEffect, useRef } from 'react'; |
2 | 2 | import type { Meta, StoryObj } from '@storybook/react'; |
3 | | -import type { WallMode } from '../components/Wall'; |
4 | | -import { MarchingAntsRect } from '../components/Wall'; |
| 3 | +import '@xterm/xterm/css/xterm.css'; |
| 4 | +import { SelectionOverlay } from '../components/SelectionOverlay'; |
| 5 | +import { |
| 6 | + focusSession, |
| 7 | + getOrCreateTerminal, |
| 8 | + getTerminalOverlayDims, |
| 9 | + mountElement, |
| 10 | + refitSession, |
| 11 | + unmountElement, |
| 12 | +} from '../lib/terminal-registry'; |
| 13 | +import { flattenScenario, SCENARIO_LS_OUTPUT } from '../lib/platform'; |
| 14 | +import { |
| 15 | + setHintToken, |
| 16 | + setSelection, |
| 17 | + type Selection, |
| 18 | + type TokenHint, |
| 19 | +} from '../lib/mouse-selection'; |
| 20 | +import { TERMINAL_BOTTOM_RADIUS_CLASS } from '../components/design'; |
5 | 21 |
|
6 | | -function SelectionOverlayDemo({ initialMode = 'command' as WallMode }) { |
7 | | - const [mode, setMode] = useState<WallMode>(initialMode); |
8 | | - const containerRef = useRef<HTMLDivElement>(null); |
9 | | - const [size, setSize] = useState({ width: 484, height: 284 }); |
| 22 | +function SelectionOverlayStory({ |
| 23 | + id, |
| 24 | + selection, |
| 25 | + hintToken = null, |
| 26 | +}: { |
| 27 | + id: string; |
| 28 | + selection: Omit<Selection, 'startedInScrollback'>; |
| 29 | + hintToken?: TokenHint | null; |
| 30 | +}) { |
| 31 | + const terminalHostRef = useRef<HTMLDivElement>(null); |
10 | 32 |
|
11 | 33 | useEffect(() => { |
12 | | - const el = containerRef.current; |
13 | | - if (!el) return; |
14 | | - const ro = new ResizeObserver(([entry]) => { |
15 | | - setSize({ width: entry.contentRect.width - 16, height: entry.contentRect.height - 16 }); |
16 | | - }); |
17 | | - ro.observe(el); |
18 | | - return () => ro.disconnect(); |
19 | | - }, []); |
20 | | - |
21 | | - const color = getComputedStyle(document.documentElement).getPropertyValue('--color-header-active-bg').trim() || '#094771'; |
22 | | - |
23 | | - const overlayStyle: React.CSSProperties = { |
24 | | - position: 'absolute', |
25 | | - inset: 8, |
26 | | - borderRadius: '0.5rem', |
27 | | - pointerEvents: 'none', |
28 | | - transition: 'border 150ms, box-shadow 150ms', |
29 | | - }; |
30 | | - |
31 | | - if (mode === 'passthrough') { |
32 | | - overlayStyle.border = `2px solid ${color}`; |
33 | | - overlayStyle.boxShadow = `0 0 15px color-mix(in srgb, ${color} 30%, transparent)`; |
34 | | - } |
| 34 | + const terminalHost = terminalHostRef.current; |
| 35 | + if (!terminalHost) return; |
| 36 | + |
| 37 | + getOrCreateTerminal(id); |
| 38 | + mountElement(id, terminalHost); |
| 39 | + |
| 40 | + const observer = new ResizeObserver(() => refitSession(id)); |
| 41 | + observer.observe(terminalHost); |
| 42 | + |
| 43 | + return () => { |
| 44 | + observer.disconnect(); |
| 45 | + unmountElement(id); |
| 46 | + }; |
| 47 | + }, [id]); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + focusSession(id, true); |
| 51 | + }, [id]); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + let cancelled = false; |
| 55 | + let timer: ReturnType<typeof setTimeout>; |
| 56 | + |
| 57 | + const applySelection = () => { |
| 58 | + if (cancelled) return; |
| 59 | + const dims = getTerminalOverlayDims(id); |
| 60 | + if (!dims || dims.cellHeight === 0) { |
| 61 | + timer = setTimeout(applySelection, 50); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + setSelection(id, { ...selection, startedInScrollback: false }); |
| 66 | + setHintToken(id, hintToken); |
| 67 | + }; |
| 68 | + |
| 69 | + timer = setTimeout(applySelection, 100); |
| 70 | + return () => { |
| 71 | + cancelled = true; |
| 72 | + clearTimeout(timer); |
| 73 | + setSelection(id, null); |
| 74 | + setHintToken(id, null); |
| 75 | + }; |
| 76 | + }, [id, selection, hintToken]); |
35 | 77 |
|
36 | 78 | return ( |
37 | | - <div ref={containerRef} style={{ width: 500, height: 300 }} className="relative bg-app-bg"> |
38 | | - {/* Simulated terminal content */} |
39 | | - <div className="p-4 font-mono text-sm text-terminal-fg"> |
40 | | - <div>user@mouseterm:~$ ls -la</div> |
41 | | - <div>total 48</div> |
42 | | - <div>drwxr-xr-x 12 user staff 384 Mar 16 10:30 .</div> |
43 | | - </div> |
44 | | - {/* Selection overlay */} |
45 | | - {mode === 'command' ? ( |
46 | | - <div style={{ position: 'absolute', inset: 8, pointerEvents: 'none' }}> |
47 | | - <MarchingAntsRect width={size.width} height={size.height} isDoor={false} color={color} /> |
48 | | - </div> |
49 | | - ) : ( |
50 | | - <div style={overlayStyle} /> |
51 | | - )} |
52 | | - {/* Mode toggle */} |
53 | | - <div className="absolute bottom-2 right-2 flex gap-2"> |
54 | | - <button |
55 | | - className={`px-3 py-1 rounded text-sm font-mono ${mode === 'passthrough' ? 'bg-header-active-bg text-header-active-fg' : 'bg-header-inactive-bg text-header-inactive-fg'}`} |
56 | | - onClick={() => setMode('passthrough')} |
57 | | - >passthrough</button> |
58 | | - <button |
59 | | - className={`px-3 py-1 rounded text-sm font-mono ${mode === 'command' ? 'bg-header-active-bg text-header-active-fg' : 'bg-header-inactive-bg text-header-inactive-fg'}`} |
60 | | - onClick={() => setMode('command')} |
61 | | - >command</button> |
62 | | - </div> |
| 79 | + <div |
| 80 | + className={`relative bg-terminal-bg ${TERMINAL_BOTTOM_RADIUS_CLASS}`} |
| 81 | + style={{ width: 620, height: 340 }} |
| 82 | + > |
| 83 | + <div ref={terminalHostRef} className="h-full w-full" /> |
| 84 | + <SelectionOverlay terminalId={id} /> |
63 | 85 | </div> |
64 | 86 | ); |
65 | 87 | } |
66 | 88 |
|
67 | | -const meta: Meta<typeof SelectionOverlayDemo> = { |
| 89 | +const meta: Meta<typeof SelectionOverlayStory> = { |
68 | 90 | title: 'Components/SelectionOverlay', |
69 | | - component: SelectionOverlayDemo, |
| 91 | + component: SelectionOverlayStory, |
| 92 | + parameters: { |
| 93 | + fakePty: { scenario: flattenScenario(SCENARIO_LS_OUTPUT) }, |
| 94 | + }, |
70 | 95 | }; |
71 | 96 |
|
72 | 97 | export default meta; |
73 | | -type Story = StoryObj<typeof SelectionOverlayDemo>; |
| 98 | +type Story = StoryObj<typeof SelectionOverlayStory>; |
| 99 | + |
| 100 | +export const LinewiseDrag: Story = { |
| 101 | + args: { |
| 102 | + id: 'selection-overlay-linewise-drag', |
| 103 | + selection: { |
| 104 | + startRow: 2, |
| 105 | + startCol: 5, |
| 106 | + endRow: 6, |
| 107 | + endCol: 24, |
| 108 | + shape: 'linewise', |
| 109 | + dragging: true, |
| 110 | + }, |
| 111 | + }, |
| 112 | +}; |
74 | 113 |
|
75 | | -export const CommandMode: Story = { |
76 | | - args: { initialMode: 'command' }, |
| 114 | +export const BlockDrag: Story = { |
| 115 | + args: { |
| 116 | + id: 'selection-overlay-block-drag', |
| 117 | + selection: { |
| 118 | + startRow: 2, |
| 119 | + startCol: 6, |
| 120 | + endRow: 5, |
| 121 | + endCol: 26, |
| 122 | + shape: 'block', |
| 123 | + dragging: true, |
| 124 | + }, |
| 125 | + }, |
77 | 126 | }; |
78 | 127 |
|
79 | | -export const PassthroughMode: Story = { |
80 | | - args: { initialMode: 'passthrough' }, |
| 128 | +export const SmartPathHint: Story = { |
| 129 | + args: { |
| 130 | + id: 'selection-overlay-smart-path-hint', |
| 131 | + selection: { |
| 132 | + startRow: 2, |
| 133 | + startCol: 5, |
| 134 | + endRow: 6, |
| 135 | + endCol: 24, |
| 136 | + shape: 'linewise', |
| 137 | + dragging: true, |
| 138 | + }, |
| 139 | + hintToken: { |
| 140 | + kind: 'path', |
| 141 | + row: 8, |
| 142 | + startCol: 35, |
| 143 | + endCol: 38, |
| 144 | + text: 'src', |
| 145 | + }, |
| 146 | + }, |
81 | 147 | }; |
0 commit comments