@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.
Files changed (70) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/__tests__/components/presence-indicator.render.test.d.ts +2 -0
  3. package/dist/__tests__/components/presence-indicator.render.test.d.ts.map +1 -0
  4. package/dist/__tests__/components/presence-indicator.render.test.js +97 -0
  5. package/dist/__tests__/components/presence-indicator.render.test.js.map +1 -0
  6. package/dist/__tests__/fields/field-renderer-null.render.test.d.ts +2 -0
  7. package/dist/__tests__/fields/field-renderer-null.render.test.d.ts.map +1 -0
  8. package/dist/__tests__/fields/field-renderer-null.render.test.js +21 -0
  9. package/dist/__tests__/fields/field-renderer-null.render.test.js.map +1 -0
  10. package/dist/__tests__/lib/post-custom-fields.test.d.ts +2 -0
  11. package/dist/__tests__/lib/post-custom-fields.test.d.ts.map +1 -0
  12. package/dist/__tests__/lib/post-custom-fields.test.js +158 -0
  13. package/dist/__tests__/lib/post-custom-fields.test.js.map +1 -0
  14. package/dist/__tests__/lib/post-editor-custom-fields-api.test.d.ts +2 -0
  15. package/dist/__tests__/lib/post-editor-custom-fields-api.test.d.ts.map +1 -0
  16. package/dist/__tests__/lib/post-editor-custom-fields-api.test.js +104 -0
  17. package/dist/__tests__/lib/post-editor-custom-fields-api.test.js.map +1 -0
  18. package/dist/__tests__/lib/post-editor-custom-fields-undo.test.d.ts +2 -0
  19. package/dist/__tests__/lib/post-editor-custom-fields-undo.test.d.ts.map +1 -0
  20. package/dist/__tests__/lib/post-editor-custom-fields-undo.test.js +128 -0
  21. package/dist/__tests__/lib/post-editor-custom-fields-undo.test.js.map +1 -0
  22. package/dist/__tests__/views/audit-media.test.js +6 -2
  23. package/dist/__tests__/views/audit-media.test.js.map +1 -1
  24. package/dist/__tests__/views/post-tab-custom-fields.render.test.d.ts +2 -0
  25. package/dist/__tests__/views/post-tab-custom-fields.render.test.d.ts.map +1 -0
  26. package/dist/__tests__/views/post-tab-custom-fields.render.test.js +164 -0
  27. package/dist/__tests__/views/post-tab-custom-fields.render.test.js.map +1 -0
  28. package/dist/components/PresenceIndicator.d.ts.map +1 -1
  29. package/dist/components/PresenceIndicator.js +10 -1
  30. package/dist/components/PresenceIndicator.js.map +1 -1
  31. package/dist/fields/FieldRenderer.d.ts.map +1 -1
  32. package/dist/fields/FieldRenderer.js +7 -1
  33. package/dist/fields/FieldRenderer.js.map +1 -1
  34. package/dist/lib/notifications-client.d.ts +1 -1
  35. package/dist/lib/post-custom-fields.d.ts +28 -0
  36. package/dist/lib/post-custom-fields.d.ts.map +1 -0
  37. package/dist/lib/post-custom-fields.js +173 -0
  38. package/dist/lib/post-custom-fields.js.map +1 -0
  39. package/dist/lib/post-editor-service.d.ts +29 -0
  40. package/dist/lib/post-editor-service.d.ts.map +1 -1
  41. package/dist/lib/post-editor-service.js +53 -0
  42. package/dist/lib/post-editor-service.js.map +1 -1
  43. package/dist/prenta-admin.css +1 -1
  44. package/dist/views/post-editor/PostRowsField.d.ts +17 -0
  45. package/dist/views/post-editor/PostRowsField.d.ts.map +1 -0
  46. package/dist/views/post-editor/PostRowsField.js +29 -0
  47. package/dist/views/post-editor/PostRowsField.js.map +1 -0
  48. package/dist/views/post-editor/PostSectionEditor.d.ts.map +1 -1
  49. package/dist/views/post-editor/PostSectionEditor.js +16 -5
  50. package/dist/views/post-editor/PostSectionEditor.js.map +1 -1
  51. package/dist/views/post-editor/PostTab.d.ts +8 -5
  52. package/dist/views/post-editor/PostTab.d.ts.map +1 -1
  53. package/dist/views/post-editor/PostTab.js +53 -15
  54. package/dist/views/post-editor/PostTab.js.map +1 -1
  55. package/package.json +6 -6
  56. package/src/__tests__/components/presence-indicator.render.test.tsx +114 -0
  57. package/src/__tests__/fields/field-renderer-null.render.test.tsx +34 -0
  58. package/src/__tests__/lib/post-custom-fields.test.ts +170 -0
  59. package/src/__tests__/lib/post-editor-custom-fields-api.test.ts +129 -0
  60. package/src/__tests__/lib/post-editor-custom-fields-undo.test.ts +152 -0
  61. package/src/__tests__/views/audit-media.test.tsx +6 -2
  62. package/src/__tests__/views/post-tab-custom-fields.render.test.tsx +239 -0
  63. package/src/components/PresenceIndicator.tsx +10 -1
  64. package/src/fields/FieldRenderer.tsx +6 -1
  65. package/src/lib/notifications-client.ts +1 -1
  66. package/src/lib/post-custom-fields.ts +191 -0
  67. package/src/lib/post-editor-service.ts +71 -0
  68. package/src/views/post-editor/PostRowsField.tsx +110 -0
  69. package/src/views/post-editor/PostSectionEditor.tsx +26 -7
  70. package/src/views/post-editor/PostTab.tsx +72 -6
@@ -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 restoreEdit = useCallback((next: EditorPost | null) => {
445
- if (!next) return
446
- setPost(next)
447
- setDirty(JSON.stringify(next) !== savedJson.current)
448
- setSaveState('idle')
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
- return { ...prev, ...patch }
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 { EditorPost } from '../../lib/post-editor-service.js'
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
- * Rail tab 3 — post identity fields only.
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. Layout, header
18
- * chrome, and template controls stay design-owned on the type template.
19
- * Address / 301 / robots / author / danger-zone belong to pages, not posts.
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
  }