tempo-sdk 0.0.15 → 0.0.18

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.
Files changed (50) hide show
  1. package/dist/canvas/editor.d.ts +164 -0
  2. package/dist/canvas/editor.js +898 -0
  3. package/dist/canvas/editor.js.map +1 -0
  4. package/dist/canvas/index.d.ts +174 -0
  5. package/dist/canvas/index.js +2301 -0
  6. package/dist/canvas/index.js.map +1 -0
  7. package/dist/canvas/parser.d.ts +108 -0
  8. package/dist/canvas/parser.js +11 -0
  9. package/dist/canvas/parser.js.map +1 -0
  10. package/dist/chunk-6Q4I3RFN.js +643 -0
  11. package/dist/chunk-6Q4I3RFN.js.map +1 -0
  12. package/dist/{chunk-77BNMPBS.js → chunk-CB5NJJJT.js} +31 -5
  13. package/dist/chunk-CB5NJJJT.js.map +1 -0
  14. package/dist/expo/plugin.cjs +30 -4
  15. package/dist/expo/plugin.cjs.map +1 -1
  16. package/dist/expo/plugin.js +1 -1
  17. package/dist/expo-babel-plugin.cjs +30 -4
  18. package/dist/expo-babel-plugin.cjs.map +1 -1
  19. package/dist/expo-babel-plugin.d.cts +1 -1
  20. package/dist/expo-babel-plugin.d.ts +1 -1
  21. package/dist/expo-babel-plugin.js +1 -1
  22. package/dist/index.d.ts +3 -34
  23. package/dist/index.js +26 -6
  24. package/dist/index.js.map +1 -1
  25. package/dist/nextjs-loader.cjs +29 -4
  26. package/dist/nextjs-loader.cjs.map +1 -1
  27. package/dist/page-xi1aI5a7.d.ts +36 -0
  28. package/package.json +18 -3
  29. package/dist/annotation-strategy-DCJHc--2.d.cts +0 -13
  30. package/dist/annotation-strategy-DCJHc--2.d.ts +0 -13
  31. package/dist/chunk-2LP5G6CD.js +0 -748
  32. package/dist/chunk-2LP5G6CD.js.map +0 -1
  33. package/dist/chunk-77BNMPBS.js.map +0 -1
  34. package/dist/chunk-F64WRL4H.js +0 -884
  35. package/dist/chunk-F64WRL4H.js.map +0 -1
  36. package/dist/chunk-G7M57ARC.js +0 -902
  37. package/dist/chunk-G7M57ARC.js.map +0 -1
  38. package/dist/chunk-GHPL4LGE.js +0 -205
  39. package/dist/chunk-GHPL4LGE.js.map +0 -1
  40. package/dist/chunk-IG3JZVZI.js +0 -846
  41. package/dist/chunk-IG3JZVZI.js.map +0 -1
  42. package/dist/chunk-WGGLHHC5.js +0 -337
  43. package/dist/chunk-WGGLHHC5.js.map +0 -1
  44. package/dist/chunk-X3K7Z2BH.js +0 -900
  45. package/dist/chunk-X3K7Z2BH.js.map +0 -1
  46. package/dist/chunk-ZHORRCQG.js +0 -756
  47. package/dist/chunk-ZHORRCQG.js.map +0 -1
  48. package/dist/index.cjs +0 -1170
  49. package/dist/index.cjs.map +0 -1
  50. package/dist/index.d.cts +0 -99
