@prenta/admin 2.16.0 → 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 +14 -0
- package/dist/__tests__/components/presence-indicator.render.test.d.ts +2 -0
- package/dist/__tests__/components/presence-indicator.render.test.d.ts.map +1 -0
- package/dist/__tests__/components/presence-indicator.render.test.js +97 -0
- package/dist/__tests__/components/presence-indicator.render.test.js.map +1 -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/audit-media.test.js +6 -2
- package/dist/__tests__/views/audit-media.test.js.map +1 -1
- 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/components/PresenceIndicator.d.ts.map +1 -1
- package/dist/components/PresenceIndicator.js +10 -1
- package/dist/components/PresenceIndicator.js.map +1 -1
- 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/notifications-client.d.ts +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 +6 -6
- package/src/__tests__/components/presence-indicator.render.test.tsx +114 -0
- 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/audit-media.test.tsx +6 -2
- package/src/__tests__/views/post-tab-custom-fields.render.test.tsx +239 -0
- package/src/components/PresenceIndicator.tsx +10 -1
- package/src/fields/FieldRenderer.tsx +6 -1
- package/src/lib/notifications-client.ts +1 -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
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
import { cleanup, fireEvent, render } from '@testing-library/react'
|
|
4
|
+
|
|
5
|
+
// The identity form has its own tests; keep this one on the custom fields.
|
|
6
|
+
vi.mock('../../views/post-editor/PostFieldsForm.js', () => ({
|
|
7
|
+
PostFieldsForm: () => null,
|
|
8
|
+
}))
|
|
9
|
+
|
|
10
|
+
const { PostTab } = await import('../../views/post-editor/PostTab.js')
|
|
11
|
+
|
|
12
|
+
afterEach(cleanup)
|
|
13
|
+
|
|
14
|
+
function post(overrides: Record<string, unknown> = {}) {
|
|
15
|
+
return {
|
|
16
|
+
id: 'cs1',
|
|
17
|
+
postType: 'case-studies',
|
|
18
|
+
title: 'Mary Frances',
|
|
19
|
+
slug: 'mary-frances',
|
|
20
|
+
excerpt: '',
|
|
21
|
+
featuredImage: '',
|
|
22
|
+
featuredImageUrl: '',
|
|
23
|
+
body: '',
|
|
24
|
+
category: '',
|
|
25
|
+
authorName: '',
|
|
26
|
+
canvasRelatedPosts: null,
|
|
27
|
+
tags: [],
|
|
28
|
+
status: 'DRAFT' as const,
|
|
29
|
+
workflowStage: 'DRAFT' as const,
|
|
30
|
+
reviewNote: null,
|
|
31
|
+
publishDate: null,
|
|
32
|
+
scheduledAt: null,
|
|
33
|
+
scheduledUnpublishAt: null,
|
|
34
|
+
sections: [],
|
|
35
|
+
seoTitle: '',
|
|
36
|
+
seoDescription: '',
|
|
37
|
+
publishedAt: null,
|
|
38
|
+
updatedAt: null,
|
|
39
|
+
customFields: { clientName: 'Mary Frances' },
|
|
40
|
+
customFieldEdits: {},
|
|
41
|
+
...overrides,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const fields = {
|
|
46
|
+
title: { type: 'text', label: 'Title' },
|
|
47
|
+
body: { type: 'richText', label: 'Body' },
|
|
48
|
+
sections: { type: 'json', label: 'Sections' },
|
|
49
|
+
clientName: { type: 'text', label: 'Client name', required: true },
|
|
50
|
+
industry: { type: 'text', label: 'Industry' },
|
|
51
|
+
ctaUrl: { type: 'url', label: 'CTA link' },
|
|
52
|
+
contact: { type: 'email', label: 'Contact email' },
|
|
53
|
+
featured: { type: 'boolean', label: 'Featured' },
|
|
54
|
+
publishedDate: { type: 'date', label: 'Published' },
|
|
55
|
+
keyMetrics: {
|
|
56
|
+
type: 'array',
|
|
57
|
+
label: 'Key metrics',
|
|
58
|
+
maxRows: 2,
|
|
59
|
+
fields: {
|
|
60
|
+
value: { type: 'text', label: 'Value', required: true },
|
|
61
|
+
label: { type: 'text', label: 'Label', required: true },
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
logo: { type: 'media', label: 'Client logo' },
|
|
65
|
+
services: { type: 'relationship', label: 'Services', relationTo: 'services' },
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function inputFor(container: HTMLElement, label: string): HTMLInputElement {
|
|
69
|
+
const labels = [...container.querySelectorAll('label')]
|
|
70
|
+
const match = labels.find((l) => l.textContent?.includes(label))
|
|
71
|
+
const input = match?.parentElement?.querySelector('input')
|
|
72
|
+
if (!input) throw new Error(`no input for ${label}`)
|
|
73
|
+
return input
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
describe('PostTab custom fields', () => {
|
|
77
|
+
it("renders the post type's own fields with their stored values", () => {
|
|
78
|
+
const { container, getByTestId } = render(
|
|
79
|
+
<PostTab post={post()} canEdit fields={fields} onChange={() => {}} />,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
const group = getByTestId('post-custom-fields')
|
|
83
|
+
for (const label of ['Client name', 'Industry', 'CTA link', 'Contact email', 'Featured']) {
|
|
84
|
+
expect(group.textContent).toContain(label)
|
|
85
|
+
}
|
|
86
|
+
expect(group.textContent).not.toContain('Unknown field type')
|
|
87
|
+
expect(inputFor(container, 'Client name').value).toBe('Mary Frances')
|
|
88
|
+
expect(inputFor(container, 'Industry').value).toBe('')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('does not offer controls that would store a placeholder or a wrong reference', () => {
|
|
92
|
+
const { getByTestId } = render(
|
|
93
|
+
<PostTab post={post()} canEdit fields={fields} onChange={() => {}} />,
|
|
94
|
+
)
|
|
95
|
+
const text = getByTestId('post-custom-fields').textContent
|
|
96
|
+
expect(text).not.toContain('Client logo')
|
|
97
|
+
expect(text).not.toContain('Services')
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('reports an edit as that one field', () => {
|
|
101
|
+
const onChange = vi.fn()
|
|
102
|
+
const { container } = render(
|
|
103
|
+
<PostTab
|
|
104
|
+
post={post({ customFieldEdits: { industry: 'Fashion' } })}
|
|
105
|
+
canEdit
|
|
106
|
+
fields={fields}
|
|
107
|
+
onChange={onChange}
|
|
108
|
+
/>,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
fireEvent.change(inputFor(container, 'Client name'), { target: { value: 'MF' } })
|
|
112
|
+
|
|
113
|
+
// The editor merges this into the pending edits, so `industry` survives.
|
|
114
|
+
expect(onChange).toHaveBeenCalledWith({ customFieldEdits: { clientName: 'MF' } })
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('renders stored null values as empty instead of crashing', () => {
|
|
118
|
+
const { container } = render(
|
|
119
|
+
<PostTab
|
|
120
|
+
post={post({ customFields: { clientName: null, ctaUrl: null, contact: null } })}
|
|
121
|
+
canEdit
|
|
122
|
+
fields={fields}
|
|
123
|
+
onChange={() => {}}
|
|
124
|
+
/>,
|
|
125
|
+
)
|
|
126
|
+
expect(inputFor(container, 'Client name').value).toBe('')
|
|
127
|
+
expect(inputFor(container, 'CTA link').value).toBe('')
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('shows the date part of a stored timestamp', () => {
|
|
131
|
+
const { container } = render(
|
|
132
|
+
<PostTab
|
|
133
|
+
post={post({ customFields: { publishedDate: '2026-11-26T15:41:39.442Z' } })}
|
|
134
|
+
canEdit
|
|
135
|
+
fields={fields}
|
|
136
|
+
onChange={() => {}}
|
|
137
|
+
/>,
|
|
138
|
+
)
|
|
139
|
+
expect(inputFor(container, 'Published').value).toBe('2026-11-26')
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('edits key metrics as flat rows and keeps the other rows', () => {
|
|
143
|
+
const onChange = vi.fn()
|
|
144
|
+
const { container } = render(
|
|
145
|
+
<PostTab
|
|
146
|
+
post={post({
|
|
147
|
+
customFields: {
|
|
148
|
+
keyMetrics: [
|
|
149
|
+
{ value: '+60%', label: 'Revenue', source: 'GA4' },
|
|
150
|
+
{ value: '+153%', label: 'Organic' },
|
|
151
|
+
],
|
|
152
|
+
},
|
|
153
|
+
})}
|
|
154
|
+
canEdit
|
|
155
|
+
fields={fields}
|
|
156
|
+
onChange={onChange}
|
|
157
|
+
/>,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
const first = container.querySelector<HTMLInputElement>('#post-rows-keyMetrics-0-value')
|
|
161
|
+
expect(first?.value).toBe('+60%')
|
|
162
|
+
fireEvent.change(first!, { target: { value: '+61%' } })
|
|
163
|
+
|
|
164
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
165
|
+
customFieldEdits: {
|
|
166
|
+
keyMetrics: [
|
|
167
|
+
{ value: '+61%', label: 'Revenue', source: 'GA4' },
|
|
168
|
+
{ value: '+153%', label: 'Organic' },
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('adds an empty row with the declared columns and stops at the row limit', () => {
|
|
175
|
+
const onChange = vi.fn()
|
|
176
|
+
const { getByText, rerender } = render(
|
|
177
|
+
<PostTab
|
|
178
|
+
post={post({ customFields: { keyMetrics: [{ value: '+60%', label: 'Revenue' }] } })}
|
|
179
|
+
canEdit
|
|
180
|
+
fields={fields}
|
|
181
|
+
onChange={onChange}
|
|
182
|
+
/>,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
fireEvent.click(getByText('Add row'))
|
|
186
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
187
|
+
customFieldEdits: {
|
|
188
|
+
keyMetrics: [
|
|
189
|
+
{ value: '+60%', label: 'Revenue' },
|
|
190
|
+
{ value: '', label: '' },
|
|
191
|
+
],
|
|
192
|
+
},
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
rerender(
|
|
196
|
+
<PostTab
|
|
197
|
+
post={post({
|
|
198
|
+
customFields: {
|
|
199
|
+
keyMetrics: [
|
|
200
|
+
{ value: '+60%', label: 'Revenue' },
|
|
201
|
+
{ value: '+153%', label: 'Organic' },
|
|
202
|
+
],
|
|
203
|
+
},
|
|
204
|
+
})}
|
|
205
|
+
canEdit
|
|
206
|
+
fields={fields}
|
|
207
|
+
onChange={onChange}
|
|
208
|
+
/>,
|
|
209
|
+
)
|
|
210
|
+
expect((getByText('Add row').closest('button') as HTMLButtonElement).disabled).toBe(true)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('shows nothing extra for a post type with only the shared fields', () => {
|
|
214
|
+
const { queryByTestId } = render(
|
|
215
|
+
<PostTab
|
|
216
|
+
post={post()}
|
|
217
|
+
canEdit
|
|
218
|
+
fields={{ title: fields.title, body: fields.body }}
|
|
219
|
+
onChange={() => {}}
|
|
220
|
+
/>,
|
|
221
|
+
)
|
|
222
|
+
expect(queryByTestId('post-custom-fields')).toBeNull()
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('disables the fields for a viewer who cannot edit, and ignores any edit that gets through', () => {
|
|
226
|
+
const onChange = vi.fn()
|
|
227
|
+
const { container, getByTestId } = render(
|
|
228
|
+
<PostTab post={post()} canEdit={false} fields={fields} onChange={onChange} />,
|
|
229
|
+
)
|
|
230
|
+
const group = getByTestId('post-custom-fields') as HTMLFieldSetElement
|
|
231
|
+
expect(group.disabled).toBe(true)
|
|
232
|
+
// inert keeps keyboard focus out of a rich text editor, which a disabled
|
|
233
|
+
// fieldset does not.
|
|
234
|
+
expect(group.hasAttribute('inert')).toBe(true)
|
|
235
|
+
|
|
236
|
+
fireEvent.change(inputFor(container, 'Client name'), { target: { value: 'MF' } })
|
|
237
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
238
|
+
})
|
|
239
|
+
})
|
|
@@ -13,6 +13,9 @@ interface PresenceIndicatorProps {
|
|
|
13
13
|
currentUserId?: string
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/** `EventSource.CLOSED`: the browser has given up and will not reconnect. */
|
|
17
|
+
const EVENT_SOURCE_CLOSED = 2
|
|
18
|
+
|
|
16
19
|
/** The avatar palette (tokens.json `--avatar-*`): white initials clear 4.5:1 on each. */
|
|
17
20
|
const COLORS = Array.from({ length: 8 }, (_, i) => `var(--avatar-${i + 1})`)
|
|
18
21
|
|
|
@@ -37,7 +40,13 @@ export function PresenceIndicator({ documentId, currentUserId }: PresenceIndicat
|
|
|
37
40
|
})
|
|
38
41
|
|
|
39
42
|
es.onerror = () => {
|
|
40
|
-
|
|
43
|
+
// The server ends the stream on purpose after a bounded lifetime
|
|
44
|
+
// (20 s on Vercel), and the browser reports that as an error
|
|
45
|
+
// before reconnecting on its own. Leave the connection and the
|
|
46
|
+
// current list alone: the first event after the reconnect carries
|
|
47
|
+
// the full list again. Only a connection the browser has given up
|
|
48
|
+
// on (an HTTP error status) is final, and then the list is stale.
|
|
49
|
+
if (es.readyState === EVENT_SOURCE_CLOSED) setUsers([])
|
|
41
50
|
}
|
|
42
51
|
|
|
43
52
|
heartbeatRef.current = setInterval(() => {
|
|
@@ -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}
|
|
@@ -131,7 +131,7 @@ export async function markAllNotificationsRead(): Promise<NotificationOutcome<nu
|
|
|
131
131
|
* poll loop in the notification bell.
|
|
132
132
|
*
|
|
133
133
|
* The browser's native `EventSource` handles reconnection for us — on
|
|
134
|
-
* a server-initiated close (the stream caps
|
|
134
|
+
* a server-initiated close (the stream caps its own lifetime by design),
|
|
135
135
|
* the connection re-opens automatically. We DO NOT abort the poll
|
|
136
136
|
* fallback in callers: SSE is a latency-win, the poll is what keeps
|
|
137
137
|
* multi-node deployments correct (each node only pushes to its own
|
|
@@ -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
|
}
|