@prenta/admin 2.16.1 → 2.16.2
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 +6 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.d.ts +2 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.d.ts.map +1 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.js +21 -0
- package/dist/__tests__/fields/field-renderer-null.render.test.js.map +1 -0
- package/dist/__tests__/lib/post-custom-fields.test.d.ts +2 -0
- package/dist/__tests__/lib/post-custom-fields.test.d.ts.map +1 -0
- package/dist/__tests__/lib/post-custom-fields.test.js +158 -0
- package/dist/__tests__/lib/post-custom-fields.test.js.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.d.ts +2 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.d.ts.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.js +104 -0
- package/dist/__tests__/lib/post-editor-custom-fields-api.test.js.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.d.ts +2 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.d.ts.map +1 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.js +128 -0
- package/dist/__tests__/lib/post-editor-custom-fields-undo.test.js.map +1 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.d.ts +2 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.d.ts.map +1 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.js +164 -0
- package/dist/__tests__/views/post-tab-custom-fields.render.test.js.map +1 -0
- package/dist/fields/FieldRenderer.d.ts.map +1 -1
- package/dist/fields/FieldRenderer.js +7 -1
- package/dist/fields/FieldRenderer.js.map +1 -1
- package/dist/lib/post-custom-fields.d.ts +28 -0
- package/dist/lib/post-custom-fields.d.ts.map +1 -0
- package/dist/lib/post-custom-fields.js +173 -0
- package/dist/lib/post-custom-fields.js.map +1 -0
- package/dist/lib/post-editor-service.d.ts +29 -0
- package/dist/lib/post-editor-service.d.ts.map +1 -1
- package/dist/lib/post-editor-service.js +53 -0
- package/dist/lib/post-editor-service.js.map +1 -1
- package/dist/prenta-admin.css +1 -1
- package/dist/views/post-editor/PostRowsField.d.ts +17 -0
- package/dist/views/post-editor/PostRowsField.d.ts.map +1 -0
- package/dist/views/post-editor/PostRowsField.js +29 -0
- package/dist/views/post-editor/PostRowsField.js.map +1 -0
- package/dist/views/post-editor/PostSectionEditor.d.ts.map +1 -1
- package/dist/views/post-editor/PostSectionEditor.js +16 -5
- package/dist/views/post-editor/PostSectionEditor.js.map +1 -1
- package/dist/views/post-editor/PostTab.d.ts +8 -5
- package/dist/views/post-editor/PostTab.d.ts.map +1 -1
- package/dist/views/post-editor/PostTab.js +53 -15
- package/dist/views/post-editor/PostTab.js.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/fields/field-renderer-null.render.test.tsx +34 -0
- package/src/__tests__/lib/post-custom-fields.test.ts +170 -0
- package/src/__tests__/lib/post-editor-custom-fields-api.test.ts +129 -0
- package/src/__tests__/lib/post-editor-custom-fields-undo.test.ts +152 -0
- package/src/__tests__/views/post-tab-custom-fields.render.test.tsx +239 -0
- package/src/fields/FieldRenderer.tsx +6 -1
- package/src/lib/post-custom-fields.ts +191 -0
- package/src/lib/post-editor-service.ts +71 -0
- package/src/views/post-editor/PostRowsField.tsx +110 -0
- package/src/views/post-editor/PostSectionEditor.tsx +26 -7
- package/src/views/post-editor/PostTab.tsx +72 -6
|
@@ -49,6 +49,9 @@ const FIELD_MAP: Record<string, React.ComponentType<any>> = {
|
|
|
49
49
|
relationship: RelationshipField,
|
|
50
50
|
date: DateField,
|
|
51
51
|
toggle: ToggleField,
|
|
52
|
+
boolean: ToggleField,
|
|
53
|
+
url: TextField,
|
|
54
|
+
email: TextField,
|
|
52
55
|
array: ArrayField,
|
|
53
56
|
blocks: BlockBuilderField,
|
|
54
57
|
group: GroupField,
|
|
@@ -71,7 +74,9 @@ export function FieldRenderer({ field, value, onChange }: FieldRendererProps) {
|
|
|
71
74
|
return (
|
|
72
75
|
<Component
|
|
73
76
|
label={field.label}
|
|
74
|
-
value
|
|
77
|
+
// Controls default a missing value but not a `null` one, and stored
|
|
78
|
+
// documents hold `null` for cleared optional fields.
|
|
79
|
+
value={value ?? undefined}
|
|
75
80
|
onChange={onChange}
|
|
76
81
|
required={field.required}
|
|
77
82
|
maxLength={field.maxLength}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { humanizeFieldName } from './post-types-service.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fields the post editor already has a home for: the Post tab's identity
|
|
5
|
+
* fields, the Article Body section, the section list, and everything the
|
|
6
|
+
* SEO & GEO tab saves. A post type's remaining fields are its custom fields.
|
|
7
|
+
*/
|
|
8
|
+
const EDITOR_OWNED_FIELDS: ReadonlySet<string> = new Set([
|
|
9
|
+
// Post tab and canvas
|
|
10
|
+
'title',
|
|
11
|
+
'slug',
|
|
12
|
+
'excerpt',
|
|
13
|
+
'featuredImage',
|
|
14
|
+
'body',
|
|
15
|
+
'category',
|
|
16
|
+
'tags',
|
|
17
|
+
'publishDate',
|
|
18
|
+
'sections',
|
|
19
|
+
// SEO & GEO tab
|
|
20
|
+
'metaTitle',
|
|
21
|
+
'metaDescription',
|
|
22
|
+
'focusKeyword',
|
|
23
|
+
'keyTakeaway',
|
|
24
|
+
'canonical',
|
|
25
|
+
'robotsPolicy',
|
|
26
|
+
'noIndex',
|
|
27
|
+
'noindex',
|
|
28
|
+
'noFollow',
|
|
29
|
+
'nofollow',
|
|
30
|
+
'ogTitle',
|
|
31
|
+
'ogDescription',
|
|
32
|
+
'ogImage',
|
|
33
|
+
'twitterTitle',
|
|
34
|
+
'twitterDescription',
|
|
35
|
+
'twitterImage',
|
|
36
|
+
'isCornerstone',
|
|
37
|
+
'schemaType',
|
|
38
|
+
'structuredDataType',
|
|
39
|
+
'structuredData',
|
|
40
|
+
'llmsInclude',
|
|
41
|
+
'llmsSection',
|
|
42
|
+
'llmsOptional',
|
|
43
|
+
'llmsDescription',
|
|
44
|
+
// Read by the server as lifecycle status or the page-builder envelope,
|
|
45
|
+
// never as a plain field value.
|
|
46
|
+
'status',
|
|
47
|
+
'data',
|
|
48
|
+
'layout',
|
|
49
|
+
'pageSettings',
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Field types the Post tab can edit and store correctly. `media`,
|
|
54
|
+
* `relationship`, `number`, `blocks`, `nav`, `componentBlock`, `color`, `json`
|
|
55
|
+
* and multi-value selects are left out: their generic controls either write a
|
|
56
|
+
* placeholder, search the wrong collection, or cannot hold the stored shape.
|
|
57
|
+
*/
|
|
58
|
+
const SIMPLE_TYPES: ReadonlySet<string> = new Set([
|
|
59
|
+
'text',
|
|
60
|
+
'richText',
|
|
61
|
+
'url',
|
|
62
|
+
'email',
|
|
63
|
+
'boolean',
|
|
64
|
+
'date',
|
|
65
|
+
'select',
|
|
66
|
+
])
|
|
67
|
+
|
|
68
|
+
/** Sub-field types a row of an `array` field may hold to be editable here. */
|
|
69
|
+
const ROW_FIELD_TYPES: ReadonlySet<string> = new Set(['text', 'url', 'email'])
|
|
70
|
+
|
|
71
|
+
export interface CustomRowField {
|
|
72
|
+
name: string
|
|
73
|
+
label: string
|
|
74
|
+
required: boolean
|
|
75
|
+
maxLength?: number
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A custom field in the shape `FieldRenderer` takes. */
|
|
79
|
+
export interface CustomFieldDefinition {
|
|
80
|
+
name: string
|
|
81
|
+
/** A `FieldRenderer` type, or `rows` for an array of plain text rows. */
|
|
82
|
+
type: string
|
|
83
|
+
label: string
|
|
84
|
+
required?: boolean
|
|
85
|
+
helpText?: string
|
|
86
|
+
options?: string[]
|
|
87
|
+
/** `rows` only: the columns of each row. */
|
|
88
|
+
rowFields?: CustomRowField[]
|
|
89
|
+
/** `rows` only. */
|
|
90
|
+
maxRows?: number
|
|
91
|
+
[key: string]: unknown
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function optionValues(options: unknown): string[] | undefined {
|
|
95
|
+
if (!Array.isArray(options)) return undefined
|
|
96
|
+
const out: string[] = []
|
|
97
|
+
for (const option of options) {
|
|
98
|
+
if (typeof option === 'string') out.push(option)
|
|
99
|
+
else if (option && typeof option === 'object') {
|
|
100
|
+
const value = (option as { value?: unknown }).value
|
|
101
|
+
if (typeof value === 'string') out.push(value)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return out
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function labelOf(def: Record<string, unknown>, name: string): string {
|
|
108
|
+
return typeof def.label === 'string' && def.label.trim() ? def.label : humanizeFieldName(name)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The columns of an array field, or null when a row holds more than plain text. */
|
|
112
|
+
function rowFieldsOf(def: Record<string, unknown>): CustomRowField[] | null {
|
|
113
|
+
const sub = def.fields
|
|
114
|
+
if (!sub || typeof sub !== 'object' || Array.isArray(sub)) return null
|
|
115
|
+
const out: CustomRowField[] = []
|
|
116
|
+
for (const [name, raw] of Object.entries(sub as Record<string, unknown>)) {
|
|
117
|
+
if (!raw || typeof raw !== 'object') return null
|
|
118
|
+
const field = raw as Record<string, unknown>
|
|
119
|
+
if (typeof field.type !== 'string' || !ROW_FIELD_TYPES.has(field.type)) return null
|
|
120
|
+
// A row is edited and stored whole, so one protected column rules out
|
|
121
|
+
// the array: showing it would expose the column, hiding it could drop it.
|
|
122
|
+
const columnAdmin = field.admin as { readOnly?: unknown } | undefined
|
|
123
|
+
if (field.hidden === true || columnAdmin?.readOnly === true) return null
|
|
124
|
+
out.push({
|
|
125
|
+
name,
|
|
126
|
+
label: labelOf(field, name),
|
|
127
|
+
required: field.required === true,
|
|
128
|
+
maxLength: typeof field.maxLength === 'number' ? field.maxLength : undefined,
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
return out.length > 0 ? out : null
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The post type's own fields, in config order: every declared field the
|
|
136
|
+
* editor does not already manage, that is not hidden or read-only, and that
|
|
137
|
+
* the Post tab has a working control for.
|
|
138
|
+
*/
|
|
139
|
+
export function customPostFields(
|
|
140
|
+
fields: Record<string, unknown> | undefined | null,
|
|
141
|
+
): CustomFieldDefinition[] {
|
|
142
|
+
if (!fields || typeof fields !== 'object') return []
|
|
143
|
+
const out: CustomFieldDefinition[] = []
|
|
144
|
+
for (const [name, raw] of Object.entries(fields)) {
|
|
145
|
+
if (EDITOR_OWNED_FIELDS.has(name)) continue
|
|
146
|
+
if (!raw || typeof raw !== 'object') continue
|
|
147
|
+
const def = raw as Record<string, unknown>
|
|
148
|
+
if (def.hidden === true) continue
|
|
149
|
+
const admin = def.admin as { description?: unknown; readOnly?: unknown } | undefined
|
|
150
|
+
if (admin?.readOnly === true) continue
|
|
151
|
+
const type = typeof def.type === 'string' ? def.type : undefined
|
|
152
|
+
if (!type) continue
|
|
153
|
+
|
|
154
|
+
const base = {
|
|
155
|
+
name,
|
|
156
|
+
label: labelOf(def, name),
|
|
157
|
+
required: def.required === true,
|
|
158
|
+
helpText:
|
|
159
|
+
typeof def.helpText === 'string'
|
|
160
|
+
? def.helpText
|
|
161
|
+
: typeof admin?.description === 'string'
|
|
162
|
+
? admin.description
|
|
163
|
+
: undefined,
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (type === 'array') {
|
|
167
|
+
const rowFields = rowFieldsOf(def)
|
|
168
|
+
if (!rowFields) continue
|
|
169
|
+
out.push({
|
|
170
|
+
...base,
|
|
171
|
+
type: 'rows',
|
|
172
|
+
rowFields,
|
|
173
|
+
maxRows: typeof def.maxRows === 'number' ? def.maxRows : undefined,
|
|
174
|
+
})
|
|
175
|
+
continue
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!SIMPLE_TYPES.has(type)) continue
|
|
179
|
+
if (type === 'select' && def.multiple === true) continue
|
|
180
|
+
// The date control is date-only; it would write `YYYY-MM-DD` over a
|
|
181
|
+
// stored time.
|
|
182
|
+
if (type === 'date' && def.includeTime === true) continue
|
|
183
|
+
out.push({
|
|
184
|
+
...base,
|
|
185
|
+
type,
|
|
186
|
+
maxLength: typeof def.maxLength === 'number' ? def.maxLength : undefined,
|
|
187
|
+
options: optionValues(def.options),
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
return out
|
|
191
|
+
}
|
|
@@ -97,6 +97,19 @@ export interface EditorPost {
|
|
|
97
97
|
seoDescription: string
|
|
98
98
|
publishedAt: string | null
|
|
99
99
|
updatedAt: string | null
|
|
100
|
+
/**
|
|
101
|
+
* Stored values of every `data` key the editor does not manage itself, as
|
|
102
|
+
* loaded. The Post tab reads the post type's own fields (a case study's
|
|
103
|
+
* client name, challenge, key metrics) from here.
|
|
104
|
+
*/
|
|
105
|
+
customFields?: Record<string, unknown>
|
|
106
|
+
/**
|
|
107
|
+
* Custom field values changed since the post was loaded. Only these are
|
|
108
|
+
* written back: the server merges a save onto the stored document, so a key
|
|
109
|
+
* that is not sent keeps its value, and a loaded copy of a key the SEO tab
|
|
110
|
+
* owns is never written back over a newer one.
|
|
111
|
+
*/
|
|
112
|
+
customFieldEdits?: Record<string, unknown>
|
|
100
113
|
}
|
|
101
114
|
|
|
102
115
|
export interface EditorPostTemplate {
|
|
@@ -139,6 +152,57 @@ const REQUIRED_POST_FIELDS = [
|
|
|
139
152
|
'metaDescription',
|
|
140
153
|
] as const
|
|
141
154
|
|
|
155
|
+
/**
|
|
156
|
+
* `data` keys the editor reads into its own `EditorPost` properties. Anything
|
|
157
|
+
* else on the document is a custom field value.
|
|
158
|
+
*/
|
|
159
|
+
const EDITOR_DATA_KEYS: ReadonlySet<string> = new Set(['title', 'slug', ...REQUIRED_POST_FIELDS])
|
|
160
|
+
|
|
161
|
+
function customFieldValues(data: Record<string, unknown>): Record<string, unknown> {
|
|
162
|
+
const out: Record<string, unknown> = {}
|
|
163
|
+
for (const [key, value] of Object.entries(data)) {
|
|
164
|
+
if (!EDITOR_DATA_KEYS.has(key)) out[key] = value
|
|
165
|
+
}
|
|
166
|
+
return out
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The value the Post tab shows for a custom field: the pending edit, else the stored value. */
|
|
170
|
+
export function customFieldValue(post: EditorPost, name: string): unknown {
|
|
171
|
+
const edits = post.customFieldEdits
|
|
172
|
+
if (edits && Object.prototype.hasOwnProperty.call(edits, name)) return edits[name]
|
|
173
|
+
return post.customFields?.[name]
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Re-express a post's custom field values against what the server last
|
|
178
|
+
* stored. Undo and redo restore an older snapshot whose pending edits were
|
|
179
|
+
* taken against an older saved state; since only edits are written, a value
|
|
180
|
+
* the snapshot merely "has" would never reach the server. After this, every
|
|
181
|
+
* custom value that differs from `saved` is a pending edit again.
|
|
182
|
+
*/
|
|
183
|
+
export function rebaseCustomFieldEdits(
|
|
184
|
+
post: EditorPost,
|
|
185
|
+
saved: EditorPost | null,
|
|
186
|
+
/**
|
|
187
|
+
* The fields the Post tab edits. Every other stored key (the SEO tab's
|
|
188
|
+
* focus keyword, canonical, …) keeps its latest saved value: an old
|
|
189
|
+
* snapshot's copy of it must never become a pending edit.
|
|
190
|
+
*/
|
|
191
|
+
fieldNames: readonly string[],
|
|
192
|
+
): EditorPost {
|
|
193
|
+
const stored = saved?.customFields ?? {}
|
|
194
|
+
const edits: Record<string, unknown> = {}
|
|
195
|
+
for (const name of fieldNames) {
|
|
196
|
+
const wanted = customFieldValue(post, name)
|
|
197
|
+
if (JSON.stringify(wanted ?? null) !== JSON.stringify(stored[name] ?? null)) {
|
|
198
|
+
// `null` clears a key the snapshot no longer has; `undefined` would be
|
|
199
|
+
// dropped from the request and leave the stored value in place.
|
|
200
|
+
edits[name] = wanted === undefined ? null : wanted
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return { ...post, customFields: stored, customFieldEdits: edits }
|
|
204
|
+
}
|
|
205
|
+
|
|
142
206
|
export function missingPostFields(config: unknown, postType: string): string[] {
|
|
143
207
|
return missingCollectionFields(config, postType, REQUIRED_POST_FIELDS)
|
|
144
208
|
}
|
|
@@ -321,6 +385,8 @@ function rowToEditorPost(postType: string, doc: DocumentApiRow): EditorPost {
|
|
|
321
385
|
seoDescription: dataStr(data, 'metaDescription'),
|
|
322
386
|
publishedAt: doc.publishedAt ?? null,
|
|
323
387
|
updatedAt: doc.updatedAt ?? null,
|
|
388
|
+
customFields: customFieldValues(data),
|
|
389
|
+
customFieldEdits: {},
|
|
324
390
|
}
|
|
325
391
|
}
|
|
326
392
|
|
|
@@ -419,6 +485,9 @@ function stripUnmanagedMarker(section: PageSection): Omit<PageSection, 'unmanage
|
|
|
419
485
|
|
|
420
486
|
function toWriteBody(post: EditorPost, extra?: Record<string, unknown>): string {
|
|
421
487
|
const payload: PostWritePayload & Record<string, unknown> = {
|
|
488
|
+
// Custom field edits go first so they can never replace a key the editor
|
|
489
|
+
// owns.
|
|
490
|
+
...post.customFieldEdits,
|
|
422
491
|
title: post.title,
|
|
423
492
|
slug: post.slug,
|
|
424
493
|
excerpt: post.excerpt,
|
|
@@ -515,5 +584,7 @@ export async function emptyPostFromTemplate(postType: string): Promise<EditorPos
|
|
|
515
584
|
seoDescription: '',
|
|
516
585
|
publishedAt: null,
|
|
517
586
|
updatedAt: null,
|
|
587
|
+
customFields: {},
|
|
588
|
+
customFieldEdits: {},
|
|
518
589
|
}
|
|
519
590
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { Plus, Trash2 } from 'lucide-react'
|
|
4
|
+
import { Button } from '../../components/ui/Button.js'
|
|
5
|
+
import type { CustomRowField } from '../../lib/post-custom-fields.js'
|
|
6
|
+
|
|
7
|
+
export interface PostRowsFieldProps {
|
|
8
|
+
name: string
|
|
9
|
+
label: string
|
|
10
|
+
helpText?: string
|
|
11
|
+
rowFields: CustomRowField[]
|
|
12
|
+
maxRows?: number
|
|
13
|
+
value: unknown
|
|
14
|
+
onChange: (rows: Array<Record<string, unknown>>) => void
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const INPUT =
|
|
18
|
+
'border-border bg-input-background text-foreground focus-visible:ring-ring w-full rounded-md border px-2.5 py-1.5 text-sm focus-visible:ring-2 focus-visible:outline-none'
|
|
19
|
+
|
|
20
|
+
/** Stored rows as objects. Anything that is not an object row is dropped from view. */
|
|
21
|
+
function toRows(value: unknown): Array<Record<string, unknown>> {
|
|
22
|
+
if (!Array.isArray(value)) return []
|
|
23
|
+
return value.filter(
|
|
24
|
+
(row): row is Record<string, unknown> =>
|
|
25
|
+
Boolean(row) && typeof row === 'object' && !Array.isArray(row),
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An array field whose rows are plain text columns (a case study's key
|
|
31
|
+
* metrics: value and label). Rows are stored flat, `{ value, label }`, the
|
|
32
|
+
* shape the collection declares. Keys a row already carries are kept.
|
|
33
|
+
*/
|
|
34
|
+
export function PostRowsField({
|
|
35
|
+
name,
|
|
36
|
+
label,
|
|
37
|
+
helpText,
|
|
38
|
+
rowFields,
|
|
39
|
+
maxRows,
|
|
40
|
+
value,
|
|
41
|
+
onChange,
|
|
42
|
+
}: PostRowsFieldProps) {
|
|
43
|
+
const rows = toRows(value)
|
|
44
|
+
const atMax = typeof maxRows === 'number' && rows.length >= maxRows
|
|
45
|
+
|
|
46
|
+
function setCell(index: number, key: string, next: string) {
|
|
47
|
+
onChange(rows.map((row, i) => (i === index ? { ...row, [key]: next } : row)))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div>
|
|
52
|
+
<p className="text-foreground mb-1 text-sm font-medium">{label}</p>
|
|
53
|
+
{helpText && <p className="text-muted-foreground mb-2 text-xs">{helpText}</p>}
|
|
54
|
+
<ul className="space-y-2">
|
|
55
|
+
{rows.map((row, index) => (
|
|
56
|
+
<li
|
|
57
|
+
key={index}
|
|
58
|
+
className="border-border bg-card flex items-start gap-2 rounded-md border p-2"
|
|
59
|
+
>
|
|
60
|
+
<div className="min-w-0 flex-1 space-y-1.5">
|
|
61
|
+
{rowFields.map((column) => {
|
|
62
|
+
const id = `post-rows-${name}-${index}-${column.name}`
|
|
63
|
+
const cell = row[column.name]
|
|
64
|
+
return (
|
|
65
|
+
<div key={column.name}>
|
|
66
|
+
<label htmlFor={id} className="text-muted-foreground mb-0.5 block text-xs">
|
|
67
|
+
{column.label}
|
|
68
|
+
{column.required && <span className="text-destructive ml-0.5">*</span>}
|
|
69
|
+
</label>
|
|
70
|
+
<input
|
|
71
|
+
id={id}
|
|
72
|
+
type="text"
|
|
73
|
+
className={INPUT}
|
|
74
|
+
value={typeof cell === 'string' ? cell : ''}
|
|
75
|
+
maxLength={column.maxLength}
|
|
76
|
+
onChange={(event) => setCell(index, column.name, event.target.value)}
|
|
77
|
+
/>
|
|
78
|
+
</div>
|
|
79
|
+
)
|
|
80
|
+
})}
|
|
81
|
+
</div>
|
|
82
|
+
<Button
|
|
83
|
+
type="button"
|
|
84
|
+
variant="ghost"
|
|
85
|
+
size="icon"
|
|
86
|
+
aria-label={`Remove ${label} row ${index + 1}`}
|
|
87
|
+
onClick={() => onChange(rows.filter((_, i) => i !== index))}
|
|
88
|
+
>
|
|
89
|
+
<Trash2 className="h-4 w-4" aria-hidden />
|
|
90
|
+
</Button>
|
|
91
|
+
</li>
|
|
92
|
+
))}
|
|
93
|
+
</ul>
|
|
94
|
+
<Button
|
|
95
|
+
type="button"
|
|
96
|
+
variant="secondary"
|
|
97
|
+
size="sm"
|
|
98
|
+
className="mt-2"
|
|
99
|
+
disabled={atMax}
|
|
100
|
+
onClick={() =>
|
|
101
|
+
onChange([...rows, Object.fromEntries(rowFields.map((column) => [column.name, '']))])
|
|
102
|
+
}
|
|
103
|
+
>
|
|
104
|
+
<Plus className="mr-1 h-3.5 w-3.5" aria-hidden />
|
|
105
|
+
Add row
|
|
106
|
+
</Button>
|
|
107
|
+
{atMax && <p className="text-muted-foreground mt-1 text-xs">Up to {maxRows} rows.</p>}
|
|
108
|
+
</div>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
@@ -26,6 +26,7 @@ import { usePageAudit } from '../../hooks/usePageAudit.js'
|
|
|
26
26
|
import {
|
|
27
27
|
emptyPostFromTemplate,
|
|
28
28
|
fetchPostForEditor,
|
|
29
|
+
rebaseCustomFieldEdits,
|
|
29
30
|
fetchTemplateForEditor,
|
|
30
31
|
missingPostFields,
|
|
31
32
|
savePostDraft,
|
|
@@ -68,6 +69,7 @@ import { inspectorIdFromFieldPath } from '../../lib/preview-bridge.js'
|
|
|
68
69
|
import { PostEditorCanvas } from './PostEditorCanvas.js'
|
|
69
70
|
import { PostFieldsModal, type PostFieldConfig } from './PostFieldsModal.js'
|
|
70
71
|
import { PostTab } from './PostTab.js'
|
|
72
|
+
import { customPostFields } from '../../lib/post-custom-fields.js'
|
|
71
73
|
|
|
72
74
|
/**
|
|
73
75
|
* The lower of the two audit scores — the tab dot should reflect the half that
|
|
@@ -441,12 +443,24 @@ export function PostSectionEditor({
|
|
|
441
443
|
[selectedId, mutateSections],
|
|
442
444
|
)
|
|
443
445
|
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
446
|
+
const customFieldNames = useMemo(
|
|
447
|
+
() => customPostFields(config?.collections?.[postType]?.fields).map((field) => field.name),
|
|
448
|
+
[config, postType],
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
const restoreEdit = useCallback(
|
|
452
|
+
(restored: EditorPost | null) => {
|
|
453
|
+
if (!restored) return
|
|
454
|
+
// Only custom field edits are written, so a snapshot taken before the
|
|
455
|
+
// last save must be re-expressed against what the server holds now.
|
|
456
|
+
const saved = savedJson.current ? (JSON.parse(savedJson.current) as EditorPost) : null
|
|
457
|
+
const next = rebaseCustomFieldEdits(restored, saved, customFieldNames)
|
|
458
|
+
setPost(next)
|
|
459
|
+
setDirty(JSON.stringify(next) !== savedJson.current)
|
|
460
|
+
setSaveState('idle')
|
|
461
|
+
},
|
|
462
|
+
[customFieldNames],
|
|
463
|
+
)
|
|
450
464
|
|
|
451
465
|
const handleUndo = useCallback(() => restoreEdit(undo()), [restoreEdit, undo])
|
|
452
466
|
const handleRedo = useCallback(() => restoreEdit(redo()), [restoreEdit, redo])
|
|
@@ -456,7 +470,12 @@ export function PostSectionEditor({
|
|
|
456
470
|
setPost((prev) => {
|
|
457
471
|
if (!prev) return prev
|
|
458
472
|
note(prev, 'fields', true)
|
|
459
|
-
|
|
473
|
+
// Custom field edits arrive one key at a time; merge them so an
|
|
474
|
+
// edit to one field never drops a pending edit to another.
|
|
475
|
+
const customFieldEdits = patch.customFieldEdits
|
|
476
|
+
? { ...prev.customFieldEdits, ...patch.customFieldEdits }
|
|
477
|
+
: prev.customFieldEdits
|
|
478
|
+
return { ...prev, ...patch, customFieldEdits }
|
|
460
479
|
})
|
|
461
480
|
setDirty(true)
|
|
462
481
|
setSaveState('idle')
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import { useState } from 'react'
|
|
4
|
-
import type
|
|
3
|
+
import { useMemo, useState } from 'react'
|
|
4
|
+
import { customFieldValue, type EditorPost } from '../../lib/post-editor-service.js'
|
|
5
|
+
import { customPostFields, type CustomFieldDefinition } from '../../lib/post-custom-fields.js'
|
|
6
|
+
import { FieldRenderer } from '../../fields/FieldRenderer.js'
|
|
5
7
|
import { PostFieldsForm, type PostFieldConfig } from './PostFieldsForm.js'
|
|
8
|
+
import { PostRowsField } from './PostRowsField.js'
|
|
6
9
|
|
|
7
10
|
export interface PostTabProps {
|
|
8
11
|
post: EditorPost
|
|
@@ -12,14 +15,41 @@ export interface PostTabProps {
|
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
/**
|
|
15
|
-
*
|
|
18
|
+
* What a control is given for a stored value. Controls default a missing
|
|
19
|
+
* value but not a `null` one, and the date control takes `YYYY-MM-DD` where
|
|
20
|
+
* documents often hold a full timestamp.
|
|
21
|
+
*/
|
|
22
|
+
function controlValue(field: CustomFieldDefinition, stored: unknown): unknown {
|
|
23
|
+
if (stored === null || stored === undefined) return undefined
|
|
24
|
+
if (field.type === 'date') return typeof stored === 'string' ? stored.slice(0, 10) : undefined
|
|
25
|
+
if (field.type === 'boolean') return stored === true
|
|
26
|
+
if (field.type === 'select') return typeof stored === 'string' ? stored : undefined
|
|
27
|
+
if (field.type === 'rows') return stored
|
|
28
|
+
return typeof stored === 'string' ? stored : undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Rail tab 3 — post fields.
|
|
16
33
|
*
|
|
17
|
-
* Title, slug, excerpt, featured image, category, and tags
|
|
18
|
-
*
|
|
19
|
-
*
|
|
34
|
+
* Title, slug, excerpt, featured image, category, and tags, then the post
|
|
35
|
+
* type's own fields (a case study's client name, challenge, key metrics).
|
|
36
|
+
* Without those a site template that reads them renders an empty page, and
|
|
37
|
+
* the editor has no other place to fill them. Layout, header chrome, and
|
|
38
|
+
* template controls stay design-owned on the type template. Address / 301 /
|
|
39
|
+
* robots / author / danger-zone belong to pages, not posts.
|
|
20
40
|
*/
|
|
21
41
|
export function PostTab({ post, canEdit, fields, onChange }: PostTabProps) {
|
|
22
42
|
const [slugTouched, setSlugTouched] = useState(Boolean(post.slug))
|
|
43
|
+
const customFields = useMemo(() => customPostFields(fields), [fields])
|
|
44
|
+
|
|
45
|
+
// Send one key. The editor merges it into the pending edits, so two changes
|
|
46
|
+
// in the same tick cannot drop each other.
|
|
47
|
+
// A disabled fieldset does not stop a rich text editor's contenteditable,
|
|
48
|
+
// so a viewer who cannot edit is also stopped here.
|
|
49
|
+
const edit = (name: string, value: unknown) => {
|
|
50
|
+
if (!canEdit) return
|
|
51
|
+
onChange({ customFieldEdits: { [name]: value } })
|
|
52
|
+
}
|
|
23
53
|
|
|
24
54
|
return (
|
|
25
55
|
<div className="p-3">
|
|
@@ -42,6 +72,42 @@ export function PostTab({ post, canEdit, fields, onChange }: PostTabProps) {
|
|
|
42
72
|
onSlugTouched={() => setSlugTouched(true)}
|
|
43
73
|
onChange={(patch) => onChange(patch)}
|
|
44
74
|
/>
|
|
75
|
+
{customFields.length > 0 && (
|
|
76
|
+
<fieldset
|
|
77
|
+
disabled={!canEdit}
|
|
78
|
+
inert={!canEdit}
|
|
79
|
+
data-testid="post-custom-fields"
|
|
80
|
+
className={`border-border mt-4 space-y-3 border-t pt-4 ${canEdit ? '' : 'pointer-events-none opacity-60'}`}
|
|
81
|
+
>
|
|
82
|
+
<legend className="text-muted-foreground float-left mb-3 w-full text-xs font-medium tracking-wide uppercase">
|
|
83
|
+
More fields
|
|
84
|
+
</legend>
|
|
85
|
+
{customFields.map((field) => {
|
|
86
|
+
const value = controlValue(field, customFieldValue(post, field.name))
|
|
87
|
+
return (
|
|
88
|
+
<div key={field.name} className="clear-both">
|
|
89
|
+
{field.type === 'rows' ? (
|
|
90
|
+
<PostRowsField
|
|
91
|
+
name={field.name}
|
|
92
|
+
label={field.label}
|
|
93
|
+
helpText={field.helpText}
|
|
94
|
+
rowFields={field.rowFields ?? []}
|
|
95
|
+
maxRows={field.maxRows}
|
|
96
|
+
value={value}
|
|
97
|
+
onChange={(rows) => edit(field.name, rows)}
|
|
98
|
+
/>
|
|
99
|
+
) : (
|
|
100
|
+
<FieldRenderer
|
|
101
|
+
field={field}
|
|
102
|
+
value={value}
|
|
103
|
+
onChange={(next) => edit(field.name, next)}
|
|
104
|
+
/>
|
|
105
|
+
)}
|
|
106
|
+
</div>
|
|
107
|
+
)
|
|
108
|
+
})}
|
|
109
|
+
</fieldset>
|
|
110
|
+
)}
|
|
45
111
|
</div>
|
|
46
112
|
)
|
|
47
113
|
}
|