tekivex-ui 2.5.2 → 2.5.3
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/dist/TkxForm-DSMDo2Ol.js +388 -0
- package/dist/TkxForm-DwV_n-eE.cjs +12 -0
- package/dist/headless.cjs +1 -1
- package/dist/headless.js +396 -195
- package/dist/i18n.cjs +1 -0
- package/dist/i18n.js +757 -0
- package/dist/index.cjs +27 -22
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4994 -5089
- package/dist/quantum.cjs +91 -0
- package/dist/quantum.js +2312 -0
- package/dist/realtime.cjs +25 -0
- package/dist/realtime.js +944 -0
- package/dist/security-C369oOWH.cjs +1 -0
- package/dist/security-CzAmaMqa.js +97 -0
- package/dist/src/components/TkxDataGrid.d.ts +9 -1
- package/dist/src/components/TkxDataGrid.d.ts.map +1 -1
- package/dist/src/components/TkxDropdown.d.ts +41 -0
- package/dist/src/components/TkxDropdown.d.ts.map +1 -0
- package/dist/src/components/TkxLiveFeed.d.ts +891 -0
- package/dist/src/components/TkxLiveFeed.d.ts.map +1 -0
- package/dist/src/components/TkxLiveLog.d.ts +892 -0
- package/dist/src/components/TkxLiveLog.d.ts.map +1 -0
- package/dist/src/components/TkxLiveMetrics.d.ts +24 -0
- package/dist/src/components/TkxLiveMetrics.d.ts.map +1 -0
- package/dist/src/components/TkxPlayground.d.ts +15 -0
- package/dist/src/components/TkxPlayground.d.ts.map +1 -0
- package/dist/src/components/TkxQuantumForm.d.ts +20 -0
- package/dist/src/components/TkxQuantumForm.d.ts.map +1 -0
- package/dist/src/components/TkxRealTimeChart.d.ts +27 -0
- package/dist/src/components/TkxRealTimeChart.d.ts.map +1 -0
- package/dist/src/components/TkxTable.d.ts +11 -1
- package/dist/src/components/TkxTable.d.ts.map +1 -1
- package/dist/src/components/TkxThemeBuilder.d.ts +9 -0
- package/dist/src/components/TkxThemeBuilder.d.ts.map +1 -0
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/engine/index.d.ts +2 -0
- package/dist/src/engine/index.d.ts.map +1 -1
- package/dist/src/engine/quantum-ai.d.ts +201 -0
- package/dist/src/engine/quantum-ai.d.ts.map +1 -0
- package/dist/src/engine/tkx.d.ts.map +1 -1
- package/dist/src/headless/index.d.ts +6 -0
- package/dist/src/headless/index.d.ts.map +1 -1
- package/dist/src/headless/useInfiniteQuery.d.ts +35 -0
- package/dist/src/headless/useInfiniteQuery.d.ts.map +1 -0
- package/dist/src/headless/useSSE.d.ts +36 -0
- package/dist/src/headless/useSSE.d.ts.map +1 -0
- package/dist/src/headless/useWebSocket.d.ts +33 -0
- package/dist/src/headless/useWebSocket.d.ts.map +1 -0
- package/dist/src/quantum/index.d.ts +9 -0
- package/dist/src/quantum/index.d.ts.map +1 -0
- package/dist/src/realtime/index.d.ts +9 -0
- package/dist/src/realtime/index.d.ts.map +1 -0
- package/dist/{TkxForm-BWK4LqY3.js → tkx-BJ10JtMv.js} +372 -721
- package/dist/tkx-BeNyC0w7.cjs +4 -0
- package/package.json +16 -1
- package/src/components/TkxDataGrid.tsx +72 -0
- package/src/components/TkxDropdown.tsx +802 -0
- package/src/components/TkxLiveFeed.tsx +329 -0
- package/src/components/TkxLiveLog.tsx +470 -0
- package/src/components/TkxLiveMetrics.tsx +291 -0
- package/src/components/TkxPlayground.tsx +736 -0
- package/src/components/TkxQuantumForm.tsx +592 -0
- package/src/components/TkxRealTimeChart.tsx +381 -0
- package/src/components/TkxTable.tsx +351 -77
- package/src/components/TkxThemeBuilder.tsx +685 -0
- package/src/components/index.ts +4 -0
- package/src/engine/index.ts +2 -0
- package/src/engine/quantum-ai.ts +1045 -0
- package/src/engine/tkx.ts +95 -4
- package/src/headless/index.ts +6 -0
- package/src/headless/useInfiniteQuery.ts +153 -0
- package/src/headless/useSSE.ts +165 -0
- package/src/headless/useWebSocket.ts +229 -0
- package/src/quantum/index.ts +39 -0
- package/src/realtime/index.ts +21 -0
- package/dist/TkxForm-ljQjX7KD.cjs +0 -15
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
// ── TkxPlayground ─────────────────────────────────────────────────────────────
|
|
2
|
+
// In-browser live component playground. Type JSX, see it render instantly.
|
|
3
|
+
// Uses new Function() evaluation, an error boundary, quantum component
|
|
4
|
+
// suggestions via AmplitudeAmplifier, and real-time render-time metrics.
|
|
5
|
+
|
|
6
|
+
import { useState, useCallback, useRef, useEffect, type ReactNode } from 'react';
|
|
7
|
+
import React from 'react';
|
|
8
|
+
import { useTheme } from '../themes';
|
|
9
|
+
import { AmplitudeAmplifier } from '../engine/quantum-ai';
|
|
10
|
+
|
|
11
|
+
// ── Public API ────────────────────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
export interface PlaygroundExample {
|
|
14
|
+
label: string;
|
|
15
|
+
code: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TkxPlaygroundProps {
|
|
19
|
+
defaultCode?: string;
|
|
20
|
+
examples?: PlaygroundExample[];
|
|
21
|
+
height?: number;
|
|
22
|
+
imports?: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ── Built-in examples ─────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
const BUILT_IN_EXAMPLES: PlaygroundExample[] = [
|
|
28
|
+
{
|
|
29
|
+
label: 'Hello World',
|
|
30
|
+
code: `<div style={{ padding: 24, fontFamily: 'sans-serif', color: '#e8e8f4' }}>
|
|
31
|
+
<h2 style={{ margin: 0 }}>👋 Hello, TkxPlayground!</h2>
|
|
32
|
+
<p style={{ marginTop: 8, opacity: 0.7 }}>Edit the code on the left to see live updates.</p>
|
|
33
|
+
</div>`,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
label: 'Button Styles',
|
|
37
|
+
code: `<div style={{ display: 'flex', gap: 12, flexWrap: 'wrap', padding: 24 }}>
|
|
38
|
+
{['primary', 'success', 'danger', 'warning', 'info'].map(color => (
|
|
39
|
+
<button
|
|
40
|
+
key={color}
|
|
41
|
+
style={{
|
|
42
|
+
padding: '8px 18px',
|
|
43
|
+
borderRadius: 8,
|
|
44
|
+
border: 'none',
|
|
45
|
+
background: color === 'primary' ? '#00f5d4' : color === 'success' ? '#06d6a0' : color === 'danger' ? '#f72585' : color === 'warning' ? '#ffbe0b' : '#3a86ff',
|
|
46
|
+
color: '#0a0a0f',
|
|
47
|
+
fontWeight: 700,
|
|
48
|
+
cursor: 'pointer',
|
|
49
|
+
textTransform: 'capitalize',
|
|
50
|
+
}}
|
|
51
|
+
>
|
|
52
|
+
{color}
|
|
53
|
+
</button>
|
|
54
|
+
))}
|
|
55
|
+
</div>`,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
label: 'Card Grid',
|
|
59
|
+
code: `<div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 16, padding: 24 }}>
|
|
60
|
+
{['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', 'Zeta'].map(name => (
|
|
61
|
+
<div key={name} style={{
|
|
62
|
+
background: '#1a1a2e',
|
|
63
|
+
borderRadius: 10,
|
|
64
|
+
border: '1px solid #2a2a3e',
|
|
65
|
+
padding: '16px',
|
|
66
|
+
}}>
|
|
67
|
+
<div style={{ fontWeight: 700, color: '#00f5d4', marginBottom: 4 }}>{name}</div>
|
|
68
|
+
<div style={{ fontSize: 12, color: '#8888aa' }}>Quantum module {name.toLowerCase()}</div>
|
|
69
|
+
</div>
|
|
70
|
+
))}
|
|
71
|
+
</div>`,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
label: 'Animated Counter',
|
|
75
|
+
code: `(() => {
|
|
76
|
+
const [count, setCount] = React.useState(0);
|
|
77
|
+
return (
|
|
78
|
+
<div style={{ padding: 32, textAlign: 'center', fontFamily: 'sans-serif' }}>
|
|
79
|
+
<div style={{ fontSize: 64, fontWeight: 900, color: '#00f5d4', marginBottom: 16 }}>
|
|
80
|
+
{count}
|
|
81
|
+
</div>
|
|
82
|
+
<div style={{ display: 'flex', gap: 12, justifyContent: 'center' }}>
|
|
83
|
+
<button onClick={() => setCount(c => c - 1)} style={{ padding: '8px 20px', borderRadius: 8, border: '1px solid #2a2a3e', background: '#1a1a2e', color: '#e8e8f4', fontSize: 20, cursor: 'pointer' }}>−</button>
|
|
84
|
+
<button onClick={() => setCount(0)} style={{ padding: '8px 20px', borderRadius: 8, border: '1px solid #2a2a3e', background: '#1a1a2e', color: '#8888aa', cursor: 'pointer' }}>Reset</button>
|
|
85
|
+
<button onClick={() => setCount(c => c + 1)} style={{ padding: '8px 20px', borderRadius: 8, border: '1px solid #2a2a3e', background: '#1a1a2e', color: '#e8e8f4', fontSize: 20, cursor: 'pointer' }}>+</button>
|
|
86
|
+
</div>
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
})()`,
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
label: 'Data Table',
|
|
93
|
+
code: `(() => {
|
|
94
|
+
const rows = [
|
|
95
|
+
{ id: 1, name: 'Hydrogen', symbol: 'H', mass: 1.008 },
|
|
96
|
+
{ id: 2, name: 'Helium', symbol: 'He', mass: 4.003 },
|
|
97
|
+
{ id: 3, name: 'Lithium', symbol: 'Li', mass: 6.941 },
|
|
98
|
+
{ id: 4, name: 'Carbon', symbol: 'C', mass: 12.011 },
|
|
99
|
+
{ id: 5, name: 'Nitrogen', symbol: 'N', mass: 14.007 },
|
|
100
|
+
];
|
|
101
|
+
const cell = { padding: '8px 14px', borderBottom: '1px solid #2a2a3e', color: '#e8e8f4', fontSize: 13 };
|
|
102
|
+
const hcell = { ...cell, color: '#8888aa', fontWeight: 600, fontSize: 11, textTransform: 'uppercase', letterSpacing: '0.06em' };
|
|
103
|
+
return (
|
|
104
|
+
<div style={{ padding: 16 }}>
|
|
105
|
+
<table style={{ width: '100%', borderCollapse: 'collapse', fontFamily: 'sans-serif' }}>
|
|
106
|
+
<thead>
|
|
107
|
+
<tr>{['ID','Element','Symbol','Atomic Mass'].map(h => <th key={h} style={hcell}>{h}</th>)}</tr>
|
|
108
|
+
</thead>
|
|
109
|
+
<tbody>
|
|
110
|
+
{rows.map(r => (
|
|
111
|
+
<tr key={r.id} style={{ transition: 'background 0.15s' }}>
|
|
112
|
+
<td style={cell}>{r.id}</td>
|
|
113
|
+
<td style={cell}>{r.name}</td>
|
|
114
|
+
<td style={{ ...cell, color: '#00f5d4', fontWeight: 700 }}>{r.symbol}</td>
|
|
115
|
+
<td style={cell}>{r.mass}</td>
|
|
116
|
+
</tr>
|
|
117
|
+
))}
|
|
118
|
+
</tbody>
|
|
119
|
+
</table>
|
|
120
|
+
</div>
|
|
121
|
+
);
|
|
122
|
+
})()`,
|
|
123
|
+
},
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
// ── Error Boundary (class component) ─────────────────────────────────────────
|
|
127
|
+
|
|
128
|
+
interface ErrorBoundaryProps {
|
|
129
|
+
children: ReactNode;
|
|
130
|
+
onError: (msg: string) => void;
|
|
131
|
+
}
|
|
132
|
+
interface ErrorBoundaryState {
|
|
133
|
+
hasError: boolean;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
137
|
+
constructor(props: ErrorBoundaryProps) {
|
|
138
|
+
super(props);
|
|
139
|
+
this.state = { hasError: false };
|
|
140
|
+
}
|
|
141
|
+
static getDerivedStateFromError(): ErrorBoundaryState {
|
|
142
|
+
return { hasError: true };
|
|
143
|
+
}
|
|
144
|
+
componentDidCatch(error: Error) {
|
|
145
|
+
this.props.onError(error.message);
|
|
146
|
+
}
|
|
147
|
+
componentDidUpdate(prev: ErrorBoundaryProps) {
|
|
148
|
+
// Reset when children change (new render)
|
|
149
|
+
if (prev.children !== this.props.children && this.state.hasError) {
|
|
150
|
+
this.setState({ hasError: false });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
render() {
|
|
154
|
+
if (this.state.hasError) return null;
|
|
155
|
+
return this.props.children;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ── Code evaluator ────────────────────────────────────────────────────────────
|
|
160
|
+
|
|
161
|
+
// Babel standalone is loaded via <script> tag in the host page (see demo/index.html)
|
|
162
|
+
// for JSX transformation. We probe for it lazily so the library bundle stays clean.
|
|
163
|
+
interface BabelStandalone {
|
|
164
|
+
transform(code: string, opts: Record<string, unknown>): { code: string };
|
|
165
|
+
}
|
|
166
|
+
function getBabel(): BabelStandalone | null {
|
|
167
|
+
if (typeof window === 'undefined') return null;
|
|
168
|
+
return (window as unknown as { Babel?: BabelStandalone }).Babel ?? null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function evalCode(
|
|
172
|
+
code: string,
|
|
173
|
+
importsObj: Record<string, unknown>,
|
|
174
|
+
): { element: ReactNode | null; error: string | null; renderMs: number } {
|
|
175
|
+
const t0 = performance.now();
|
|
176
|
+
try {
|
|
177
|
+
const trimmed = code.trim();
|
|
178
|
+
|
|
179
|
+
// Always wrap user code in a function component so React hooks (useState,
|
|
180
|
+
// useEffect, ...) work — they require an active render context to dispatch.
|
|
181
|
+
// The user can write a single JSX expression OR a function body that returns JSX.
|
|
182
|
+
const looksLikeExpression = trimmed.startsWith('<') || trimmed.startsWith('(');
|
|
183
|
+
const componentBody = looksLikeExpression
|
|
184
|
+
? `return (${trimmed});`
|
|
185
|
+
: trimmed; // assume the user wrote `const x = ...; return <jsx>;`
|
|
186
|
+
|
|
187
|
+
let source = `
|
|
188
|
+
function PlaygroundRoot() {
|
|
189
|
+
${componentBody}
|
|
190
|
+
}
|
|
191
|
+
return PlaygroundRoot;
|
|
192
|
+
`;
|
|
193
|
+
|
|
194
|
+
const babel = getBabel();
|
|
195
|
+
if (babel) {
|
|
196
|
+
source = babel.transform(source, {
|
|
197
|
+
presets: ['react'],
|
|
198
|
+
filename: 'playground.jsx',
|
|
199
|
+
}).code ?? source;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
203
|
+
const fn = new Function(
|
|
204
|
+
'React',
|
|
205
|
+
'imports',
|
|
206
|
+
`
|
|
207
|
+
"use strict";
|
|
208
|
+
const { ${Object.keys(importsObj).join(', ')} } = imports;
|
|
209
|
+
${source}
|
|
210
|
+
`,
|
|
211
|
+
) as (r: typeof React, i: Record<string, unknown>) => React.ComponentType;
|
|
212
|
+
const Component = fn(React, importsObj);
|
|
213
|
+
const element = React.createElement(Component);
|
|
214
|
+
const renderMs = parseFloat((performance.now() - t0).toFixed(2));
|
|
215
|
+
return { element, error: null, renderMs };
|
|
216
|
+
} catch (err) {
|
|
217
|
+
const renderMs = parseFloat((performance.now() - t0).toFixed(2));
|
|
218
|
+
return { element: null, error: err instanceof Error ? err.message : String(err), renderMs };
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ── Line numbers overlay ──────────────────────────────────────────────────────
|
|
223
|
+
|
|
224
|
+
function LineNumbers({ code, lineHeight }: { code: string; lineHeight: number }) {
|
|
225
|
+
const lines = code.split('\n');
|
|
226
|
+
return (
|
|
227
|
+
<div
|
|
228
|
+
aria-hidden="true"
|
|
229
|
+
style={{
|
|
230
|
+
position: 'absolute',
|
|
231
|
+
top: 0,
|
|
232
|
+
left: 0,
|
|
233
|
+
width: '36px',
|
|
234
|
+
padding: '12px 0',
|
|
235
|
+
textAlign: 'right',
|
|
236
|
+
paddingRight: '8px',
|
|
237
|
+
userSelect: 'none',
|
|
238
|
+
pointerEvents: 'none',
|
|
239
|
+
fontSize: '12px',
|
|
240
|
+
lineHeight: `${lineHeight}px`,
|
|
241
|
+
color: '#555577',
|
|
242
|
+
fontFamily: '"Fira Code", "Cascadia Code", "Consolas", monospace',
|
|
243
|
+
boxSizing: 'border-box',
|
|
244
|
+
}}
|
|
245
|
+
>
|
|
246
|
+
{lines.map((_, i) => (
|
|
247
|
+
<div key={i}>{i + 1}</div>
|
|
248
|
+
))}
|
|
249
|
+
</div>
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ── Amplifier (amplitude-based suggestions) ───────────────────────────────────
|
|
254
|
+
|
|
255
|
+
const COMPONENT_CORPUS = [
|
|
256
|
+
'TkxButton', 'TkxInput', 'TkxCard', 'TkxModal', 'TkxTable',
|
|
257
|
+
'TkxSelect', 'TkxCheckbox', 'TkxRadio', 'TkxSwitch', 'TkxSlider',
|
|
258
|
+
'TkxBadge', 'TkxAlert', 'TkxToast', 'TkxTooltip', 'TkxPopover',
|
|
259
|
+
'TkxTabs', 'TkxAccordion', 'TkxDrawer', 'TkxDropdown', 'TkxMenu',
|
|
260
|
+
'TkxForm', 'TkxDataGrid', 'TkxChart', 'TkxSpinner', 'TkxSkeleton',
|
|
261
|
+
'TkxAvatar', 'TkxTag', 'TkxDivider', 'TkxProgress', 'TkxStatistic',
|
|
262
|
+
'TkxQuantumForm', 'TkxPlayground',
|
|
263
|
+
];
|
|
264
|
+
|
|
265
|
+
const amplifier = new AmplitudeAmplifier();
|
|
266
|
+
|
|
267
|
+
function extractLastToken(code: string): string {
|
|
268
|
+
const match = code.match(/([A-Z][a-zA-Z0-9]*)$/);
|
|
269
|
+
return match ? match[1] : '';
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function querySuggestions(query: string, topK = 4): { name: string; amplitude: number }[] {
|
|
273
|
+
if (!query || query.length < 2) return [];
|
|
274
|
+
const q = query.toLowerCase();
|
|
275
|
+
const results = amplifier.amplify(
|
|
276
|
+
COMPONENT_CORPUS,
|
|
277
|
+
(name) => {
|
|
278
|
+
const lower = name.toLowerCase();
|
|
279
|
+
if (lower.startsWith(q)) return 1.0;
|
|
280
|
+
if (lower.includes(q)) return 0.7;
|
|
281
|
+
// bigram overlap
|
|
282
|
+
let score = 0;
|
|
283
|
+
for (let i = 0; i < q.length - 1; i++) {
|
|
284
|
+
if (lower.includes(q.slice(i, i + 2))) score += 0.15;
|
|
285
|
+
}
|
|
286
|
+
return Math.min(score, 0.5);
|
|
287
|
+
},
|
|
288
|
+
0.1,
|
|
289
|
+
);
|
|
290
|
+
return results.slice(0, topK).map((r) => ({
|
|
291
|
+
name: COMPONENT_CORPUS[r.index] ?? '',
|
|
292
|
+
amplitude: parseFloat(r.combined.toFixed(3)),
|
|
293
|
+
})).filter((s) => s.name !== '');
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ── Main Playground Component ─────────────────────────────────────────────────
|
|
297
|
+
|
|
298
|
+
export function TkxPlayground({
|
|
299
|
+
defaultCode,
|
|
300
|
+
examples: userExamples,
|
|
301
|
+
height = 480,
|
|
302
|
+
imports = {},
|
|
303
|
+
}: TkxPlaygroundProps) {
|
|
304
|
+
const theme = useTheme();
|
|
305
|
+
const allExamples = [...BUILT_IN_EXAMPLES, ...(userExamples ?? [])];
|
|
306
|
+
const initialCode = defaultCode ?? BUILT_IN_EXAMPLES[0]!.code;
|
|
307
|
+
|
|
308
|
+
const [code, setCode] = useState(initialCode);
|
|
309
|
+
const [preview, setPreview] = useState<{ element: ReactNode | null; error: string | null; renderMs: number }>({
|
|
310
|
+
element: null,
|
|
311
|
+
error: null,
|
|
312
|
+
renderMs: 0,
|
|
313
|
+
});
|
|
314
|
+
const [boundaryError, setBoundaryError] = useState<string | null>(null);
|
|
315
|
+
const [suggestions, setSuggestions] = useState<{ name: string; amplitude: number }[]>([]);
|
|
316
|
+
const [copied, setCopied] = useState(false);
|
|
317
|
+
const [activeExample, setActiveExample] = useState<number>(0);
|
|
318
|
+
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
319
|
+
const LINE_HEIGHT = 18;
|
|
320
|
+
|
|
321
|
+
// Debounced evaluation
|
|
322
|
+
const evalTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
323
|
+
|
|
324
|
+
const runEval = useCallback(
|
|
325
|
+
(src: string) => {
|
|
326
|
+
setBoundaryError(null);
|
|
327
|
+
const result = evalCode(src, imports);
|
|
328
|
+
setPreview(result);
|
|
329
|
+
},
|
|
330
|
+
[imports],
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
useEffect(() => {
|
|
334
|
+
if (evalTimerRef.current) clearTimeout(evalTimerRef.current);
|
|
335
|
+
evalTimerRef.current = setTimeout(() => runEval(code), 300);
|
|
336
|
+
return () => {
|
|
337
|
+
if (evalTimerRef.current) clearTimeout(evalTimerRef.current);
|
|
338
|
+
};
|
|
339
|
+
}, [code, runEval]);
|
|
340
|
+
|
|
341
|
+
// Initial render
|
|
342
|
+
useEffect(() => {
|
|
343
|
+
runEval(initialCode);
|
|
344
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
345
|
+
}, []);
|
|
346
|
+
|
|
347
|
+
const handleCodeChange = useCallback(
|
|
348
|
+
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
349
|
+
const val = e.target.value;
|
|
350
|
+
setCode(val);
|
|
351
|
+
// Quantum suggestions
|
|
352
|
+
const token = extractLastToken(val);
|
|
353
|
+
if (token.length >= 2) {
|
|
354
|
+
setSuggestions(querySuggestions(token, 4));
|
|
355
|
+
} else {
|
|
356
|
+
setSuggestions([]);
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
[],
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
const handleExampleSelect = useCallback(
|
|
363
|
+
(idx: number) => {
|
|
364
|
+
const ex = allExamples[idx];
|
|
365
|
+
if (!ex) return;
|
|
366
|
+
setActiveExample(idx);
|
|
367
|
+
setCode(ex.code);
|
|
368
|
+
setSuggestions([]);
|
|
369
|
+
},
|
|
370
|
+
[allExamples],
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
const handleCopy = useCallback(async () => {
|
|
374
|
+
try {
|
|
375
|
+
await navigator.clipboard.writeText(code);
|
|
376
|
+
setCopied(true);
|
|
377
|
+
setTimeout(() => setCopied(false), 1500);
|
|
378
|
+
} catch {
|
|
379
|
+
/* ignore */
|
|
380
|
+
}
|
|
381
|
+
}, [code]);
|
|
382
|
+
|
|
383
|
+
const handleReset = useCallback(() => {
|
|
384
|
+
setCode(initialCode);
|
|
385
|
+
setActiveExample(0);
|
|
386
|
+
setSuggestions([]);
|
|
387
|
+
}, [initialCode]);
|
|
388
|
+
|
|
389
|
+
const applySuggestion = useCallback(
|
|
390
|
+
(name: string) => {
|
|
391
|
+
const token = extractLastToken(code);
|
|
392
|
+
if (token) {
|
|
393
|
+
setCode((prev) => prev.slice(0, prev.lastIndexOf(token)) + name);
|
|
394
|
+
}
|
|
395
|
+
setSuggestions([]);
|
|
396
|
+
},
|
|
397
|
+
[code],
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
const hasError = preview.error !== null || boundaryError !== null;
|
|
401
|
+
const errorMsg = preview.error ?? boundaryError ?? '';
|
|
402
|
+
|
|
403
|
+
const editorBg = theme.bg;
|
|
404
|
+
const previewBg = theme.surfaceAlt;
|
|
405
|
+
const borderColor = theme.border;
|
|
406
|
+
const statusBg = theme.surface;
|
|
407
|
+
|
|
408
|
+
const editorAreaStyle: React.CSSProperties = {
|
|
409
|
+
width: '50%',
|
|
410
|
+
display: 'flex',
|
|
411
|
+
flexDirection: 'column',
|
|
412
|
+
borderRight: `1px solid ${borderColor}`,
|
|
413
|
+
position: 'relative',
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const toolbarStyle: React.CSSProperties = {
|
|
417
|
+
display: 'flex',
|
|
418
|
+
alignItems: 'center',
|
|
419
|
+
gap: '6px',
|
|
420
|
+
padding: '6px 10px',
|
|
421
|
+
borderBottom: `1px solid ${borderColor}`,
|
|
422
|
+
backgroundColor: theme.surface,
|
|
423
|
+
flexWrap: 'wrap',
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const iconBtnStyle: React.CSSProperties = {
|
|
427
|
+
padding: '3px 10px',
|
|
428
|
+
borderRadius: '6px',
|
|
429
|
+
border: `1px solid ${borderColor}`,
|
|
430
|
+
backgroundColor: 'transparent',
|
|
431
|
+
color: theme.textMuted,
|
|
432
|
+
fontSize: '11px',
|
|
433
|
+
fontWeight: 600,
|
|
434
|
+
cursor: 'pointer',
|
|
435
|
+
fontFamily: 'inherit',
|
|
436
|
+
transition: 'color 0.15s, border-color 0.15s',
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
return (
|
|
440
|
+
<div
|
|
441
|
+
style={{
|
|
442
|
+
display: 'flex',
|
|
443
|
+
flexDirection: 'column',
|
|
444
|
+
border: `1px solid ${borderColor}`,
|
|
445
|
+
borderRadius: '12px',
|
|
446
|
+
overflow: 'hidden',
|
|
447
|
+
backgroundColor: editorBg,
|
|
448
|
+
fontFamily: 'inherit',
|
|
449
|
+
boxSizing: 'border-box',
|
|
450
|
+
position: 'relative',
|
|
451
|
+
}}
|
|
452
|
+
>
|
|
453
|
+
{/* ── Header bar ── */}
|
|
454
|
+
<div
|
|
455
|
+
style={{
|
|
456
|
+
display: 'flex',
|
|
457
|
+
alignItems: 'center',
|
|
458
|
+
justifyContent: 'space-between',
|
|
459
|
+
padding: '8px 14px',
|
|
460
|
+
borderBottom: `1px solid ${borderColor}`,
|
|
461
|
+
backgroundColor: theme.surface,
|
|
462
|
+
}}
|
|
463
|
+
>
|
|
464
|
+
<span style={{ fontSize: '12px', fontWeight: 700, color: theme.primary, letterSpacing: '0.05em' }}>
|
|
465
|
+
⚛ TkxPlayground
|
|
466
|
+
</span>
|
|
467
|
+
<span style={{ fontSize: '10px', color: theme.textMuted }}>Live JSX Preview</span>
|
|
468
|
+
</div>
|
|
469
|
+
|
|
470
|
+
{/* ── Example tabs ── */}
|
|
471
|
+
<div
|
|
472
|
+
style={{
|
|
473
|
+
display: 'flex',
|
|
474
|
+
gap: '4px',
|
|
475
|
+
padding: '6px 10px',
|
|
476
|
+
borderBottom: `1px solid ${borderColor}`,
|
|
477
|
+
backgroundColor: theme.surface,
|
|
478
|
+
overflowX: 'auto',
|
|
479
|
+
}}
|
|
480
|
+
>
|
|
481
|
+
{allExamples.map((ex, i) => (
|
|
482
|
+
<button
|
|
483
|
+
key={i}
|
|
484
|
+
onClick={() => handleExampleSelect(i)}
|
|
485
|
+
style={{
|
|
486
|
+
padding: '3px 10px',
|
|
487
|
+
borderRadius: '6px',
|
|
488
|
+
border: `1px solid ${i === activeExample ? theme.primary : borderColor}`,
|
|
489
|
+
backgroundColor: i === activeExample ? `${theme.primary}20` : 'transparent',
|
|
490
|
+
color: i === activeExample ? theme.primary : theme.textMuted,
|
|
491
|
+
fontSize: '11px',
|
|
492
|
+
fontWeight: i === activeExample ? 700 : 400,
|
|
493
|
+
cursor: 'pointer',
|
|
494
|
+
whiteSpace: 'nowrap',
|
|
495
|
+
fontFamily: 'inherit',
|
|
496
|
+
transition: 'all 0.15s',
|
|
497
|
+
}}
|
|
498
|
+
>
|
|
499
|
+
{ex.label}
|
|
500
|
+
</button>
|
|
501
|
+
))}
|
|
502
|
+
</div>
|
|
503
|
+
|
|
504
|
+
{/* ── Split pane ── */}
|
|
505
|
+
<div style={{ display: 'flex', height: `${height}px` }}>
|
|
506
|
+
{/* Left: Editor */}
|
|
507
|
+
<div style={editorAreaStyle}>
|
|
508
|
+
{/* Toolbar */}
|
|
509
|
+
<div style={toolbarStyle}>
|
|
510
|
+
<span style={{ fontSize: '11px', color: theme.textMuted, marginRight: 'auto' }}>editor.tsx</span>
|
|
511
|
+
<button onClick={handleCopy} style={iconBtnStyle} title="Copy code">
|
|
512
|
+
{copied ? '✓ Copied' : '⎘ Copy'}
|
|
513
|
+
</button>
|
|
514
|
+
<button onClick={handleReset} style={iconBtnStyle} title="Reset to default">
|
|
515
|
+
↺ Reset
|
|
516
|
+
</button>
|
|
517
|
+
</div>
|
|
518
|
+
|
|
519
|
+
{/* Code area */}
|
|
520
|
+
<div style={{ position: 'relative', flex: 1, overflow: 'hidden' }}>
|
|
521
|
+
<LineNumbers code={code} lineHeight={LINE_HEIGHT} />
|
|
522
|
+
<textarea
|
|
523
|
+
ref={textareaRef}
|
|
524
|
+
value={code}
|
|
525
|
+
onChange={handleCodeChange}
|
|
526
|
+
spellCheck={false}
|
|
527
|
+
autoCorrect="off"
|
|
528
|
+
autoCapitalize="off"
|
|
529
|
+
style={{
|
|
530
|
+
position: 'absolute',
|
|
531
|
+
top: 0,
|
|
532
|
+
left: 0,
|
|
533
|
+
width: '100%',
|
|
534
|
+
height: '100%',
|
|
535
|
+
padding: '12px 12px 12px 44px',
|
|
536
|
+
margin: 0,
|
|
537
|
+
border: 'none',
|
|
538
|
+
outline: 'none',
|
|
539
|
+
resize: 'none',
|
|
540
|
+
backgroundColor: editorBg,
|
|
541
|
+
color: theme.text,
|
|
542
|
+
fontSize: '12px',
|
|
543
|
+
lineHeight: `${LINE_HEIGHT}px`,
|
|
544
|
+
fontFamily: '"Fira Code", "Cascadia Code", "Consolas", "Courier New", monospace',
|
|
545
|
+
boxSizing: 'border-box',
|
|
546
|
+
overflowY: 'auto',
|
|
547
|
+
tabSize: 2,
|
|
548
|
+
}}
|
|
549
|
+
/>
|
|
550
|
+
</div>
|
|
551
|
+
|
|
552
|
+
{/* Quantum suggestions overlay */}
|
|
553
|
+
{suggestions.length > 0 && (
|
|
554
|
+
<div
|
|
555
|
+
style={{
|
|
556
|
+
position: 'absolute',
|
|
557
|
+
bottom: '40px',
|
|
558
|
+
left: '44px',
|
|
559
|
+
zIndex: 10,
|
|
560
|
+
backgroundColor: theme.surface,
|
|
561
|
+
border: `1px solid ${theme.primary}44`,
|
|
562
|
+
borderRadius: '8px',
|
|
563
|
+
overflow: 'hidden',
|
|
564
|
+
boxShadow: `0 4px 16px rgba(0,0,0,0.3)`,
|
|
565
|
+
minWidth: '180px',
|
|
566
|
+
}}
|
|
567
|
+
>
|
|
568
|
+
<div
|
|
569
|
+
style={{
|
|
570
|
+
padding: '4px 10px 4px',
|
|
571
|
+
fontSize: '10px',
|
|
572
|
+
color: theme.primary,
|
|
573
|
+
fontWeight: 700,
|
|
574
|
+
borderBottom: `1px solid ${borderColor}`,
|
|
575
|
+
letterSpacing: '0.05em',
|
|
576
|
+
}}
|
|
577
|
+
>
|
|
578
|
+
⚛ Quantum Suggestions
|
|
579
|
+
</div>
|
|
580
|
+
{suggestions.map((s) => (
|
|
581
|
+
<button
|
|
582
|
+
key={s.name}
|
|
583
|
+
onClick={() => applySuggestion(s.name)}
|
|
584
|
+
style={{
|
|
585
|
+
display: 'flex',
|
|
586
|
+
alignItems: 'center',
|
|
587
|
+
justifyContent: 'space-between',
|
|
588
|
+
width: '100%',
|
|
589
|
+
padding: '6px 10px',
|
|
590
|
+
border: 'none',
|
|
591
|
+
backgroundColor: 'transparent',
|
|
592
|
+
color: theme.text,
|
|
593
|
+
fontSize: '12px',
|
|
594
|
+
fontFamily: '"Fira Code", monospace',
|
|
595
|
+
cursor: 'pointer',
|
|
596
|
+
textAlign: 'left',
|
|
597
|
+
gap: '12px',
|
|
598
|
+
}}
|
|
599
|
+
>
|
|
600
|
+
<span>{s.name}</span>
|
|
601
|
+
<span
|
|
602
|
+
style={{
|
|
603
|
+
fontSize: '10px',
|
|
604
|
+
color: theme.textMuted,
|
|
605
|
+
flexShrink: 0,
|
|
606
|
+
}}
|
|
607
|
+
>
|
|
608
|
+
{Math.round(s.amplitude * 100)}%
|
|
609
|
+
</span>
|
|
610
|
+
</button>
|
|
611
|
+
))}
|
|
612
|
+
</div>
|
|
613
|
+
)}
|
|
614
|
+
</div>
|
|
615
|
+
|
|
616
|
+
{/* Right: Preview */}
|
|
617
|
+
<div
|
|
618
|
+
style={{
|
|
619
|
+
width: '50%',
|
|
620
|
+
display: 'flex',
|
|
621
|
+
flexDirection: 'column',
|
|
622
|
+
}}
|
|
623
|
+
>
|
|
624
|
+
<div
|
|
625
|
+
style={{
|
|
626
|
+
padding: '6px 10px',
|
|
627
|
+
borderBottom: `1px solid ${borderColor}`,
|
|
628
|
+
backgroundColor: theme.surface,
|
|
629
|
+
fontSize: '11px',
|
|
630
|
+
color: theme.textMuted,
|
|
631
|
+
fontWeight: 600,
|
|
632
|
+
}}
|
|
633
|
+
>
|
|
634
|
+
Preview
|
|
635
|
+
</div>
|
|
636
|
+
<div
|
|
637
|
+
style={{
|
|
638
|
+
flex: 1,
|
|
639
|
+
overflow: 'auto',
|
|
640
|
+
backgroundColor: previewBg,
|
|
641
|
+
position: 'relative',
|
|
642
|
+
}}
|
|
643
|
+
>
|
|
644
|
+
{hasError ? (
|
|
645
|
+
<div
|
|
646
|
+
style={{
|
|
647
|
+
padding: '16px',
|
|
648
|
+
color: theme.danger,
|
|
649
|
+
fontFamily: '"Fira Code", monospace',
|
|
650
|
+
fontSize: '12px',
|
|
651
|
+
lineHeight: 1.6,
|
|
652
|
+
whiteSpace: 'pre-wrap',
|
|
653
|
+
wordBreak: 'break-word',
|
|
654
|
+
}}
|
|
655
|
+
>
|
|
656
|
+
<div
|
|
657
|
+
style={{
|
|
658
|
+
marginBottom: '8px',
|
|
659
|
+
fontWeight: 700,
|
|
660
|
+
display: 'flex',
|
|
661
|
+
alignItems: 'center',
|
|
662
|
+
gap: '6px',
|
|
663
|
+
}}
|
|
664
|
+
>
|
|
665
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
|
|
666
|
+
<path d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
|
|
667
|
+
</svg>
|
|
668
|
+
Runtime Error
|
|
669
|
+
</div>
|
|
670
|
+
{errorMsg}
|
|
671
|
+
</div>
|
|
672
|
+
) : (
|
|
673
|
+
<ErrorBoundary onError={setBoundaryError}>
|
|
674
|
+
<div style={{ minHeight: '100%' }}>{preview.element}</div>
|
|
675
|
+
</ErrorBoundary>
|
|
676
|
+
)}
|
|
677
|
+
</div>
|
|
678
|
+
</div>
|
|
679
|
+
</div>
|
|
680
|
+
|
|
681
|
+
{/* ── Status bar ── */}
|
|
682
|
+
<div
|
|
683
|
+
style={{
|
|
684
|
+
display: 'flex',
|
|
685
|
+
alignItems: 'center',
|
|
686
|
+
gap: '16px',
|
|
687
|
+
padding: '4px 12px',
|
|
688
|
+
backgroundColor: statusBg,
|
|
689
|
+
borderTop: `1px solid ${borderColor}`,
|
|
690
|
+
fontSize: '11px',
|
|
691
|
+
color: theme.textMuted,
|
|
692
|
+
}}
|
|
693
|
+
>
|
|
694
|
+
{/* Error / OK indicator */}
|
|
695
|
+
<span
|
|
696
|
+
style={{
|
|
697
|
+
display: 'flex',
|
|
698
|
+
alignItems: 'center',
|
|
699
|
+
gap: '4px',
|
|
700
|
+
color: hasError ? theme.danger : theme.success,
|
|
701
|
+
fontWeight: 600,
|
|
702
|
+
}}
|
|
703
|
+
>
|
|
704
|
+
<span
|
|
705
|
+
style={{
|
|
706
|
+
width: '6px',
|
|
707
|
+
height: '6px',
|
|
708
|
+
borderRadius: '50%',
|
|
709
|
+
backgroundColor: hasError ? theme.danger : theme.success,
|
|
710
|
+
display: 'inline-block',
|
|
711
|
+
}}
|
|
712
|
+
/>
|
|
713
|
+
{hasError ? 'Error' : 'OK'}
|
|
714
|
+
</span>
|
|
715
|
+
|
|
716
|
+
{/* Render time */}
|
|
717
|
+
<span>⏱ {preview.renderMs} ms</span>
|
|
718
|
+
|
|
719
|
+
{/* Lines */}
|
|
720
|
+
<span>{code.split('\n').length} lines</span>
|
|
721
|
+
|
|
722
|
+
{/* Characters */}
|
|
723
|
+
<span>{code.length} chars</span>
|
|
724
|
+
|
|
725
|
+
{/* Quantum suggestions hint */}
|
|
726
|
+
{suggestions.length > 0 && (
|
|
727
|
+
<span style={{ marginLeft: 'auto', color: theme.primary, fontWeight: 600 }}>
|
|
728
|
+
⚛ {suggestions.length} suggestion{suggestions.length > 1 ? 's' : ''}
|
|
729
|
+
</span>
|
|
730
|
+
)}
|
|
731
|
+
</div>
|
|
732
|
+
</div>
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
TkxPlayground.displayName = 'TkxPlayground';
|