@kitcms/ui 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +69 -2
- package/package.json +47 -3
- package/src/index.ts +1 -0
- package/src/lib/Admin.svelte +345 -0
- package/src/lib/fields/ArrayField.svelte +134 -0
- package/src/lib/fields/BlocksField.svelte +201 -0
- package/src/lib/fields/CheckboxField.svelte +48 -0
- package/src/lib/fields/DateField.svelte +70 -0
- package/src/lib/fields/EmailField.svelte +39 -0
- package/src/lib/fields/FieldShell.svelte +70 -0
- package/src/lib/fields/GroupField.svelte +40 -0
- package/src/lib/fields/JsonField.svelte +74 -0
- package/src/lib/fields/NumberField.svelte +51 -0
- package/src/lib/fields/RelationshipField.svelte +11 -0
- package/src/lib/fields/RenderFields.svelte +32 -0
- package/src/lib/fields/RenderNodes.svelte +147 -0
- package/src/lib/fields/RichTextField.svelte +310 -0
- package/src/lib/fields/RowCard.svelte +229 -0
- package/src/lib/fields/SelectField.svelte +43 -0
- package/src/lib/fields/SlugField.svelte +85 -0
- package/src/lib/fields/TextField.svelte +41 -0
- package/src/lib/fields/TextareaField.svelte +40 -0
- package/src/lib/fields/UploadField.svelte +26 -0
- package/src/lib/fields/registry.ts +41 -0
- package/src/lib/fields/relation/RelationField.svelte +256 -0
- package/src/lib/fields/relation/RelationPicker.svelte +246 -0
- package/src/lib/fields/relation/api.ts +66 -0
- package/src/lib/fields/rowDrag.svelte.ts +41 -0
- package/src/lib/fields/rows.ts +70 -0
- package/src/lib/fields/types.ts +35 -0
- package/src/lib/form/context.ts +26 -0
- package/src/lib/format.ts +106 -0
- package/src/lib/index.ts +20 -0
- package/src/lib/links.ts +32 -0
- package/src/lib/media/MediaPicker.svelte +327 -0
- package/src/lib/media/util.ts +36 -0
- package/src/lib/preview/BlocksPreview.svelte +37 -0
- package/src/lib/preview/LazyPreview.svelte +75 -0
- package/src/lib/preview/PreviewPane.svelte +98 -0
- package/src/lib/preview/blocks.ts +39 -0
- package/src/lib/preview/load.ts +21 -0
- package/src/lib/preview/relations.ts +70 -0
- package/src/lib/preview/types.ts +75 -0
- package/src/lib/preview/useRelation.svelte.ts +58 -0
- package/src/lib/theme.css +193 -0
- package/src/lib/upload/FileUpload.svelte +216 -0
- package/src/lib/views/Dashboard.svelte +64 -0
- package/src/lib/views/Edit.svelte +557 -0
- package/src/lib/views/FirstUser.svelte +63 -0
- package/src/lib/views/List.svelte +557 -0
- package/src/lib/views/Login.svelte +58 -0
- package/src/lib/views/Panel.svelte +175 -0
- package/src/lib/views/Versions.svelte +144 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Modal media library for one upload collection: search, "Load more" paging, an upload drop
|
|
3
|
+
// zone (new files are selected right away), single/multiple selection and Confirm/Cancel.
|
|
4
|
+
//
|
|
5
|
+
// Props are the collection slug plus, optionally, its upload rules (`mimeTypes`, `maxFileSize`)
|
|
6
|
+
// for client-side pre-validation. UploadField only knows the target slug, so it omits `upload`
|
|
7
|
+
// and the server remains the authority on what is accepted.
|
|
8
|
+
//
|
|
9
|
+
// `onselect` receives only the NEWLY chosen documents. `selectedIds` (already chosen elsewhere,
|
|
10
|
+
// e.g. in the field) are shown as "Added", can't be toggled in `multiple` mode and count toward `max`.
|
|
11
|
+
import { untrack } from 'svelte';
|
|
12
|
+
import { searchPage, type PickerDoc } from '../fields/relation/api';
|
|
13
|
+
import FileUpload from '../upload/FileUpload.svelte';
|
|
14
|
+
import { fileLabel, isImageMime, metaLine, thumbUrl } from './util';
|
|
15
|
+
|
|
16
|
+
let {
|
|
17
|
+
open = $bindable(false),
|
|
18
|
+
collection,
|
|
19
|
+
upload,
|
|
20
|
+
multiple = false,
|
|
21
|
+
max,
|
|
22
|
+
selectedIds = [],
|
|
23
|
+
onselect,
|
|
24
|
+
}: {
|
|
25
|
+
open?: boolean;
|
|
26
|
+
/** Upload collection slug. */
|
|
27
|
+
collection: string;
|
|
28
|
+
/** Upload rules of the collection, when known (enables client-side file checks). */
|
|
29
|
+
upload?: { mimeTypes: string[]; maxFileSize: number };
|
|
30
|
+
multiple?: boolean;
|
|
31
|
+
/** Maximum total selection, including `selectedIds`. */
|
|
32
|
+
max?: number;
|
|
33
|
+
selectedIds?: string[];
|
|
34
|
+
onselect: (docs: PickerDoc[]) => void;
|
|
35
|
+
} = $props();
|
|
36
|
+
|
|
37
|
+
const PAGE = 24;
|
|
38
|
+
const titleId = `kc-mp-${Math.random().toString(36).slice(2, 8)}`;
|
|
39
|
+
let dialog: HTMLDialogElement | undefined = $state();
|
|
40
|
+
|
|
41
|
+
let q = $state('');
|
|
42
|
+
let docs = $state<PickerDoc[]>([]);
|
|
43
|
+
let page = $state(1);
|
|
44
|
+
let hasNext = $state(false);
|
|
45
|
+
let loading = $state(false);
|
|
46
|
+
let error = $state('');
|
|
47
|
+
let chosen = $state<PickerDoc[]>([]);
|
|
48
|
+
|
|
49
|
+
const rules = $derived(upload ?? { mimeTypes: [], maxFileSize: Number.POSITIVE_INFINITY });
|
|
50
|
+
const taken = $derived(multiple ? selectedIds.length : 0);
|
|
51
|
+
const room = $derived(max === undefined ? Number.POSITIVE_INFINITY : Math.max(max - taken, 0));
|
|
52
|
+
const isChosen = (id: string) => chosen.some((d) => d.id === id);
|
|
53
|
+
|
|
54
|
+
// Keep the native dialog in sync with `open` (Escape / backdrop close updates it back).
|
|
55
|
+
$effect(() => {
|
|
56
|
+
if (!dialog) return;
|
|
57
|
+
if (open && !dialog.open) {
|
|
58
|
+
chosen = [];
|
|
59
|
+
q = '';
|
|
60
|
+
dialog.showModal();
|
|
61
|
+
} else if (!open && dialog.open) dialog.close();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
let seq = 0;
|
|
65
|
+
async function load(reset: boolean) {
|
|
66
|
+
const mine = ++seq;
|
|
67
|
+
loading = true;
|
|
68
|
+
error = '';
|
|
69
|
+
const next = reset ? 1 : page + 1;
|
|
70
|
+
try {
|
|
71
|
+
const res = await searchPage(collection, q, next, PAGE);
|
|
72
|
+
if (mine !== seq) return;
|
|
73
|
+
docs = reset ? res.docs : [...docs, ...res.docs.filter((d) => !docs.some((e) => e.id === d.id))];
|
|
74
|
+
page = next;
|
|
75
|
+
hasNext = res.hasNextPage;
|
|
76
|
+
} catch {
|
|
77
|
+
if (mine === seq) error = 'Could not load the media library.';
|
|
78
|
+
} finally {
|
|
79
|
+
if (mine === seq) loading = false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// (Re)load when opened or the search text changes (debounced).
|
|
84
|
+
$effect(() => {
|
|
85
|
+
if (!open) return;
|
|
86
|
+
const text = q;
|
|
87
|
+
const t = setTimeout(() => untrack(() => void load(true)), text ? 250 : 0);
|
|
88
|
+
return () => clearTimeout(t);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
function toggle(doc: PickerDoc) {
|
|
92
|
+
if (!multiple) {
|
|
93
|
+
chosen = isChosen(doc.id) ? [] : [doc];
|
|
94
|
+
} else if (isChosen(doc.id)) {
|
|
95
|
+
chosen = chosen.filter((d) => d.id !== doc.id);
|
|
96
|
+
} else if (chosen.length < room) {
|
|
97
|
+
chosen = [...chosen, doc];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function uploaded(raw: Record<string, unknown>) {
|
|
102
|
+
const doc: PickerDoc = {
|
|
103
|
+
id: String(raw.id),
|
|
104
|
+
title: String(raw.filename ?? raw.id),
|
|
105
|
+
url: typeof raw.url === 'string' ? raw.url : undefined,
|
|
106
|
+
mimeType: typeof raw.mimeType === 'string' ? raw.mimeType : undefined,
|
|
107
|
+
filename: typeof raw.filename === 'string' ? raw.filename : undefined,
|
|
108
|
+
width: typeof raw.width === 'number' ? raw.width : null,
|
|
109
|
+
height: typeof raw.height === 'number' ? raw.height : null,
|
|
110
|
+
filesize: typeof raw.filesize === 'number' ? raw.filesize : undefined,
|
|
111
|
+
};
|
|
112
|
+
docs = [doc, ...docs.filter((d) => d.id !== doc.id)];
|
|
113
|
+
if (!multiple) chosen = [doc];
|
|
114
|
+
else if (chosen.length < room) chosen = [...chosen, doc];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function confirm() {
|
|
118
|
+
if (chosen.length) onselect(chosen);
|
|
119
|
+
open = false;
|
|
120
|
+
}
|
|
121
|
+
</script>
|
|
122
|
+
|
|
123
|
+
<dialog
|
|
124
|
+
bind:this={dialog}
|
|
125
|
+
class="kc-mp"
|
|
126
|
+
aria-labelledby={titleId}
|
|
127
|
+
onclose={() => (open = false)}
|
|
128
|
+
onclick={(e) => {
|
|
129
|
+
if (e.target === dialog) open = false;
|
|
130
|
+
}}
|
|
131
|
+
>
|
|
132
|
+
{#if open}
|
|
133
|
+
<div class="kc-mp__body">
|
|
134
|
+
<header class="kc-mp__head">
|
|
135
|
+
<h2 id={titleId}>Media library</h2>
|
|
136
|
+
<button type="button" class="kc-btn kc-btn--ghost" aria-label="Close" onclick={() => (open = false)}>×</button>
|
|
137
|
+
</header>
|
|
138
|
+
|
|
139
|
+
<input
|
|
140
|
+
class="kc-input"
|
|
141
|
+
type="search"
|
|
142
|
+
placeholder="Search files"
|
|
143
|
+
aria-label="Search files"
|
|
144
|
+
bind:value={q}
|
|
145
|
+
/>
|
|
146
|
+
|
|
147
|
+
<FileUpload collection={{ slug: collection, upload: rules }} showCurrent={false} onuploaded={uploaded} />
|
|
148
|
+
|
|
149
|
+
{#if error}<p class="kc-mp__error" role="alert">{error}</p>{/if}
|
|
150
|
+
|
|
151
|
+
<ul class="kc-mp__grid" aria-label="Files" aria-busy={loading}>
|
|
152
|
+
{#each docs as doc (doc.id)}
|
|
153
|
+
{@const already = multiple && selectedIds.includes(doc.id)}
|
|
154
|
+
{@const picked = isChosen(doc.id)}
|
|
155
|
+
<li>
|
|
156
|
+
<button
|
|
157
|
+
type="button"
|
|
158
|
+
class="kc-card kc-mp__tile"
|
|
159
|
+
class:kc-mp__tile--picked={picked}
|
|
160
|
+
aria-pressed={picked || already}
|
|
161
|
+
disabled={already || (multiple && !picked && chosen.length >= room)}
|
|
162
|
+
onclick={() => toggle(doc)}
|
|
163
|
+
>
|
|
164
|
+
<span class="kc-mp__thumb">
|
|
165
|
+
{#if isImageMime(doc.mimeType) && doc.url}
|
|
166
|
+
<img src={thumbUrl(doc.url, 240)} alt="" loading="lazy" />
|
|
167
|
+
{:else}
|
|
168
|
+
<span class="kc-mp__file" aria-hidden="true">{fileLabel(doc.mimeType, doc.filename)}</span>
|
|
169
|
+
{/if}
|
|
170
|
+
</span>
|
|
171
|
+
<span class="kc-mp__name">{doc.filename ?? doc.title}</span>
|
|
172
|
+
<span class="kc-mp__meta">{already ? 'Added' : metaLine(doc)}</span>
|
|
173
|
+
</button>
|
|
174
|
+
</li>
|
|
175
|
+
{/each}
|
|
176
|
+
</ul>
|
|
177
|
+
{#if !loading && docs.length === 0 && !error}
|
|
178
|
+
<p class="kc-muted">{q ? 'No files match your search.' : 'No files yet. Upload one above.'}</p>
|
|
179
|
+
{/if}
|
|
180
|
+
{#if hasNext}
|
|
181
|
+
<button type="button" class="kc-btn kc-mp__more" disabled={loading} onclick={() => load(false)}>
|
|
182
|
+
{loading ? 'Loading...' : 'Load more'}
|
|
183
|
+
</button>
|
|
184
|
+
{/if}
|
|
185
|
+
|
|
186
|
+
<footer class="kc-mp__foot">
|
|
187
|
+
<span class="kc-muted" aria-live="polite">
|
|
188
|
+
{chosen.length} selected{max !== undefined ? ` (max ${max})` : ''}
|
|
189
|
+
</span>
|
|
190
|
+
<button type="button" class="kc-btn" onclick={() => (open = false)}>Cancel</button>
|
|
191
|
+
<button type="button" class="kc-btn kc-btn--primary" disabled={chosen.length === 0} onclick={confirm}>
|
|
192
|
+
Confirm
|
|
193
|
+
</button>
|
|
194
|
+
</footer>
|
|
195
|
+
</div>
|
|
196
|
+
{/if}
|
|
197
|
+
</dialog>
|
|
198
|
+
|
|
199
|
+
<style>
|
|
200
|
+
.kc-mp {
|
|
201
|
+
width: min(60rem, 94vw);
|
|
202
|
+
max-height: 90vh;
|
|
203
|
+
padding: 0;
|
|
204
|
+
border: 1px solid var(--kc-border);
|
|
205
|
+
border-radius: var(--kc-radius);
|
|
206
|
+
background: var(--kc-surface);
|
|
207
|
+
color: var(--kc-text);
|
|
208
|
+
}
|
|
209
|
+
.kc-mp::backdrop {
|
|
210
|
+
background: rgb(0 0 0 / 0.5);
|
|
211
|
+
}
|
|
212
|
+
.kc-mp__body {
|
|
213
|
+
display: grid;
|
|
214
|
+
gap: 0.75rem;
|
|
215
|
+
padding: 1rem;
|
|
216
|
+
}
|
|
217
|
+
/* Small screens: a full-screen sheet; the footer (Confirm/Cancel) stays in reach. */
|
|
218
|
+
@media (max-width: 640px) {
|
|
219
|
+
.kc-mp[open] {
|
|
220
|
+
width: 100vw;
|
|
221
|
+
max-width: 100vw;
|
|
222
|
+
height: 100dvh;
|
|
223
|
+
max-height: 100dvh;
|
|
224
|
+
margin: 0;
|
|
225
|
+
border: 0;
|
|
226
|
+
border-radius: 0;
|
|
227
|
+
}
|
|
228
|
+
.kc-mp__body {
|
|
229
|
+
min-height: 100%;
|
|
230
|
+
align-content: start;
|
|
231
|
+
grid-auto-rows: auto;
|
|
232
|
+
padding: 0.75rem;
|
|
233
|
+
}
|
|
234
|
+
.kc-mp__grid {
|
|
235
|
+
grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
|
|
236
|
+
}
|
|
237
|
+
.kc-mp__foot {
|
|
238
|
+
position: sticky;
|
|
239
|
+
bottom: 0;
|
|
240
|
+
align-self: end;
|
|
241
|
+
margin: 0 -0.75rem -0.75rem;
|
|
242
|
+
padding: 0.75rem;
|
|
243
|
+
background: var(--kc-surface);
|
|
244
|
+
border-top: 1px solid var(--kc-border);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
.kc-mp__head {
|
|
248
|
+
display: flex;
|
|
249
|
+
align-items: center;
|
|
250
|
+
justify-content: space-between;
|
|
251
|
+
}
|
|
252
|
+
.kc-mp__head h2 {
|
|
253
|
+
margin: 0;
|
|
254
|
+
font-size: 1.1rem;
|
|
255
|
+
}
|
|
256
|
+
.kc-mp__error {
|
|
257
|
+
margin: 0;
|
|
258
|
+
color: var(--kc-danger);
|
|
259
|
+
}
|
|
260
|
+
.kc-mp__grid {
|
|
261
|
+
list-style: none;
|
|
262
|
+
margin: 0;
|
|
263
|
+
padding: 0;
|
|
264
|
+
display: grid;
|
|
265
|
+
grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
|
|
266
|
+
gap: 0.5rem;
|
|
267
|
+
}
|
|
268
|
+
.kc-mp__tile {
|
|
269
|
+
width: 100%;
|
|
270
|
+
display: grid;
|
|
271
|
+
gap: 0.15rem;
|
|
272
|
+
padding: 0.4rem;
|
|
273
|
+
text-align: left;
|
|
274
|
+
cursor: pointer;
|
|
275
|
+
font: inherit;
|
|
276
|
+
color: inherit;
|
|
277
|
+
}
|
|
278
|
+
.kc-mp__tile:disabled {
|
|
279
|
+
opacity: 0.55;
|
|
280
|
+
cursor: not-allowed;
|
|
281
|
+
}
|
|
282
|
+
.kc-mp__tile--picked {
|
|
283
|
+
border-color: var(--kc-accent);
|
|
284
|
+
outline: 2px solid var(--kc-accent);
|
|
285
|
+
}
|
|
286
|
+
.kc-mp__thumb {
|
|
287
|
+
display: grid;
|
|
288
|
+
place-items: center;
|
|
289
|
+
aspect-ratio: 1;
|
|
290
|
+
overflow: hidden;
|
|
291
|
+
background: var(--kc-bg);
|
|
292
|
+
border-radius: calc(var(--kc-radius) - 2px);
|
|
293
|
+
}
|
|
294
|
+
.kc-mp__thumb img {
|
|
295
|
+
width: 100%;
|
|
296
|
+
height: 100%;
|
|
297
|
+
object-fit: cover;
|
|
298
|
+
}
|
|
299
|
+
.kc-mp__file {
|
|
300
|
+
font-weight: 700;
|
|
301
|
+
color: var(--kc-muted);
|
|
302
|
+
}
|
|
303
|
+
.kc-mp__name {
|
|
304
|
+
font-size: 0.8rem;
|
|
305
|
+
font-weight: 600;
|
|
306
|
+
overflow: hidden;
|
|
307
|
+
text-overflow: ellipsis;
|
|
308
|
+
white-space: nowrap;
|
|
309
|
+
}
|
|
310
|
+
.kc-mp__meta {
|
|
311
|
+
font-size: 0.7rem;
|
|
312
|
+
color: var(--kc-muted);
|
|
313
|
+
min-height: 1em;
|
|
314
|
+
}
|
|
315
|
+
.kc-mp__more {
|
|
316
|
+
justify-self: center;
|
|
317
|
+
}
|
|
318
|
+
.kc-mp__foot {
|
|
319
|
+
display: flex;
|
|
320
|
+
align-items: center;
|
|
321
|
+
justify-content: flex-end;
|
|
322
|
+
gap: 0.5rem;
|
|
323
|
+
}
|
|
324
|
+
.kc-mp__foot > span {
|
|
325
|
+
margin-right: auto;
|
|
326
|
+
}
|
|
327
|
+
</style>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Shared helpers for the media library grid and the media picker.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Thumbnail URL for a stored file. For now this returns the original URL.
|
|
5
|
+
* TODO(Phase 2): decide on Cloudflare Image Transformations and return
|
|
6
|
+
* `/cdn-cgi/image/width=${width},fit=cover,format=auto/${url}` here.
|
|
7
|
+
*/
|
|
8
|
+
export function thumbUrl(url: string, width: number): string {
|
|
9
|
+
void width;
|
|
10
|
+
return url;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function formatSize(n: number): string {
|
|
14
|
+
if (n < 1024) return `${n} B`;
|
|
15
|
+
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
|
|
16
|
+
return `${(n / 1024 / 1024).toFixed(1)} MB`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isImageMime(mime: unknown): boolean {
|
|
20
|
+
return typeof mime === 'string' && mime.startsWith('image/');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Short file-type label shown on non-image tiles. */
|
|
24
|
+
export function fileLabel(mime: unknown, filename: unknown): string {
|
|
25
|
+
if (typeof filename === 'string' && filename.includes('.')) return filename.split('.').pop()!.slice(0, 5).toUpperCase();
|
|
26
|
+
if (typeof mime === 'string' && mime.includes('/')) return mime.split('/')[1]!.slice(0, 5).toUpperCase();
|
|
27
|
+
return 'FILE';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function metaLine(input: object): string {
|
|
31
|
+
const doc = input as { filesize?: unknown; width?: unknown; height?: unknown };
|
|
32
|
+
const parts: string[] = [];
|
|
33
|
+
if (typeof doc.filesize === 'number') parts.push(formatSize(doc.filesize));
|
|
34
|
+
if (typeof doc.width === 'number' && typeof doc.height === 'number') parts.push(`${doc.width}×${doc.height}`);
|
|
35
|
+
return parts.join(' · ');
|
|
36
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Default preview: every block of every `blocks` field, in document order. Blocks without a
|
|
3
|
+
// registered preview render as a labelled placeholder.
|
|
4
|
+
import type { AdminPreviews } from './types';
|
|
5
|
+
import type { BlockEntry } from './blocks';
|
|
6
|
+
import LazyPreview from './LazyPreview.svelte';
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
entries,
|
|
10
|
+
loaders,
|
|
11
|
+
doc,
|
|
12
|
+
}: {
|
|
13
|
+
entries: BlockEntry[];
|
|
14
|
+
loaders: NonNullable<AdminPreviews['blocks']>;
|
|
15
|
+
doc: Record<string, unknown>;
|
|
16
|
+
} = $props();
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
{#each entries as entry (entry.key)}
|
|
20
|
+
{@const loader = loaders[entry.block.blockType]}
|
|
21
|
+
{#if loader}
|
|
22
|
+
<LazyPreview {loader} props={{ block: entry.block, doc }} label={`${entry.label} preview`} />
|
|
23
|
+
{:else}
|
|
24
|
+
<div class="placeholder" data-testid="block-placeholder">{entry.label}</div>
|
|
25
|
+
{/if}
|
|
26
|
+
{/each}
|
|
27
|
+
|
|
28
|
+
<style>
|
|
29
|
+
.placeholder {
|
|
30
|
+
padding: 0.75rem 1rem;
|
|
31
|
+
margin: 0.5rem 0;
|
|
32
|
+
border: 1px dashed var(--kc-border);
|
|
33
|
+
border-radius: var(--kc-radius);
|
|
34
|
+
color: var(--kc-muted);
|
|
35
|
+
font: 13px/1.4 var(--kc-font);
|
|
36
|
+
}
|
|
37
|
+
</style>
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Loads a preview component lazily and renders it. `$effect` never runs on the server, so SSR
|
|
3
|
+
// only ever renders the placeholder and no preview code runs there. Load and render errors
|
|
4
|
+
// show inline and never reach the form.
|
|
5
|
+
import { loadComponent, type AnyComponent, type AnyLoader } from './load';
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
loader,
|
|
9
|
+
props,
|
|
10
|
+
label = 'preview',
|
|
11
|
+
}: {
|
|
12
|
+
loader: AnyLoader;
|
|
13
|
+
props: Record<string, unknown>;
|
|
14
|
+
label?: string;
|
|
15
|
+
} = $props();
|
|
16
|
+
|
|
17
|
+
let Comp = $state.raw<AnyComponent | undefined>();
|
|
18
|
+
let loadError = $state<string | undefined>();
|
|
19
|
+
|
|
20
|
+
$effect(() => {
|
|
21
|
+
const l = loader;
|
|
22
|
+
let live = true;
|
|
23
|
+
Comp = undefined;
|
|
24
|
+
loadError = undefined;
|
|
25
|
+
loadComponent(l).then(
|
|
26
|
+
(c) => {
|
|
27
|
+
if (live) Comp = c;
|
|
28
|
+
},
|
|
29
|
+
(e: unknown) => {
|
|
30
|
+
if (live) loadError = e instanceof Error ? e.message : String(e);
|
|
31
|
+
},
|
|
32
|
+
);
|
|
33
|
+
return () => {
|
|
34
|
+
live = false;
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
{#if loadError}
|
|
40
|
+
<div class="err" role="alert">Couldn't load the {label}: {loadError}</div>
|
|
41
|
+
{:else if Comp}
|
|
42
|
+
<svelte:boundary>
|
|
43
|
+
<Comp {...props} />
|
|
44
|
+
{#snippet failed(error, reset)}
|
|
45
|
+
<div class="err" role="alert">
|
|
46
|
+
The {label} failed to render: {error instanceof Error ? error.message : String(error)}
|
|
47
|
+
<button type="button" class="retry" onclick={reset}>Retry</button>
|
|
48
|
+
</div>
|
|
49
|
+
{/snippet}
|
|
50
|
+
</svelte:boundary>
|
|
51
|
+
{:else}
|
|
52
|
+
<p class="loading" role="status">Loading preview…</p>
|
|
53
|
+
{/if}
|
|
54
|
+
|
|
55
|
+
<style>
|
|
56
|
+
.err {
|
|
57
|
+
padding: 0.6rem 0.75rem;
|
|
58
|
+
border: 1px solid var(--kc-danger);
|
|
59
|
+
border-radius: var(--kc-radius);
|
|
60
|
+
color: var(--kc-danger);
|
|
61
|
+
background: var(--kc-surface);
|
|
62
|
+
font: 13px/1.4 var(--kc-font);
|
|
63
|
+
}
|
|
64
|
+
.retry {
|
|
65
|
+
margin-left: 0.5rem;
|
|
66
|
+
font: inherit;
|
|
67
|
+
cursor: pointer;
|
|
68
|
+
}
|
|
69
|
+
.loading {
|
|
70
|
+
margin: 0;
|
|
71
|
+
padding: 1rem;
|
|
72
|
+
color: var(--kc-muted);
|
|
73
|
+
font: 13px/1.4 var(--kc-font);
|
|
74
|
+
}
|
|
75
|
+
</style>
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// The preview pane: header with the desktop/mobile width toggle, and a scrollable body. The
|
|
3
|
+
// body holds `.kitcms-preview`, which resets the admin's inherited typography/colours
|
|
4
|
+
// (`all: initial`) so site styles that preview components bring along aren't affected by the
|
|
5
|
+
// admin theme. The preview always renders on a light canvas (system `Canvas`/`CanvasText`).
|
|
6
|
+
// Preview components must treat `doc`/`block` as READ-ONLY: they are the live form state.
|
|
7
|
+
import type { Snippet } from 'svelte';
|
|
8
|
+
|
|
9
|
+
let { children }: { children: Snippet } = $props();
|
|
10
|
+
let width = $state<'desktop' | 'mobile'>('desktop');
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<aside class="pane" aria-label="Live preview" data-testid="preview-pane">
|
|
14
|
+
<div class="bar">
|
|
15
|
+
<strong>Preview</strong>
|
|
16
|
+
<span class="spacer"></span>
|
|
17
|
+
<div class="seg" role="group" aria-label="Preview width">
|
|
18
|
+
<button
|
|
19
|
+
type="button"
|
|
20
|
+
class="kc-btn kc-btn--ghost"
|
|
21
|
+
aria-pressed={width === 'desktop'}
|
|
22
|
+
data-testid="preview-desktop"
|
|
23
|
+
onclick={() => (width = 'desktop')}>Desktop</button
|
|
24
|
+
>
|
|
25
|
+
<button
|
|
26
|
+
type="button"
|
|
27
|
+
class="kc-btn kc-btn--ghost"
|
|
28
|
+
aria-pressed={width === 'mobile'}
|
|
29
|
+
data-testid="preview-mobile"
|
|
30
|
+
onclick={() => (width = 'mobile')}>Mobile</button
|
|
31
|
+
>
|
|
32
|
+
</div>
|
|
33
|
+
</div>
|
|
34
|
+
<div class="scroll">
|
|
35
|
+
<div class="frame" data-width={width}>
|
|
36
|
+
<div class="kitcms-preview">{@render children()}</div>
|
|
37
|
+
</div>
|
|
38
|
+
</div>
|
|
39
|
+
</aside>
|
|
40
|
+
|
|
41
|
+
<style>
|
|
42
|
+
.pane {
|
|
43
|
+
display: grid;
|
|
44
|
+
grid-template-rows: auto minmax(0, 1fr);
|
|
45
|
+
border: 1px solid var(--kc-border);
|
|
46
|
+
border-radius: var(--kc-radius);
|
|
47
|
+
background: var(--kc-surface);
|
|
48
|
+
overflow: hidden;
|
|
49
|
+
max-height: calc(100vh - 2rem);
|
|
50
|
+
min-height: 12rem;
|
|
51
|
+
}
|
|
52
|
+
.bar {
|
|
53
|
+
display: flex;
|
|
54
|
+
align-items: center;
|
|
55
|
+
gap: 0.5rem;
|
|
56
|
+
padding: 0.4rem 0.75rem;
|
|
57
|
+
border-bottom: 1px solid var(--kc-border);
|
|
58
|
+
}
|
|
59
|
+
.spacer {
|
|
60
|
+
flex: 1;
|
|
61
|
+
}
|
|
62
|
+
.seg {
|
|
63
|
+
display: inline-flex;
|
|
64
|
+
gap: 0.25rem;
|
|
65
|
+
}
|
|
66
|
+
.seg button[aria-pressed='true'] {
|
|
67
|
+
background: var(--kc-accent);
|
|
68
|
+
color: var(--kc-accent-text);
|
|
69
|
+
}
|
|
70
|
+
.scroll {
|
|
71
|
+
overflow: auto;
|
|
72
|
+
padding: 0.75rem;
|
|
73
|
+
background: var(--kc-bg);
|
|
74
|
+
}
|
|
75
|
+
.frame {
|
|
76
|
+
width: 100%;
|
|
77
|
+
margin: 0 auto;
|
|
78
|
+
border: 1px solid var(--kc-border);
|
|
79
|
+
border-radius: var(--kc-radius);
|
|
80
|
+
overflow: hidden;
|
|
81
|
+
background: var(--kc-surface);
|
|
82
|
+
}
|
|
83
|
+
.frame[data-width='mobile'] {
|
|
84
|
+
width: 390px;
|
|
85
|
+
max-width: 100%;
|
|
86
|
+
}
|
|
87
|
+
/* Resets inherited admin styles for the previewed site content. */
|
|
88
|
+
.kitcms-preview {
|
|
89
|
+
all: initial;
|
|
90
|
+
display: block;
|
|
91
|
+
background: Canvas;
|
|
92
|
+
color: CanvasText;
|
|
93
|
+
font-family: system-ui, sans-serif;
|
|
94
|
+
line-height: 1.5;
|
|
95
|
+
min-height: 6rem;
|
|
96
|
+
container-type: inline-size;
|
|
97
|
+
}
|
|
98
|
+
</style>
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Finds the blocks of a document's live values, in document order (also inside group/array/blocks).
|
|
2
|
+
import type { ClientField } from '@kitcms/core/client';
|
|
3
|
+
|
|
4
|
+
export interface BlockEntry {
|
|
5
|
+
/** The live block object (`{ id, blockType, ...fields }`), taken from the form state. */
|
|
6
|
+
block: Record<string, unknown> & { blockType: string };
|
|
7
|
+
/** The block's label. */
|
|
8
|
+
label: string;
|
|
9
|
+
key: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function collectBlocks(fields: ClientField[], values: Record<string, unknown>, path = ''): BlockEntry[] {
|
|
13
|
+
const out: BlockEntry[] = [];
|
|
14
|
+
for (const f of fields) {
|
|
15
|
+
const v = values?.[f.name];
|
|
16
|
+
const at = `${path}${f.name}`;
|
|
17
|
+
if (f.type === 'group') {
|
|
18
|
+
if (v && typeof v === 'object') out.push(...collectBlocks(f.fields, v as Record<string, unknown>, `${at}.`));
|
|
19
|
+
} else if (f.type === 'array') {
|
|
20
|
+
if (Array.isArray(v)) {
|
|
21
|
+
v.forEach((row, i) => {
|
|
22
|
+
if (row && typeof row === 'object') out.push(...collectBlocks(f.fields, row, `${at}.${i}.`));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
} else if (f.type === 'blocks' && Array.isArray(v)) {
|
|
26
|
+
v.forEach((row, i) => {
|
|
27
|
+
if (!row || typeof row !== 'object' || typeof row.blockType !== 'string') return;
|
|
28
|
+
const def = f.blocks.find((b) => b.slug === row.blockType);
|
|
29
|
+
out.push({
|
|
30
|
+
block: row,
|
|
31
|
+
label: def?.labels.singular ?? row.blockType,
|
|
32
|
+
key: typeof row.id === 'string' && row.id ? row.id : `${at}.${i}`,
|
|
33
|
+
});
|
|
34
|
+
if (def) out.push(...collectBlocks(def.fields, row, `${at}.${i}.`));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Lazy component loading with a per-loader cache. Browser only (called from effects).
|
|
2
|
+
import type { Component } from 'svelte';
|
|
3
|
+
|
|
4
|
+
// Preview components have caller-defined props, so the loader is typed loosely here.
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
export type AnyComponent = Component<any>;
|
|
7
|
+
export type AnyLoader = () => Promise<{ default: AnyComponent }>;
|
|
8
|
+
|
|
9
|
+
const cache = new WeakMap<object, Promise<AnyComponent>>();
|
|
10
|
+
|
|
11
|
+
export function loadComponent(loader: AnyLoader): Promise<AnyComponent> {
|
|
12
|
+
let p = cache.get(loader);
|
|
13
|
+
if (!p) {
|
|
14
|
+
p = loader().then((m) => m.default);
|
|
15
|
+
const mine = p;
|
|
16
|
+
// A failed import is not cached, so a retry (or a fixed HMR) can succeed.
|
|
17
|
+
mine.catch(() => cache.delete(loader));
|
|
18
|
+
cache.set(loader, mine);
|
|
19
|
+
}
|
|
20
|
+
return p;
|
|
21
|
+
}
|