@@ -0,0 +1,898 @@
1
+ import {
2
+ parseCanvasFile
3
+ } from "../chunk-6Q4I3RFN.js";
4
+
5
+ // canvas/editor.ts
6
+ import { parseSync } from "oxc-parser";
7
+ function applyCanvasSourceEdits(source, edits) {
8
+ const sorted = [...edits].sort((left, right) => right.start - left.start);
9
+ let previousStart = source.length + 1;
10
+ let nextSource = source;
11
+ for (const edit of sorted) {
12
+ if (edit.start < 0 || edit.end < edit.start || edit.end > source.length || edit.end > previousStart) {
13
+ throw new Error(
14
+ `Invalid overlapping canvas source edit ${edit.start}:${edit.end}`
15
+ );
16
+ }
17
+ nextSource = nextSource.slice(0, edit.start) + edit.value + nextSource.slice(edit.end);
18
+ previousStart = edit.start;
19
+ }
20
+ return nextSource;
21
+ }
22
+ function updateCanvasName(options) {
23
+ return editCanvasStringAttribute(
24
+ options,
25
+ "name",
26
+ requireNonEmpty(options.name, "Canvas name"),
27
+ (parsed) => ({
28
+ valueSpan: parsed.nameSpan,
29
+ attributeSpan: parsed.nameAttributeSpan
30
+ })
31
+ );
32
+ }
33
+ function updateCanvasPath(options) {
34
+ return editCanvasStringAttribute(
35
+ options,
36
+ "path",
37
+ requireNonEmpty(options.path, "Canvas path"),
38
+ (parsed) => ({
39
+ valueSpan: parsed.pathSpan,
40
+ attributeSpan: parsed.pathAttributeSpan
41
+ })
42
+ );
43
+ }
44
+ function updateCanvasBackgroundColor(options) {
45
+ return editCanvasStringAttribute(
46
+ options,
47
+ "backgroundColor",
48
+ options.backgroundColor && options.backgroundColor.trim() !== "" ? options.backgroundColor : void 0,
49
+ (parsed) => ({
50
+ valueSpan: parsed.backgroundColorSpan,
51
+ attributeSpan: parsed.backgroundColorAttributeSpan
52
+ })
53
+ );
54
+ }
55
+ function editCanvasStringAttribute(options, attributeName, value, selectSpans) {
56
+ const parsed = parseCanvasFile(options.filePath, options.source);
57
+ const spans = selectSpans(parsed);
58
+ const edit = upsertJsxStringAttribute({
59
+ source: options.source,
60
+ openingElementSpan: requiredSpan(
61
+ parsed.canvasOpeningElementSpan,
62
+ "Canvas opening element not found"
63
+ ),
64
+ valueSpan: spans.valueSpan,
65
+ attributeSpan: spans.attributeSpan,
66
+ name: attributeName,
67
+ value
68
+ });
69
+ return applyResult(options.source, parsed, edit ? [edit] : []);
70
+ }
71
+ function updateStoryboardName(options) {
72
+ const parsed = parseCanvasFile(options.filePath, options.source);
73
+ const storyboard = findStoryboard(parsed, options.storyboardId);
74
+ if (storyboard.legacyV1) {
75
+ const edit2 = upsertObjectStringProperty({
76
+ source: options.source,
77
+ objectClose: storyboard.legacyV1.objectClose,
78
+ valueSpan: storyboard.nameSpan,
79
+ propertySpan: storyboard.legacyV1.namePropertySpan,
80
+ name: "name",
81
+ value: options.name && options.name.trim() !== "" ? options.name : void 0
82
+ });
83
+ return applyResult(options.source, parsed, edit2 ? [edit2] : []);
84
+ }
85
+ const edit = upsertJsxStringAttribute({
86
+ source: options.source,
87
+ openingElementSpan: requireStoryboardOpeningSpan(
88
+ storyboard,
89
+ options.storyboardId
90
+ ),
91
+ valueSpan: storyboard.nameSpan,
92
+ attributeSpan: storyboard.nameAttributeSpan,
93
+ name: "name",
94
+ value: options.name && options.name.trim() !== "" ? options.name : void 0
95
+ });
96
+ return applyResult(options.source, parsed, edit ? [edit] : []);
97
+ }
98
+ function updateStoryboardId(options) {
99
+ const nextId = requireNonEmpty(options.nextId, "Storyboard id");
100
+ const parsed = parseCanvasFile(options.filePath, options.source);
101
+ const storyboard = findStoryboard(parsed, options.storyboardId);
102
+ const duplicate = parsed.storyboards.find(
103
+ (item) => item.storyboardKey === nextId && item.storyboardKey !== storyboard.storyboardKey
104
+ );
105
+ if (duplicate) {
106
+ throw new Error(`Storyboard id "${nextId}" already exists`);
107
+ }
108
+ const edit = upsertJsxStringAttribute({
109
+ source: options.source,
110
+ openingElementSpan: requireStoryboardOpeningSpan(
111
+ storyboard,
112
+ options.storyboardId
113
+ ),
114
+ valueSpan: storyboard.idSpan,
115
+ attributeSpan: storyboard.idAttributeSpan,
116
+ name: "id",
117
+ value: nextId
118
+ });
119
+ return applyResult(options.source, parsed, [edit]);
120
+ }
121
+ function updateStoryboardLayout(options) {
122
+ const parsed = parseCanvasFile(options.filePath, options.source);
123
+ const storyboard = findStoryboard(parsed, options.storyboardId);
124
+ const spans = storyboard.layoutSpans;
125
+ if (!spans) {
126
+ throw new Error(
127
+ `Storyboard "${options.storyboardId}" has no editable layout`
128
+ );
129
+ }
130
+ const edits = [];
131
+ for (const key of ["x", "y", "width", "height"]) {
132
+ if (options.layout[key] === void 0) continue;
133
+ const span = spans[key];
134
+ if (!span) {
135
+ throw new Error(
136
+ `Storyboard "${options.storyboardId}" layout.${key} is not editable`
137
+ );
138
+ }
139
+ edits.push({
140
+ start: span.start,
141
+ end: span.end,
142
+ value: String(roundLayoutNumber(options.layout[key]))
143
+ });
144
+ }
145
+ if (options.layout.zIndex !== void 0) {
146
+ const value = String(roundLayoutNumber(options.layout.zIndex));
147
+ edits.push(
148
+ spans.zIndex ? { start: spans.zIndex.start, end: spans.zIndex.end, value } : insertLayoutProperty(options.source, spans, `zIndex: ${value}`)
149
+ );
150
+ }
151
+ if (options.layout.intrinsicSizing !== void 0) {
152
+ const value = JSON.stringify(options.layout.intrinsicSizing);
153
+ edits.push(
154
+ spans.intrinsicSizing ? {
155
+ start: spans.intrinsicSizing.start,
156
+ end: spans.intrinsicSizing.end,
157
+ value
158
+ } : insertLayoutProperty(
159
+ options.source,
160
+ spans,
161
+ `intrinsicSizing: ${value}`
162
+ )
163
+ );
164
+ }
165
+ return applyResult(options.source, parsed, edits);
166
+ }
167
+ function updateStoryboardComponent(options) {
168
+ const parsed = parseCanvasFile(options.filePath, options.source);
169
+ const storyboard = findStoryboard(parsed, options.storyboardId);
170
+ if (storyboard.kind !== "component") {
171
+ throw new Error(
172
+ `Storyboard "${options.storyboardId}" is not a component storyboard`
173
+ );
174
+ }
175
+ const imports = [];
176
+ if (options.componentImport) {
177
+ const importLine = buildMissingComponentImportLine(
178
+ parsed,
179
+ options.source,
180
+ options.componentImport
181
+ );
182
+ if (importLine) imports.push(importLine);
183
+ }
184
+ const importEdit = buildImportInsertionEdit(options.source, parsed, imports);
185
+ const componentEdit = upsertJsxExpressionAttribute({
186
+ source: options.source,
187
+ openingElementSpan: requireStoryboardOpeningSpan(
188
+ storyboard,
189
+ options.storyboardId
190
+ ),
191
+ expressionSpan: storyboard.component?.span,
192
+ attributeSpan: storyboard.component?.attributeSpan,
193
+ name: "component",
194
+ value: requireNonEmpty(
195
+ options.componentExpression,
196
+ "Storyboard component expression"
197
+ )
198
+ });
199
+ return applyResult(options.source, parsed, [
200
+ ...importEdit ? [importEdit] : [],
201
+ componentEdit
202
+ ]);
203
+ }
204
+ function updateStoryboardProps(options) {
205
+ const parsed = parseCanvasFile(options.filePath, options.source);
206
+ const storyboard = findStoryboard(parsed, options.storyboardId);
207
+ const edit = upsertJsxExpressionAttribute({
208
+ source: options.source,
209
+ openingElementSpan: requireStoryboardOpeningSpan(
210
+ storyboard,
211
+ options.storyboardId
212
+ ),
213
+ expressionSpan: storyboard.props?.span,
214
+ attributeSpan: storyboard.props?.attributeSpan,
215
+ name: "props",
216
+ value: options.propsExpression && options.propsExpression.trim() !== "" ? options.propsExpression.trim() : void 0
217
+ });
218
+ return applyResult(options.source, parsed, edit ? [edit] : []);
219
+ }
220
+ function updateRouteStoryboardRoute(options) {
221
+ const parsed = parseCanvasFile(options.filePath, options.source);
222
+ const storyboard = findStoryboard(parsed, options.storyboardId);
223
+ if (storyboard.kind !== "route") {
224
+ throw new Error(
225
+ `Storyboard "${options.storyboardId}" is not a route storyboard`
226
+ );
227
+ }
228
+ const edit = upsertJsxStringAttribute({
229
+ source: options.source,
230
+ openingElementSpan: requireStoryboardOpeningSpan(
231
+ storyboard,
232
+ options.storyboardId
233
+ ),
234
+ valueSpan: storyboard.routeSpan,
235
+ attributeSpan: storyboard.routeAttributeSpan,
236
+ name: "route",
237
+ value: requireNonEmpty(options.route, "Route storyboard route")
238
+ });
239
+ return applyResult(options.source, parsed, [edit]);
240
+ }
241
+ function updateHtmlStoryboardSource(options) {
242
+ const parsed = parseCanvasFile(options.filePath, options.source);
243
+ const storyboard = findStoryboard(parsed, options.storyboardId);
244
+ if (storyboard.kind !== "html") {
245
+ throw new Error(
246
+ `Storyboard "${options.storyboardId}" is not an html storyboard`
247
+ );
248
+ }
249
+ const filepath = options.filepath && options.filepath.trim() !== "" ? options.filepath.trim() : void 0;
250
+ const src = options.src && options.src.trim() !== "" ? options.src : void 0;
251
+ if (!filepath && !src) {
252
+ throw new Error("HTML storyboard source requires filepath or src");
253
+ }
254
+ const openingElementSpan = requireStoryboardOpeningSpan(
255
+ storyboard,
256
+ options.storyboardId
257
+ );
258
+ const filepathEdit = upsertJsxStringAttribute({
259
+ source: options.source,
260
+ openingElementSpan,
261
+ valueSpan: storyboard.filepathSpan,
262
+ attributeSpan: storyboard.filepathAttributeSpan,
263
+ name: "filepath",
264
+ value: filepath
265
+ });
266
+ const srcEdit = upsertJsxStringAttribute({
267
+ source: options.source,
268
+ openingElementSpan,
269
+ valueSpan: storyboard.srcSpan,
270
+ attributeSpan: storyboard.srcAttributeSpan,
271
+ name: "src",
272
+ value: src
273
+ });
274
+ return applyResult(options.source, parsed, [filepathEdit, srcEdit]);
275
+ }
276
+ function addCanvasStoryboard(options) {
277
+ const parsed = parseCanvasFile(options.filePath, options.source);
278
+ if (parsed.storyboards.some(
279
+ (item) => item.storyboardKey === options.storyboard.id
280
+ )) {
281
+ throw new Error(`Storyboard id "${options.storyboard.id}" already exists`);
282
+ }
283
+ const imports = [];
284
+ const sdkComponent = sdkComponentForStoryboard(options.storyboard.kind);
285
+ const sdkTag = resolveSdkTag(parsed, sdkComponent);
286
+ let storyboardTag = sdkTag.tag;
287
+ if (sdkTag.importLine) {
288
+ imports.push(sdkTag.importLine);
289
+ storyboardTag = sdkTag.tag;
290
+ }
291
+ let componentExpression;
292
+ if (options.storyboard.kind === "component") {
293
+ componentExpression = options.storyboard.componentExpression ?? options.storyboard.componentImport?.localName;
294
+ if (!componentExpression) {
295
+ throw new Error(
296
+ "Component storyboard requires componentExpression or componentImport"
297
+ );
298
+ }
299
+ if (options.storyboard.componentImport) {
300
+ const importLine = buildMissingComponentImportLine(
301
+ parsed,
302
+ options.source,
303
+ options.storyboard.componentImport
304
+ );
305
+ if (importLine) imports.push(importLine);
306
+ }
307
+ }
308
+ const importEdit = buildImportInsertionEdit(options.source, parsed, imports);
309
+ const insertionEdit = buildCanvasChildInsertionEdit({
310
+ source: options.source,
311
+ parsed,
312
+ tag: storyboardTag,
313
+ storyboard: options.storyboard,
314
+ componentExpression
315
+ });
316
+ const edits = [...importEdit ? [importEdit] : [], insertionEdit];
317
+ return applyResult(options.source, parsed, edits);
318
+ }
319
+ function removeCanvasStoryboard(options) {
320
+ const parsed = parseCanvasFile(options.filePath, options.source);
321
+ const storyboard = findStoryboard(parsed, options.storyboardId);
322
+ const edits = [
323
+ {
324
+ ...expandToFullLines(options.source, storyboard.jsxElementSpan),
325
+ value: ""
326
+ }
327
+ ];
328
+ if (options.removeUnusedComponentImport ?? true) {
329
+ const importEdit = buildUnusedComponentImportRemoval({
330
+ source: options.source,
331
+ parsed,
332
+ deletedStoryboard: storyboard,
333
+ deletionEdit: edits[0]
334
+ });
335
+ if (importEdit) edits.push(importEdit);
336
+ }
337
+ const result = applyResult(options.source, parsed, edits);
338
+ return {
339
+ ...result,
340
+ source: collapseBlankLines(result.source, detectNewline(options.source))
341
+ };
342
+ }
343
+ function applyResult(source, parsed, edits) {
344
+ const compactEdits = edits.filter((edit) => {
345
+ if (!edit) return false;
346
+ return source.slice(edit.start, edit.end) !== edit.value;
347
+ });
348
+ return {
349
+ source: applyCanvasSourceEdits(source, compactEdits),
350
+ edits: compactEdits,
351
+ parsed
352
+ };
353
+ }
354
+ function findStoryboard(parsed, storyboardId) {
355
+ const storyboard = parsed.storyboards.find(
356
+ (item) => item.storyboardKey === storyboardId
357
+ );
358
+ if (!storyboard) {
359
+ throw new Error(`Storyboard "${storyboardId}" not found`);
360
+ }
361
+ return storyboard;
362
+ }
363
+ function requireStoryboardOpeningSpan(storyboard, storyboardId) {
364
+ return requiredSpan(
365
+ storyboard.openingElementSpan,
366
+ `Storyboard "${storyboardId}" opening element not found`
367
+ );
368
+ }
369
+ function upsertJsxStringAttribute(options) {
370
+ if (options.value === void 0) {
371
+ if (!options.attributeSpan) return null;
372
+ return {
373
+ ...expandJsxAttributeRemoval(options.source, options.attributeSpan),
374
+ value: ""
375
+ };
376
+ }
377
+ const value = JSON.stringify(options.value);
378
+ if (options.valueSpan) {
379
+ return {
380
+ start: options.valueSpan.start,
381
+ end: options.valueSpan.end,
382
+ value
383
+ };
384
+ }
385
+ const offset = findOpeningElementAttributeInsertionOffset(
386
+ options.source,
387
+ options.openingElementSpan
388
+ );
389
+ return {
390
+ start: offset,
391
+ end: offset,
392
+ value: ` ${options.name}={${value}}`
393
+ };
394
+ }
395
+ function upsertObjectStringProperty(options) {
396
+ if (options.value === void 0) {
397
+ if (!options.propertySpan) return null;
398
+ return {
399
+ ...expandObjectPropertyRemoval(options.source, options.propertySpan),
400
+ value: ""
401
+ };
402
+ }
403
+ const value = JSON.stringify(options.value);
404
+ if (options.valueSpan) {
405
+ return {
406
+ start: options.valueSpan.start,
407
+ end: options.valueSpan.end,
408
+ value
409
+ };
410
+ }
411
+ if (typeof options.objectClose !== "number") {
412
+ throw new Error(`Object close not found for "${options.name}" property`);
413
+ }
414
+ return insertObjectProperty(
415
+ options.source,
416
+ options.objectClose,
417
+ `${options.name}: ${value}`
418
+ );
419
+ }
420
+ function upsertJsxExpressionAttribute(options) {
421
+ if (options.value === void 0) {
422
+ if (!options.attributeSpan) return null;
423
+ return {
424
+ ...expandJsxAttributeRemoval(options.source, options.attributeSpan),
425
+ value: ""
426
+ };
427
+ }
428
+ if (options.expressionSpan) {
429
+ return {
430
+ start: options.expressionSpan.start,
431
+ end: options.expressionSpan.end,
432
+ value: options.value
433
+ };
434
+ }
435
+ const offset = findOpeningElementAttributeInsertionOffset(
436
+ options.source,
437
+ options.openingElementSpan
438
+ );
439
+ return {
440
+ start: offset,
441
+ end: offset,
442
+ value: ` ${options.name}={${options.value}}`
443
+ };
444
+ }
445
+ function insertLayoutProperty(source, spans, propertySource) {
446
+ if (typeof spans.layoutClose !== "number") {
447
+ throw new Error("Layout object close span not found");
448
+ }
449
+ return insertObjectProperty(source, spans.layoutClose, propertySource);
450
+ }
451
+ function insertObjectProperty(source, objectClose, propertySource) {
452
+ const offset = trimWhitespaceBeforeOffset(source, objectClose);
453
+ const previous = previousMeaningfulCharacter(source, offset);
454
+ return {
455
+ start: offset,
456
+ end: offset,
457
+ value: `${previous === "{" || previous === "," ? " " : ", "}${propertySource}`
458
+ };
459
+ }
460
+ function formatStoryboardElement(options) {
461
+ const newline = detectNewline(options.source);
462
+ const closingIndent = lineIndentAt(options.source, options.insertionOffset);
463
+ const indent = `${closingIndent} `;
464
+ const firstLineIndent = " ";
465
+ const propIndent = `${indent} `;
466
+ const props = storyboardElementProps(
467
+ options.storyboard,
468
+ options.componentExpression
469
+ );
470
+ return `${firstLineIndent}<${options.tag}${newline}${props.map((prop) => `${propIndent}${prop}`).join(newline)}${newline}${indent}/>${newline}${closingIndent}`;
471
+ }
472
+ function buildCanvasChildInsertionEdit(options) {
473
+ const insertionOffset = options.parsed.canvasChildrenInsertionOffset;
474
+ if (typeof insertionOffset === "number") {
475
+ return {
476
+ start: insertionOffset,
477
+ end: insertionOffset,
478
+ value: formatStoryboardElement({
479
+ source: options.source,
480
+ insertionOffset,
481
+ tag: options.tag,
482
+ storyboard: options.storyboard,
483
+ componentExpression: options.componentExpression
484
+ })
485
+ };
486
+ }
487
+ const openingSpan = options.parsed.canvasOpeningElementSpan;
488
+ if (!openingSpan) {
489
+ throw new Error("Canvas closing element not found");
490
+ }
491
+ const openingSource = options.source.slice(
492
+ openingSpan.start,
493
+ openingSpan.end
494
+ );
495
+ const selfClosingMatch = openingSource.match(/\s*\/\s*>$/);
496
+ const canvasName = openingSource.match(
497
+ /^<\s*([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)/
498
+ )?.[1];
499
+ if (!selfClosingMatch || !canvasName) {
500
+ throw new Error("Canvas closing element not found");
501
+ }
502
+ const newline = detectNewline(options.source);
503
+ const replacementStart = openingSpan.end - selfClosingMatch[0].length;
504
+ const closingIndent = lineIndentAt(options.source, openingSpan.start);
505
+ const childIndent = `${closingIndent} `;
506
+ const propIndent = `${childIndent} `;
507
+ const props = storyboardElementProps(
508
+ options.storyboard,
509
+ options.componentExpression
510
+ );
511
+ return {
512
+ start: replacementStart,
513
+ end: openingSpan.end,
514
+ value: `>${newline}${childIndent}<${options.tag}${newline}${props.map((prop) => `${propIndent}${prop}`).join(
515
+ newline
516
+ )}${newline}${childIndent}/>${newline}${closingIndent}</${canvasName}>`
517
+ };
518
+ }
519
+ function storyboardElementProps(storyboard, componentExpression) {
520
+ const idProp = formatJsxStringAttribute(
521
+ "id",
522
+ requireNonEmpty(storyboard.id, "Storyboard id")
523
+ );
524
+ if (storyboard.kind === "legacy-v1") {
525
+ return [idProp, `storyboard={${storyboard.storyboardExpression}}`];
526
+ }
527
+ return [
528
+ idProp,
529
+ ...storyboard.name && storyboard.name.trim() !== "" ? [formatJsxStringAttribute("name", storyboard.name)] : [],
530
+ ...storyboardKindProps(storyboard, componentExpression),
531
+ `layout={{ ${formatLayout(storyboard.layout)} }}`
532
+ ];
533
+ }
534
+ function formatJsxStringAttribute(name, value) {
535
+ const encoded = JSON.stringify(value);
536
+ const plainSafe = encoded === `"${value}"` && !value.includes("&");
537
+ return plainSafe ? `${name}=${encoded}` : `${name}={${encoded}}`;
538
+ }
539
+ function storyboardKindProps(storyboard, componentExpression) {
540
+ if (storyboard.kind === "component") {
541
+ return [
542
+ `component={${componentExpression}}`,
543
+ ...storyboard.props && storyboard.props.trim() !== "" ? [`props={${storyboard.props.trim()}}`] : []
544
+ ];
545
+ }
546
+ if (storyboard.kind === "route") {
547
+ return [formatJsxStringAttribute("route", storyboard.route)];
548
+ }
549
+ return [
550
+ ...storyboard.filepath ? [formatJsxStringAttribute("filepath", storyboard.filepath)] : [],
551
+ ...storyboard.src ? [formatJsxStringAttribute("src", storyboard.src)] : []
552
+ ];
553
+ }
554
+ function formatLayout(layout) {
555
+ return [
556
+ `x: ${roundLayoutNumber(layout.x)}`,
557
+ `y: ${roundLayoutNumber(layout.y)}`,
558
+ `width: ${roundLayoutNumber(layout.width)}`,
559
+ `height: ${roundLayoutNumber(layout.height)}`,
560
+ ...layout.zIndex === void 0 ? [] : [`zIndex: ${roundLayoutNumber(layout.zIndex)}`],
561
+ ...layout.intrinsicSizing ? [`intrinsicSizing: ${JSON.stringify(layout.intrinsicSizing)}`] : []
562
+ ].join(", ");
563
+ }
564
+ function sdkComponentForStoryboard(kind) {
565
+ if (kind === "route") return "RouteStoryboard";
566
+ if (kind === "html") return "HtmlStoryboard";
567
+ if (kind === "legacy-v1") return "LegacyStoryboardV1";
568
+ return "Storyboard";
569
+ }
570
+ function resolveSdkTag(parsed, component) {
571
+ for (const item of parsed.imports) {
572
+ if (item.source !== "tempo-sdk/canvas") continue;
573
+ if (item.kind === "namespace")
574
+ return { tag: `${item.localName}.${component}` };
575
+ if (item.importedName === component) return { tag: item.localName };
576
+ }
577
+ const localName = pickAvailableIdentifier(parsed, `TempoCanvas${component}`);
578
+ return {
579
+ tag: localName,
580
+ importLine: `import { ${component} as ${localName} } from "tempo-sdk/canvas";`
581
+ };
582
+ }
583
+ function buildMissingComponentImportLine(parsed, source, componentImport) {
584
+ const existing = parsed.imports.find(
585
+ (item) => item.localName === componentImport.localName
586
+ );
587
+ if (existing) {
588
+ if (existing.source !== componentImport.source) {
589
+ throw new Error(
590
+ `Import binding "${componentImport.localName}" already exists`
591
+ );
592
+ }
593
+ return null;
594
+ }
595
+ if (sourceContainsIdentifierNode(
596
+ parsed.filePath,
597
+ source,
598
+ componentImport.localName
599
+ )) {
600
+ throw new Error(`Identifier "${componentImport.localName}" already exists`);
601
+ }
602
+ if (!componentImport.importedName || componentImport.importedName === "default") {
603
+ return `import ${componentImport.localName} from ${JSON.stringify(
604
+ componentImport.source
605
+ )};`;
606
+ }
607
+ const specifier = componentImport.importedName === componentImport.localName ? componentImport.localName : `${componentImport.importedName} as ${componentImport.localName}`;
608
+ return `import { ${specifier} } from ${JSON.stringify(componentImport.source)};`;
609
+ }
610
+ function buildImportInsertionEdit(source, parsed, importLines) {
611
+ if (importLines.length === 0) return null;
612
+ const newline = detectNewline(source);
613
+ const uniqueLines = Array.from(new Set(importLines));
614
+ const lastImport = parsed.imports.reduce(
615
+ (latest, item) => !latest || item.declarationSpan.end > latest.declarationSpan.end ? item : latest,
616
+ null
617
+ );
618
+ if (lastImport) {
619
+ return {
620
+ start: lastImport.declarationSpan.end,
621
+ end: lastImport.declarationSpan.end,
622
+ value: `${newline}${uniqueLines.join(newline)}`
623
+ };
624
+ }
625
+ const offset = topLevelInsertionOffset(source);
626
+ return {
627
+ start: offset,
628
+ end: offset,
629
+ value: `${uniqueLines.join(newline)}${newline}`
630
+ };
631
+ }
632
+ function buildUnusedComponentImportRemoval(options) {
633
+ const componentImport = options.deletedStoryboard.component?.import;
634
+ if (!componentImport || componentImport.importKind === "type") return null;
635
+ const usedByRemainingStoryboard = options.parsed.storyboards.some(
636
+ (storyboard) => storyboard.storyboardKey !== options.deletedStoryboard.storyboardKey && storyboard.component?.import?.localName === componentImport.localName
637
+ );
638
+ if (usedByRemainingStoryboard) return null;
639
+ const sourceAfterStoryboardDelete = applyCanvasSourceEdits(options.source, [
640
+ options.deletionEdit
641
+ ]);
642
+ if (componentImport.specifierCount === 1) {
643
+ const sourceWithoutImport2 = removeSpanText(
644
+ sourceAfterStoryboardDelete,
645
+ componentImport.declarationSpan
646
+ );
647
+ if (hasIdentifier(sourceWithoutImport2, componentImport.localName))
648
+ return null;
649
+ return {
650
+ ...expandToFullLines(options.source, componentImport.declarationSpan),
651
+ value: ""
652
+ };
653
+ }
654
+ if (componentImport.kind !== "named" || componentImport.hasDefaultSpecifier || componentImport.hasNamespaceSpecifier || componentImport.namedSpecifierCount <= 1) {
655
+ return null;
656
+ }
657
+ const specifierRange = expandImportSpecifierRemovalRange(
658
+ options.source,
659
+ componentImport.span
660
+ );
661
+ const sourceWithoutImport = removeSpanText(
662
+ sourceAfterStoryboardDelete,
663
+ specifierRange
664
+ );
665
+ if (hasIdentifier(sourceWithoutImport, componentImport.localName))
666
+ return null;
667
+ return {
668
+ ...specifierRange,
669
+ value: ""
670
+ };
671
+ }
672
+ function expandImportSpecifierRemovalRange(source, span) {
673
+ const lineStart = Math.max(source.lastIndexOf("\n", span.start - 1) + 1, 0);
674
+ const nextNewline = source.indexOf("\n", span.end);
675
+ const lineEnd = nextNewline >= 0 ? nextNewline + 1 : source.length;
676
+ const before = source.slice(lineStart, span.start);
677
+ const after = source.slice(span.end, lineEnd);
678
+ if (/^[ \t]*$/.test(before) && /^[ \t]*(,)?[ \t]*(\r?\n)?$/.test(after)) {
679
+ return { start: lineStart, end: lineEnd };
680
+ }
681
+ let start = span.start;
682
+ let end = span.end;
683
+ while (end < source.length && /\s/.test(source[end] ?? "")) end++;
684
+ if (source[end] === ",") {
685
+ end++;
686
+ while (end < source.length && (source[end] === " " || source[end] === " ")) {
687
+ end++;
688
+ }
689
+ return { start, end };
690
+ }
691
+ while (start > 0 && (source[start - 1] === " " || source[start - 1] === " ")) {
692
+ start--;
693
+ }
694
+ if (source[start - 1] === ",") {
695
+ start--;
696
+ while (start > 0 && (source[start - 1] === " " || source[start - 1] === " ")) {
697
+ start--;
698
+ }
699
+ }
700
+ return { start, end };
701
+ }
702
+ function removeSpanText(source, span) {
703
+ return source.slice(0, span.start) + source.slice(span.end);
704
+ }
705
+ function findOpeningElementAttributeInsertionOffset(source, span) {
706
+ const close = source.lastIndexOf(">", span.end - 1);
707
+ if (close < span.start) {
708
+ throw new Error("JSX opening element close not found");
709
+ }
710
+ return source[close - 1] === "/" ? close - 1 : close;
711
+ }
712
+ function expandJsxAttributeRemoval(source, span) {
713
+ let start = span.start;
714
+ let end = span.end;
715
+ while (start > 0 && (source[start - 1] === " " || source[start - 1] === " ")) {
716
+ start--;
717
+ }
718
+ while (end < source.length && (source[end] === " " || source[end] === " ")) {
719
+ end++;
720
+ }
721
+ return { start, end };
722
+ }
723
+ function expandObjectPropertyRemoval(source, span) {
724
+ let start = span.start;
725
+ let end = span.end;
726
+ while (start > 0 && /\s/.test(source[start - 1] ?? "")) start--;
727
+ while (end < source.length && /\s/.test(source[end] ?? "")) end++;
728
+ if (source[end] === ",") {
729
+ end++;
730
+ while (end < source.length && (source[end] === " " || source[end] === " ")) {
731
+ end++;
732
+ }
733
+ return { start, end };
734
+ }
735
+ let comma = start - 1;
736
+ while (comma >= 0 && /\s/.test(source[comma] ?? "")) comma--;
737
+ if (source[comma] === ",") {
738
+ start = comma;
739
+ }
740
+ return { start, end };
741
+ }
742
+ function expandToFullLines(source, span) {
743
+ let start = span.start;
744
+ let end = span.end;
745
+ while (start > 0 && source[start - 1] !== "\n" && source[start - 1] !== "\r") {
746
+ start--;
747
+ }
748
+ if (start > 0) {
749
+ if (source[start - 1] === "\n") start--;
750
+ if (start > 0 && source[start - 1] === "\r") start--;
751
+ }
752
+ while (end < source.length && (source[end] === " " || source[end] === " ")) {
753
+ end++;
754
+ }
755
+ if (source.slice(end, end + 2) === "\r\n") {
756
+ end += 2;
757
+ } else if (source[end] === "\n" || source[end] === "\r") {
758
+ end++;
759
+ }
760
+ return { start, end };
761
+ }
762
+ function expandStatementForward(source, span) {
763
+ let end = span.end;
764
+ while (end < source.length && (source[end] === " " || source[end] === " ")) {
765
+ end++;
766
+ }
767
+ if (source.slice(end, end + 2) === "\r\n") {
768
+ end += 2;
769
+ } else if (source[end] === "\n" || source[end] === "\r") {
770
+ end++;
771
+ }
772
+ return { start: span.start, end };
773
+ }
774
+ function lineIndentAt(source, offset) {
775
+ const lineStart = Math.max(source.lastIndexOf("\n", offset - 1) + 1, 0);
776
+ const linePrefix = source.slice(lineStart, offset);
777
+ const match = linePrefix.match(/^[ \t]*/);
778
+ return match?.[0] ?? "";
779
+ }
780
+ function previousMeaningfulCharacter(source, offset) {
781
+ for (let index = offset - 1; index >= 0; index -= 1) {
782
+ const character = source[index] ?? "";
783
+ if (!/\s/.test(character)) return character;
784
+ }
785
+ return "";
786
+ }
787
+ function trimWhitespaceBeforeOffset(source, offset) {
788
+ let nextOffset = offset;
789
+ while (nextOffset > 0 && /\s/.test(source[nextOffset - 1] ?? "")) {
790
+ nextOffset--;
791
+ }
792
+ return nextOffset;
793
+ }
794
+ function collapseBlankLines(source, newline) {
795
+ return newline === "\r\n" ? source.replace(/(\r\n){3,}/g, "\r\n\r\n") : source.replace(/\n{3,}/g, "\n\n");
796
+ }
797
+ function detectNewline(source) {
798
+ return source.includes("\r\n") ? "\r\n" : "\n";
799
+ }
800
+ function topLevelInsertionOffset(source) {
801
+ let offset = 0;
802
+ if (source.startsWith("#!")) {
803
+ const newline = source.indexOf("\n");
804
+ offset = newline >= 0 ? newline + 1 : source.length;
805
+ }
806
+ while (true) {
807
+ const nextLineEnd = source.indexOf("\n", offset);
808
+ const end = nextLineEnd >= 0 ? nextLineEnd + 1 : source.length;
809
+ const line = source.slice(offset, end).trim();
810
+ if (!/^["']use [^"']+["'];?$/.test(line)) break;
811
+ offset = end;
812
+ }
813
+ return offset;
814
+ }
815
+ function pickAvailableIdentifier(parsed, base) {
816
+ const usedImports = new Set(parsed.imports.map((item) => item.localName));
817
+ if (!usedImports.has(base)) return base;
818
+ for (let index = 2; index < 1e4; index += 1) {
819
+ const candidate = `${base}${index}`;
820
+ if (!usedImports.has(candidate)) return candidate;
821
+ }
822
+ throw new Error(`Unable to find available identifier for ${base}`);
823
+ }
824
+ function hasIdentifier(source, identifier) {
825
+ return new RegExp(`\\b${escapeRegExp(identifier)}\\b`).test(source);
826
+ }
827
+ function sourceContainsIdentifierNode(filePath, source, name) {
828
+ const program = parseSync(filePath, source).program;
829
+ let found = false;
830
+ walkWithParent(program, (node) => {
831
+ if (found) return;
832
+ if ((node.type === "Identifier" || node.type === "JSXIdentifier") && node.name === name) {
833
+ found = true;
834
+ }
835
+ });
836
+ return found;
837
+ }
838
+ function walkWithParent(node, visit, parent = null, key = null, seen = /* @__PURE__ */ new Set()) {
839
+ if (!node || typeof node !== "object" || seen.has(node)) return;
840
+ seen.add(node);
841
+ if (typeof node.type === "string") visit(node, parent, key);
842
+ for (const [childKey, value] of Object.entries(node)) {
843
+ if (!value || typeof value !== "object") continue;
844
+ if (Array.isArray(value)) {
845
+ for (const item of value)
846
+ walkWithParent(item, visit, node, childKey, seen);
847
+ } else {
848
+ walkWithParent(value, visit, node, childKey, seen);
849
+ }
850
+ }
851
+ }
852
+ function escapeRegExp(value) {
853
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
854
+ }
855
+ function roundLayoutNumber(value) {
856
+ if (typeof value !== "number" || !Number.isFinite(value)) {
857
+ throw new Error("Layout values must be finite numbers");
858
+ }
859
+ return Math.round(value);
860
+ }
861
+ function requireNonEmpty(value, label) {
862
+ const trimmed = value.trim();
863
+ if (!trimmed) {
864
+ throw new Error(`${label} must be non-empty`);
865
+ }
866
+ return trimmed;
867
+ }
868
+ function requiredSpan(value, message) {
869
+ if (value === void 0) {
870
+ throw new Error(message);
871
+ }
872
+ return value;
873
+ }
874
+ export {
875
+ addCanvasStoryboard,
876
+ applyCanvasSourceEdits,
877
+ collapseBlankLines,
878
+ detectNewline,
879
+ expandStatementForward,
880
+ expandToFullLines,
881
+ formatJsxStringAttribute,
882
+ hasIdentifier,
883
+ removeCanvasStoryboard,
884
+ sourceContainsIdentifierNode,
885
+ topLevelInsertionOffset,
886
+ updateCanvasBackgroundColor,
887
+ updateCanvasName,
888
+ updateCanvasPath,
889
+ updateHtmlStoryboardSource,
890
+ updateRouteStoryboardRoute,
891
+ updateStoryboardComponent,
892
+ updateStoryboardId,
893
+ updateStoryboardLayout,
894
+ updateStoryboardName,
895
+ updateStoryboardProps,
896
+ walkWithParent
897
+ };
898
+ //# sourceMappingURL=editor.js.map