@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.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -2
  3. package/package.json +47 -3
  4. package/src/index.ts +1 -0
  5. package/src/lib/Admin.svelte +345 -0
  6. package/src/lib/fields/ArrayField.svelte +134 -0
  7. package/src/lib/fields/BlocksField.svelte +201 -0
  8. package/src/lib/fields/CheckboxField.svelte +48 -0
  9. package/src/lib/fields/DateField.svelte +70 -0
  10. package/src/lib/fields/EmailField.svelte +39 -0
  11. package/src/lib/fields/FieldShell.svelte +70 -0
  12. package/src/lib/fields/GroupField.svelte +40 -0
  13. package/src/lib/fields/JsonField.svelte +74 -0
  14. package/src/lib/fields/NumberField.svelte +51 -0
  15. package/src/lib/fields/RelationshipField.svelte +11 -0
  16. package/src/lib/fields/RenderFields.svelte +32 -0
  17. package/src/lib/fields/RenderNodes.svelte +147 -0
  18. package/src/lib/fields/RichTextField.svelte +310 -0
  19. package/src/lib/fields/RowCard.svelte +229 -0
  20. package/src/lib/fields/SelectField.svelte +43 -0
  21. package/src/lib/fields/SlugField.svelte +85 -0
  22. package/src/lib/fields/TextField.svelte +41 -0
  23. package/src/lib/fields/TextareaField.svelte +40 -0
  24. package/src/lib/fields/UploadField.svelte +26 -0
  25. package/src/lib/fields/registry.ts +41 -0
  26. package/src/lib/fields/relation/RelationField.svelte +256 -0
  27. package/src/lib/fields/relation/RelationPicker.svelte +246 -0
  28. package/src/lib/fields/relation/api.ts +66 -0
  29. package/src/lib/fields/rowDrag.svelte.ts +41 -0
  30. package/src/lib/fields/rows.ts +70 -0
  31. package/src/lib/fields/types.ts +35 -0
  32. package/src/lib/form/context.ts +26 -0
  33. package/src/lib/format.ts +106 -0
  34. package/src/lib/index.ts +20 -0
  35. package/src/lib/links.ts +32 -0
  36. package/src/lib/media/MediaPicker.svelte +327 -0
  37. package/src/lib/media/util.ts +36 -0
  38. package/src/lib/preview/BlocksPreview.svelte +37 -0
  39. package/src/lib/preview/LazyPreview.svelte +75 -0
  40. package/src/lib/preview/PreviewPane.svelte +98 -0
  41. package/src/lib/preview/blocks.ts +39 -0
  42. package/src/lib/preview/load.ts +21 -0
  43. package/src/lib/preview/relations.ts +70 -0
  44. package/src/lib/preview/types.ts +75 -0
  45. package/src/lib/preview/useRelation.svelte.ts +58 -0
  46. package/src/lib/theme.css +193 -0
  47. package/src/lib/upload/FileUpload.svelte +216 -0
  48. package/src/lib/views/Dashboard.svelte +64 -0
  49. package/src/lib/views/Edit.svelte +557 -0
  50. package/src/lib/views/FirstUser.svelte +63 -0
  51. package/src/lib/views/List.svelte +557 -0
  52. package/src/lib/views/Login.svelte +58 -0
  53. package/src/lib/views/Panel.svelte +175 -0
  54. package/src/lib/views/Versions.svelte +144 -0
