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.
- package/LICENSE +21 -0
- package/README.md +97 -2
- package/bin/canvas-to-html.js +4 -0
- package/bin/canvas-to-pdf.js +4 -0
- package/dist/cli.js +903 -0
- package/package.json +52 -4
- package/src/sdk/charts.tsx +411 -0
- package/src/sdk/extras.tsx +461 -0
- package/src/sdk/hooks.tsx +28 -0
- package/src/sdk/index.tsx +55 -0
- package/src/sdk/tokens.ts +261 -0
- package/src/sdk/ui.tsx +779 -0
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursor-canvas-export",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Export a Cursor canvas to HTML and a one-page PDF. Headless, for agents and the command line.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20"
|
|
9
|
+
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"cursor-canvas-export": "./dist/cli.js",
|
|
12
|
+
"canvas-to-html": "./bin/canvas-to-html.js",
|
|
13
|
+
"canvas-to-pdf": "./bin/canvas-to-pdf.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"bin",
|
|
18
|
+
"src/sdk",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/flockentanz/cursor-canvas-export.git"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"cursor",
|
|
28
|
+
"canvas",
|
|
29
|
+
"mcp",
|
|
30
|
+
"pdf",
|
|
31
|
+
"html"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "node scripts/build.mjs",
|
|
35
|
+
"prepublishOnly": "npm run build",
|
|
36
|
+
"canvas": "tsx src/cli.ts",
|
|
37
|
+
"test": "tsx --test --test-concurrency=1 test/**/*.test.ts"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
41
|
+
"esbuild": "^0.25.10",
|
|
42
|
+
"playwright": "^1.56.1",
|
|
43
|
+
"react": "^19.2.0",
|
|
44
|
+
"react-dom": "^19.2.0",
|
|
45
|
+
"zod": "^4.6.7"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^24.9.1",
|
|
49
|
+
"@types/react": "^19.2.2",
|
|
50
|
+
"@types/react-dom": "^19.2.2",
|
|
51
|
+
"tsx": "^4.20.6",
|
|
52
|
+
"typescript": "^5.9.3"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
import { useHostTheme } from "./hooks";
|
|
3
|
+
import { chartColorSequence, fontStack, toneColor, type ChartTone } from "./tokens";
|
|
4
|
+
|
|
5
|
+
type Series = { name: string; data: number[]; tone?: ChartTone };
|
|
6
|
+
type ReferenceLine = { value: number; label?: string; tone?: ChartTone };
|
|
7
|
+
|
|
8
|
+
function formatValue(value: number, prefix = "", suffix = ""): string {
|
|
9
|
+
const rounded = Number.isInteger(value) ? String(value) : String(Math.round(value * 100) / 100);
|
|
10
|
+
return `${prefix}${rounded}${suffix}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function seriesColor(series: Series[], seriesIndex: number, categoryIndex: number): string {
|
|
14
|
+
const entry = series[seriesIndex];
|
|
15
|
+
if (entry?.tone) return toneColor[entry.tone];
|
|
16
|
+
if (series.length === 1) return chartColorSequence[categoryIndex % chartColorSequence.length];
|
|
17
|
+
return chartColorSequence[seriesIndex % chartColorSequence.length];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function domain(values: number[], beginAtZero: boolean, yMin?: number, yMax?: number): [number, number] {
|
|
21
|
+
const dataMin = Math.min(...values, 0);
|
|
22
|
+
const dataMax = Math.max(...values, 0);
|
|
23
|
+
const min = yMin ?? (beginAtZero ? Math.min(0, dataMin) : dataMin);
|
|
24
|
+
const max = yMax ?? dataMax;
|
|
25
|
+
if (min === max) return [min, min + 1];
|
|
26
|
+
return [min, max];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function BarChart({
|
|
30
|
+
categories,
|
|
31
|
+
series,
|
|
32
|
+
height = 240,
|
|
33
|
+
stacked = false,
|
|
34
|
+
horizontal = false,
|
|
35
|
+
normalized = false,
|
|
36
|
+
valueSuffix = "",
|
|
37
|
+
valuePrefix = "",
|
|
38
|
+
showValues,
|
|
39
|
+
beginAtZero = true,
|
|
40
|
+
yMin,
|
|
41
|
+
yMax,
|
|
42
|
+
referenceLines,
|
|
43
|
+
style,
|
|
44
|
+
}: {
|
|
45
|
+
categories: string[];
|
|
46
|
+
series: Series[];
|
|
47
|
+
height?: number;
|
|
48
|
+
stacked?: boolean;
|
|
49
|
+
horizontal?: boolean;
|
|
50
|
+
normalized?: boolean;
|
|
51
|
+
valueSuffix?: string;
|
|
52
|
+
valuePrefix?: string;
|
|
53
|
+
showValues?: boolean;
|
|
54
|
+
beginAtZero?: boolean;
|
|
55
|
+
yMin?: number;
|
|
56
|
+
yMax?: number;
|
|
57
|
+
referenceLines?: ReferenceLine[];
|
|
58
|
+
style?: CSSProperties;
|
|
59
|
+
}) {
|
|
60
|
+
const theme = useHostTheme();
|
|
61
|
+
const isStacked = stacked || normalized;
|
|
62
|
+
const labelsOn =
|
|
63
|
+
showValues ??
|
|
64
|
+
(!isStacked && series.length === 1 && categories.length <= 8);
|
|
65
|
+
const flat = categories.flatMap((_, index) => series.map((item) => item.data[index] ?? 0));
|
|
66
|
+
const [min, max] = domain(
|
|
67
|
+
[...flat, ...(referenceLines?.map((line) => line.value) ?? [])],
|
|
68
|
+
beginAtZero || isStacked,
|
|
69
|
+
isStacked ? 0 : yMin,
|
|
70
|
+
yMax,
|
|
71
|
+
);
|
|
72
|
+
const span = max - min;
|
|
73
|
+
|
|
74
|
+
const percent = (value: number) => `${Math.max(0, Math.min(100, ((value - min) / span) * 100))}%`;
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<div data-chart="bar" style={{ display: "flex", flexDirection: "column", gap: 8, ...style }}>
|
|
78
|
+
<div
|
|
79
|
+
style={{
|
|
80
|
+
minHeight: height,
|
|
81
|
+
display: "flex",
|
|
82
|
+
flexDirection: horizontal ? "column" : "row",
|
|
83
|
+
alignItems: horizontal ? "stretch" : "flex-end",
|
|
84
|
+
gap: horizontal ? 10 : 8,
|
|
85
|
+
justifyContent: horizontal ? "space-evenly" : "flex-start",
|
|
86
|
+
}}
|
|
87
|
+
>
|
|
88
|
+
{categories.map((category, categoryIndex) => {
|
|
89
|
+
const values = series.map((item) => item.data[categoryIndex] ?? 0);
|
|
90
|
+
const total = values.reduce((sum, value) => sum + value, 0) || 1;
|
|
91
|
+
return (
|
|
92
|
+
<div
|
|
93
|
+
key={category}
|
|
94
|
+
style={
|
|
95
|
+
horizontal
|
|
96
|
+
? {
|
|
97
|
+
display: "grid",
|
|
98
|
+
gridTemplateColumns: "minmax(160px, 1.15fr) minmax(0, 1.7fr) auto",
|
|
99
|
+
columnGap: 10,
|
|
100
|
+
alignItems: "center",
|
|
101
|
+
}
|
|
102
|
+
: { flex: "1 1 0", minWidth: 0, display: "flex", flexDirection: "column", gap: 6, height: "100%" }
|
|
103
|
+
}
|
|
104
|
+
>
|
|
105
|
+
{horizontal ? (
|
|
106
|
+
<div
|
|
107
|
+
style={{
|
|
108
|
+
fontFamily: fontStack,
|
|
109
|
+
fontSize: 12,
|
|
110
|
+
lineHeight: "16px",
|
|
111
|
+
color: theme.text.secondary,
|
|
112
|
+
textAlign: "right",
|
|
113
|
+
}}
|
|
114
|
+
>
|
|
115
|
+
{category}
|
|
116
|
+
</div>
|
|
117
|
+
) : null}
|
|
118
|
+
<div
|
|
119
|
+
style={
|
|
120
|
+
horizontal
|
|
121
|
+
? {
|
|
122
|
+
height: 16,
|
|
123
|
+
borderRadius: 3,
|
|
124
|
+
background: theme.fill.tertiary,
|
|
125
|
+
display: "flex",
|
|
126
|
+
overflow: "hidden",
|
|
127
|
+
}
|
|
128
|
+
: {
|
|
129
|
+
flex: 1,
|
|
130
|
+
display: "flex",
|
|
131
|
+
alignItems: "flex-end",
|
|
132
|
+
gap: isStacked ? 0 : 3,
|
|
133
|
+
height: height - 28,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
>
|
|
137
|
+
{series.map((item, seriesIndex) => {
|
|
138
|
+
const value = values[seriesIndex] ?? 0;
|
|
139
|
+
const portion = normalized ? (value / total) * 100 : undefined;
|
|
140
|
+
const color = seriesColor(series, seriesIndex, categoryIndex);
|
|
141
|
+
return (
|
|
142
|
+
<div
|
|
143
|
+
key={item.name}
|
|
144
|
+
data-bar=""
|
|
145
|
+
title={`${item.name}: ${formatValue(value, valuePrefix, valueSuffix)}`}
|
|
146
|
+
style={{
|
|
147
|
+
background: color,
|
|
148
|
+
height: horizontal ? "100%" : percent(value),
|
|
149
|
+
width: horizontal
|
|
150
|
+
? normalized
|
|
151
|
+
? `${portion}%`
|
|
152
|
+
: isStacked
|
|
153
|
+
? percent(value)
|
|
154
|
+
: series.length === 1
|
|
155
|
+
? percent(value)
|
|
156
|
+
: percent(value)
|
|
157
|
+
: isStacked
|
|
158
|
+
? "100%"
|
|
159
|
+
: `${100 / series.length}%`,
|
|
160
|
+
minWidth: horizontal && value > 0 ? 2 : 0,
|
|
161
|
+
borderRadius: horizontal && !isStacked ? 3 : 3,
|
|
162
|
+
}}
|
|
163
|
+
/>
|
|
164
|
+
);
|
|
165
|
+
})}
|
|
166
|
+
</div>
|
|
167
|
+
{horizontal && labelsOn ? (
|
|
168
|
+
<div
|
|
169
|
+
data-bar-value=""
|
|
170
|
+
style={{
|
|
171
|
+
fontFamily: fontStack,
|
|
172
|
+
fontSize: 12,
|
|
173
|
+
lineHeight: "16px",
|
|
174
|
+
color: theme.text.primary,
|
|
175
|
+
fontVariantNumeric: "tabular-nums",
|
|
176
|
+
whiteSpace: "nowrap",
|
|
177
|
+
}}
|
|
178
|
+
>
|
|
179
|
+
{formatValue(values[0] ?? 0, valuePrefix, valueSuffix)}
|
|
180
|
+
</div>
|
|
181
|
+
) : null}
|
|
182
|
+
{horizontal ? null : (
|
|
183
|
+
<div
|
|
184
|
+
style={{
|
|
185
|
+
fontFamily: fontStack,
|
|
186
|
+
fontSize: 12,
|
|
187
|
+
lineHeight: "16px",
|
|
188
|
+
color: theme.text.secondary,
|
|
189
|
+
textAlign: "center",
|
|
190
|
+
overflowWrap: "anywhere",
|
|
191
|
+
}}
|
|
192
|
+
>
|
|
193
|
+
{category}
|
|
194
|
+
{labelsOn ? (
|
|
195
|
+
<div data-bar-value="" style={{ color: theme.text.primary }}>
|
|
196
|
+
{formatValue(values[0] ?? 0, valuePrefix, valueSuffix)}
|
|
197
|
+
</div>
|
|
198
|
+
) : null}
|
|
199
|
+
</div>
|
|
200
|
+
)}
|
|
201
|
+
</div>
|
|
202
|
+
);
|
|
203
|
+
})}
|
|
204
|
+
</div>
|
|
205
|
+
{series.length > 1 ? <Legend series={series} /> : null}
|
|
206
|
+
</div>
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function Legend({ series }: { series: Series[] }) {
|
|
211
|
+
const theme = useHostTheme();
|
|
212
|
+
return (
|
|
213
|
+
<div style={{ display: "flex", flexWrap: "wrap", gap: 12 }}>
|
|
214
|
+
{series.map((item, index) => (
|
|
215
|
+
<span
|
|
216
|
+
key={item.name}
|
|
217
|
+
style={{
|
|
218
|
+
display: "inline-flex",
|
|
219
|
+
alignItems: "center",
|
|
220
|
+
gap: 6,
|
|
221
|
+
fontFamily: fontStack,
|
|
222
|
+
fontSize: 12,
|
|
223
|
+
color: theme.text.secondary,
|
|
224
|
+
}}
|
|
225
|
+
>
|
|
226
|
+
<span
|
|
227
|
+
style={{
|
|
228
|
+
width: 8,
|
|
229
|
+
height: 8,
|
|
230
|
+
borderRadius: 2,
|
|
231
|
+
background: seriesColor(series, index, 0),
|
|
232
|
+
}}
|
|
233
|
+
/>
|
|
234
|
+
{item.name}
|
|
235
|
+
</span>
|
|
236
|
+
))}
|
|
237
|
+
</div>
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function LineChart({
|
|
242
|
+
categories,
|
|
243
|
+
series,
|
|
244
|
+
height = 240,
|
|
245
|
+
fill = false,
|
|
246
|
+
valueSuffix = "",
|
|
247
|
+
valuePrefix = "",
|
|
248
|
+
showValues = false,
|
|
249
|
+
beginAtZero = true,
|
|
250
|
+
yMin,
|
|
251
|
+
yMax,
|
|
252
|
+
referenceLines,
|
|
253
|
+
style,
|
|
254
|
+
}: {
|
|
255
|
+
categories: string[];
|
|
256
|
+
series: Series[];
|
|
257
|
+
height?: number;
|
|
258
|
+
fill?: boolean;
|
|
259
|
+
valueSuffix?: string;
|
|
260
|
+
valuePrefix?: string;
|
|
261
|
+
showValues?: boolean;
|
|
262
|
+
showHoverGuide?: boolean;
|
|
263
|
+
beginAtZero?: boolean;
|
|
264
|
+
yMin?: number;
|
|
265
|
+
yMax?: number;
|
|
266
|
+
referenceLines?: ReferenceLine[];
|
|
267
|
+
style?: CSSProperties;
|
|
268
|
+
}) {
|
|
269
|
+
const theme = useHostTheme();
|
|
270
|
+
const width = 640;
|
|
271
|
+
const pad = { top: 16, right: 16, bottom: 28, left: 36 };
|
|
272
|
+
const innerW = width - pad.left - pad.right;
|
|
273
|
+
const innerH = height - pad.top - pad.bottom;
|
|
274
|
+
const values = series.flatMap((item) => item.data);
|
|
275
|
+
const [min, max] = domain(
|
|
276
|
+
[...values, ...(referenceLines?.map((line) => line.value) ?? [])],
|
|
277
|
+
beginAtZero,
|
|
278
|
+
yMin,
|
|
279
|
+
yMax,
|
|
280
|
+
);
|
|
281
|
+
const xAt = (index: number) =>
|
|
282
|
+
pad.left + (categories.length <= 1 ? innerW / 2 : (index / (categories.length - 1)) * innerW);
|
|
283
|
+
const yAt = (value: number) => pad.top + ((max - value) / (max - min)) * innerH;
|
|
284
|
+
|
|
285
|
+
return (
|
|
286
|
+
<div data-chart="line" style={style}>
|
|
287
|
+
<svg viewBox={`0 0 ${width} ${height}`} width="100%" height={height} role="img">
|
|
288
|
+
{series.map((item, seriesIndex) => {
|
|
289
|
+
const color = seriesColor(series, seriesIndex, 0);
|
|
290
|
+
const points = item.data.map((value, index) => `${xAt(index)},${yAt(value)}`).join(" ");
|
|
291
|
+
const area = `${pad.left},${pad.top + innerH} ${points} ${xAt(item.data.length - 1)},${pad.top + innerH}`;
|
|
292
|
+
return (
|
|
293
|
+
<g key={item.name}>
|
|
294
|
+
{fill ? <polygon points={area} fill={color} opacity={0.18} /> : null}
|
|
295
|
+
<polyline points={points} fill="none" stroke={color} strokeWidth="2" />
|
|
296
|
+
{item.data.map((value, index) => (
|
|
297
|
+
<g key={index}>
|
|
298
|
+
<circle cx={xAt(index)} cy={yAt(value)} r="3" fill={color} />
|
|
299
|
+
{showValues ? (
|
|
300
|
+
<text
|
|
301
|
+
x={xAt(index)}
|
|
302
|
+
y={yAt(value) - 8}
|
|
303
|
+
textAnchor="middle"
|
|
304
|
+
fill={theme.text.secondary}
|
|
305
|
+
fontSize="11"
|
|
306
|
+
fontFamily={fontStack}
|
|
307
|
+
>
|
|
308
|
+
{formatValue(value, valuePrefix, valueSuffix)}
|
|
309
|
+
</text>
|
|
310
|
+
) : null}
|
|
311
|
+
</g>
|
|
312
|
+
))}
|
|
313
|
+
</g>
|
|
314
|
+
);
|
|
315
|
+
})}
|
|
316
|
+
{categories.map((category, index) => (
|
|
317
|
+
<text
|
|
318
|
+
key={category}
|
|
319
|
+
x={xAt(index)}
|
|
320
|
+
y={height - 8}
|
|
321
|
+
textAnchor="middle"
|
|
322
|
+
fill={theme.text.tertiary}
|
|
323
|
+
fontSize="11"
|
|
324
|
+
fontFamily={fontStack}
|
|
325
|
+
>
|
|
326
|
+
{category}
|
|
327
|
+
</text>
|
|
328
|
+
))}
|
|
329
|
+
</svg>
|
|
330
|
+
{series.length > 1 ? <Legend series={series} /> : null}
|
|
331
|
+
</div>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function PieChart({
|
|
336
|
+
data,
|
|
337
|
+
size = 200,
|
|
338
|
+
donut = false,
|
|
339
|
+
style,
|
|
340
|
+
}: {
|
|
341
|
+
data: Array<{ label: string; value: number; tone?: ChartTone }>;
|
|
342
|
+
size?: number;
|
|
343
|
+
donut?: boolean;
|
|
344
|
+
style?: CSSProperties;
|
|
345
|
+
}) {
|
|
346
|
+
const theme = useHostTheme();
|
|
347
|
+
const total = data.reduce((sum, item) => sum + Math.max(0, item.value), 0) || 1;
|
|
348
|
+
const radius = size / 2 - 4;
|
|
349
|
+
const center = size / 2;
|
|
350
|
+
let cursor = -Math.PI / 2;
|
|
351
|
+
const slices = data.map((item, index) => {
|
|
352
|
+
const angle = (Math.max(0, item.value) / total) * Math.PI * 2;
|
|
353
|
+
const start = cursor;
|
|
354
|
+
cursor += angle;
|
|
355
|
+
const large = angle > Math.PI ? 1 : 0;
|
|
356
|
+
const x1 = center + radius * Math.cos(start);
|
|
357
|
+
const y1 = center + radius * Math.sin(start);
|
|
358
|
+
const x2 = center + radius * Math.cos(start + angle);
|
|
359
|
+
const y2 = center + radius * Math.sin(start + angle);
|
|
360
|
+
const path = `M ${center} ${center} L ${x1} ${y1} A ${radius} ${radius} 0 ${large} 1 ${x2} ${y2} Z`;
|
|
361
|
+
const color = item.tone ? toneColor[item.tone] : chartColorSequence[index % chartColorSequence.length];
|
|
362
|
+
return { ...item, path, color };
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
return (
|
|
366
|
+
<div data-chart="pie" style={{ display: "flex", gap: 16, alignItems: "center", ...style }}>
|
|
367
|
+
<svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} role="img">
|
|
368
|
+
{slices.map((slice) => (
|
|
369
|
+
<path key={slice.label} d={slice.path} fill={slice.color} />
|
|
370
|
+
))}
|
|
371
|
+
{donut ? (
|
|
372
|
+
<>
|
|
373
|
+
<circle cx={center} cy={center} r={radius * 0.58} fill={theme.bg.editor} />
|
|
374
|
+
<text
|
|
375
|
+
x={center}
|
|
376
|
+
y={center}
|
|
377
|
+
textAnchor="middle"
|
|
378
|
+
dominantBaseline="middle"
|
|
379
|
+
fill={theme.text.primary}
|
|
380
|
+
fontSize="16"
|
|
381
|
+
fontFamily={fontStack}
|
|
382
|
+
fontWeight={590}
|
|
383
|
+
>
|
|
384
|
+
{total}
|
|
385
|
+
</text>
|
|
386
|
+
</>
|
|
387
|
+
) : null}
|
|
388
|
+
</svg>
|
|
389
|
+
<div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
|
|
390
|
+
{slices.map((slice) => (
|
|
391
|
+
<span
|
|
392
|
+
key={slice.label}
|
|
393
|
+
style={{
|
|
394
|
+
display: "inline-flex",
|
|
395
|
+
gap: 6,
|
|
396
|
+
alignItems: "center",
|
|
397
|
+
fontFamily: fontStack,
|
|
398
|
+
fontSize: 12,
|
|
399
|
+
color: theme.text.secondary,
|
|
400
|
+
}}
|
|
401
|
+
>
|
|
402
|
+
<span style={{ width: 8, height: 8, borderRadius: 2, background: slice.color }} />
|
|
403
|
+
{slice.label}
|
|
404
|
+
</span>
|
|
405
|
+
))}
|
|
406
|
+
</div>
|
|
407
|
+
</div>
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export type ChartLabel = ReactNode;
|