@kitcms/ui 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +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,201 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Blocks: an ordered list of rows, each one of the block types the config allows. Like
|
|
3
|
+
// ArrayField, rows render in array order with their CURRENT index in every input name.
|
|
4
|
+
import { tick } from 'svelte';
|
|
5
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
6
|
+
import { FORM } from '@kitcms/core/client';
|
|
7
|
+
import { getFormContext } from '../form/context';
|
|
8
|
+
import FieldShell from './FieldShell.svelte';
|
|
9
|
+
import RenderFields from './RenderFields.svelte';
|
|
10
|
+
import RowCard from './RowCard.svelte';
|
|
11
|
+
import { RowDrag } from './rowDrag.svelte';
|
|
12
|
+
import { cloneRow, emptyRow, moveItem, rowSummary, type Row } from './rows';
|
|
13
|
+
import type { FieldProps } from './types';
|
|
14
|
+
|
|
15
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'blocks', 'client'>> = $props();
|
|
16
|
+
|
|
17
|
+
const ctx = getFormContext();
|
|
18
|
+
const readOnly = $derived(Boolean(field.admin.readOnly) || ctx.readOnly);
|
|
19
|
+
|
|
20
|
+
if (!Array.isArray(value)) value = [];
|
|
21
|
+
const rows = $derived(value as Row[]);
|
|
22
|
+
|
|
23
|
+
const blockBySlug = $derived(new Map(field.blocks.map((b) => [b.slug, b])));
|
|
24
|
+
const full = $derived(field.maxRows !== undefined && rows.length >= field.maxRows);
|
|
25
|
+
const atMin = $derived(field.minRows !== undefined && rows.length <= field.minRows);
|
|
26
|
+
|
|
27
|
+
let collapsed = $state<Record<string, boolean>>({});
|
|
28
|
+
/** Which "add block" menu is open: the insert index after a row (1..n), or -1 for the one at the end. */
|
|
29
|
+
let menuAt = $state<number | null>(null);
|
|
30
|
+
let root: HTMLElement | undefined = $state();
|
|
31
|
+
let addButton: HTMLButtonElement | undefined = $state();
|
|
32
|
+
|
|
33
|
+
const rowKey = (row: Row, i: number) => (typeof row.id === 'string' && row.id ? row.id : `#${i}`);
|
|
34
|
+
const hasError = (i: number) => Object.keys(ctx.errors).some((k) => k.startsWith(`${path}.${i}.`));
|
|
35
|
+
|
|
36
|
+
async function focusRow(id: string) {
|
|
37
|
+
await tick();
|
|
38
|
+
root?.querySelector<HTMLElement>(`[data-row-id="${CSS.escape(id)}"] [data-row-focus]`)?.focus();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function insert(slug: string, at: number) {
|
|
42
|
+
const block = blockBySlug.get(slug);
|
|
43
|
+
menuAt = null;
|
|
44
|
+
if (!block || full || readOnly) return;
|
|
45
|
+
const row = emptyRow(block.fields, { blockType: slug });
|
|
46
|
+
value = [...rows.slice(0, at), row, ...rows.slice(at)];
|
|
47
|
+
void focusRow(row.id as string);
|
|
48
|
+
}
|
|
49
|
+
function move(from: number, to: number) {
|
|
50
|
+
value = moveItem(rows, from, to);
|
|
51
|
+
}
|
|
52
|
+
function duplicate(i: number) {
|
|
53
|
+
if (full) return;
|
|
54
|
+
const copy = cloneRow($state.snapshot(rows[i]!) as Row);
|
|
55
|
+
value = [...rows.slice(0, i + 1), copy, ...rows.slice(i + 1)];
|
|
56
|
+
void focusRow(copy.id as string);
|
|
57
|
+
}
|
|
58
|
+
async function remove(i: number) {
|
|
59
|
+
if (atMin) return;
|
|
60
|
+
value = rows.filter((_, j) => j !== i);
|
|
61
|
+
await tick();
|
|
62
|
+
addButton?.focus();
|
|
63
|
+
}
|
|
64
|
+
function menuKeydown(e: KeyboardEvent) {
|
|
65
|
+
if (e.key === 'Escape') {
|
|
66
|
+
menuAt = null;
|
|
67
|
+
addButton?.focus();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const drag = new RowDrag(move);
|
|
72
|
+
</script>
|
|
73
|
+
|
|
74
|
+
{#snippet menu(at: number)}
|
|
75
|
+
<ul class="kc-block-menu kc-card" role="menu" aria-label="Choose a block" onkeydown={menuKeydown}>
|
|
76
|
+
{#each field.blocks as block (block.slug)}
|
|
77
|
+
<li role="none">
|
|
78
|
+
<button type="button" role="menuitem" class="kc-btn kc-btn--ghost" onclick={() => insert(block.slug, at)}>
|
|
79
|
+
<span>{block.labels.singular}</span>
|
|
80
|
+
<span class="kc-muted kc-block-menu__slug">{block.slug}</span>
|
|
81
|
+
</button>
|
|
82
|
+
</li>
|
|
83
|
+
{/each}
|
|
84
|
+
</ul>
|
|
85
|
+
{/snippet}
|
|
86
|
+
|
|
87
|
+
<FieldShell {field} {path} as="fieldset">
|
|
88
|
+
<div class="kc-rows" bind:this={root}>
|
|
89
|
+
<input type="hidden" name={FORM.marker(path)} value="1" disabled={readOnly} />
|
|
90
|
+
{#if field.minRows !== undefined || field.maxRows !== undefined}
|
|
91
|
+
<p class="kc-muted kc-rows__hint">
|
|
92
|
+
{rows.length}
|
|
93
|
+
{rows.length === 1 ? 'block' : 'blocks'}{#if field.minRows !== undefined}, min {field.minRows}{/if}{#if field.maxRows !== undefined},
|
|
94
|
+
max {field.maxRows}{/if}
|
|
95
|
+
</p>
|
|
96
|
+
{/if}
|
|
97
|
+
{#each rows as row, i (rowKey(row, i))}
|
|
98
|
+
{@const rowPath = `${path}.${i}`}
|
|
99
|
+
{@const slug = typeof row.blockType === 'string' ? row.blockType : ''}
|
|
100
|
+
{@const block = blockBySlug.get(slug)}
|
|
101
|
+
<RowCard
|
|
102
|
+
index={i}
|
|
103
|
+
rowId={rowKey(row, i)}
|
|
104
|
+
title={block ? block.labels.singular : `Unknown block "${slug}"`}
|
|
105
|
+
subtitle={block ? rowSummary(block.fields, row) : ''}
|
|
106
|
+
collapsed={collapsed[rowKey(row, i)] === true}
|
|
107
|
+
hasError={hasError(i)}
|
|
108
|
+
{readOnly}
|
|
109
|
+
count={rows.length}
|
|
110
|
+
canDuplicate={!full && block !== undefined}
|
|
111
|
+
dragging={drag.from === i}
|
|
112
|
+
dropTarget={drag.over === i && drag.from !== i}
|
|
113
|
+
ontoggle={() => (collapsed[rowKey(row, i)] = !collapsed[rowKey(row, i)])}
|
|
114
|
+
onmove={(d) => move(i, i + d)}
|
|
115
|
+
onduplicate={() => duplicate(i)}
|
|
116
|
+
onremove={() => remove(i)}
|
|
117
|
+
ondragstart={(e) => drag.start(i, e)}
|
|
118
|
+
ondragend={drag.end}
|
|
119
|
+
ondragover={(e) => drag.dragover(i, e)}
|
|
120
|
+
ondrop={(e) => drag.drop(i, e)}
|
|
121
|
+
>
|
|
122
|
+
{#snippet actions()}
|
|
123
|
+
{#if !full}
|
|
124
|
+
<button
|
|
125
|
+
type="button"
|
|
126
|
+
class="kc-btn kc-btn--ghost"
|
|
127
|
+
aria-label="Insert a block after row {i + 1}"
|
|
128
|
+
aria-haspopup="menu"
|
|
129
|
+
aria-expanded={menuAt === i + 1}
|
|
130
|
+
onclick={() => (menuAt = menuAt === i + 1 ? null : i + 1)}>Insert after</button
|
|
131
|
+
>
|
|
132
|
+
{/if}
|
|
133
|
+
{/snippet}
|
|
134
|
+
<input type="hidden" name="{rowPath}.id" value={typeof row.id === 'string' ? row.id : ''} />
|
|
135
|
+
<input type="hidden" name="{rowPath}.blockType" value={slug} />
|
|
136
|
+
{#if block}
|
|
137
|
+
<RenderFields
|
|
138
|
+
fields={block.fields}
|
|
139
|
+
layout={block.layout}
|
|
140
|
+
bind:values={() => rows[i]!, (v) => (rows[i] = v)}
|
|
141
|
+
prefix={rowPath}
|
|
142
|
+
/>
|
|
143
|
+
{:else}
|
|
144
|
+
<p class="kc-alert" role="alert">
|
|
145
|
+
This block type ("{slug}") is no longer in the config. Its content can't be edited here and is
|
|
146
|
+
dropped when you save, unless you remove the row first or restore the block.
|
|
147
|
+
</p>
|
|
148
|
+
{/if}
|
|
149
|
+
</RowCard>
|
|
150
|
+
{#if menuAt === i + 1}{@render menu(i + 1)}{/if}
|
|
151
|
+
{/each}
|
|
152
|
+
{#if !readOnly}
|
|
153
|
+
<div class="kc-block-add">
|
|
154
|
+
<button
|
|
155
|
+
type="button"
|
|
156
|
+
class="kc-btn"
|
|
157
|
+
bind:this={addButton}
|
|
158
|
+
disabled={full}
|
|
159
|
+
aria-haspopup="menu"
|
|
160
|
+
aria-expanded={menuAt === -1}
|
|
161
|
+
onclick={() => (menuAt = menuAt === -1 ? null : -1)}>Add block</button
|
|
162
|
+
>
|
|
163
|
+
{#if menuAt === -1}{@render menu(rows.length)}{/if}
|
|
164
|
+
</div>
|
|
165
|
+
{/if}
|
|
166
|
+
</div>
|
|
167
|
+
</FieldShell>
|
|
168
|
+
|
|
169
|
+
<style>
|
|
170
|
+
.kc-rows {
|
|
171
|
+
display: grid;
|
|
172
|
+
gap: 0.75rem;
|
|
173
|
+
min-width: 0;
|
|
174
|
+
}
|
|
175
|
+
.kc-rows__hint {
|
|
176
|
+
margin: 0;
|
|
177
|
+
font-size: 0.8125rem;
|
|
178
|
+
}
|
|
179
|
+
.kc-block-add {
|
|
180
|
+
display: grid;
|
|
181
|
+
gap: 0.5rem;
|
|
182
|
+
justify-items: start;
|
|
183
|
+
}
|
|
184
|
+
.kc-block-menu {
|
|
185
|
+
list-style: none;
|
|
186
|
+
margin: 0;
|
|
187
|
+
padding: 0.25rem;
|
|
188
|
+
display: grid;
|
|
189
|
+
gap: 0.125rem;
|
|
190
|
+
min-width: 14rem;
|
|
191
|
+
}
|
|
192
|
+
.kc-block-menu button {
|
|
193
|
+
width: 100%;
|
|
194
|
+
justify-content: space-between;
|
|
195
|
+
display: flex;
|
|
196
|
+
gap: 1rem;
|
|
197
|
+
}
|
|
198
|
+
.kc-block-menu__slug {
|
|
199
|
+
font-size: 0.75rem;
|
|
200
|
+
}
|
|
201
|
+
</style>
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Boolean checkbox. Same structure as TextField (the reference); see ./types.ts for the contract.
|
|
3
|
+
// The hidden presence marker makes "unchecked" submit a value (an unchecked box submits nothing).
|
|
4
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
5
|
+
import { FORM } from '@kitcms/core/client';
|
|
6
|
+
import { getFormContext } from '../form/context';
|
|
7
|
+
import FieldShell from './FieldShell.svelte';
|
|
8
|
+
import { fieldId, type FieldProps } from './types';
|
|
9
|
+
|
|
10
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'checkbox', 'client'>> = $props();
|
|
11
|
+
|
|
12
|
+
const ctx = getFormContext();
|
|
13
|
+
const id = $derived(fieldId(path));
|
|
14
|
+
const readOnly = $derived(Boolean(field.admin.readOnly) || ctx.readOnly);
|
|
15
|
+
const describedBy = $derived(
|
|
16
|
+
[field.admin.description && `${id}-desc`, ctx.errors[path] && `${id}-err`].filter(Boolean).join(' ') ||
|
|
17
|
+
undefined,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// null/undefined is unchecked.
|
|
21
|
+
const get = () => value === true;
|
|
22
|
+
const set = (v: boolean) => {
|
|
23
|
+
value = v;
|
|
24
|
+
};
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<FieldShell {field} {path} as="div">
|
|
28
|
+
<input type="hidden" name={FORM.marker(path)} value="1" disabled={readOnly} />
|
|
29
|
+
<div class="kc-check">
|
|
30
|
+
<input
|
|
31
|
+
{id}
|
|
32
|
+
type="checkbox"
|
|
33
|
+
name={path}
|
|
34
|
+
bind:checked={get, set}
|
|
35
|
+
disabled={readOnly}
|
|
36
|
+
aria-invalid={ctx.errors[path] ? 'true' : undefined}
|
|
37
|
+
aria-describedby={describedBy}
|
|
38
|
+
/>
|
|
39
|
+
</div>
|
|
40
|
+
</FieldShell>
|
|
41
|
+
|
|
42
|
+
<style>
|
|
43
|
+
.kc-check {
|
|
44
|
+
display: flex;
|
|
45
|
+
align-items: center;
|
|
46
|
+
gap: 0.5rem;
|
|
47
|
+
}
|
|
48
|
+
</style>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Date (`picker: 'day'`) or date and time (`picker: 'dayAndTime'`). Same structure as TextField
|
|
3
|
+
// (the reference); see ./types.ts for the contract.
|
|
4
|
+
//
|
|
5
|
+
// The stored value is an ISO string (`2026-11-16T00:00:00.000Z`). Day pickers store UTC midnight.
|
|
6
|
+
// Date-time pickers convert the browser's local time to UTC ISO.
|
|
7
|
+
//
|
|
8
|
+
// Form submission: the visible input has `name={path}` and submits `YYYY-MM-DD` or the local
|
|
9
|
+
// `YYYY-MM-DDTHH:mm`, which carries no timezone. A hidden input with the exact ISO value is
|
|
10
|
+
// rendered AFTER it, only when there is a value. The server takes the last value for a name, so
|
|
11
|
+
// the ISO string wins. With JS disabled the visible input still submits, and the hidden one is
|
|
12
|
+
// absent only when the value is empty (then the visible empty string means null).
|
|
13
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
14
|
+
import { getFormContext } from '../form/context';
|
|
15
|
+
import FieldShell from './FieldShell.svelte';
|
|
16
|
+
import { fieldId, type FieldProps } from './types';
|
|
17
|
+
|
|
18
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'date', 'client'>> = $props();
|
|
19
|
+
|
|
20
|
+
const ctx = getFormContext();
|
|
21
|
+
const id = $derived(fieldId(path));
|
|
22
|
+
const readOnly = $derived(Boolean(field.admin.readOnly) || ctx.readOnly);
|
|
23
|
+
const describedBy = $derived(
|
|
24
|
+
[field.admin.description && `${id}-desc`, ctx.errors[path] && `${id}-err`].filter(Boolean).join(' ') ||
|
|
25
|
+
undefined,
|
|
26
|
+
);
|
|
27
|
+
const dayAndTime = $derived(field.admin.picker === 'dayAndTime');
|
|
28
|
+
const iso = $derived(typeof value === 'string' ? value : '');
|
|
29
|
+
|
|
30
|
+
const pad = (n: number) => String(n).padStart(2, '0');
|
|
31
|
+
|
|
32
|
+
const get = (): string => {
|
|
33
|
+
const d = new Date(iso);
|
|
34
|
+
if (iso === '' || Number.isNaN(d.getTime())) return '';
|
|
35
|
+
if (!dayAndTime) return d.toISOString().slice(0, 10);
|
|
36
|
+
// Local wall-clock time for <input type="datetime-local">.
|
|
37
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
38
|
+
};
|
|
39
|
+
const set = (v: string) => {
|
|
40
|
+
if (v === '') {
|
|
41
|
+
value = null;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!dayAndTime) {
|
|
45
|
+
value = `${v}T00:00:00.000Z`;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// `new Date` reads a date-time without offset as local time. Invalid input is kept raw so the
|
|
49
|
+
// server reports it.
|
|
50
|
+
const d = new Date(v);
|
|
51
|
+
value = Number.isNaN(d.getTime()) ? v : d.toISOString();
|
|
52
|
+
};
|
|
53
|
+
</script>
|
|
54
|
+
|
|
55
|
+
<FieldShell {field} {path}>
|
|
56
|
+
<input
|
|
57
|
+
{id}
|
|
58
|
+
class="kc-input"
|
|
59
|
+
type={dayAndTime ? 'datetime-local' : 'date'}
|
|
60
|
+
name={path}
|
|
61
|
+
bind:value={get, set}
|
|
62
|
+
required={field.required}
|
|
63
|
+
disabled={readOnly}
|
|
64
|
+
aria-invalid={ctx.errors[path] ? 'true' : undefined}
|
|
65
|
+
aria-describedby={describedBy}
|
|
66
|
+
/>
|
|
67
|
+
{#if iso !== ''}
|
|
68
|
+
<input type="hidden" name={path} value={iso} disabled={readOnly} />
|
|
69
|
+
{/if}
|
|
70
|
+
</FieldShell>
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Email address. Same structure as TextField (the reference); see ./types.ts for the contract.
|
|
3
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
4
|
+
import { getFormContext } from '../form/context';
|
|
5
|
+
import FieldShell from './FieldShell.svelte';
|
|
6
|
+
import { fieldId, type FieldProps } from './types';
|
|
7
|
+
|
|
8
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'email', 'client'>> = $props();
|
|
9
|
+
|
|
10
|
+
const ctx = getFormContext();
|
|
11
|
+
const id = $derived(fieldId(path));
|
|
12
|
+
const readOnly = $derived(Boolean(field.admin.readOnly) || ctx.readOnly);
|
|
13
|
+
const describedBy = $derived(
|
|
14
|
+
[field.admin.description && `${id}-desc`, ctx.errors[path] && `${id}-err`].filter(Boolean).join(' ') ||
|
|
15
|
+
undefined,
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
// The form state may hold null; the input works with strings (function binding, Svelte ≥ 5.9).
|
|
19
|
+
const get = () => (typeof value === 'string' ? value : '');
|
|
20
|
+
const set = (v: string) => {
|
|
21
|
+
value = v === '' ? null : v;
|
|
22
|
+
};
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<FieldShell {field} {path}>
|
|
26
|
+
<input
|
|
27
|
+
{id}
|
|
28
|
+
class="kc-input"
|
|
29
|
+
type="email"
|
|
30
|
+
name={path}
|
|
31
|
+
autocomplete="email"
|
|
32
|
+
bind:value={get, set}
|
|
33
|
+
required={field.required}
|
|
34
|
+
placeholder={field.admin.placeholder}
|
|
35
|
+
disabled={readOnly}
|
|
36
|
+
aria-invalid={ctx.errors[path] ? 'true' : undefined}
|
|
37
|
+
aria-describedby={describedBy}
|
|
38
|
+
/>
|
|
39
|
+
</FieldShell>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Label, required mark, description and error for one field. Every field component uses it.
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
import type { ClientField } from '@kitcms/core/client';
|
|
5
|
+
import { getFormContext } from '../form/context';
|
|
6
|
+
import { fieldId } from './types';
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
field,
|
|
10
|
+
path,
|
|
11
|
+
children,
|
|
12
|
+
/** `fieldset` for groups of inputs (radio-like widgets, containers); `label` wraps nothing. */
|
|
13
|
+
as = 'div',
|
|
14
|
+
}: { field: ClientField; path: string; children: Snippet; as?: 'div' | 'fieldset' } = $props();
|
|
15
|
+
|
|
16
|
+
const ctx = getFormContext();
|
|
17
|
+
const id = $derived(fieldId(path));
|
|
18
|
+
const error = $derived(ctx.errors[path]);
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
{#if as === 'fieldset'}
|
|
22
|
+
<fieldset class="kc-field" class:kc-field--error={error} data-path={path}>
|
|
23
|
+
<legend class="kc-field__label">
|
|
24
|
+
{field.label}{#if field.required}<span class="kc-field__req" aria-hidden="true">*</span>{/if}
|
|
25
|
+
</legend>
|
|
26
|
+
{@render children()}
|
|
27
|
+
{#if field.admin.description}<p class="kc-field__desc" id="{id}-desc">{field.admin.description}</p>{/if}
|
|
28
|
+
{#if error}<p class="kc-field__error" id="{id}-err" role="alert">{error}</p>{/if}
|
|
29
|
+
</fieldset>
|
|
30
|
+
{:else}
|
|
31
|
+
<div class="kc-field" class:kc-field--error={error} data-path={path}>
|
|
32
|
+
<label class="kc-field__label" for={id}>
|
|
33
|
+
{field.label}{#if field.required}<span class="kc-field__req" aria-hidden="true">*</span>{/if}
|
|
34
|
+
</label>
|
|
35
|
+
{@render children()}
|
|
36
|
+
{#if field.admin.description}<p class="kc-field__desc" id="{id}-desc">{field.admin.description}</p>{/if}
|
|
37
|
+
{#if error}<p class="kc-field__error" id="{id}-err" role="alert">{error}</p>{/if}
|
|
38
|
+
</div>
|
|
39
|
+
{/if}
|
|
40
|
+
|
|
41
|
+
<style>
|
|
42
|
+
.kc-field {
|
|
43
|
+
display: grid;
|
|
44
|
+
gap: 0.375rem;
|
|
45
|
+
border: 0;
|
|
46
|
+
padding: 0;
|
|
47
|
+
margin: 0;
|
|
48
|
+
min-width: 0;
|
|
49
|
+
}
|
|
50
|
+
.kc-field__label {
|
|
51
|
+
font-weight: 500;
|
|
52
|
+
font-size: 0.875rem;
|
|
53
|
+
color: var(--kc-text);
|
|
54
|
+
padding: 0;
|
|
55
|
+
}
|
|
56
|
+
.kc-field__req {
|
|
57
|
+
color: var(--kc-danger);
|
|
58
|
+
margin-left: 0.125rem;
|
|
59
|
+
}
|
|
60
|
+
.kc-field__desc {
|
|
61
|
+
margin: 0;
|
|
62
|
+
font-size: 0.8125rem;
|
|
63
|
+
color: var(--kc-muted);
|
|
64
|
+
}
|
|
65
|
+
.kc-field__error {
|
|
66
|
+
margin: 0;
|
|
67
|
+
font-size: 0.8125rem;
|
|
68
|
+
color: var(--kc-danger);
|
|
69
|
+
}
|
|
70
|
+
</style>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Group: a named object of sub-fields, submitted as `path.child` inputs.
|
|
3
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
4
|
+
import { FORM } from '@kitcms/core/client';
|
|
5
|
+
import { getFormContext } from '../form/context';
|
|
6
|
+
import FieldShell from './FieldShell.svelte';
|
|
7
|
+
import RenderFields from './RenderFields.svelte';
|
|
8
|
+
import type { FieldProps } from './types';
|
|
9
|
+
|
|
10
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'group', 'client'>> = $props();
|
|
11
|
+
|
|
12
|
+
const ctx = getFormContext();
|
|
13
|
+
const readOnly = $derived(Boolean(field.admin.readOnly) || ctx.readOnly);
|
|
14
|
+
|
|
15
|
+
// RenderFields binds into an object, so make sure there is one (synchronously, so SSR has it too).
|
|
16
|
+
if (value === null || value === undefined || typeof value !== 'object' || Array.isArray(value)) value = {};
|
|
17
|
+
const get = () => value as Record<string, unknown>;
|
|
18
|
+
const set = (v: Record<string, unknown>) => {
|
|
19
|
+
value = v;
|
|
20
|
+
};
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<FieldShell {field} {path} as="fieldset">
|
|
24
|
+
<!-- A disabled fieldset disables every descendant control (marker included), so a read-only
|
|
25
|
+
group submits nothing and the stored value is kept. -->
|
|
26
|
+
<fieldset class="kc-group" disabled={readOnly}>
|
|
27
|
+
<input type="hidden" name={FORM.marker(path)} value="1" />
|
|
28
|
+
<RenderFields fields={field.fields} layout={field.layout} bind:values={get, set} prefix={path} />
|
|
29
|
+
</fieldset>
|
|
30
|
+
</FieldShell>
|
|
31
|
+
|
|
32
|
+
<style>
|
|
33
|
+
.kc-group {
|
|
34
|
+
display: grid;
|
|
35
|
+
gap: 1rem;
|
|
36
|
+
padding: 0.75rem;
|
|
37
|
+
border: 1px solid var(--kc-border);
|
|
38
|
+
border-radius: var(--kc-radius);
|
|
39
|
+
}
|
|
40
|
+
</style>
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Arbitrary JSON, edited as text. The textarea submits the raw text (the server parses it and
|
|
3
|
+
// reports invalid JSON as a field error, see core admin/form.ts). The form state holds the
|
|
4
|
+
// parsed value; while the text doesn't parse, the last valid value is kept and a hint is shown.
|
|
5
|
+
import { untrack } from 'svelte';
|
|
6
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
7
|
+
import { getFormContext } from '../form/context';
|
|
8
|
+
import FieldShell from './FieldShell.svelte';
|
|
9
|
+
import { fieldId, type FieldProps } from './types';
|
|
10
|
+
|
|
11
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'json', 'client'>> = $props();
|
|
12
|
+
|
|
13
|
+
const ctx = getFormContext();
|
|
14
|
+
const id = $derived(fieldId(path));
|
|
15
|
+
const readOnly = $derived(Boolean(field.admin.readOnly) || ctx.readOnly);
|
|
16
|
+
|
|
17
|
+
const format = (v: unknown) => (v === null || v === undefined ? '' : JSON.stringify(v, null, 2));
|
|
18
|
+
// Seeded once at creation (runs during SSR); the edit view re-creates fields per document.
|
|
19
|
+
let text = $state(untrack(() => format(value)));
|
|
20
|
+
let invalid = $state(false);
|
|
21
|
+
|
|
22
|
+
const describedBy = $derived(
|
|
23
|
+
[
|
|
24
|
+
field.admin.description && `${id}-desc`,
|
|
25
|
+
ctx.errors[path] && `${id}-err`,
|
|
26
|
+
invalid && `${id}-json`,
|
|
27
|
+
]
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.join(' ') || undefined,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
function oninput() {
|
|
33
|
+
if (text.trim() === '') {
|
|
34
|
+
value = null;
|
|
35
|
+
invalid = false;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
value = JSON.parse(text) as unknown;
|
|
40
|
+
invalid = false;
|
|
41
|
+
} catch {
|
|
42
|
+
invalid = true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<FieldShell {field} {path}>
|
|
48
|
+
<textarea
|
|
49
|
+
{id}
|
|
50
|
+
class="kc-input kc-json"
|
|
51
|
+
name={path}
|
|
52
|
+
rows={field.admin.rows ?? 6}
|
|
53
|
+
spellcheck="false"
|
|
54
|
+
bind:value={text}
|
|
55
|
+
{oninput}
|
|
56
|
+
placeholder={field.admin.placeholder}
|
|
57
|
+
disabled={readOnly}
|
|
58
|
+
aria-invalid={invalid || ctx.errors[path] ? 'true' : undefined}
|
|
59
|
+
aria-describedby={describedBy}
|
|
60
|
+
></textarea>
|
|
61
|
+
{#if invalid}<p class="kc-json__hint" id="{id}-json">Not valid JSON yet.</p>{/if}
|
|
62
|
+
</FieldShell>
|
|
63
|
+
|
|
64
|
+
<style>
|
|
65
|
+
.kc-json {
|
|
66
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
67
|
+
font-size: 0.8125rem;
|
|
68
|
+
}
|
|
69
|
+
.kc-json__hint {
|
|
70
|
+
margin: 0;
|
|
71
|
+
font-size: 0.8125rem;
|
|
72
|
+
color: var(--kc-danger);
|
|
73
|
+
}
|
|
74
|
+
</style>
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Number input. Same structure as TextField (the reference); see ./types.ts for the contract.
|
|
3
|
+
// The input works with strings: `''` is null. A value that doesn't parse is kept as the raw
|
|
4
|
+
// string so the server's validation reports it instead of the field silently clearing.
|
|
5
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
6
|
+
import { getFormContext } from '../form/context';
|
|
7
|
+
import FieldShell from './FieldShell.svelte';
|
|
8
|
+
import { fieldId, type FieldProps } from './types';
|
|
9
|
+
|
|
10
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'number', 'client'>> = $props();
|
|
11
|
+
|
|
12
|
+
const ctx = getFormContext();
|
|
13
|
+
const id = $derived(fieldId(path));
|
|
14
|
+
const readOnly = $derived(Boolean(field.admin.readOnly) || ctx.readOnly);
|
|
15
|
+
const describedBy = $derived(
|
|
16
|
+
[field.admin.description && `${id}-desc`, ctx.errors[path] && `${id}-err`].filter(Boolean).join(' ') ||
|
|
17
|
+
undefined,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const get = () => {
|
|
21
|
+
if (typeof value === 'number') return String(value);
|
|
22
|
+
if (typeof value === 'string') return value;
|
|
23
|
+
return '';
|
|
24
|
+
};
|
|
25
|
+
const set = (v: string) => {
|
|
26
|
+
if (v === '') {
|
|
27
|
+
value = null;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const n = Number(v);
|
|
31
|
+
value = Number.isNaN(n) ? v : n;
|
|
32
|
+
};
|
|
33
|
+
</script>
|
|
34
|
+
|
|
35
|
+
<FieldShell {field} {path}>
|
|
36
|
+
<input
|
|
37
|
+
{id}
|
|
38
|
+
class="kc-input"
|
|
39
|
+
type="number"
|
|
40
|
+
name={path}
|
|
41
|
+
min={field.min}
|
|
42
|
+
max={field.max}
|
|
43
|
+
step={field.admin.step ?? (field.integer ? 1 : 'any')}
|
|
44
|
+
bind:value={get, set}
|
|
45
|
+
required={field.required}
|
|
46
|
+
placeholder={field.admin.placeholder}
|
|
47
|
+
disabled={readOnly}
|
|
48
|
+
aria-invalid={ctx.errors[path] ? 'true' : undefined}
|
|
49
|
+
aria-describedby={describedBy}
|
|
50
|
+
/>
|
|
51
|
+
</FieldShell>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// (Core types relationship and upload as one union member, hence the combined FieldOf.)
|
|
3
|
+
// Relationship: a reference (or ordered list of references) to documents of another collection.
|
|
4
|
+
import type { FieldOf } from '@kitcms/core/client';
|
|
5
|
+
import RelationField from './relation/RelationField.svelte';
|
|
6
|
+
import type { FieldProps } from './types';
|
|
7
|
+
|
|
8
|
+
let { field, path, value = $bindable() }: FieldProps<FieldOf<'relationship' | 'upload', 'client'>> = $props();
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<RelationField {field} {path} bind:value kind="relationship" />
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Renders one container's fields (a collection, a group, an array row or a block) by walking its
|
|
3
|
+
// admin layout tree. `values` is the container's object inside the form state; fields bind to
|
|
4
|
+
// `values[field.name]`. `prefix` is the container's dot path ('' at the document root).
|
|
5
|
+
import type { ClientField, LayoutNode } from '@kitcms/core/client';
|
|
6
|
+
import { getFormContext } from '../form/context';
|
|
7
|
+
import RenderNodes from './RenderNodes.svelte';
|
|
8
|
+
|
|
9
|
+
let {
|
|
10
|
+
fields,
|
|
11
|
+
layout,
|
|
12
|
+
values = $bindable(),
|
|
13
|
+
prefix = '',
|
|
14
|
+
/** Only render fields in this admin position (top level of the edit view). */
|
|
15
|
+
position,
|
|
16
|
+
}: {
|
|
17
|
+
fields: ClientField[];
|
|
18
|
+
layout: LayoutNode[];
|
|
19
|
+
values: Record<string, unknown>;
|
|
20
|
+
prefix?: string;
|
|
21
|
+
position?: 'main' | 'sidebar';
|
|
22
|
+
} = $props();
|
|
23
|
+
|
|
24
|
+
const ctx = getFormContext();
|
|
25
|
+
const byName = $derived(new Map(fields.map((f) => [f.name, f])));
|
|
26
|
+
const visible = (f: ClientField) =>
|
|
27
|
+
!f.admin.hidden &&
|
|
28
|
+
!(prefix === '' && ctx.hidden.has(f.name)) &&
|
|
29
|
+
(position === undefined || (f.admin.position ?? 'main') === position);
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<RenderNodes nodes={layout} {byName} {visible} bind:values {prefix} />
|