@@ -0,0 +1,70 @@
1
+ // The batching + caching core behind `useRelation` (plain module, no runes, so it is testable).
2
+ // Lookups requested in the same microtask for one collection become one `lookupDocs` call. Results
3
+ // (including "not found") are cached for the page's lifetime, so repeated ids never refetch.
4
+ import { lookupDocs, type PickerDoc } from '../fields/relation/api';
5
+
6
+ type Entry = { doc: PickerDoc | null } | { pending: Promise<PickerDoc | null> };
7
+
8
+ const cache = new Map<string, Entry>();
9
+ const waiters = new Map<string, Map<string, Array<{ ok: (d: PickerDoc | null) => void; fail: (e: unknown) => void }>>>();
10
+ let scheduled = false;
11
+
12
+ const key = (collection: string, id: string) => `${collection}\u0000${id}`;
13
+
14
+ /** The cached doc, synchronously: `undefined` when not loaded yet, `null` when it doesn't exist. */
15
+ export function peekRelation(collection: string, id: string): PickerDoc | null | undefined {
16
+ const e = cache.get(key(collection, id));
17
+ return e && 'doc' in e ? e.doc : undefined;
18
+ }
19
+
20
+ async function flush() {
21
+ scheduled = false;
22
+ const batches = [...waiters];
23
+ waiters.clear();
24
+ await Promise.all(
25
+ batches.map(async ([collection, byId]) => {
26
+ const ids = [...byId.keys()];
27
+ try {
28
+ const docs = await lookupDocs(collection, ids);
29
+ const found = new Map(docs.map((d) => [d.id, d]));
30
+ for (const [id, list] of byId) {
31
+ const doc = found.get(id) ?? null;
32
+ cache.set(key(collection, id), { doc });
33
+ for (const w of list) w.ok(doc);
34
+ }
35
+ } catch (e) {
36
+ for (const [id, list] of byId) {
37
+ cache.delete(key(collection, id)); // allow a retry later
38
+ for (const w of list) w.fail(e);
39
+ }
40
+ }
41
+ }),
42
+ );
43
+ }
44
+
45
+ /** Resolve one doc by id (cached, batched per collection per microtask). */
46
+ export function fetchRelation(collection: string, id: string): Promise<PickerDoc | null> {
47
+ const k = key(collection, id);
48
+ const hit = cache.get(k);
49
+ if (hit) return 'doc' in hit ? Promise.resolve(hit.doc) : hit.pending;
50
+ const pending = new Promise<PickerDoc | null>((ok, fail) => {
51
+ let byId = waiters.get(collection);
52
+ if (!byId) waiters.set(collection, (byId = new Map()));
53
+ let list = byId.get(id);
54
+ if (!list) byId.set(id, (list = []));
55
+ list.push({ ok, fail });
56
+ });
57
+ cache.set(k, { pending });
58
+ if (!scheduled) {
59
+ scheduled = true;
60
+ queueMicrotask(() => void flush());
61
+ }
62
+ return pending;
63
+ }
64
+
65
+ /** Test helper: forget everything. */
66
+ export function clearRelationCache() {
67
+ cache.clear();
68
+ waiters.clear();
69
+ scheduled = false;
70
+ }
@@ -0,0 +1,75 @@
1
+ // THE LIVE PREVIEW CONTRACT (plan §13.5, Decision 23).
2
+ //
3
+ // Preview components are browser code; the kitcms config is server code. So previews are
4
+ // registered in a client-safe file, `src/kitcms.admin.ts`, and handed to `<Admin admin={...}>`
5
+ // by the generated `+page.svelte`:
6
+ //
7
+ // // src/kitcms.admin.ts
8
+ // import { defineAdmin } from '@kitcms/ui';
9
+ // export default defineAdmin({
10
+ // previews: {
11
+ // collections: { pages: () => import('#lib/previews/Page.svelte') },
12
+ // globals: { site: () => import('#lib/previews/Site.svelte') },
13
+ // blocks: { hero: () => import('#lib/blocks/Hero.svelte') },
14
+ // },
15
+ // });
16
+ //
17
+ // Components are loaded lazily (dynamic import), only on edit views that need them.
18
+ //
19
+ // Props:
20
+ // - collection/global preview: `{ doc }`, the LIVE form state of the document (a Svelte `$state`
21
+ // object: it updates on every keystroke, with no delay). Read-only by contract: never write it.
22
+ // - block preview: `{ block, doc }`: the block's live values (with `id` and `blockType`) and the
23
+ // whole document's live values.
24
+ // Relationship/upload values are ids. Resolve them with `useRelation(collection, () => id)`.
25
+ //
26
+ // The edit view shows the preview pane when the collection/global has a preview, or when any
27
+ // `blocks` field of the document has a block with a preview (a default preview then renders the
28
+ // blocks in order, using each block's component, and a labelled placeholder for the others).
29
+ // Previews render in the admin DOM inside `.kitcms-preview` (no iframe), with a desktop/mobile
30
+ // width toggle (100% / 390px). Site CSS that previews need must be imported by the preview
31
+ // components themselves and should be scoped (Svelte component styles are).
32
+
33
+ import type { Component } from 'svelte';
34
+ import type { CollectionSlug, GlobalSlug, RegisteredConfig } from '@kitcms/core';
35
+
36
+ /** A lazily imported Svelte component (`() => import('./X.svelte')`). */
37
+ export type LazyComponent<Props extends Record<string, unknown>> = () => Promise<{
38
+ default: Component<Props>;
39
+ }>;
40
+
41
+ /** Live form values of a document (partial while editing). */
42
+ export type PreviewDoc = Record<string, unknown>;
43
+
44
+ export interface DocPreviewProps {
45
+ /** The live form state. Read-only. */
46
+ doc: PreviewDoc;
47
+ [key: string]: unknown;
48
+ }
49
+
50
+ export interface BlockPreviewProps {
51
+ /** The block's live values: `{ id, blockType, ...fields }`. Read-only. */
52
+ block: PreviewDoc & { id?: string; blockType: string };
53
+ /** The whole document's live values. Read-only. */
54
+ doc: PreviewDoc;
55
+ [key: string]: unknown;
56
+ }
57
+
58
+ type Slugs<S> = string extends S ? string : S & string;
59
+
60
+ export interface AdminPreviews {
61
+ collections?: Partial<Record<Slugs<CollectionSlug<RegisteredConfig>>, LazyComponent<DocPreviewProps>>>;
62
+ globals?: Partial<Record<Slugs<GlobalSlug<RegisteredConfig>>, LazyComponent<DocPreviewProps>>>;
63
+ /** By block slug (shared by every `blocks` field that uses the block). */
64
+ blocks?: Record<string, LazyComponent<BlockPreviewProps>>;
65
+ }
66
+
67
+ /** Client-side admin options. Everything here is shipped to the browser. */
68
+ export interface AdminClientConfig {
69
+ previews?: AdminPreviews;
70
+ }
71
+
72
+ /** Declares the client-side admin options in `src/kitcms.admin.ts` (identity at runtime). */
73
+ export function defineAdmin(config: AdminClientConfig): AdminClientConfig {
74
+ return config;
75
+ }
@@ -0,0 +1,58 @@
1
+ // `useRelation(collection, () => id)`: resolves a relationship/upload id inside a preview component
2
+ // to its picker doc (`title`, and for uploads `url`, `width`, `height`, `mimeType`). Reactive:
3
+ // re-resolves when the id changes. Lookups are batched per collection and cached (./relations.ts).
4
+ // Call it during component initialisation (it uses an effect), never on the server (no effect runs).
5
+ import type { PickerDoc } from '../fields/relation/api';
6
+ import { fetchRelation, peekRelation } from './relations';
7
+
8
+ export interface RelationState {
9
+ /** The resolved doc; `undefined` while loading, when there is no id, or when it doesn't exist. */
10
+ readonly current: PickerDoc | undefined;
11
+ readonly loading: boolean;
12
+ }
13
+
14
+ export function useRelation(collection: string, id: () => string | null | undefined): RelationState {
15
+ let current = $state.raw<PickerDoc | undefined>();
16
+ let loading = $state(false);
17
+
18
+ $effect(() => {
19
+ const wanted = id();
20
+ if (!wanted) {
21
+ current = undefined;
22
+ loading = false;
23
+ return;
24
+ }
25
+ const cached = peekRelation(collection, wanted);
26
+ if (cached !== undefined) {
27
+ current = cached ?? undefined;
28
+ loading = false;
29
+ return;
30
+ }
31
+ let live = true;
32
+ loading = true;
33
+ fetchRelation(collection, wanted).then(
34
+ (doc) => {
35
+ if (!live) return;
36
+ current = doc ?? undefined;
37
+ loading = false;
38
+ },
39
+ () => {
40
+ if (!live) return;
41
+ current = undefined;
42
+ loading = false;
43
+ },
44
+ );
45
+ return () => {
46
+ live = false;
47
+ };
48
+ });
49
+
50
+ return {
51
+ get current() {
52
+ return current;
53
+ },
54
+ get loading() {
55
+ return loading;
56
+ },
57
+ };
58
+ }
@@ -0,0 +1,193 @@
1
+ /* Admin theme. Everything is scoped under .kitcms-admin so it never leaks into the site. */
2
+ .kitcms-admin {
3
+ --kc-bg: #f6f7f9;
4
+ --kc-surface: #ffffff;
5
+ --kc-text: #14171f;
6
+ --kc-muted: #667085;
7
+ --kc-border: #dfe3ea;
8
+ --kc-accent: #3b5bdb;
9
+ --kc-accent-text: #ffffff;
10
+ --kc-danger: #c92a2a;
11
+ --kc-success: #2b8a3e;
12
+ --kc-radius: 8px;
13
+ --kc-font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
14
+
15
+ font-family: var(--kc-font);
16
+ font-size: 14px;
17
+ line-height: 1.5;
18
+ color: var(--kc-text);
19
+ background: var(--kc-bg);
20
+ min-height: 100vh;
21
+ box-sizing: border-box;
22
+ }
23
+ @media (prefers-color-scheme: dark) {
24
+ .kitcms-admin {
25
+ --kc-bg: #0f1115;
26
+ --kc-surface: #181b22;
27
+ --kc-text: #e8eaf0;
28
+ --kc-muted: #98a2b3;
29
+ --kc-border: #2b303b;
30
+ --kc-accent: #748ffc;
31
+ --kc-accent-text: #0f1115;
32
+ --kc-danger: #ff8787;
33
+ --kc-success: #69db7c;
34
+ color-scheme: dark;
35
+ }
36
+ }
37
+ .kitcms-admin *,
38
+ .kitcms-admin *::before,
39
+ .kitcms-admin *::after {
40
+ box-sizing: border-box;
41
+ }
42
+ .kitcms-admin a {
43
+ color: var(--kc-accent);
44
+ }
45
+ .kitcms-admin h1,
46
+ .kitcms-admin h2,
47
+ .kitcms-admin h3 {
48
+ margin: 0;
49
+ line-height: 1.25;
50
+ }
51
+ .kitcms-admin :focus-visible {
52
+ outline: 2px solid var(--kc-accent);
53
+ outline-offset: 2px;
54
+ }
55
+
56
+ .kitcms-admin .kc-input {
57
+ width: 100%;
58
+ font: inherit;
59
+ color: var(--kc-text);
60
+ background: var(--kc-surface);
61
+ border: 1px solid var(--kc-border);
62
+ border-radius: var(--kc-radius);
63
+ padding: 0.4rem 0.6rem;
64
+ min-height: 2.25rem;
65
+ }
66
+ .kitcms-admin textarea.kc-input {
67
+ min-height: 5rem;
68
+ resize: vertical;
69
+ }
70
+ .kitcms-admin .kc-input:disabled {
71
+ opacity: 0.7;
72
+ cursor: not-allowed;
73
+ }
74
+ .kitcms-admin .kc-input[aria-invalid='true'] {
75
+ border-color: var(--kc-danger);
76
+ }
77
+
78
+ .kitcms-admin .kc-btn {
79
+ display: inline-flex;
80
+ align-items: center;
81
+ justify-content: center;
82
+ gap: 0.4rem;
83
+ font: inherit;
84
+ font-weight: 500;
85
+ text-decoration: none;
86
+ cursor: pointer;
87
+ color: var(--kc-text);
88
+ background: var(--kc-surface);
89
+ border: 1px solid var(--kc-border);
90
+ border-radius: var(--kc-radius);
91
+ padding: 0.4rem 0.9rem;
92
+ min-height: 2.25rem;
93
+ }
94
+ .kitcms-admin .kc-btn:hover {
95
+ border-color: var(--kc-muted);
96
+ }
97
+ .kitcms-admin .kc-btn:disabled {
98
+ opacity: 0.6;
99
+ cursor: not-allowed;
100
+ }
101
+ .kitcms-admin .kc-btn--primary {
102
+ color: var(--kc-accent-text);
103
+ background: var(--kc-accent);
104
+ border-color: var(--kc-accent);
105
+ }
106
+ .kitcms-admin .kc-btn--primary:hover {
107
+ filter: brightness(1.08);
108
+ border-color: var(--kc-accent);
109
+ }
110
+ .kitcms-admin .kc-btn--danger {
111
+ color: var(--kc-danger);
112
+ border-color: var(--kc-danger);
113
+ background: transparent;
114
+ }
115
+ .kitcms-admin .kc-btn--danger:hover {
116
+ border-color: var(--kc-danger);
117
+ background: color-mix(in srgb, var(--kc-danger) 10%, transparent);
118
+ }
119
+ .kitcms-admin .kc-btn--ghost {
120
+ background: transparent;
121
+ border-color: transparent;
122
+ }
123
+ .kitcms-admin .kc-btn--ghost:hover {
124
+ background: color-mix(in srgb, var(--kc-muted) 15%, transparent);
125
+ border-color: transparent;
126
+ }
127
+
128
+ .kitcms-admin .kc-card {
129
+ background: var(--kc-surface);
130
+ border: 1px solid var(--kc-border);
131
+ border-radius: var(--kc-radius);
132
+ padding: 1rem;
133
+ }
134
+ .kitcms-admin .kc-alert {
135
+ margin: 0;
136
+ padding: 0.6rem 0.8rem;
137
+ border-radius: var(--kc-radius);
138
+ border: 1px solid var(--kc-danger);
139
+ color: var(--kc-danger);
140
+ background: color-mix(in srgb, var(--kc-danger) 8%, transparent);
141
+ }
142
+ .kitcms-admin .kc-alert--success {
143
+ border-color: var(--kc-success);
144
+ color: var(--kc-success);
145
+ background: color-mix(in srgb, var(--kc-success) 8%, transparent);
146
+ }
147
+ .kitcms-admin .kc-badge {
148
+ display: inline-block;
149
+ padding: 0.1rem 0.5rem;
150
+ font-size: 0.75rem;
151
+ font-weight: 600;
152
+ border: 1px solid var(--kc-border);
153
+ border-radius: 999px;
154
+ color: var(--kc-muted);
155
+ white-space: nowrap;
156
+ }
157
+ .kitcms-admin .kc-badge--published {
158
+ border-color: var(--kc-success);
159
+ color: var(--kc-success);
160
+ }
161
+ .kitcms-admin .kc-badge--changed {
162
+ border-color: var(--kc-accent);
163
+ color: var(--kc-text);
164
+ }
165
+ .kitcms-admin .kc-muted {
166
+ color: var(--kc-muted);
167
+ }
168
+
169
+ /* Small screens and touch: tap targets >= 44px, 16px form text (no iOS zoom on focus). */
170
+ @media (max-width: 800px), (pointer: coarse) {
171
+ .kitcms-admin .kc-btn,
172
+ .kitcms-admin .kc-input {
173
+ min-height: 2.75rem;
174
+ }
175
+ .kitcms-admin .kc-input {
176
+ font-size: 1rem;
177
+ }
178
+ .kitcms-admin input[type='checkbox'],
179
+ .kitcms-admin input[type='radio'] {
180
+ width: 1.25rem;
181
+ height: 1.25rem;
182
+ }
183
+ }
184
+ @media (prefers-reduced-motion: reduce) {
185
+ .kitcms-admin *,
186
+ .kitcms-admin *::before,
187
+ .kitcms-admin *::after {
188
+ animation-duration: 0.01ms !important;
189
+ animation-iteration-count: 1 !important;
190
+ transition-duration: 0.01ms !important;
191
+ scroll-behavior: auto !important;
192
+ }
193
+ }
@@ -0,0 +1,216 @@
1
+ <script lang="ts">
2
+ import { resolve } from '$app/paths';
3
+ import type { ClientCollection } from '@kitcms/core/client';
4
+
5
+ interface Props {
6
+ /** Only `slug` and `upload` are used, so the media picker can pass just a slug + options. */
7
+ collection: Pick<ClientCollection, 'slug' | 'upload'>;
8
+ /** Edit mode: the stored document (uploading replaces its file). */
9
+ doc?: Record<string, unknown>;
10
+ onuploaded?: (doc: Record<string, unknown>) => void;
11
+ /** Show the last uploaded/current file above the drop zone (default true; off in the media library and picker). */
12
+ showCurrent?: boolean;
13
+ }
14
+ let { collection, doc, onuploaded, showCurrent = true }: Props = $props();
15
+
16
+ let current = $state<Record<string, unknown> | undefined>(undefined);
17
+ const shown = $derived(current ?? doc);
18
+ let over = $state(false);
19
+ let progress = $state<number | null>(null);
20
+ let error = $state('');
21
+ let input: HTMLInputElement | undefined = $state();
22
+
23
+ const rules = $derived(collection.upload || null);
24
+ const accept = $derived(rules && rules.mimeTypes.length ? rules.mimeTypes.join(',') : undefined);
25
+
26
+ function formatSize(n: number): string {
27
+ if (n < 1024) return `${n} B`;
28
+ if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
29
+ return `${(n / 1024 / 1024).toFixed(1)} MB`;
30
+ }
31
+
32
+ function mimeOk(type: string): boolean {
33
+ if (!rules || rules.mimeTypes.length === 0) return true;
34
+ return rules.mimeTypes.some((m) => (m.endsWith('/*') ? type.startsWith(m.slice(0, -1)) : type === m));
35
+ }
36
+
37
+ function check(file: File): string | null {
38
+ if (!rules) return `"${collection.slug}" does not accept uploads.`;
39
+ if (file.size === 0) return 'The file is empty.';
40
+ if (file.size > rules.maxFileSize) {
41
+ return `The file is ${formatSize(file.size)}; the limit is ${formatSize(rules.maxFileSize)}.`;
42
+ }
43
+ if (!mimeOk(file.type)) {
44
+ return `${file.type || 'This file type'} is not allowed. Allowed: ${rules.mimeTypes.join(', ')}.`;
45
+ }
46
+ return null;
47
+ }
48
+
49
+ async function dimensions(file: File): Promise<{ width: number; height: number } | null> {
50
+ if (!file.type.startsWith('image/')) return null;
51
+ try {
52
+ const bmp = await createImageBitmap(file);
53
+ const out = { width: bmp.width, height: bmp.height };
54
+ bmp.close();
55
+ return out;
56
+ } catch {
57
+ return null;
58
+ }
59
+ }
60
+
61
+ async function upload(file: File) {
62
+ error = '';
63
+ const problem = check(file);
64
+ if (problem) {
65
+ error = problem;
66
+ return;
67
+ }
68
+ const form = new FormData();
69
+ form.set('file', file);
70
+ if (doc?.id) form.set('id', String(doc.id));
71
+ const dims = await dimensions(file);
72
+ if (dims) {
73
+ form.set('width', String(dims.width));
74
+ form.set('height', String(dims.height));
75
+ }
76
+ const url = resolve('/(kitcms)/api/[...path]', { path: `upload/${collection.slug}` });
77
+ progress = 0;
78
+ const xhr = new XMLHttpRequest();
79
+ xhr.open('POST', url);
80
+ xhr.upload.onprogress = (e) => {
81
+ if (e.lengthComputable) progress = Math.round((e.loaded / e.total) * 100);
82
+ };
83
+ xhr.onerror = () => {
84
+ progress = null;
85
+ error = 'The upload failed. Check your connection and try again.';
86
+ };
87
+ xhr.onload = () => {
88
+ progress = null;
89
+ let body: Record<string, unknown> = {};
90
+ try {
91
+ body = JSON.parse(xhr.responseText) as Record<string, unknown>;
92
+ } catch {
93
+ /* not JSON */
94
+ }
95
+ if (xhr.status >= 200 && xhr.status < 300) {
96
+ const saved = (body.doc as Record<string, unknown> | undefined) ?? body;
97
+ current = saved;
98
+ onuploaded?.(saved);
99
+ } else {
100
+ error = typeof body.error === 'string' ? body.error : `The upload failed (${xhr.status}).`;
101
+ }
102
+ };
103
+ xhr.send(form);
104
+ }
105
+
106
+ function pick(files: FileList | null | undefined) {
107
+ const file = files?.[0];
108
+ if (file) void upload(file);
109
+ if (input) input.value = '';
110
+ }
111
+
112
+ const busy = $derived(progress !== null);
113
+ </script>
114
+
115
+ <div class="kc-upload">
116
+ {#if showCurrent && shown && typeof shown.filename === 'string'}
117
+ <div class="kc-upload__current">
118
+ {#if typeof shown.mimeType === 'string' && shown.mimeType.startsWith('image/') && typeof shown.url === 'string'}
119
+ <img class="kc-upload__preview" src={shown.url} alt={typeof shown.alt === 'string' ? shown.alt : ''} />
120
+ {/if}
121
+ <div>
122
+ <div class="kc-upload__name">{shown.filename}</div>
123
+ <div class="kc-upload__meta">
124
+ {shown.mimeType ?? ''}{typeof shown.filesize === 'number' ? ` · ${formatSize(shown.filesize)}` : ''}{typeof shown.width === 'number' && typeof shown.height === 'number' ? ` · ${shown.width}×${shown.height}` : ''}
125
+ </div>
126
+ </div>
127
+ </div>
128
+ {/if}
129
+
130
+ <div
131
+ class="kc-upload__zone"
132
+ class:kc-upload__zone--over={over}
133
+ role="group"
134
+ aria-label="File upload"
135
+ ondragover={(e) => {
136
+ e.preventDefault();
137
+ over = true;
138
+ }}
139
+ ondragleave={() => (over = false)}
140
+ ondrop={(e) => {
141
+ e.preventDefault();
142
+ over = false;
143
+ if (!busy) pick(e.dataTransfer?.files);
144
+ }}
145
+ >
146
+ <span>Drop a file here or</span>
147
+ <button type="button" class="kc-btn" disabled={busy} onclick={() => input?.click()}>
148
+ {shown?.filename ? 'Replace file' : 'Choose file'}
149
+ </button>
150
+ <input bind:this={input} type="file" {accept} hidden onchange={(e) => pick(e.currentTarget.files)} />
151
+ {#if rules && Number.isFinite(rules.maxFileSize)}
152
+ <small class="kc-upload__hint">
153
+ Max {formatSize(rules.maxFileSize)}{rules.mimeTypes.length ? ` · ${rules.mimeTypes.join(', ')}` : ''}
154
+ </small>
155
+ {/if}
156
+ </div>
157
+
158
+ {#if progress !== null}
159
+ <progress class="kc-upload__progress" max="100" value={progress}>{progress}%</progress>
160
+ {/if}
161
+ {#if error}
162
+ <p class="kc-upload__error" role="alert">{error}</p>
163
+ {/if}
164
+ </div>
165
+
166
+ <style>
167
+ .kc-upload {
168
+ display: grid;
169
+ gap: 0.75rem;
170
+ }
171
+ .kc-upload__current {
172
+ display: flex;
173
+ gap: 0.75rem;
174
+ align-items: center;
175
+ }
176
+ .kc-upload__preview {
177
+ max-width: 8rem;
178
+ max-height: 8rem;
179
+ object-fit: contain;
180
+ border: 1px solid var(--kc-border);
181
+ border-radius: var(--kc-radius);
182
+ background: var(--kc-surface);
183
+ }
184
+ .kc-upload__name {
185
+ font-weight: 600;
186
+ word-break: break-all;
187
+ }
188
+ .kc-upload__meta,
189
+ .kc-upload__hint {
190
+ color: var(--kc-muted);
191
+ font-size: 0.85rem;
192
+ }
193
+ .kc-upload__zone {
194
+ display: flex;
195
+ flex-wrap: wrap;
196
+ align-items: center;
197
+ justify-content: center;
198
+ gap: 0.6rem;
199
+ padding: 1.25rem;
200
+ border: 2px dashed var(--kc-border);
201
+ border-radius: var(--kc-radius);
202
+ color: var(--kc-muted);
203
+ }
204
+ .kc-upload__zone--over {
205
+ border-color: var(--kc-accent);
206
+ background: color-mix(in srgb, var(--kc-accent) 8%, transparent);
207
+ }
208
+ .kc-upload__progress {
209
+ width: 100%;
210
+ accent-color: var(--kc-accent);
211
+ }
212
+ .kc-upload__error {
213
+ margin: 0;
214
+ color: var(--kc-danger);
215
+ }
216
+ </style>
@@ -0,0 +1,64 @@
1
+ <script lang="ts">
2
+ import type { AdminDataOf } from '@kitcms/core/client';
3
+ import { collectionHref, createHref, globalHref } from '../links';
4
+
5
+ let { data }: { data: AdminDataOf<'dashboard'> } = $props();
6
+ </script>
7
+
8
+ <!-- eslint-disable svelte/no-navigation-without-resolve -- every href comes from ../links, which calls resolve() -->
9
+
10
+ <h1>Dashboard</h1>
11
+ <div class="grid">
12
+ {#each data.collections as c (c.slug)}
13
+ <section class="kc-card">
14
+ <h2><a href={collectionHref(c.slug)}>{c.label}</a></h2>
15
+ {#if c.description}<p class="kc-muted">{c.description}</p>{/if}
16
+ {#if c.totalDocs !== -1}
17
+ <p class="count">{c.totalDocs} {c.totalDocs === 1 ? 'document' : 'documents'}</p>
18
+ {/if}
19
+ <a class="kc-btn" href={createHref(c.slug)}>Create</a>
20
+ </section>
21
+ {/each}
22
+ {#each data.globals as g (g.slug)}
23
+ <section class="kc-card" data-testid="global-card">
24
+ <h2><a href={globalHref(g.slug)}>{g.label}</a></h2>
25
+ {#if g.description}<p class="kc-muted">{g.description}</p>{/if}
26
+ <p class="kc-muted">Global</p>
27
+ <a class="kc-btn" href={globalHref(g.slug)}>Edit</a>
28
+ </section>
29
+ {/each}
30
+ </div>
31
+
32
+ <style>
33
+ .grid {
34
+ display: grid;
35
+ grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
36
+ gap: 1rem;
37
+ margin-top: 1rem;
38
+ }
39
+ section {
40
+ display: grid;
41
+ gap: 0.5rem;
42
+ align-content: start;
43
+ }
44
+ section a.kc-btn {
45
+ justify-self: start;
46
+ }
47
+ h2 {
48
+ font-size: 1.05rem;
49
+ }
50
+ h2 a {
51
+ text-decoration: none;
52
+ color: inherit;
53
+ }
54
+ h2 a:hover {
55
+ text-decoration: underline;
56
+ }
57
+ p {
58
+ margin: 0;
59
+ }
60
+ .count {
61
+ font-size: 1.25rem;
62
+ font-weight: 600;
63
+ }
64
+ </style>