cogens 0.2.1 → 0.3.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/CHANGELOG.md +10 -0
- package/README.md +21 -2
- package/dist/admin/app.d.ts.map +1 -1
- package/dist/admin/app.js +179 -38
- package/dist/admin/app.js.map +1 -1
- package/dist/admin/fields.d.ts +2 -0
- package/dist/admin/fields.d.ts.map +1 -1
- package/dist/admin/fields.js +4 -2
- package/dist/admin/fields.js.map +1 -1
- package/dist/admin/index.d.ts +2 -2
- package/dist/admin/index.d.ts.map +1 -1
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.js.map +1 -1
- package/dist/admin/types.d.ts +52 -1
- package/dist/admin/types.d.ts.map +1 -1
- package/dist/admin/views.d.ts +37 -8
- package/dist/admin/views.d.ts.map +1 -1
- package/dist/admin/views.js +40 -17
- package/dist/admin/views.js.map +1 -1
- package/dist/core/config/field.d.ts +18 -2
- package/dist/core/config/field.d.ts.map +1 -1
- package/dist/core/config/field.js +12 -1
- package/dist/core/config/field.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/admin/app.ts +181 -36
- package/src/admin/fields.ts +5 -2
- package/src/admin/index.ts +3 -2
- package/src/admin/types.ts +52 -1
- package/src/admin/views.ts +99 -21
- package/src/core/config/field.ts +19 -2
- package/src/core/index.ts +1 -1
package/src/admin/views.ts
CHANGED
|
@@ -103,23 +103,74 @@ export function signInPage(shell: Shell, next: string, error?: string): Html {
|
|
|
103
103
|
});
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
// ── The
|
|
106
|
+
// ── The home page ───────────────────────────────────────────────────
|
|
107
107
|
|
|
108
|
-
export
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
108
|
+
export interface HomeSection {
|
|
109
|
+
title: string;
|
|
110
|
+
line?: string;
|
|
111
|
+
items: { href: string; name: string }[];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The home page: the sections given, in order, each with its line and its items; a section with no items is not drawn. */
|
|
115
|
+
export function homePage(shell: Shell, sections: HomeSection[]): Html {
|
|
116
|
+
const drawn = sections.filter(s => s.items.length > 0);
|
|
112
117
|
return page(shell, {
|
|
113
|
-
title:
|
|
118
|
+
title: shell.siteName,
|
|
114
119
|
body:
|
|
115
|
-
|
|
120
|
+
drawn.length === 0
|
|
116
121
|
? html`<p>There is nothing here you may read.</p>`
|
|
117
|
-
: html`${
|
|
118
|
-
|
|
119
|
-
|
|
122
|
+
: html`${drawn.map(
|
|
123
|
+
s => html`<section class="stack"><h2>${s.title}</h2>${s.line ? html`<p>${s.line}</p>` : ''}<ul class="stack">${s.items.map(
|
|
124
|
+
i => html`<li><a href="${i.href}">${i.name}</a></li>`
|
|
125
|
+
)}</ul></section>`
|
|
126
|
+
)}`
|
|
120
127
|
});
|
|
121
128
|
}
|
|
122
129
|
|
|
130
|
+
// ── Columns ─────────────────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
/** A column of a list after the display name: a field (with its meta) or a built-in column (without). */
|
|
133
|
+
export interface ListColumn {
|
|
134
|
+
name: string;
|
|
135
|
+
label: string;
|
|
136
|
+
meta?: FieldMeta;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** One cell: a field as its read form (a reference as the referred name, a select as its label), a status as a badge. */
|
|
140
|
+
function cell(column: ListColumn, value: unknown, refs: ResolveRef): Html {
|
|
141
|
+
if (column.name === 'status' || column.name === 'state') return typeof value === 'string' ? statusBadge(value) : html``;
|
|
142
|
+
if (column.meta) return renderField({ name: column.name, meta: column.meta, value, refs }, 'read');
|
|
143
|
+
return value === undefined || value === null || value === '' ? html`<span data-tone="secondary" data-variant="ghost">—</span>` : html`<span>${String(value)}</span>`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ── What belongs to an entry ────────────────────────────────────────
|
|
147
|
+
|
|
148
|
+
/** A read-only list on an entry's or row's page: its children of one type, or a host's related rows. */
|
|
149
|
+
export interface Belonging {
|
|
150
|
+
label: string;
|
|
151
|
+
items: { href: string | null; label: string; status?: string }[];
|
|
152
|
+
/** How many there are in all; more than `items` when the list is cut short. */
|
|
153
|
+
total: number;
|
|
154
|
+
/** Where the whole list is, when there is such a page. */
|
|
155
|
+
more?: string;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function belongings(lists: Belonging[]): Html {
|
|
159
|
+
return html`${lists.map(
|
|
160
|
+
b => html`<section class="stack" aria-label="${b.label}"><h2>${b.label}</h2>${
|
|
161
|
+
b.items.length === 0
|
|
162
|
+
? html`<p>None.</p>`
|
|
163
|
+
: html`<ul class="stack">${b.items.map(
|
|
164
|
+
i => html`<li>${i.href ? html`<a href="${i.href}">${i.label}</a>` : html`<span>${i.label}</span>`}${i.status ? html` ${statusBadge(i.status)}` : ''}</li>`
|
|
165
|
+
)}</ul>${
|
|
166
|
+
b.total > b.items.length
|
|
167
|
+
? html`<p>${b.more ? html`<a href="${b.more}">and ${b.total - b.items.length} more</a>` : html`and ${b.total - b.items.length} more`}</p>`
|
|
168
|
+
: ''
|
|
169
|
+
}`
|
|
170
|
+
}</section>`
|
|
171
|
+
)}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
123
174
|
// ── An entry list ───────────────────────────────────────────────────
|
|
124
175
|
|
|
125
176
|
export interface ListInput {
|
|
@@ -132,6 +183,11 @@ export interface ListInput {
|
|
|
132
183
|
limit: number;
|
|
133
184
|
hasNext: boolean;
|
|
134
185
|
canCreate: boolean;
|
|
186
|
+
/** The parent entry the list is filtered to, by id. */
|
|
187
|
+
parent: string;
|
|
188
|
+
/** The columns the host named; absent shows slug, status, owner and when updated. */
|
|
189
|
+
columns?: ListColumn[];
|
|
190
|
+
refs: ResolveRef;
|
|
135
191
|
notice?: string;
|
|
136
192
|
}
|
|
137
193
|
|
|
@@ -147,7 +203,9 @@ export function entryListPage(shell: Shell, input: ListInput): Html {
|
|
|
147
203
|
const { type } = input;
|
|
148
204
|
const base = `${mount}/t/${type.id}`;
|
|
149
205
|
const title = (e: Entry) => String(e.data[type.displayField] ?? '') || e.slug || e.id;
|
|
150
|
-
const filters = { q: input.q, status: input.status, owner: input.owner };
|
|
206
|
+
const filters = { q: input.q, status: input.status, owner: input.owner, parent: input.parent };
|
|
207
|
+
const columns = input.columns;
|
|
208
|
+
const valueOf = (e: Entry, name: string): unknown => (name in e && name !== 'data' ? (e as unknown as Record<string, unknown>)[name] : e.data[name]);
|
|
151
209
|
return page(shell, {
|
|
152
210
|
title: type.name,
|
|
153
211
|
crumbs: [],
|
|
@@ -157,18 +215,26 @@ export function entryListPage(shell: Shell, input: ListInput): Html {
|
|
|
157
215
|
<input type="search" name="q" value="${input.q}" placeholder="Slug" aria-label="Search by slug">
|
|
158
216
|
<select name="status" aria-label="Status" data-autosubmit><option value="">Any status</option><option value="published"${input.status === 'published' ? ' selected' : ''}>Published</option><option value="draft"${input.status === 'draft' ? ' selected' : ''}>Draft</option></select>${
|
|
159
217
|
type.owned ? html`<input type="text" name="owner" value="${input.owner}" placeholder="Owner id" aria-label="Owner">` : ''
|
|
160
|
-
}
|
|
218
|
+
}${input.parent ? html`<input type="hidden" name="parent" value="${input.parent}">` : ''}
|
|
161
219
|
<button type="submit" data-variant="outline">Filter</button>${
|
|
162
|
-
input.q || input.status || input.owner ? html`<a href="${base}">Clear</a>` : ''
|
|
220
|
+
input.q || input.status || input.owner || input.parent ? html`<a href="${base}">Clear</a>` : ''
|
|
163
221
|
}${input.canCreate ? html`<a href="${base}/new">New ${type.name.replace(/s$/, '')}</a>` : ''}
|
|
164
222
|
</form>${
|
|
165
223
|
input.entries.length === 0
|
|
166
224
|
? html`<p>No entries.</p>`
|
|
167
225
|
: html`
|
|
168
226
|
<table>
|
|
169
|
-
<thead><tr><th scope="col">${type.fieldMeta[type.displayField]?.label ?? 'Title'}</th
|
|
227
|
+
<thead><tr><th scope="col">${type.fieldMeta[type.displayField]?.label ?? 'Title'}</th>${
|
|
228
|
+
columns
|
|
229
|
+
? columns.map(col => html`<th scope="col">${col.label}</th>`)
|
|
230
|
+
: html`<th scope="col">Slug</th><th scope="col">Status</th>${type.owned ? html`<th scope="col">Owner</th>` : ''}<th scope="col">Updated</th>`
|
|
231
|
+
}</tr></thead>
|
|
170
232
|
<tbody>${input.entries.map(
|
|
171
|
-
e => html`<tr><td><a href="${base}/${e.id}">${title(e)}</a></td
|
|
233
|
+
e => html`<tr><td><a href="${base}/${e.id}">${title(e)}</a></td>${
|
|
234
|
+
columns
|
|
235
|
+
? columns.map(col => html`<td>${cell(col, valueOf(e, col.name), input.refs)}</td>`)
|
|
236
|
+
: html`<td>${e.slug ?? ''}</td><td>${statusBadge(e.status)}</td>${type.owned ? html`<td>${e.owner_id ?? ''}</td>` : ''}<td>${e.updated_at}</td>`
|
|
237
|
+
}</tr>`
|
|
172
238
|
)}</tbody>
|
|
173
239
|
</table>`
|
|
174
240
|
}
|
|
@@ -228,6 +294,8 @@ export interface EntryInput {
|
|
|
228
294
|
canUnpublish: boolean;
|
|
229
295
|
canDelete: boolean;
|
|
230
296
|
actions: AdminAction<any>[];
|
|
297
|
+
/** Its children and the host's related rows. */
|
|
298
|
+
belongings: Belonging[];
|
|
231
299
|
notice?: string;
|
|
232
300
|
problems?: string[];
|
|
233
301
|
}
|
|
@@ -266,7 +334,7 @@ export function entryPage(shell: Shell, input: EntryInput): Html {
|
|
|
266
334
|
input.canDelete
|
|
267
335
|
? html`<form method="get" action="${base}/${entry.id}/delete"><button type="submit" data-tone="danger" data-variant="outline">Delete</button></form>`
|
|
268
336
|
: ''
|
|
269
|
-
}</section
|
|
337
|
+
}</section>${belongings(input.belongings)}`
|
|
270
338
|
: ''
|
|
271
339
|
}`
|
|
272
340
|
});
|
|
@@ -311,17 +379,26 @@ export function externalListPage(
|
|
|
311
379
|
rows: ExternalRow[],
|
|
312
380
|
offset: number,
|
|
313
381
|
limit: number,
|
|
314
|
-
hasNext: boolean
|
|
382
|
+
hasNext: boolean,
|
|
383
|
+
columns?: ListColumn[],
|
|
384
|
+
refs?: ResolveRef
|
|
315
385
|
): Html {
|
|
316
386
|
const base = `${shell.mount}/x/${collection.id}`;
|
|
317
387
|
const label = (r: ExternalRow) => String(r[collection.displayField] ?? '') || r.id;
|
|
388
|
+
const resolve: ResolveRef = refs ?? ((_, id) => ({ href: null, label: id }));
|
|
318
389
|
return page(shell, {
|
|
319
390
|
title: collection.name,
|
|
320
391
|
body: html`${
|
|
321
392
|
rows.length === 0
|
|
322
393
|
? html`<p>No rows.</p>`
|
|
323
|
-
: html`<table><thead><tr><th scope="col">${collection.fields[collection.displayField]?.meta.label ?? 'Name'}</th
|
|
324
|
-
|
|
394
|
+
: html`<table><thead><tr><th scope="col">${collection.fields[collection.displayField]?.meta.label ?? 'Name'}</th>${
|
|
395
|
+
columns ? columns.map(col => html`<th scope="col">${col.label}</th>`) : html`<th scope="col">State</th>`
|
|
396
|
+
}</tr></thead><tbody>${rows.map(
|
|
397
|
+
r => html`<tr><td><a href="${base}/${r.id}">${label(r)}</a></td>${
|
|
398
|
+
columns
|
|
399
|
+
? columns.map(col => html`<td>${cell(col, r[col.name], resolve)}</td>`)
|
|
400
|
+
: html`<td>${statusBadge(typeof r.state === 'string' ? r.state : r.status)}</td>`
|
|
401
|
+
}</tr>`
|
|
325
402
|
)}</tbody></table>`
|
|
326
403
|
}<nav class="cluster" aria-label="Pages">${offset > 0 ? html`<a href="${base}${query({ offset: Math.max(offset - limit, 0) })}">Previous</a>` : ''}${
|
|
327
404
|
hasNext ? html`<a href="${base}${query({ offset: offset + limit })}">Next</a>` : ''
|
|
@@ -335,7 +412,8 @@ export function externalRowPage(
|
|
|
335
412
|
row: ExternalRow,
|
|
336
413
|
refs: ResolveRef,
|
|
337
414
|
actions: AdminAction<any>[],
|
|
338
|
-
notice?: string
|
|
415
|
+
notice?: string,
|
|
416
|
+
related: Belonging[] = []
|
|
339
417
|
): Html {
|
|
340
418
|
const base = `${shell.mount}/x/${collection.id}`;
|
|
341
419
|
const title = String(row[collection.displayField] ?? '') || row.id;
|
|
@@ -355,6 +433,6 @@ export function externalRowPage(
|
|
|
355
433
|
a => html`<form method="get" action="${base}/${row.id}/do/${a.id}"><button type="submit" data-tone="${a.destructive ? 'danger' : 'secondary'}" data-variant="${a.destructive ? 'outline' : 'solid'}">${a.label}</button></form>`
|
|
356
434
|
)}</section>`
|
|
357
435
|
: ''
|
|
358
|
-
}`
|
|
436
|
+
}${belongings(related)}`
|
|
359
437
|
});
|
|
360
438
|
}
|
package/src/core/config/field.ts
CHANGED
|
@@ -30,7 +30,10 @@ export interface FieldMeta {
|
|
|
30
30
|
/** A system field is writable only by a caller whose rule lists it by name. */
|
|
31
31
|
system: boolean;
|
|
32
32
|
refContentType?: string;
|
|
33
|
+
/** A select's stored values, in order. */
|
|
33
34
|
options?: string[];
|
|
35
|
+
/** A select's label for each value that has one; a value with none is shown as itself. */
|
|
36
|
+
optionLabels?: Record<string, string>;
|
|
34
37
|
/** Stored when a create leaves the field out; set by the store, so a system field may have one. */
|
|
35
38
|
default?: unknown;
|
|
36
39
|
}
|
|
@@ -85,6 +88,10 @@ export interface StoredFile {
|
|
|
85
88
|
url: string;
|
|
86
89
|
}
|
|
87
90
|
|
|
91
|
+
/** A select's option: its stored value, or the value with the label the admin shows for it. */
|
|
92
|
+
export type SelectOption = string | { readonly value: string; readonly label: string };
|
|
93
|
+
type OptionValue<O> = O extends string ? O : O extends { readonly value: infer V extends string } ? V : never;
|
|
94
|
+
|
|
88
95
|
export const field = {
|
|
89
96
|
text: (label: string) => make<string>(label, 'text', { type: 'string' }),
|
|
90
97
|
longText: (label: string) => make<string>(label, 'longText', { type: 'string' }),
|
|
@@ -96,8 +103,18 @@ export const field = {
|
|
|
96
103
|
number: (label: string) => make<number>(label, 'number', { type: 'number' }),
|
|
97
104
|
/** An ISO 8601 date and time. */
|
|
98
105
|
date: (label: string) => make<string>(label, 'date', { type: 'string', format: 'date-time' }),
|
|
99
|
-
|
|
100
|
-
|
|
106
|
+
/**
|
|
107
|
+
* One of a fixed list. An option is its stored value, or `{ value, label }`: the admin shows and offers the label
|
|
108
|
+
* and stores the value.
|
|
109
|
+
*/
|
|
110
|
+
select: <const O extends readonly SelectOption[]>(label: string, opts: { options: O }) => {
|
|
111
|
+
const values = opts.options.map(o => (typeof o === 'string' ? o : o.value));
|
|
112
|
+
const labelled = opts.options.filter((o): o is Exclude<SelectOption, string> => typeof o !== 'string');
|
|
113
|
+
return make<OptionValue<O[number]>>(label, 'select', { type: 'string', enum: values }, {
|
|
114
|
+
options: values,
|
|
115
|
+
...(labelled.length > 0 ? { optionLabels: Object.fromEntries(labelled.map(o => [o.value, o.label])) } : {})
|
|
116
|
+
});
|
|
117
|
+
},
|
|
101
118
|
/** The id of an entry of another type. */
|
|
102
119
|
ref: (contentType: string, label: string) =>
|
|
103
120
|
make<string>(label, 'reference', { type: 'string' }, { refContentType: contentType }),
|
package/src/core/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type {
|
|
|
14
14
|
CmsConfig, ContentType, ContentTypeOptions, Fields, GroupConfig, Infer, ServiceAction, ServiceConfig
|
|
15
15
|
} from './config/define.ts';
|
|
16
16
|
export { field, FieldBuilder } from './config/field.ts';
|
|
17
|
-
export type { FieldMeta, FieldType, StoredFile } from './config/field.ts';
|
|
17
|
+
export type { FieldMeta, FieldType, SelectOption, StoredFile } from './config/field.ts';
|
|
18
18
|
|
|
19
19
|
export { OPERATIONS } from './access/can.ts';
|
|
20
20
|
export type { AccessContext, AccessFn, Can, Grant, GrantOptions } from './access/can.ts';
|