@jimmy_harika/websitekit 0.3.1 → 0.4.0
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/package.json +2 -2
- package/src/editor/Inspector.tsx +285 -0
- package/src/registry.ts +26 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jimmy_harika/websitekit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Snow Labs website builder engine. Block-agnostic, curated-section. One shared builder across every Snow Labs app
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Snow Labs website builder engine. Block-agnostic, curated-section. One shared builder across every Snow Labs app — engine + design-system-agnostic blocks + per-industry catalogs (author, photographer, …). Each app plugs in a catalog + theme + persistence adapter.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"private": false,
|
|
7
7
|
"publishConfig": {
|
package/src/editor/Inspector.tsx
CHANGED
|
@@ -237,6 +237,22 @@ function Field({
|
|
|
237
237
|
}
|
|
238
238
|
onAltChange={(alt) => field.altKey && setProp(field.altKey, alt)}
|
|
239
239
|
/>
|
|
240
|
+
) : field.widget === "imageList" ? (
|
|
241
|
+
<ImageListField
|
|
242
|
+
value={Array.isArray(value) ? (value as string[]) : []}
|
|
243
|
+
max={field.max}
|
|
244
|
+
onChange={onChange}
|
|
245
|
+
/>
|
|
246
|
+
) : field.widget === "itemList" ? (
|
|
247
|
+
<ItemListField
|
|
248
|
+
value={
|
|
249
|
+
Array.isArray(value) ? (value as Record<string, string>[]) : []
|
|
250
|
+
}
|
|
251
|
+
itemFields={field.itemFields}
|
|
252
|
+
imageKey={field.imageKey}
|
|
253
|
+
addLabel={field.addLabel}
|
|
254
|
+
onChange={onChange}
|
|
255
|
+
/>
|
|
240
256
|
) : (
|
|
241
257
|
<input
|
|
242
258
|
type={field.widget === "url" ? "url" : "text"}
|
|
@@ -339,3 +355,272 @@ function ImageField({
|
|
|
339
355
|
</div>
|
|
340
356
|
);
|
|
341
357
|
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Upload files through the app-injected capability, falling back to local
|
|
361
|
+
* object URLs when the app didn't wire `uploadImage` (dev/demo). Object URLs
|
|
362
|
+
* are tracked and revoked on unmount so replaced previews don't leak.
|
|
363
|
+
*/
|
|
364
|
+
function useImageUploads() {
|
|
365
|
+
const caps = useContext(EditorCapsContext);
|
|
366
|
+
const [busy, setBusy] = useState(false);
|
|
367
|
+
const blobsRef = useRef<string[]>([]);
|
|
368
|
+
useEffect(() => {
|
|
369
|
+
const blobs = blobsRef.current;
|
|
370
|
+
return () => {
|
|
371
|
+
for (const b of blobs) URL.revokeObjectURL(b);
|
|
372
|
+
};
|
|
373
|
+
}, []);
|
|
374
|
+
async function upload(files: File[]): Promise<string[]> {
|
|
375
|
+
setBusy(true);
|
|
376
|
+
try {
|
|
377
|
+
const urls: string[] = [];
|
|
378
|
+
for (const file of files) {
|
|
379
|
+
if (caps.uploadImage) {
|
|
380
|
+
urls.push(await caps.uploadImage(file));
|
|
381
|
+
} else {
|
|
382
|
+
const u = URL.createObjectURL(file);
|
|
383
|
+
blobsRef.current.push(u);
|
|
384
|
+
urls.push(u);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return urls;
|
|
388
|
+
} finally {
|
|
389
|
+
setBusy(false);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return { upload, busy };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Move `arr[from]` to `to`, returning a new array. */
|
|
396
|
+
function moved<T>(arr: T[], from: number, to: number): T[] {
|
|
397
|
+
const next = [...arr];
|
|
398
|
+
const [x] = next.splice(from, 1);
|
|
399
|
+
next.splice(to, 0, x);
|
|
400
|
+
return next;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** `imageList` widget — thumbnails with reorder/remove + multi-file upload. */
|
|
404
|
+
function ImageListField({
|
|
405
|
+
value,
|
|
406
|
+
max,
|
|
407
|
+
onChange,
|
|
408
|
+
}: {
|
|
409
|
+
value: string[];
|
|
410
|
+
max?: number;
|
|
411
|
+
onChange: (val: unknown) => void;
|
|
412
|
+
}) {
|
|
413
|
+
const { upload, busy } = useImageUploads();
|
|
414
|
+
const ref = useRef<HTMLInputElement>(null);
|
|
415
|
+
const [urlDraft, setUrlDraft] = useState("");
|
|
416
|
+
const room = max ? Math.max(0, max - value.length) : Infinity;
|
|
417
|
+
|
|
418
|
+
async function pick(e: ChangeEvent<HTMLInputElement>) {
|
|
419
|
+
const files = Array.from(e.target.files ?? []);
|
|
420
|
+
e.target.value = "";
|
|
421
|
+
if (!files.length) return;
|
|
422
|
+
const urls = await upload(max ? files.slice(0, room) : files);
|
|
423
|
+
if (urls.length) onChange([...value, ...urls]);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return (
|
|
427
|
+
<div className="space-y-2">
|
|
428
|
+
{value.length > 0 ? (
|
|
429
|
+
<div className="grid grid-cols-3 gap-1.5">
|
|
430
|
+
{value.map((url, i) => (
|
|
431
|
+
<div key={`${url}-${i}`} className="group relative">
|
|
432
|
+
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
433
|
+
<img src={url} alt="" className="aspect-square w-full rounded-md object-cover" />
|
|
434
|
+
<div className="absolute inset-0 hidden items-end justify-between rounded-md bg-black/40 p-1 group-hover:flex">
|
|
435
|
+
<div className="flex gap-0.5">
|
|
436
|
+
<button
|
|
437
|
+
title="Move left"
|
|
438
|
+
disabled={i === 0}
|
|
439
|
+
onClick={() => onChange(moved(value, i, i - 1))}
|
|
440
|
+
className="rounded bg-white/90 px-1 text-[11px] leading-5 text-black disabled:opacity-40"
|
|
441
|
+
>
|
|
442
|
+
←
|
|
443
|
+
</button>
|
|
444
|
+
<button
|
|
445
|
+
title="Move right"
|
|
446
|
+
disabled={i === value.length - 1}
|
|
447
|
+
onClick={() => onChange(moved(value, i, i + 1))}
|
|
448
|
+
className="rounded bg-white/90 px-1 text-[11px] leading-5 text-black disabled:opacity-40"
|
|
449
|
+
>
|
|
450
|
+
→
|
|
451
|
+
</button>
|
|
452
|
+
</div>
|
|
453
|
+
<button
|
|
454
|
+
title="Remove"
|
|
455
|
+
onClick={() => onChange(value.filter((_, j) => j !== i))}
|
|
456
|
+
className="rounded bg-white/90 px-1 text-[11px] leading-5 text-black"
|
|
457
|
+
>
|
|
458
|
+
×
|
|
459
|
+
</button>
|
|
460
|
+
</div>
|
|
461
|
+
</div>
|
|
462
|
+
))}
|
|
463
|
+
</div>
|
|
464
|
+
) : (
|
|
465
|
+
<div className="flex h-16 items-center justify-center rounded-md border border-dashed text-xs text-muted-foreground/70">
|
|
466
|
+
No images yet
|
|
467
|
+
</div>
|
|
468
|
+
)}
|
|
469
|
+
<button
|
|
470
|
+
onClick={() => ref.current?.click()}
|
|
471
|
+
disabled={busy || room === 0}
|
|
472
|
+
className="w-full rounded-md border px-2 py-1.5 text-xs hover:bg-muted disabled:opacity-50"
|
|
473
|
+
>
|
|
474
|
+
{busy ? "Uploading…" : room === 0 ? `Limit of ${max} reached` : "Add images"}
|
|
475
|
+
</button>
|
|
476
|
+
<input ref={ref} type="file" accept="image/*" multiple hidden onChange={pick} />
|
|
477
|
+
<div className="flex gap-1.5">
|
|
478
|
+
<input
|
|
479
|
+
value={urlDraft}
|
|
480
|
+
onChange={(e) => setUrlDraft(e.target.value)}
|
|
481
|
+
placeholder="or paste image URL"
|
|
482
|
+
className="min-w-0 flex-1 rounded-md border border-border px-2.5 py-1.5 text-xs"
|
|
483
|
+
/>
|
|
484
|
+
<button
|
|
485
|
+
disabled={!urlDraft.trim() || room === 0}
|
|
486
|
+
onClick={() => {
|
|
487
|
+
onChange([...value, urlDraft.trim()]);
|
|
488
|
+
setUrlDraft("");
|
|
489
|
+
}}
|
|
490
|
+
className="rounded-md border px-2 py-1.5 text-xs hover:bg-muted disabled:opacity-50"
|
|
491
|
+
>
|
|
492
|
+
Add
|
|
493
|
+
</button>
|
|
494
|
+
</div>
|
|
495
|
+
</div>
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** `itemList` widget — structured rows (mini-form each) with optional image chip. */
|
|
500
|
+
function ItemListField({
|
|
501
|
+
value,
|
|
502
|
+
itemFields,
|
|
503
|
+
imageKey,
|
|
504
|
+
addLabel,
|
|
505
|
+
onChange,
|
|
506
|
+
}: {
|
|
507
|
+
value: Record<string, string>[];
|
|
508
|
+
itemFields: { key: string; label: string; placeholder?: string }[];
|
|
509
|
+
imageKey?: string;
|
|
510
|
+
addLabel?: string;
|
|
511
|
+
onChange: (val: unknown) => void;
|
|
512
|
+
}) {
|
|
513
|
+
const { upload, busy } = useImageUploads();
|
|
514
|
+
const fileRef = useRef<HTMLInputElement>(null);
|
|
515
|
+
// Which row the pending file-pick belongs to.
|
|
516
|
+
const pickRow = useRef<number | null>(null);
|
|
517
|
+
|
|
518
|
+
const setRow = (i: number, patch: Record<string, string>) =>
|
|
519
|
+
onChange(value.map((row, j) => (j === i ? { ...row, ...patch } : row)));
|
|
520
|
+
|
|
521
|
+
async function pick(e: ChangeEvent<HTMLInputElement>) {
|
|
522
|
+
const file = e.target.files?.[0];
|
|
523
|
+
e.target.value = "";
|
|
524
|
+
const i = pickRow.current;
|
|
525
|
+
pickRow.current = null;
|
|
526
|
+
if (!file || i == null || !imageKey) return;
|
|
527
|
+
const [url] = await upload([file]);
|
|
528
|
+
if (url) setRow(i, { [imageKey]: url });
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return (
|
|
532
|
+
<div className="space-y-2">
|
|
533
|
+
{value.map((row, i) => (
|
|
534
|
+
<div key={i} className="space-y-1.5 rounded-md border border-border p-2">
|
|
535
|
+
<div className="flex items-start gap-2">
|
|
536
|
+
{imageKey && (
|
|
537
|
+
<button
|
|
538
|
+
title={row[imageKey] ? "Replace photo" : "Add photo"}
|
|
539
|
+
disabled={busy}
|
|
540
|
+
onClick={() => {
|
|
541
|
+
pickRow.current = i;
|
|
542
|
+
fileRef.current?.click();
|
|
543
|
+
}}
|
|
544
|
+
className="relative size-10 shrink-0 overflow-hidden rounded-full border border-dashed"
|
|
545
|
+
>
|
|
546
|
+
{row[imageKey] ? (
|
|
547
|
+
// eslint-disable-next-line @next/next/no-img-element
|
|
548
|
+
<img src={row[imageKey]} alt="" className="size-full object-cover" />
|
|
549
|
+
) : (
|
|
550
|
+
<span className="text-[10px] text-muted-foreground/70">+</span>
|
|
551
|
+
)}
|
|
552
|
+
</button>
|
|
553
|
+
)}
|
|
554
|
+
<div className="min-w-0 flex-1 space-y-1">
|
|
555
|
+
{itemFields.map((f) => (
|
|
556
|
+
<input
|
|
557
|
+
key={f.key}
|
|
558
|
+
defaultValue={row[f.key] ?? ""}
|
|
559
|
+
placeholder={f.placeholder ?? f.label}
|
|
560
|
+
onBlur={(e) => {
|
|
561
|
+
if (e.target.value !== (row[f.key] ?? "")) {
|
|
562
|
+
setRow(i, { [f.key]: e.target.value });
|
|
563
|
+
}
|
|
564
|
+
}}
|
|
565
|
+
className="w-full rounded-md border border-border px-2 py-1 text-xs"
|
|
566
|
+
/>
|
|
567
|
+
))}
|
|
568
|
+
</div>
|
|
569
|
+
<div className="flex shrink-0 flex-col gap-0.5">
|
|
570
|
+
<button
|
|
571
|
+
title="Move up"
|
|
572
|
+
disabled={i === 0}
|
|
573
|
+
onClick={() => onChange(moved(value, i, i - 1))}
|
|
574
|
+
className="rounded border px-1 text-[10px] leading-4 disabled:opacity-40"
|
|
575
|
+
>
|
|
576
|
+
↑
|
|
577
|
+
</button>
|
|
578
|
+
<button
|
|
579
|
+
title="Move down"
|
|
580
|
+
disabled={i === value.length - 1}
|
|
581
|
+
onClick={() => onChange(moved(value, i, i + 1))}
|
|
582
|
+
className="rounded border px-1 text-[10px] leading-4 disabled:opacity-40"
|
|
583
|
+
>
|
|
584
|
+
↓
|
|
585
|
+
</button>
|
|
586
|
+
<button
|
|
587
|
+
title="Remove"
|
|
588
|
+
onClick={() => onChange(value.filter((_, j) => j !== i))}
|
|
589
|
+
className="rounded border px-1 text-[10px] leading-4 text-red-600"
|
|
590
|
+
>
|
|
591
|
+
×
|
|
592
|
+
</button>
|
|
593
|
+
</div>
|
|
594
|
+
</div>
|
|
595
|
+
{imageKey && row[imageKey] && (
|
|
596
|
+
<button
|
|
597
|
+
onClick={() => setRow(i, { [imageKey]: "" })}
|
|
598
|
+
className="text-[10px] text-muted-foreground/70 hover:text-foreground"
|
|
599
|
+
>
|
|
600
|
+
Remove photo
|
|
601
|
+
</button>
|
|
602
|
+
)}
|
|
603
|
+
</div>
|
|
604
|
+
))}
|
|
605
|
+
<button
|
|
606
|
+
onClick={() =>
|
|
607
|
+
onChange([
|
|
608
|
+
...value,
|
|
609
|
+
Object.fromEntries(
|
|
610
|
+
[...itemFields.map((f) => f.key), ...(imageKey ? [imageKey] : [])].map((k) => [
|
|
611
|
+
k,
|
|
612
|
+
"",
|
|
613
|
+
]),
|
|
614
|
+
),
|
|
615
|
+
])
|
|
616
|
+
}
|
|
617
|
+
className="w-full rounded-md border px-2 py-1.5 text-xs hover:bg-muted"
|
|
618
|
+
>
|
|
619
|
+
{addLabel ?? "Add item"}
|
|
620
|
+
</button>
|
|
621
|
+
{imageKey && (
|
|
622
|
+
<input ref={fileRef} type="file" accept="image/*" hidden onChange={pick} />
|
|
623
|
+
)}
|
|
624
|
+
</div>
|
|
625
|
+
);
|
|
626
|
+
}
|
package/src/registry.ts
CHANGED
|
@@ -55,6 +55,32 @@ export type FieldDescriptor =
|
|
|
55
55
|
altKey?: string;
|
|
56
56
|
placeholder?: string;
|
|
57
57
|
help?: string;
|
|
58
|
+
}
|
|
59
|
+
| {
|
|
60
|
+
key: string;
|
|
61
|
+
label: string;
|
|
62
|
+
/** Multi-image manager for `string[]` props — upload several, reorder, remove. */
|
|
63
|
+
widget: "imageList";
|
|
64
|
+
/** Soft cap on image count (layout sanity). */
|
|
65
|
+
max?: number;
|
|
66
|
+
help?: string;
|
|
67
|
+
}
|
|
68
|
+
| {
|
|
69
|
+
key: string;
|
|
70
|
+
label: string;
|
|
71
|
+
/**
|
|
72
|
+
* Structured row list for `Array<Record<string, string>>` props — one
|
|
73
|
+
* mini-form per row (add/remove/reorder), e.g. wedding-party members or
|
|
74
|
+
* testimonials. Set `imageKey` to give each row an image chip + upload.
|
|
75
|
+
*/
|
|
76
|
+
widget: "itemList";
|
|
77
|
+
/** Text sub-fields per row, rendered in order. */
|
|
78
|
+
itemFields: { key: string; label: string; placeholder?: string }[];
|
|
79
|
+
/** Row sub-field holding an image URL (upload chip per row). */
|
|
80
|
+
imageKey?: string;
|
|
81
|
+
/** Label for the add-row button, e.g. "Add person". */
|
|
82
|
+
addLabel?: string;
|
|
83
|
+
help?: string;
|
|
58
84
|
};
|
|
59
85
|
|
|
60
86
|
export interface SectionDefinition<
|