cursor-canvas-export 0.0.0-stage → 0.1.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.
@@ -0,0 +1,461 @@
1
+ import { useState, type CSSProperties, type ReactNode } from "react";
2
+ import { useHostTheme } from "./hooks";
3
+ import { fontStack, monoStack, toneColor, usageColorSequence } from "./tokens";
4
+ import { CanvasChevron, Card, CardBody, CardHeader, mergeStyle } from "./ui";
5
+
6
+ export function Swatch({ color, style }: { color: keyof typeof toneColor | string; style?: CSSProperties }) {
7
+ const theme = useHostTheme();
8
+ const fill = theme.category[color as keyof typeof theme.category] ?? theme.fill.primary;
9
+ return (
10
+ <span
11
+ style={mergeStyle(
12
+ { display: "inline-block", width: 24, height: 24, borderRadius: 4, background: fill },
13
+ style,
14
+ )}
15
+ />
16
+ );
17
+ }
18
+
19
+ export function UsageBar({
20
+ segments,
21
+ total,
22
+ topLeftLabel,
23
+ topRightLabel,
24
+ style,
25
+ }: {
26
+ segments: readonly { id: string; value: number; color?: string }[];
27
+ total: number;
28
+ topLeftLabel?: ReactNode;
29
+ topRightLabel?: ReactNode;
30
+ style?: CSSProperties;
31
+ }) {
32
+ const theme = useHostTheme();
33
+ const safeTotal = total > 0 ? total : 1;
34
+ const used = segments.reduce((sum, segment) => sum + (Number.isFinite(segment.value) ? Math.max(0, segment.value) : 0), 0);
35
+ return (
36
+ <div style={mergeStyle({ display: "flex", flexDirection: "column", gap: 6 }, style)}>
37
+ {topLeftLabel != null || topRightLabel != null ? (
38
+ <div style={{ display: "flex", justifyContent: "space-between", fontSize: 12, color: theme.text.secondary }}>
39
+ <span>{topLeftLabel}</span>
40
+ <span>{topRightLabel}</span>
41
+ </div>
42
+ ) : null}
43
+ <div style={{ display: "flex", height: 8, borderRadius: 99, overflow: "hidden", background: theme.fill.tertiary }}>
44
+ {segments.map((segment, index) => {
45
+ const key = segment.color ?? usageColorSequence[index % usageColorSequence.length];
46
+ const color = theme.category[key as keyof typeof theme.category] ?? toneColor.info;
47
+ const width = ((Number.isFinite(segment.value) ? Math.max(0, segment.value) : 0) / safeTotal) * 100;
48
+ return <span key={segment.id} style={{ width: `${width}%`, background: color }} />;
49
+ })}
50
+ <span style={{ width: `${Math.max(0, safeTotal - used) / safeTotal * 100}%` }} />
51
+ </div>
52
+ </div>
53
+ );
54
+ }
55
+
56
+ export function CollapsibleSection({
57
+ title,
58
+ leading,
59
+ count,
60
+ trailing,
61
+ children,
62
+ defaultOpen = false,
63
+ style,
64
+ }: {
65
+ title: string;
66
+ leading?: ReactNode;
67
+ count?: number;
68
+ trailing?: ReactNode;
69
+ children?: ReactNode;
70
+ defaultOpen?: boolean;
71
+ style?: CSSProperties;
72
+ }) {
73
+ const theme = useHostTheme();
74
+ const [open, setOpen] = useState(defaultOpen);
75
+ return (
76
+ <div style={style}>
77
+ <button
78
+ type="button"
79
+ onClick={() => setOpen((value) => !value)}
80
+ style={{
81
+ display: "flex",
82
+ alignItems: "center",
83
+ gap: 8,
84
+ width: "100%",
85
+ padding: "6px 0",
86
+ border: 0,
87
+ background: "transparent",
88
+ color: theme.text.primary,
89
+ fontFamily: fontStack,
90
+ fontSize: 14,
91
+ textAlign: "left",
92
+ cursor: "pointer",
93
+ }}
94
+ >
95
+ <CanvasChevron expanded={open} />
96
+ {leading}
97
+ <span style={{ fontWeight: 590 }}>{title}</span>
98
+ {count != null ? <span style={{ color: theme.text.tertiary }}>{count}</span> : null}
99
+ <span style={{ flex: 1 }} />
100
+ <span style={{ color: theme.text.tertiary }}>{trailing}</span>
101
+ </button>
102
+ {open ? <div style={{ paddingLeft: 20 }}>{children}</div> : null}
103
+ </div>
104
+ );
105
+ }
106
+
107
+ export function DiffStats({
108
+ additions = 0,
109
+ deletions = 0,
110
+ style,
111
+ }: {
112
+ additions?: number;
113
+ deletions?: number;
114
+ style?: CSSProperties;
115
+ }) {
116
+ if (additions === 0 && deletions === 0) return null;
117
+ return (
118
+ <span style={mergeStyle({ fontFamily: monoStack, fontSize: 12, fontVariantNumeric: "tabular-nums" }, style)}>
119
+ {additions > 0 ? <span style={{ color: toneColor.success }}>+{additions}</span> : null}
120
+ {additions > 0 && deletions > 0 ? " " : null}
121
+ {deletions > 0 ? <span style={{ color: toneColor.danger }}>-{deletions}</span> : null}
122
+ </span>
123
+ );
124
+ }
125
+
126
+ export function DiffView({
127
+ lines,
128
+ style,
129
+ }: {
130
+ lines: Array<{ type: "added" | "removed" | "unchanged"; content: string; lineNumber?: number }>;
131
+ path?: string;
132
+ language?: string;
133
+ style?: CSSProperties;
134
+ }) {
135
+ const theme = useHostTheme();
136
+ const background = {
137
+ added: theme.diff.insertedLine,
138
+ removed: theme.diff.removedLine,
139
+ unchanged: "transparent",
140
+ } as const;
141
+ return (
142
+ <div style={mergeStyle({ fontFamily: monoStack, fontSize: 12, lineHeight: "18px" }, style)}>
143
+ {lines.map((line, index) => (
144
+ <div key={index} style={{ display: "flex", gap: 8, background: background[line.type], padding: "0 8px" }}>
145
+ <span style={{ width: 32, textAlign: "right", color: theme.text.quaternary, flex: "none" }}>
146
+ {line.lineNumber ?? index + 1}
147
+ </span>
148
+ <span style={{ color: line.type === "added" ? toneColor.success : line.type === "removed" ? toneColor.danger : theme.text.primary, whiteSpace: "pre-wrap" }}>
149
+ {line.type === "added" ? "+" : line.type === "removed" ? "-" : " "}
150
+ {line.content}
151
+ </span>
152
+ </div>
153
+ ))}
154
+ </div>
155
+ );
156
+ }
157
+
158
+ const fieldStyle = (theme: ReturnType<typeof useHostTheme>): CSSProperties => ({
159
+ fontFamily: fontStack,
160
+ fontSize: 13,
161
+ color: theme.text.primary,
162
+ background: theme.fill.tertiary,
163
+ border: `1px solid ${theme.stroke.secondary}`,
164
+ borderRadius: 6,
165
+ padding: "4px 8px",
166
+ });
167
+
168
+ export function TextInput(props: {
169
+ value?: string;
170
+ onChange?: (value: string) => void;
171
+ placeholder?: string;
172
+ disabled?: boolean;
173
+ type?: string;
174
+ style?: CSSProperties;
175
+ }) {
176
+ const theme = useHostTheme();
177
+ return (
178
+ <input
179
+ value={props.value}
180
+ placeholder={props.placeholder}
181
+ disabled={props.disabled}
182
+ type={props.type ?? "text"}
183
+ onChange={(event) => props.onChange?.(event.target.value)}
184
+ style={mergeStyle({ ...fieldStyle(theme), height: 28 }, props.style)}
185
+ />
186
+ );
187
+ }
188
+
189
+ export function TextArea(props: {
190
+ value?: string;
191
+ onChange?: (value: string) => void;
192
+ placeholder?: string;
193
+ disabled?: boolean;
194
+ rows?: number;
195
+ style?: CSSProperties;
196
+ }) {
197
+ const theme = useHostTheme();
198
+ return (
199
+ <textarea
200
+ value={props.value}
201
+ placeholder={props.placeholder}
202
+ disabled={props.disabled}
203
+ rows={props.rows ?? 4}
204
+ onChange={(event) => props.onChange?.(event.target.value)}
205
+ style={mergeStyle(fieldStyle(theme), props.style)}
206
+ />
207
+ );
208
+ }
209
+
210
+ export function Select(props: {
211
+ value?: string;
212
+ onChange?: (value: string) => void;
213
+ options?: Array<{ value: string; label: string }>;
214
+ disabled?: boolean;
215
+ style?: CSSProperties;
216
+ }) {
217
+ const theme = useHostTheme();
218
+ return (
219
+ <select
220
+ value={props.value}
221
+ disabled={props.disabled}
222
+ onChange={(event) => props.onChange?.(event.target.value)}
223
+ style={mergeStyle({ ...fieldStyle(theme), height: 28 }, props.style)}
224
+ >
225
+ {props.options?.map((option) => (
226
+ <option key={option.value} value={option.value}>
227
+ {option.label}
228
+ </option>
229
+ ))}
230
+ </select>
231
+ );
232
+ }
233
+
234
+ export function Checkbox(props: {
235
+ checked?: boolean;
236
+ onChange?: (checked: boolean) => void;
237
+ label?: ReactNode;
238
+ disabled?: boolean;
239
+ style?: CSSProperties;
240
+ }) {
241
+ const theme = useHostTheme();
242
+ return (
243
+ <label style={mergeStyle({ display: "inline-flex", gap: 8, alignItems: "center", color: theme.text.primary, fontSize: 14 }, props.style)}>
244
+ <input
245
+ type="checkbox"
246
+ checked={props.checked}
247
+ disabled={props.disabled}
248
+ onChange={(event) => props.onChange?.(event.target.checked)}
249
+ />
250
+ {props.label}
251
+ </label>
252
+ );
253
+ }
254
+
255
+ export function Toggle(props: {
256
+ checked?: boolean;
257
+ onChange?: (checked: boolean) => void;
258
+ disabled?: boolean;
259
+ style?: CSSProperties;
260
+ }) {
261
+ return <Checkbox checked={props.checked} onChange={props.onChange} disabled={props.disabled} style={props.style} />;
262
+ }
263
+
264
+ export function IconButton(props: {
265
+ children?: ReactNode;
266
+ onClick?: () => void;
267
+ disabled?: boolean;
268
+ title?: string;
269
+ style?: CSSProperties;
270
+ }) {
271
+ const theme = useHostTheme();
272
+ return (
273
+ <button
274
+ type="button"
275
+ title={props.title}
276
+ disabled={props.disabled}
277
+ onClick={props.onClick}
278
+ style={mergeStyle(
279
+ {
280
+ width: 24,
281
+ height: 24,
282
+ display: "inline-flex",
283
+ alignItems: "center",
284
+ justifyContent: "center",
285
+ border: 0,
286
+ borderRadius: 6,
287
+ background: "transparent",
288
+ color: theme.text.primary,
289
+ },
290
+ props.style,
291
+ )}
292
+ >
293
+ {props.children}
294
+ </button>
295
+ );
296
+ }
297
+
298
+ type TodoItem = { id: string; content: string; status: "pending" | "in_progress" | "completed" | "cancelled" };
299
+
300
+ export function TodoList({
301
+ todos,
302
+ dimmedTodoIds,
303
+ style,
304
+ }: {
305
+ todos: readonly TodoItem[];
306
+ dimmedTodoIds?: ReadonlySet<string>;
307
+ onTodoClick?: (todo: TodoItem) => void;
308
+ style?: CSSProperties;
309
+ }) {
310
+ const theme = useHostTheme();
311
+ if (todos.length === 0) return null;
312
+ return (
313
+ <div style={mergeStyle({ display: "flex", flexDirection: "column", gap: 6 }, style)}>
314
+ {todos.map((todo) => (
315
+ <div
316
+ key={todo.id}
317
+ style={{
318
+ fontSize: 14,
319
+ color: dimmedTodoIds?.has(todo.id) ? theme.text.quaternary : theme.text.primary,
320
+ textDecoration: todo.status === "completed" || todo.status === "cancelled" ? "line-through" : "none",
321
+ }}
322
+ >
323
+ {todo.content}
324
+ </div>
325
+ ))}
326
+ </div>
327
+ );
328
+ }
329
+
330
+ export function TodoListCard(props: {
331
+ todos: readonly TodoItem[];
332
+ dimmedTodoIds?: ReadonlySet<string>;
333
+ defaultExpanded?: boolean;
334
+ onTodoClick?: (todo: TodoItem) => void;
335
+ style?: CSSProperties;
336
+ }) {
337
+ const done = props.todos.filter((todo) => todo.status === "completed").length;
338
+ return (
339
+ <Card collapsible defaultOpen={props.defaultExpanded ?? true} style={props.style}>
340
+ <CardHeader>
341
+ {done} of {props.todos.length} Done
342
+ </CardHeader>
343
+ <CardBody>
344
+ <TodoList todos={props.todos} dimmedTodoIds={props.dimmedTodoIds} onTodoClick={props.onTodoClick} />
345
+ </CardBody>
346
+ </Card>
347
+ );
348
+ }
349
+
350
+ export type DAGLayoutOptions = {
351
+ nodes: Array<{ id: string }>;
352
+ edges: Array<{ from: string; to: string }>;
353
+ direction?: "vertical" | "horizontal";
354
+ nodeWidth?: number;
355
+ nodeHeight?: number;
356
+ rankGap?: number;
357
+ nodeGap?: number;
358
+ padding?: number;
359
+ };
360
+
361
+ export function computeDAGLayout(options: DAGLayoutOptions) {
362
+ const direction = options.direction ?? "vertical";
363
+ const nodeWidth = options.nodeWidth ?? 160;
364
+ const nodeHeight = options.nodeHeight ?? 40;
365
+ const rankGap = options.rankGap ?? 64;
366
+ const nodeGap = options.nodeGap ?? 48;
367
+ const padding = options.padding ?? 24;
368
+ const ids = options.nodes.map((node) => node.id);
369
+ const known = new Set(ids);
370
+ const edges = options.edges.filter((edge) => known.has(edge.from) && known.has(edge.to));
371
+ const outgoing = new Map<string, string[]>(ids.map((id) => [id, []]));
372
+ for (const edge of edges) outgoing.get(edge.from)?.push(edge.to);
373
+
374
+ const color = new Map<string, 0 | 1 | 2>(ids.map((id) => [id, 0]));
375
+ const back = new Set<string>();
376
+ const edgeKey = (from: string, to: string) => `${from}\0${to}`;
377
+ const visit = (id: string) => {
378
+ color.set(id, 1);
379
+ for (const next of outgoing.get(id) ?? []) {
380
+ const state = color.get(next);
381
+ if (state === 1) back.add(edgeKey(id, next));
382
+ else if (state === 0) visit(next);
383
+ }
384
+ color.set(id, 2);
385
+ };
386
+ for (const id of ids) if (color.get(id) === 0) visit(id);
387
+
388
+ const incoming = new Map<string, string[]>(ids.map((id) => [id, []]));
389
+ for (const edge of edges) {
390
+ if (!back.has(edgeKey(edge.from, edge.to))) incoming.get(edge.to)?.push(edge.from);
391
+ }
392
+ const rank = new Map<string, number>();
393
+ const ranking = (id: string): number => {
394
+ const cached = rank.get(id);
395
+ if (cached != null) return cached;
396
+ const preds = incoming.get(id) ?? [];
397
+ const value = preds.length === 0 ? 0 : Math.max(...preds.map(ranking)) + 1;
398
+ rank.set(id, value);
399
+ return value;
400
+ };
401
+ for (const id of ids) ranking(id);
402
+
403
+ const byRank = new Map<number, string[]>();
404
+ for (const id of ids) {
405
+ const level = rank.get(id) ?? 0;
406
+ const list = byRank.get(level) ?? [];
407
+ list.push(id);
408
+ byRank.set(level, list);
409
+ }
410
+ const levels = [...byRank.keys()].sort((a, b) => a - b);
411
+ const order = new Map<string, number>();
412
+ for (const level of levels) byRank.get(level)?.forEach((id, index) => order.set(id, index));
413
+
414
+ const along = (level: number) => padding + level * ((direction === "vertical" ? nodeHeight : nodeWidth) + rankGap);
415
+ const across = (index: number) => padding + index * ((direction === "vertical" ? nodeWidth : nodeHeight) + nodeGap);
416
+
417
+ const nodes = ids.map((id) => {
418
+ const level = rank.get(id) ?? 0;
419
+ const index = order.get(id) ?? 0;
420
+ const x = direction === "vertical" ? across(index) : along(level);
421
+ const y = direction === "vertical" ? along(level) : across(index);
422
+ return { id, x, y, rank: level, order: index };
423
+ });
424
+ const nodeById = new Map(nodes.map((node) => [node.id, node]));
425
+ const laidEdges = edges.map((edge) => {
426
+ const source = nodeById.get(edge.from)!;
427
+ const target = nodeById.get(edge.to)!;
428
+ const vertical = direction === "vertical";
429
+ return {
430
+ from: edge.from,
431
+ to: edge.to,
432
+ sourceX: source.x + (vertical ? nodeWidth / 2 : nodeWidth),
433
+ sourceY: source.y + (vertical ? nodeHeight : nodeHeight / 2),
434
+ targetX: target.x + (vertical ? nodeWidth / 2 : 0),
435
+ targetY: target.y + (vertical ? 0 : nodeHeight / 2),
436
+ isBackEdge: back.has(edgeKey(edge.from, edge.to)),
437
+ };
438
+ });
439
+ const ranks = levels.map((level) => {
440
+ const nodeIds = byRank.get(level) ?? [];
441
+ const first = nodeById.get(nodeIds[0])!;
442
+ const span = Math.max(0, nodeIds.length - 1);
443
+ const width = direction === "vertical" ? nodeWidth + span * (nodeWidth + nodeGap) : nodeWidth;
444
+ const height = direction === "vertical" ? nodeHeight : nodeHeight + span * (nodeHeight + nodeGap);
445
+ return { rank: level, x: first.x, y: first.y, width, height, nodeIds };
446
+ });
447
+ const maxAcross = Math.max(1, ...levels.map((level) => byRank.get(level)?.length ?? 1));
448
+ const maxRank = levels.length === 0 ? 0 : Math.max(...levels);
449
+ const width =
450
+ padding * 2 +
451
+ (direction === "vertical"
452
+ ? maxAcross * nodeWidth + Math.max(0, maxAcross - 1) * nodeGap
453
+ : (maxRank + 1) * nodeWidth + maxRank * rankGap);
454
+ const height =
455
+ padding * 2 +
456
+ (direction === "vertical"
457
+ ? (maxRank + 1) * nodeHeight + maxRank * rankGap
458
+ : maxAcross * nodeHeight + Math.max(0, maxAcross - 1) * nodeGap);
459
+
460
+ return { nodes, edges: laidEdges, ranks, direction, width, height };
461
+ }
@@ -0,0 +1,28 @@
1
+ import { useState } from "react";
2
+ import { buildTheme, type ThemeKind } from "./tokens";
3
+
4
+ let themeKind: ThemeKind = "dark";
5
+ let stateSeed: Record<string, unknown> = {};
6
+
7
+ export function setExportTheme(kind: ThemeKind): void {
8
+ themeKind = kind;
9
+ }
10
+
11
+ export function setCanvasStateSeed(seed: Record<string, unknown>): void {
12
+ stateSeed = seed ?? {};
13
+ }
14
+
15
+ export function useHostTheme() {
16
+ return buildTheme(themeKind);
17
+ }
18
+
19
+ export type SetCanvasState<T> = (action: T | ((prev: T) => T)) => void;
20
+
21
+ export function useCanvasState<T>(key: string, defaultValue: T): [T, SetCanvasState<T>] {
22
+ const initial = (key in stateSeed ? stateSeed[key] : defaultValue) as T;
23
+ return useState(initial);
24
+ }
25
+
26
+ export function useCanvasAction(): (action: unknown) => void {
27
+ return () => {};
28
+ }
@@ -0,0 +1,55 @@
1
+ export type { CSSProperties, RefObject } from "react";
2
+ export { useEffect, useMemo, useRef, useState } from "react";
3
+
4
+ export {
5
+ canvasPaletteDark,
6
+ canvasPaletteLight,
7
+ canvasTokens,
8
+ canvasTokensLight,
9
+ categoryPaletteDark,
10
+ categoryPaletteLight,
11
+ colorPalette,
12
+ usageColorSequence,
13
+ } from "./tokens";
14
+
15
+ export { BarChart, LineChart, PieChart } from "./charts";
16
+ export { setCanvasStateSeed, setExportTheme, useCanvasAction, useCanvasState, useHostTheme } from "./hooks";
17
+ export {
18
+ Checkbox,
19
+ CollapsibleSection,
20
+ DiffStats,
21
+ DiffView,
22
+ IconButton,
23
+ Select,
24
+ Swatch,
25
+ TextArea,
26
+ TextInput,
27
+ TodoList,
28
+ TodoListCard,
29
+ Toggle,
30
+ UsageBar,
31
+ computeDAGLayout,
32
+ } from "./extras";
33
+ export {
34
+ Button,
35
+ Callout,
36
+ CanvasChevron,
37
+ Card,
38
+ CardBody,
39
+ CardHeader,
40
+ Code,
41
+ Divider,
42
+ Grid,
43
+ H1,
44
+ H2,
45
+ H3,
46
+ Link,
47
+ Pill,
48
+ Row,
49
+ Spacer,
50
+ Stack,
51
+ Stat,
52
+ Table,
53
+ Text,
54
+ mergeStyle,
55
+ } from "./ui";