@fragments-sdk/viewer 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +84 -0
- package/index.html +28 -0
- package/package.json +71 -0
- package/src/__tests__/a11y-fixes.test.ts +358 -0
- package/src/__tests__/jsx-parser.test.ts +502 -0
- package/src/__tests__/render-utils.test.ts +232 -0
- package/src/__tests__/style-utils.test.ts +404 -0
- package/src/app/index.ts +1 -0
- package/src/assets/fragments-logo.ts +4 -0
- package/src/assets/fragments_logo.png +0 -0
- package/src/components/AccessibilityPanel.tsx +1457 -0
- package/src/components/ActionCapture.tsx +172 -0
- package/src/components/ActionsPanel.tsx +332 -0
- package/src/components/AllVariantsPreview.tsx +78 -0
- package/src/components/App.tsx +604 -0
- package/src/components/BottomPanel.tsx +288 -0
- package/src/components/CodePanel.naming.test.tsx +59 -0
- package/src/components/CodePanel.tsx +118 -0
- package/src/components/CommandPalette.tsx +392 -0
- package/src/components/ComponentDocView.tsx +164 -0
- package/src/components/ComponentGraph.tsx +380 -0
- package/src/components/ComponentHeader.tsx +88 -0
- package/src/components/ContractPanel.tsx +241 -0
- package/src/components/DeviceMockup.tsx +156 -0
- package/src/components/EmptyVariantMessage.tsx +54 -0
- package/src/components/ErrorBoundary.tsx +97 -0
- package/src/components/FigmaEmbed.tsx +238 -0
- package/src/components/FragmentEditor.tsx +525 -0
- package/src/components/FragmentRenderer.tsx +61 -0
- package/src/components/HeaderSearch.tsx +24 -0
- package/src/components/HealthDashboard.tsx +441 -0
- package/src/components/HmrStatusIndicator.tsx +61 -0
- package/src/components/Icons.tsx +479 -0
- package/src/components/InteractionsPanel.tsx +757 -0
- package/src/components/IsolatedPreviewFrame.tsx +390 -0
- package/src/components/IsolatedRender.tsx +113 -0
- package/src/components/KeyboardShortcutsHelp.tsx +53 -0
- package/src/components/LandingPage.tsx +420 -0
- package/src/components/Layout.tsx +27 -0
- package/src/components/LeftSidebar.tsx +472 -0
- package/src/components/LoadErrorMessage.tsx +102 -0
- package/src/components/MultiViewportPreview.tsx +527 -0
- package/src/components/NoVariantsMessage.tsx +59 -0
- package/src/components/PanelShell.tsx +161 -0
- package/src/components/PerformancePanel.tsx +304 -0
- package/src/components/PreviewArea.tsx +254 -0
- package/src/components/PreviewAside.tsx +168 -0
- package/src/components/PreviewFrameHost.tsx +304 -0
- package/src/components/PreviewToolbar.tsx +80 -0
- package/src/components/PropsEditor.tsx +506 -0
- package/src/components/PropsTable.tsx +111 -0
- package/src/components/RelationsSection.tsx +88 -0
- package/src/components/ResizablePanel.tsx +271 -0
- package/src/components/RightSidebar.tsx +102 -0
- package/src/components/RuntimeToolsRegistrar.tsx +17 -0
- package/src/components/ScreenshotButton.tsx +90 -0
- package/src/components/ShadowPreview.tsx +204 -0
- package/src/components/Sidebar.tsx +169 -0
- package/src/components/SkeletonLoader.tsx +161 -0
- package/src/components/ThemeProvider.tsx +42 -0
- package/src/components/Toast.tsx +3 -0
- package/src/components/TokenStylePanel.tsx +699 -0
- package/src/components/TopToolbar.tsx +159 -0
- package/src/components/Untitled +1 -0
- package/src/components/UsageSection.tsx +95 -0
- package/src/components/VariantMatrix.tsx +391 -0
- package/src/components/VariantRenderer.tsx +131 -0
- package/src/components/VariantTabs.tsx +40 -0
- package/src/components/ViewerHeader.tsx +69 -0
- package/src/components/ViewerStateSync.tsx +52 -0
- package/src/components/ViewportSelector.tsx +172 -0
- package/src/components/WebMCPDevTools.tsx +503 -0
- package/src/components/WebMCPIntegration.tsx +47 -0
- package/src/components/WebMCPStatusIndicator.tsx +60 -0
- package/src/components/_future/CreatePage.tsx +835 -0
- package/src/components/viewer-utils.ts +16 -0
- package/src/composition-renderer.ts +381 -0
- package/src/constants/index.ts +1 -0
- package/src/constants/ui.ts +166 -0
- package/src/entry.tsx +335 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useA11yCache.ts +383 -0
- package/src/hooks/useA11yService.ts +364 -0
- package/src/hooks/useActions.ts +138 -0
- package/src/hooks/useAppState.ts +147 -0
- package/src/hooks/useCompiledFragments.ts +42 -0
- package/src/hooks/useFigmaIntegration.ts +132 -0
- package/src/hooks/useHmrStatus.ts +109 -0
- package/src/hooks/useKeyboardShortcuts.ts +270 -0
- package/src/hooks/usePreviewBridge.ts +347 -0
- package/src/hooks/useScrollSpy.ts +78 -0
- package/src/hooks/useShadowStyles.ts +221 -0
- package/src/hooks/useUrlState.ts +318 -0
- package/src/hooks/useViewSettings.ts +111 -0
- package/src/intelligence/healthReport.ts +505 -0
- package/src/intelligence/styleDrift.ts +340 -0
- package/src/intelligence/usageScanner.ts +309 -0
- package/src/jsx-parser.ts +486 -0
- package/src/preview-frame-entry.tsx +25 -0
- package/src/preview-frame.html +148 -0
- package/src/render-template.html +68 -0
- package/src/render-utils.ts +311 -0
- package/src/shared/ComponentDocContent.module.scss +10 -0
- package/src/shared/ComponentDocContent.module.scss.d.ts +2 -0
- package/src/shared/ComponentDocContent.tsx +274 -0
- package/src/shared/DocsHeaderBar.tsx +129 -0
- package/src/shared/DocsPageAsideHost.tsx +89 -0
- package/src/shared/DocsPageShell.tsx +124 -0
- package/src/shared/DocsSearchCommand.tsx +99 -0
- package/src/shared/DocsSidebarNav.tsx +66 -0
- package/src/shared/PropsTable.module.scss +68 -0
- package/src/shared/PropsTable.module.scss.d.ts +2 -0
- package/src/shared/PropsTable.tsx +76 -0
- package/src/shared/VariantPreviewCard.module.scss +114 -0
- package/src/shared/VariantPreviewCard.module.scss.d.ts +2 -0
- package/src/shared/VariantPreviewCard.tsx +137 -0
- package/src/shared/docs-data/index.ts +32 -0
- package/src/shared/docs-data/mcp-configs.ts +72 -0
- package/src/shared/docs-data/palettes.ts +75 -0
- package/src/shared/docs-data/setup-examples.ts +55 -0
- package/src/shared/docs-layout.scss +28 -0
- package/src/shared/docs-layout.scss.d.ts +2 -0
- package/src/shared/index.ts +34 -0
- package/src/shared/types.ts +53 -0
- package/src/style-utils.ts +414 -0
- package/src/styles/globals.css +278 -0
- package/src/types/a11y.ts +197 -0
- package/src/utils/a11y-fixes.ts +509 -0
- package/src/utils/actionExport.ts +372 -0
- package/src/utils/colorSchemes.ts +201 -0
- package/src/utils/contrast.ts +246 -0
- package/src/utils/detectRelationships.ts +256 -0
- package/src/webmcp/__tests__/analytics.test.ts +108 -0
- package/src/webmcp/analytics.ts +165 -0
- package/src/webmcp/index.ts +3 -0
- package/src/webmcp/posthog-bridge.ts +39 -0
- package/src/webmcp/runtime-tools.ts +152 -0
- package/src/webmcp/scan-utils.ts +135 -0
- package/src/webmcp/use-tool-analytics.ts +69 -0
- package/src/webmcp/viewer-state.ts +45 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command Palette - Cmd+K Quick Switcher
|
|
3
|
+
*
|
|
4
|
+
* A keyboard-driven command palette for:
|
|
5
|
+
* - Quick navigation to components
|
|
6
|
+
* - Fuzzy search across all components and variants
|
|
7
|
+
* - Keyboard navigation (arrows, enter, escape)
|
|
8
|
+
*
|
|
9
|
+
* Uses Fragments UI Dialog for the overlay shell.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useState, useEffect, useRef, useMemo, useCallback } from "react";
|
|
13
|
+
import { Dialog, Stack, Text, Badge, Separator, Input, Box } from '@fragments-sdk/ui';
|
|
14
|
+
import type { FragmentDefinition } from '@fragments-sdk/core';
|
|
15
|
+
import { SearchIcon, ChevronRightIcon } from "./Icons.js";
|
|
16
|
+
|
|
17
|
+
interface CommandPaletteProps {
|
|
18
|
+
/** Whether the palette is open */
|
|
19
|
+
isOpen: boolean;
|
|
20
|
+
/** Callback to close the palette */
|
|
21
|
+
onClose: () => void;
|
|
22
|
+
/** All available fragments */
|
|
23
|
+
fragments: Array<{ path: string; fragment: FragmentDefinition }>;
|
|
24
|
+
/** Callback when a component is selected */
|
|
25
|
+
onSelectComponent: (path: string) => void;
|
|
26
|
+
/** Callback when a variant is selected */
|
|
27
|
+
onSelectVariant: (path: string, variantIndex: number) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface SearchResult {
|
|
31
|
+
type: "component" | "variant";
|
|
32
|
+
path: string;
|
|
33
|
+
componentName: string;
|
|
34
|
+
variantName?: string;
|
|
35
|
+
variantIndex?: number;
|
|
36
|
+
category: string;
|
|
37
|
+
score: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function CommandPalette({
|
|
41
|
+
isOpen,
|
|
42
|
+
onClose,
|
|
43
|
+
fragments,
|
|
44
|
+
onSelectComponent,
|
|
45
|
+
onSelectVariant,
|
|
46
|
+
}: CommandPaletteProps) {
|
|
47
|
+
const [query, setQuery] = useState("");
|
|
48
|
+
const [selectedIndex, setSelectedIndex] = useState(0);
|
|
49
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
50
|
+
const listRef = useRef<HTMLDivElement>(null);
|
|
51
|
+
|
|
52
|
+
// Build search results with fuzzy matching
|
|
53
|
+
const results = useMemo(() => {
|
|
54
|
+
const allResults: SearchResult[] = [];
|
|
55
|
+
|
|
56
|
+
for (const { path, fragment } of fragments) {
|
|
57
|
+
const componentName = fragment.meta.name;
|
|
58
|
+
const category = fragment.meta.category;
|
|
59
|
+
|
|
60
|
+
// Add component result
|
|
61
|
+
allResults.push({
|
|
62
|
+
type: "component",
|
|
63
|
+
path,
|
|
64
|
+
componentName,
|
|
65
|
+
category,
|
|
66
|
+
score: 0,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Add variant results
|
|
70
|
+
if (fragment.variants) {
|
|
71
|
+
for (let i = 0; i < fragment.variants.length; i++) {
|
|
72
|
+
const variant = fragment.variants[i];
|
|
73
|
+
allResults.push({
|
|
74
|
+
type: "variant",
|
|
75
|
+
path,
|
|
76
|
+
componentName,
|
|
77
|
+
variantName: variant.name,
|
|
78
|
+
variantIndex: i,
|
|
79
|
+
category,
|
|
80
|
+
score: 0,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!query.trim()) {
|
|
87
|
+
// No query - show all components sorted alphabetically
|
|
88
|
+
return allResults
|
|
89
|
+
.filter((r) => r.type === "component")
|
|
90
|
+
.sort((a, b) => a.componentName.localeCompare(b.componentName))
|
|
91
|
+
.slice(0, 20);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Fuzzy match and score results
|
|
95
|
+
const lowerQuery = query.toLowerCase();
|
|
96
|
+
const scored = allResults
|
|
97
|
+
.map((result) => {
|
|
98
|
+
const searchText = result.variantName
|
|
99
|
+
? `${result.componentName} ${result.variantName}`
|
|
100
|
+
: result.componentName;
|
|
101
|
+
const score = fuzzyScore(searchText.toLowerCase(), lowerQuery);
|
|
102
|
+
return { ...result, score };
|
|
103
|
+
})
|
|
104
|
+
.filter((r) => r.score > 0)
|
|
105
|
+
.sort((a, b) => b.score - a.score)
|
|
106
|
+
.slice(0, 20);
|
|
107
|
+
|
|
108
|
+
return scored;
|
|
109
|
+
}, [fragments, query]);
|
|
110
|
+
|
|
111
|
+
// Reset selection when results change
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
setSelectedIndex(0);
|
|
114
|
+
}, [results]);
|
|
115
|
+
|
|
116
|
+
// Focus input when opened
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
if (isOpen) {
|
|
119
|
+
setQuery("");
|
|
120
|
+
setSelectedIndex(0);
|
|
121
|
+
setTimeout(() => inputRef.current?.focus(), 0);
|
|
122
|
+
}
|
|
123
|
+
}, [isOpen]);
|
|
124
|
+
|
|
125
|
+
// Scroll selected item into view
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
if (listRef.current && results.length > 0) {
|
|
128
|
+
const selectedEl = listRef.current.children[selectedIndex] as HTMLElement;
|
|
129
|
+
if (selectedEl) {
|
|
130
|
+
selectedEl.scrollIntoView({ block: "nearest" });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}, [selectedIndex, results.length]);
|
|
134
|
+
|
|
135
|
+
const handleKeyDown = useCallback(
|
|
136
|
+
(e: React.KeyboardEvent) => {
|
|
137
|
+
switch (e.key) {
|
|
138
|
+
case "ArrowDown":
|
|
139
|
+
e.preventDefault();
|
|
140
|
+
setSelectedIndex((i) => Math.min(i + 1, results.length - 1));
|
|
141
|
+
break;
|
|
142
|
+
case "ArrowUp":
|
|
143
|
+
e.preventDefault();
|
|
144
|
+
setSelectedIndex((i) => Math.max(i - 1, 0));
|
|
145
|
+
break;
|
|
146
|
+
case "Enter":
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
if (results[selectedIndex]) {
|
|
149
|
+
handleSelect(results[selectedIndex]);
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
case "Escape":
|
|
153
|
+
e.preventDefault();
|
|
154
|
+
onClose();
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
[results, selectedIndex, onClose]
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const handleSelect = (result: SearchResult) => {
|
|
162
|
+
if (result.type === "variant" && result.variantIndex !== undefined) {
|
|
163
|
+
onSelectVariant(result.path, result.variantIndex);
|
|
164
|
+
} else {
|
|
165
|
+
onSelectComponent(result.path);
|
|
166
|
+
}
|
|
167
|
+
onClose();
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
if (!isOpen) return null;
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<Dialog open={isOpen} onOpenChange={(open: boolean) => { if (!open) onClose(); }}>
|
|
174
|
+
<Dialog.Content size="lg">
|
|
175
|
+
{/* Search input */}
|
|
176
|
+
<Stack direction="row" align="center" style={{
|
|
177
|
+
padding: '12px 16px',
|
|
178
|
+
borderBottom: '2px solid #3b82f6',
|
|
179
|
+
}}>
|
|
180
|
+
<SearchIcon style={{ width: 20, height: 20, color: '#3b82f6', marginRight: 12 }} />
|
|
181
|
+
<input
|
|
182
|
+
ref={inputRef}
|
|
183
|
+
type="text"
|
|
184
|
+
value={query}
|
|
185
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
186
|
+
onKeyDown={handleKeyDown}
|
|
187
|
+
placeholder="Search components and variants..."
|
|
188
|
+
style={{
|
|
189
|
+
flex: 1,
|
|
190
|
+
background: 'transparent',
|
|
191
|
+
color: 'var(--text-primary)',
|
|
192
|
+
fontSize: '16px',
|
|
193
|
+
border: 'none',
|
|
194
|
+
outline: 'none',
|
|
195
|
+
}}
|
|
196
|
+
/>
|
|
197
|
+
<Badge size="sm" variant="default">esc</Badge>
|
|
198
|
+
</Stack>
|
|
199
|
+
|
|
200
|
+
{/* Results */}
|
|
201
|
+
<Box ref={listRef} overflow="auto" style={{ maxHeight: '320px', padding: '8px 0' }}>
|
|
202
|
+
{results.length === 0 ? (
|
|
203
|
+
<Box padding="lg" style={{ textAlign: 'center' }}>
|
|
204
|
+
<Text color="tertiary">
|
|
205
|
+
{query ? (
|
|
206
|
+
<>No results for “{query}”</>
|
|
207
|
+
) : (
|
|
208
|
+
<>Start typing to search...</>
|
|
209
|
+
)}
|
|
210
|
+
</Text>
|
|
211
|
+
</Box>
|
|
212
|
+
) : (
|
|
213
|
+
results.map((result, index) => (
|
|
214
|
+
<button
|
|
215
|
+
key={`${result.path}-${result.variantName || "component"}`}
|
|
216
|
+
onClick={() => handleSelect(result)}
|
|
217
|
+
style={{
|
|
218
|
+
width: '100%',
|
|
219
|
+
padding: '8px 16px',
|
|
220
|
+
display: 'flex',
|
|
221
|
+
alignItems: 'center',
|
|
222
|
+
gap: '12px',
|
|
223
|
+
textAlign: 'left',
|
|
224
|
+
transition: 'background-color 150ms',
|
|
225
|
+
border: 'none',
|
|
226
|
+
cursor: 'pointer',
|
|
227
|
+
backgroundColor: index === selectedIndex ? 'rgba(59, 130, 246, 0.1)' : 'transparent',
|
|
228
|
+
color: index === selectedIndex ? 'var(--text-primary)' : 'var(--text-secondary)',
|
|
229
|
+
}}
|
|
230
|
+
onMouseEnter={(e) => {
|
|
231
|
+
setSelectedIndex(index);
|
|
232
|
+
if (index !== selectedIndex) {
|
|
233
|
+
e.currentTarget.style.backgroundColor = 'var(--bg-hover)';
|
|
234
|
+
}
|
|
235
|
+
}}
|
|
236
|
+
onMouseLeave={(e) => {
|
|
237
|
+
if (index !== selectedIndex) {
|
|
238
|
+
e.currentTarget.style.backgroundColor = 'transparent';
|
|
239
|
+
}
|
|
240
|
+
}}
|
|
241
|
+
>
|
|
242
|
+
{/* Icon/Badge */}
|
|
243
|
+
<Badge
|
|
244
|
+
size="sm"
|
|
245
|
+
style={{
|
|
246
|
+
flexShrink: 0,
|
|
247
|
+
width: '32px',
|
|
248
|
+
height: '32px',
|
|
249
|
+
borderRadius: '8px',
|
|
250
|
+
display: 'flex',
|
|
251
|
+
alignItems: 'center',
|
|
252
|
+
justifyContent: 'center',
|
|
253
|
+
backgroundColor: result.type === "component"
|
|
254
|
+
? 'rgba(59, 130, 246, 0.15)'
|
|
255
|
+
: 'rgba(168, 85, 247, 0.15)',
|
|
256
|
+
color: result.type === "component"
|
|
257
|
+
? '#3b82f6'
|
|
258
|
+
: '#a855f7',
|
|
259
|
+
}}
|
|
260
|
+
>
|
|
261
|
+
{result.type === "component" ? "C" : "V"}
|
|
262
|
+
</Badge>
|
|
263
|
+
|
|
264
|
+
{/* Content */}
|
|
265
|
+
<div style={{ flex: 1, minWidth: 0 }}>
|
|
266
|
+
<Stack direction="row" align="center" gap="sm">
|
|
267
|
+
<Text weight="medium" style={{
|
|
268
|
+
overflow: 'hidden',
|
|
269
|
+
textOverflow: 'ellipsis',
|
|
270
|
+
whiteSpace: 'nowrap',
|
|
271
|
+
}}>
|
|
272
|
+
{result.componentName}
|
|
273
|
+
</Text>
|
|
274
|
+
{result.variantName && (
|
|
275
|
+
<>
|
|
276
|
+
<ChevronRightIcon style={{ width: 12, height: 12, color: 'var(--text-tertiary)', flexShrink: 0 }} />
|
|
277
|
+
<Text color="secondary" style={{
|
|
278
|
+
overflow: 'hidden',
|
|
279
|
+
textOverflow: 'ellipsis',
|
|
280
|
+
whiteSpace: 'nowrap',
|
|
281
|
+
}}>
|
|
282
|
+
{result.variantName}
|
|
283
|
+
</Text>
|
|
284
|
+
</>
|
|
285
|
+
)}
|
|
286
|
+
</Stack>
|
|
287
|
+
<Text size="xs" color="tertiary" style={{
|
|
288
|
+
overflow: 'hidden',
|
|
289
|
+
textOverflow: 'ellipsis',
|
|
290
|
+
whiteSpace: 'nowrap',
|
|
291
|
+
}}>
|
|
292
|
+
{result.category}
|
|
293
|
+
</Text>
|
|
294
|
+
</div>
|
|
295
|
+
|
|
296
|
+
{/* Keyboard hint for selected */}
|
|
297
|
+
{index === selectedIndex && (
|
|
298
|
+
<Badge size="sm" variant="default">enter</Badge>
|
|
299
|
+
)}
|
|
300
|
+
</button>
|
|
301
|
+
))
|
|
302
|
+
)}
|
|
303
|
+
</Box>
|
|
304
|
+
|
|
305
|
+
{/* Footer */}
|
|
306
|
+
<Box paddingX="md" paddingY="sm" borderTop>
|
|
307
|
+
<Stack direction="row" align="center" justify="between">
|
|
308
|
+
<Stack direction="row" align="center" gap="md">
|
|
309
|
+
<Stack direction="row" align="center" gap="xs">
|
|
310
|
+
<Badge size="sm" variant="default">↑</Badge>
|
|
311
|
+
<Badge size="sm" variant="default">↓</Badge>
|
|
312
|
+
<Text size="xs" color="tertiary">to navigate</Text>
|
|
313
|
+
</Stack>
|
|
314
|
+
<Stack direction="row" align="center" gap="xs">
|
|
315
|
+
<Badge size="sm" variant="default">↵</Badge>
|
|
316
|
+
<Text size="xs" color="tertiary">to select</Text>
|
|
317
|
+
</Stack>
|
|
318
|
+
</Stack>
|
|
319
|
+
<Text size="xs" color="tertiary">
|
|
320
|
+
{results.length} result{results.length !== 1 ? "s" : ""}
|
|
321
|
+
</Text>
|
|
322
|
+
</Stack>
|
|
323
|
+
</Box>
|
|
324
|
+
</Dialog.Content>
|
|
325
|
+
</Dialog>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Simple fuzzy matching score
|
|
331
|
+
* Returns 0 for no match, higher values for better matches
|
|
332
|
+
*/
|
|
333
|
+
function fuzzyScore(text: string, query: string): number {
|
|
334
|
+
if (!query) return 1;
|
|
335
|
+
|
|
336
|
+
// Require substring match for short queries to avoid scattered single-char noise
|
|
337
|
+
if (query.length <= 3 && !text.includes(query)) {
|
|
338
|
+
return 0;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
let score = 0;
|
|
342
|
+
let queryIndex = 0;
|
|
343
|
+
let consecutiveBonus = 0;
|
|
344
|
+
let lastMatchIndex = -2;
|
|
345
|
+
let maxGap = 0;
|
|
346
|
+
|
|
347
|
+
for (let i = 0; i < text.length && queryIndex < query.length; i++) {
|
|
348
|
+
if (text[i] === query[queryIndex]) {
|
|
349
|
+
score += 1;
|
|
350
|
+
|
|
351
|
+
// Bonus for consecutive matches
|
|
352
|
+
if (i === lastMatchIndex + 1) {
|
|
353
|
+
consecutiveBonus += 2;
|
|
354
|
+
score += consecutiveBonus;
|
|
355
|
+
} else {
|
|
356
|
+
consecutiveBonus = 0;
|
|
357
|
+
// Track max gap between matches
|
|
358
|
+
if (lastMatchIndex >= 0) {
|
|
359
|
+
const gap = i - lastMatchIndex;
|
|
360
|
+
if (gap > maxGap) maxGap = gap;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Bonus for matching at word start
|
|
365
|
+
if (i === 0 || text[i - 1] === " " || text[i - 1] === "-" || text[i - 1] === "_") {
|
|
366
|
+
score += 3;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
lastMatchIndex = i;
|
|
370
|
+
queryIndex++;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Only return score if all query characters were found
|
|
375
|
+
if (queryIndex < query.length) return 0;
|
|
376
|
+
|
|
377
|
+
// Penalize large gaps between matched characters
|
|
378
|
+
if (maxGap > 5) {
|
|
379
|
+
score -= maxGap;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Bonus for shorter results (more relevant)
|
|
383
|
+
score += Math.max(0, 20 - text.length);
|
|
384
|
+
|
|
385
|
+
// Bonus for exact match
|
|
386
|
+
if (text === query) score += 50;
|
|
387
|
+
|
|
388
|
+
// Bonus for starts with
|
|
389
|
+
if (text.startsWith(query)) score += 30;
|
|
390
|
+
|
|
391
|
+
return Math.max(score, 0) || 0;
|
|
392
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
FragmentDefinition,
|
|
4
|
+
FragmentVariant,
|
|
5
|
+
PropDefinition,
|
|
6
|
+
} from '@fragments-sdk/core';
|
|
7
|
+
import type { DocProp } from "../shared/types";
|
|
8
|
+
import { ComponentDocContent } from "../shared/ComponentDocContent";
|
|
9
|
+
import { VariantPreviewCard } from "../shared/VariantPreviewCard";
|
|
10
|
+
import { Badge, Card, CardBody, Stack, Text } from "@fragments-sdk/ui";
|
|
11
|
+
import { PreviewArea } from "./PreviewArea.js";
|
|
12
|
+
import { resolveDisplayedCode } from "./CodePanel.js";
|
|
13
|
+
import { getVariantSectionId } from "./viewer-utils.js";
|
|
14
|
+
import type { ZoomLevel, ViewportPreset, ViewportSize } from "../constants/ui.js";
|
|
15
|
+
import type { PreviewTheme } from "../hooks/useViewSettings.js";
|
|
16
|
+
|
|
17
|
+
interface ComponentDocViewProps {
|
|
18
|
+
fragment: { path: string; fragment: FragmentDefinition };
|
|
19
|
+
fragments: Array<{ path: string; fragment: FragmentDefinition }>;
|
|
20
|
+
renderVariantContent: (variant: FragmentVariant) => ReactNode;
|
|
21
|
+
onNavigateToComponent: (name: string) => void;
|
|
22
|
+
zoom: ZoomLevel;
|
|
23
|
+
viewport: ViewportPreset;
|
|
24
|
+
customSize: ViewportSize;
|
|
25
|
+
previewTheme: PreviewTheme;
|
|
26
|
+
showComparison: boolean;
|
|
27
|
+
allFigmaUrls: string[];
|
|
28
|
+
onRetry: () => void;
|
|
29
|
+
previewKeyBase: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function mapPropsToDocProps(props: Record<string, PropDefinition>): Record<string, DocProp> {
|
|
33
|
+
const record: Record<string, DocProp> = {};
|
|
34
|
+
for (const [name, prop] of Object.entries(props)) {
|
|
35
|
+
record[name] = {
|
|
36
|
+
type: prop.type,
|
|
37
|
+
description: prop.description || "",
|
|
38
|
+
required: prop.required,
|
|
39
|
+
default: prop.default,
|
|
40
|
+
values: prop.values ? [...prop.values] : undefined,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return record;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function ComponentDocView({
|
|
47
|
+
fragment,
|
|
48
|
+
fragments,
|
|
49
|
+
renderVariantContent,
|
|
50
|
+
onNavigateToComponent,
|
|
51
|
+
zoom,
|
|
52
|
+
viewport,
|
|
53
|
+
customSize,
|
|
54
|
+
previewTheme,
|
|
55
|
+
showComparison,
|
|
56
|
+
allFigmaUrls,
|
|
57
|
+
onRetry,
|
|
58
|
+
previewKeyBase,
|
|
59
|
+
}: ComponentDocViewProps) {
|
|
60
|
+
const def = fragment.fragment;
|
|
61
|
+
const componentName = def.meta.name;
|
|
62
|
+
|
|
63
|
+
const propsRecord = mapPropsToDocProps(def.props);
|
|
64
|
+
const packageName = typeof window !== 'undefined' ? window.__FRAGMENTS_PACKAGE_NAME__ : null;
|
|
65
|
+
|
|
66
|
+
const usageData = {
|
|
67
|
+
when: def.usage?.when || [],
|
|
68
|
+
whenNot: def.usage?.whenNot || [],
|
|
69
|
+
guidelines: def.usage?.guidelines || [],
|
|
70
|
+
accessibility: def.usage?.accessibility || [],
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const relations = def.relations || [];
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<ComponentDocContent
|
|
77
|
+
name={componentName}
|
|
78
|
+
description={def.meta.description || ""}
|
|
79
|
+
componentId={componentName}
|
|
80
|
+
packageName={packageName || undefined}
|
|
81
|
+
props={propsRecord}
|
|
82
|
+
variants={def.variants.map((v) => ({
|
|
83
|
+
name: v.name,
|
|
84
|
+
description: v.description,
|
|
85
|
+
code: v.code,
|
|
86
|
+
}))}
|
|
87
|
+
usage={usageData}
|
|
88
|
+
relations={relations}
|
|
89
|
+
dependencies={def.meta.dependencies}
|
|
90
|
+
renderVariant={(variant, index) => {
|
|
91
|
+
const fragVariant = def.variants[index];
|
|
92
|
+
if (!fragVariant) return null;
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<VariantPreviewCard
|
|
96
|
+
key={variant.name}
|
|
97
|
+
name={variant.name}
|
|
98
|
+
description={variant.description}
|
|
99
|
+
code={resolveDisplayedCode(componentName, fragVariant)}
|
|
100
|
+
id={getVariantSectionId(componentName, variant.name)}
|
|
101
|
+
>
|
|
102
|
+
<PreviewArea
|
|
103
|
+
componentName={componentName}
|
|
104
|
+
fragmentPath={fragment.path}
|
|
105
|
+
variant={fragVariant}
|
|
106
|
+
variants={def.variants}
|
|
107
|
+
zoom={zoom}
|
|
108
|
+
viewport={viewport}
|
|
109
|
+
customSize={customSize}
|
|
110
|
+
previewTheme={previewTheme}
|
|
111
|
+
showMatrixView={false}
|
|
112
|
+
showMultiViewport={false}
|
|
113
|
+
showComparison={showComparison}
|
|
114
|
+
figmaUrl={fragVariant.figma || def.meta.figma}
|
|
115
|
+
allFigmaUrls={allFigmaUrls}
|
|
116
|
+
onSelectVariant={() => {}}
|
|
117
|
+
onRetry={onRetry}
|
|
118
|
+
renderContent={() => renderVariantContent(fragVariant)}
|
|
119
|
+
previewKey={`${previewKeyBase}-${index}`}
|
|
120
|
+
/>
|
|
121
|
+
</VariantPreviewCard>
|
|
122
|
+
);
|
|
123
|
+
}}
|
|
124
|
+
renderRelatedLink={(comp, relationship, note, key) => {
|
|
125
|
+
const target = fragments.find(
|
|
126
|
+
(s) => s.fragment.meta.name.toLowerCase() === comp.toLowerCase()
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<a
|
|
131
|
+
key={key}
|
|
132
|
+
onClick={(e) => {
|
|
133
|
+
e.preventDefault();
|
|
134
|
+
onNavigateToComponent(comp);
|
|
135
|
+
}}
|
|
136
|
+
href="#"
|
|
137
|
+
style={{
|
|
138
|
+
textDecoration: "none",
|
|
139
|
+
color: "inherit",
|
|
140
|
+
cursor: target ? "pointer" : "default",
|
|
141
|
+
opacity: target ? 1 : 0.6,
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
<Card variant="outlined">
|
|
145
|
+
<CardBody>
|
|
146
|
+
<Stack gap="xs">
|
|
147
|
+
<Stack direction="row" align="center" justify="between">
|
|
148
|
+
<Text weight="semibold">{comp}</Text>
|
|
149
|
+
<Badge size="sm">{relationship}</Badge>
|
|
150
|
+
</Stack>
|
|
151
|
+
{note && (
|
|
152
|
+
<Text size="sm" color="secondary">
|
|
153
|
+
{note}
|
|
154
|
+
</Text>
|
|
155
|
+
)}
|
|
156
|
+
</Stack>
|
|
157
|
+
</CardBody>
|
|
158
|
+
</Card>
|
|
159
|
+
</a>
|
|
160
|
+
);
|
|
161
|
+
}}
|
|
162
|
+
/>
|
|
163
|
+
);
|
|
164
|
+
}
|