@velocity-astro/cms-admin 0.1.0-beta.6 → 0.1.0-beta.7

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 (109) hide show
  1. package/app-dist/assets/{RichTextEditor-BiKJJtkz.js → RichTextEditor-BpltDhwX.js} +1 -1
  2. package/app-dist/assets/index-Bx7Xh90l.js +80 -0
  3. package/app-dist/assets/index-DLXIqdLb.css +2 -0
  4. package/app-dist/index.html +5 -3
  5. package/dist/app/AdminLayout.d.ts.map +1 -1
  6. package/dist/app/AdminLayout.js +1 -1
  7. package/dist/app/AdminLayout.js.map +1 -1
  8. package/dist/app/paths.d.ts +2 -0
  9. package/dist/app/paths.d.ts.map +1 -1
  10. package/dist/app/paths.js +2 -0
  11. package/dist/app/paths.js.map +1 -1
  12. package/dist/client/queries.d.ts.map +1 -1
  13. package/dist/client/queries.js +32 -7
  14. package/dist/client/queries.js.map +1 -1
  15. package/dist/fields/BlocksControl.d.ts +6 -0
  16. package/dist/fields/BlocksControl.d.ts.map +1 -1
  17. package/dist/fields/BlocksControl.js +301 -89
  18. package/dist/fields/BlocksControl.js.map +1 -1
  19. package/dist/fields/SiteImagePicker.d.ts +22 -0
  20. package/dist/fields/SiteImagePicker.d.ts.map +1 -0
  21. package/dist/fields/SiteImagePicker.js +42 -0
  22. package/dist/fields/SiteImagePicker.js.map +1 -0
  23. package/dist/fields/block-icons.d.ts.map +1 -1
  24. package/dist/fields/block-icons.js +3 -1
  25. package/dist/fields/block-icons.js.map +1 -1
  26. package/dist/fields/carry.d.ts +19 -0
  27. package/dist/fields/carry.d.ts.map +1 -0
  28. package/dist/fields/carry.js +61 -0
  29. package/dist/fields/carry.js.map +1 -0
  30. package/dist/fields/controls.d.ts.map +1 -1
  31. package/dist/fields/controls.js +82 -58
  32. package/dist/fields/controls.js.map +1 -1
  33. package/dist/fields/field-control.d.ts +80 -0
  34. package/dist/fields/field-control.d.ts.map +1 -1
  35. package/dist/fields/field-control.js.map +1 -1
  36. package/dist/fields/page-focus.d.ts +32 -3
  37. package/dist/fields/page-focus.d.ts.map +1 -1
  38. package/dist/fields/page-focus.js +55 -1
  39. package/dist/fields/page-focus.js.map +1 -1
  40. package/dist/lib/open-site.d.ts +6 -0
  41. package/dist/lib/open-site.d.ts.map +1 -0
  42. package/dist/lib/open-site.js +8 -0
  43. package/dist/lib/open-site.js.map +1 -0
  44. package/dist/lib/rich-from-html.d.ts +9 -0
  45. package/dist/lib/rich-from-html.d.ts.map +1 -0
  46. package/dist/lib/rich-from-html.js +129 -0
  47. package/dist/lib/rich-from-html.js.map +1 -0
  48. package/dist/lib/sittings.d.ts +16 -0
  49. package/dist/lib/sittings.d.ts.map +1 -0
  50. package/dist/lib/sittings.js +29 -0
  51. package/dist/lib/sittings.js.map +1 -0
  52. package/dist/lib/use-history.d.ts +24 -0
  53. package/dist/lib/use-history.d.ts.map +1 -0
  54. package/dist/lib/use-history.js +48 -0
  55. package/dist/lib/use-history.js.map +1 -0
  56. package/dist/lib/use-narrow.d.ts +5 -0
  57. package/dist/lib/use-narrow.d.ts.map +1 -0
  58. package/dist/lib/use-narrow.js +19 -0
  59. package/dist/lib/use-narrow.js.map +1 -0
  60. package/dist/lib/use-save-key.d.ts +15 -0
  61. package/dist/lib/use-save-key.d.ts.map +1 -0
  62. package/dist/lib/use-save-key.js +27 -0
  63. package/dist/lib/use-save-key.js.map +1 -0
  64. package/dist/lib/use-undo-keys.d.ts +14 -0
  65. package/dist/lib/use-undo-keys.d.ts.map +1 -0
  66. package/dist/lib/use-undo-keys.js +46 -0
  67. package/dist/lib/use-undo-keys.js.map +1 -0
  68. package/dist/messages/en.d.ts +29 -1
  69. package/dist/messages/en.d.ts.map +1 -1
  70. package/dist/messages/en.js +29 -1
  71. package/dist/messages/en.js.map +1 -1
  72. package/dist/page-bridge/preview-bridge.d.ts +3 -3
  73. package/dist/page-bridge/preview-bridge.d.ts.map +1 -1
  74. package/dist/page-bridge/preview-bridge.js +868 -33
  75. package/dist/page-bridge/preview-bridge.js.map +1 -1
  76. package/dist/screens/Contact/ContactScreen.js +1 -1
  77. package/dist/screens/Contact/ContactScreen.js.map +1 -1
  78. package/dist/screens/Contacts/ContactsScreen.js +2 -2
  79. package/dist/screens/Contacts/ContactsScreen.js.map +1 -1
  80. package/dist/screens/ContentList/ContentListScreen.d.ts.map +1 -1
  81. package/dist/screens/ContentList/ContentListScreen.js +5 -2
  82. package/dist/screens/ContentList/ContentListScreen.js.map +1 -1
  83. package/dist/screens/ContentType/ContentTypeScreen.js +1 -1
  84. package/dist/screens/ContentType/ContentTypeScreen.js.map +1 -1
  85. package/dist/screens/EntryEditor/EntryEditorScreen.d.ts.map +1 -1
  86. package/dist/screens/EntryEditor/EntryEditorScreen.js +5 -3
  87. package/dist/screens/EntryEditor/EntryEditorScreen.js.map +1 -1
  88. package/dist/screens/EntryEditor/EntryForm.d.ts +2 -0
  89. package/dist/screens/EntryEditor/EntryForm.d.ts.map +1 -1
  90. package/dist/screens/EntryEditor/EntryForm.js +164 -19
  91. package/dist/screens/EntryEditor/EntryForm.js.map +1 -1
  92. package/dist/screens/EntryEditor/HistoryDialog.d.ts +2 -1
  93. package/dist/screens/EntryEditor/HistoryDialog.d.ts.map +1 -1
  94. package/dist/screens/EntryEditor/HistoryDialog.js +24 -5
  95. package/dist/screens/EntryEditor/HistoryDialog.js.map +1 -1
  96. package/dist/screens/EntryEditor/PagePane.d.ts +12 -2
  97. package/dist/screens/EntryEditor/PagePane.d.ts.map +1 -1
  98. package/dist/screens/EntryEditor/PagePane.js +54 -19
  99. package/dist/screens/EntryEditor/PagePane.js.map +1 -1
  100. package/dist/screens/EntryEditor/ThemeCard.d.ts +32 -0
  101. package/dist/screens/EntryEditor/ThemeCard.d.ts.map +1 -0
  102. package/dist/screens/EntryEditor/ThemeCard.js +67 -0
  103. package/dist/screens/EntryEditor/ThemeCard.js.map +1 -0
  104. package/dist/screens/Home/HomeScreen.d.ts.map +1 -1
  105. package/dist/screens/Home/HomeScreen.js +8 -3
  106. package/dist/screens/Home/HomeScreen.js.map +1 -1
  107. package/package.json +5 -5
  108. package/app-dist/assets/index-BgVjqwy6.js +0 -80
  109. package/app-dist/assets/index-BvWFILf6.css +0 -2
@@ -1,19 +1,21 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { liveFields } from '@velocity-astro/cms-core';
3
- import { ConfirmDialog, OptionPicker, SectionList, Text } from '@velocity-astro/cms-ui';
3
+ import { Button, ConfirmDialog, Dialog, OptionPicker, SectionList, Text } from '@velocity-astro/cms-ui';
4
4
  import { Copy, Eye, EyeOff, Trash2 } from 'lucide-react';
5
5
  import { useEffect, useRef, useState } from 'react';
6
6
  import { localized, withValue } from '../lib/content.js';
7
7
  import { useLast } from '../lib/use-closing.js';
8
8
  import { useLocale, useMessages } from '../messages/messages-context.js';
9
9
  import { blockIcon } from './block-icons.js';
10
+ import { carriedSection } from './carry.js';
10
11
  import { FieldGrid } from './FieldGrid.js';
11
12
  import { useControlFor } from './FieldSummary.js';
12
- import { usePageFocus } from './page-focus.js';
13
+ import { PageFocusInside, usePageFocus } from './page-focus.js';
14
+ import { SiteImagePicker } from './SiteImagePicker.js';
13
15
  const isSection = (value) => typeof value === 'object' && value !== null && typeof value.id === 'string' && typeof value.block === 'string';
14
16
  /**
15
17
  * A few of a section's own words: the first line of text it holds, in the order of its fields.
16
- * `isWords` leaves out the fields that hold a choice or a figure, which say nothing about it.
18
+ * `isWords` leaves out the fields that hold a choice, a figure or a picture's name, which say nothing about it.
17
19
  */
18
20
  function summaryOf(block, section, isWords) {
19
21
  for (const field of liveFields(block.fields)) {
@@ -23,109 +25,219 @@ function summaryOf(block, section, isWords) {
23
25
  }
24
26
  return undefined;
25
27
  }
28
+ const blocksOf = (field) => (Array.isArray(field.options['blocks']) ? field.options['blocks'] : []);
29
+ const maxOf = (field) => (typeof field.options['max'] === 'number' ? field.options['max'] : undefined);
30
+ const isFull = (list) => list.max !== undefined && list.sections.length >= list.max;
31
+ const withData = (list, id, data) => list.to(list.sections.map((existing) => (existing.id === id ? { ...existing, data: data } : existing)));
32
+ /** A list and every list inside its sections, outermost first. */
33
+ function gather(list, controlFor) {
34
+ const inner = list.sections.flatMap((section, index) => {
35
+ const block = blockOf(list.blocks, section);
36
+ if (!block || block.retiredAt !== undefined)
37
+ return [];
38
+ return liveFields(block.fields).flatMap((field) => (controlFor(field.type)?.sections?.(field, section.data[field.id], controlFor) ?? []).flatMap((found) => {
39
+ const place = found.place === '' ? field.key : `${field.key}.${found.place}`;
40
+ return gather({
41
+ key: `${section.id}:${place}`,
42
+ group: blocksOf(found.field).filter((block) => block.retiredAt === undefined).map((block) => block.key).sort().join('|'),
43
+ blocks: blocksOf(found.field),
44
+ sections: found.sections.filter(isSection),
45
+ max: maxOf(found.field),
46
+ path: `${list.path}.${index}.${place}`,
47
+ inside: [...list.inside, section.id],
48
+ to: (next) => withData(list, section.id, withValue(section.data, field.id, found.put(next))),
49
+ }, controlFor);
50
+ }));
51
+ });
52
+ return [list, ...inner];
53
+ }
54
+ function locate(lists, id) {
55
+ for (const list of lists) {
56
+ const at = list.sections.findIndex((section) => section.id === id);
57
+ const section = list.sections[at];
58
+ if (section)
59
+ return { list, at, section };
60
+ }
61
+ return undefined;
62
+ }
63
+ /** A copy of a section with an id of its own, and one for every section inside it. */
64
+ export function copyOf(value) {
65
+ if (Array.isArray(value))
66
+ return value.map((item) => copyOf(item));
67
+ if (typeof value !== 'object' || value === null)
68
+ return value;
69
+ const copied = Object.fromEntries(Object.entries(value).map(([key, inner]) => [key, copyOf(inner)]));
70
+ return (isSection(value) ? { ...copied, id: crypto.randomUUID() } : copied);
71
+ }
72
+ /** The places of one kind (words, pictures, links) a section holds, by the keys the page knows its fields by. */
73
+ function placesIn(list, section, ask, controlFor) {
74
+ const block = blockOf(list.blocks, section);
75
+ if (!block || block.retiredAt !== undefined)
76
+ return [];
77
+ return liveFields(block.fields).flatMap((inner) => {
78
+ const control = controlFor(inner.type);
79
+ return ((control && ask(control, inner, section.data[inner.id])) ?? []).map((found) => ({
80
+ ...found,
81
+ place: found.place === '' ? inner.key : `${inner.key}.${found.place}`,
82
+ put: (held) => withValue(section.data, inner.id, found.put(held)),
83
+ }));
84
+ });
85
+ }
26
86
  /**
27
87
  * The sections of a page. Each is one of the blocks the field allows and opens to that block's
28
88
  * own fields, drawn by their own controls. What a section looks like on the site is the
29
89
  * template's business: here it is only data.
90
+ *
91
+ * A block may hold sections of its own (the columns of a row). In the form those are drawn by
92
+ * the control of the field that holds them, which is this one again. To the page, the field that
93
+ * holds the whole page answers for every section in it, however deep.
30
94
  */
31
95
  export function BlocksControl({ field, label, help, value, onChange, path, problem, problems }) {
32
96
  const messages = useMessages();
33
97
  const locale = useLocale();
34
- const blocks = Array.isArray(field.options['blocks']) ? field.options['blocks'] : [];
98
+ const blocks = blocksOf(field);
35
99
  const sections = Array.isArray(value) ? value.filter(isSection) : [];
36
- const max = typeof field.options['max'] === 'number' ? field.options['max'] : undefined;
100
+ const max = maxOf(field);
37
101
  const [openIds, setOpenIds] = useState([]);
38
102
  const [isPicking, setPicking] = useState(false);
39
- // Where the section being picked goes: a place among the sections, or the end.
40
- const [pickAt, setPickAt] = useState(null);
103
+ // Where the section being picked goes: the list, and a place among its sections or the end.
104
+ const [pick, setPick] = useState({ key: '', at: null });
41
105
  const [removingId, setRemovingId] = useState(null);
106
+ // The picture being changed from the page: its section and its place in it.
107
+ const [picturing, setPicturing] = useState(null);
108
+ // The link being changed from the page, the same way.
109
+ const [linking, setLinking] = useState(null);
42
110
  // The field last pressed on the page, until the person turns to something else in the form.
43
111
  const [pointed, setPointed] = useState(null);
44
112
  const controlFor = useControlFor();
45
113
  const focus = usePageFocus();
46
114
  const pressed = focus?.pressed;
115
+ const opened = focus?.opened;
47
116
  const change = (next) => onChange(next.length === 0 ? undefined : next);
117
+ const listsOf = (all) => gather({ key: '', group: '', blocks, sections: all, max, path, inside: [], to: (next) => next }, controlFor);
118
+ const lists = listsOf(sections);
119
+ const own = lists[0];
120
+ const wordsOf = (list, section) => placesIn(list, section, (control, inner, held) => control.words?.(inner, held, controlFor), controlFor);
121
+ const picturesOf = (list, section) => placesIn(list, section, (control, inner, held) => control.pictures?.(inner, held, controlFor), controlFor);
122
+ const linksOf = (list, section) => placesIn(list, section, (control, inner, held) => control.links?.(inner, held, controlFor), controlFor);
123
+ const itemsOf = (list, section) => placesIn(list, section, (control, inner, held) => control.lists?.(inner, held, controlFor), controlFor);
124
+ const swatchesOf = (list, section) => placesIn(list, section, (control, inner, held) => control.swatches?.(inner, held, controlFor), controlFor);
125
+ // What the handlers below work from: what the form holds now, whenever the page speaks.
126
+ const held = useRef({ sections, lists, listsOf, change, wordsOf, itemsOf, swatchesOf });
127
+ useEffect(() => {
128
+ held.current = { sections, lists, listsOf, change, wordsOf, itemsOf, swatchesOf };
129
+ });
48
130
  const open = (id) => {
49
131
  setOpenIds((ids) => (ids.includes(id) ? ids : [...ids, id]));
50
132
  focus?.open(id);
51
133
  };
134
+ // A section opens with every section it is inside.
135
+ const reveal = (id) => {
136
+ const wanted = [...(locate(held.current.lists, id)?.list.inside ?? []), id];
137
+ setOpenIds((ids) => (wanted.every((one) => ids.includes(one)) ? ids : [...new Set([...ids, ...wanted])]));
138
+ };
52
139
  // A section pressed on the page beside the form opens here.
53
140
  useEffect(() => {
54
141
  if (!pressed)
55
142
  return;
56
- setOpenIds((ids) => (ids.includes(pressed.id) ? ids : [...ids, pressed.id]));
143
+ reveal(pressed.id);
57
144
  // A field inside a list is pointed at by the list it is in.
58
145
  const [key] = pressed.field?.split('.') ?? [];
59
146
  setPointed(key === undefined ? null : { section: pressed.id, key, at: pressed.at });
60
147
  }, [pressed]);
61
- // Where each section holds plain words, by the keys the page knows its fields by.
62
- const wordsOf = (section) => {
63
- const block = blockOf(blocks, section);
64
- if (!block || block.retiredAt !== undefined)
65
- return [];
66
- return liveFields(block.fields).flatMap((inner) => (controlFor(inner.type)?.words?.(inner, section.data[inner.id], controlFor) ?? []).map(({ place, put }) => ({
67
- place: place === '' ? inner.key : `${inner.key}.${place}`,
68
- put: (words) => withValue(section.data, inner.id, put(words)),
69
- })));
70
- };
71
- const typable = Object.fromEntries(sections.map((section) => [section.id, wordsOf(section).map(({ place }) => place)]));
148
+ // So does one opened from somewhere else: a section added inside another from the page.
149
+ useEffect(() => {
150
+ if (opened !== undefined)
151
+ reveal(opened);
152
+ }, [opened]);
153
+ const isInside = focus?.isInside === true;
154
+ const typable = isInside ? {} : Object.fromEntries(lists.flatMap((list) => list.sections.map((section) => [section.id, wordsOf(list, section).map(({ place }) => place)])));
72
155
  const typableSaid = JSON.stringify(typable);
73
156
  const setTypable = focus?.setTypable;
74
157
  useEffect(() => setTypable?.(JSON.parse(typableSaid)), [setTypable, typableSaid]);
75
158
  // Words typed on the page go into the form as they are typed. Several may arrive before the
76
159
  // form is drawn again, so each builds on the one before.
77
- const held = useRef({ sections, wordsOf, change });
78
- useEffect(() => {
79
- held.current = { sections, wordsOf, change };
80
- });
81
160
  const onTyped = focus?.onTyped;
82
- useEffect(() => onTyped?.((id, field, words) => {
161
+ useEffect(() => onTyped?.((id, place, words) => {
83
162
  const now = held.current;
84
- const section = now.sections.find((candidate) => candidate.id === id);
85
- const place = section && now.wordsOf(section).find((candidate) => candidate.place === field);
86
- if (!section || !place)
163
+ const found = locate(now.lists, id);
164
+ const at = found && now.wordsOf(found.list, found.section).find((candidate) => candidate.place === place);
165
+ if (!found || !at)
87
166
  return;
88
- const next = now.sections.map((existing) => (existing.id === id ? { ...existing, data: place.put(words) } : existing));
89
- held.current = { ...now, sections: next };
167
+ const next = withData(found.list, id, at.put(words));
168
+ held.current = { ...now, sections: next, lists: now.listsOf(next) };
90
169
  now.change(next);
91
170
  }), [onTyped]);
92
171
  const nameOf = (block) => (block ? localized(block.label, locale) : messages.editor.sectionUnknown);
93
172
  // What the page may offer on each section it shows. Hidden and retired sections are not on the page.
94
- const isFull = max !== undefined && sections.length >= max;
95
- const offered = {
96
- labels: {
97
- up: messages.editor.pageMoveUp,
98
- down: messages.editor.pageMoveDown,
99
- duplicate: messages.editor.pageDuplicate,
100
- remove: messages.editor.pageRemove,
101
- add: messages.editor.pageAdd,
102
- addBefore: messages.editor.pageAddBefore,
103
- addAfter: messages.editor.pageAddAfter,
104
- },
105
- sections: Object.fromEntries(sections.map((section, at) => [section.id, { name: nameOf(blockOf(blocks, section)), up: at > 0, down: at < sections.length - 1, copy: !isFull, add: !isFull }])),
106
- };
173
+ const offered = isInside
174
+ ? undefined
175
+ : {
176
+ labels: {
177
+ up: messages.editor.pageMoveUp,
178
+ down: messages.editor.pageMoveDown,
179
+ duplicate: messages.editor.pageDuplicate,
180
+ remove: messages.editor.pageRemove,
181
+ add: messages.editor.pageAdd,
182
+ addBefore: messages.editor.pageAddBefore,
183
+ addAfter: messages.editor.pageAddAfter,
184
+ picture: messages.editor.pagePicture,
185
+ link: messages.editor.pageLink,
186
+ bold: messages.editor.richText.bold,
187
+ italic: messages.editor.richText.italic,
188
+ textLink: messages.editor.richText.link,
189
+ itemUp: messages.editor.pageItemUp,
190
+ itemDown: messages.editor.pageItemDown,
191
+ itemDuplicate: messages.editor.pageItemDuplicate,
192
+ itemRemove: messages.editor.pageItemRemove,
193
+ },
194
+ sections: Object.fromEntries(lists.flatMap((list) => list.sections.map((section, at) => [
195
+ section.id,
196
+ {
197
+ name: nameOf(blockOf(list.blocks, section)),
198
+ up: at > 0,
199
+ down: at < list.sections.length - 1,
200
+ copy: !isFull(list),
201
+ add: !isFull(list),
202
+ pictures: picturesOf(list, section).map(({ place }) => place),
203
+ links: linksOf(list, section).map(({ place }) => place),
204
+ lists: itemsOf(list, section).map((found) => ({ field: found.place, count: found.items.length, full: found.isFull })),
205
+ swatches: swatchesOf(list, section).map((found) => ({ field: found.place, name: localized(found.field.label, locale), ...(found.chosen !== undefined && { chosen: found.chosen }), choices: found.choices.map((choice) => ({ value: choice.value, name: choice.name(locale), colour: choice.colour })) })),
206
+ list: list.key,
207
+ group: list.group,
208
+ },
209
+ ]))),
210
+ slots: Object.fromEntries(lists.slice(1).map((list) => [list.key, { group: list.group, empty: list.sections.length === 0, full: isFull(list) }])),
211
+ };
107
212
  const offeredSaid = JSON.stringify(offered);
108
213
  const setTools = focus?.setTools;
109
- useEffect(() => setTools?.(JSON.parse(offeredSaid)), [setTools, offeredSaid]);
214
+ useEffect(() => {
215
+ if (offeredSaid !== undefined)
216
+ setTools?.(JSON.parse(offeredSaid));
217
+ }, [setTools, offeredSaid]);
110
218
  const remove = (id) => {
111
- change(sections.filter((section) => section.id !== id));
219
+ const found = locate(lists, id);
220
+ if (found)
221
+ change(found.list.to(found.list.sections.filter((section) => section.id !== id)));
112
222
  setOpenIds((ids) => ids.filter((candidate) => candidate !== id));
113
223
  };
114
224
  const act = (id, action) => {
115
- const at = sections.findIndex((section) => section.id === id);
116
- const section = sections[at];
117
- if (!section)
225
+ const found = locate(held.current.lists, id);
226
+ if (!found)
118
227
  return;
228
+ const { list, at, section } = found;
119
229
  if (action === 'duplicate') {
120
- const copy = { ...section, id: crypto.randomUUID(), data: structuredClone(section.data) };
121
- change([...sections.slice(0, at + 1), copy, ...sections.slice(at + 1)]);
230
+ if (isFull(list))
231
+ return;
232
+ const copy = copyOf(section);
233
+ change(list.to([...list.sections.slice(0, at + 1), copy, ...list.sections.slice(at + 1)]));
122
234
  open(copy.id);
123
235
  }
124
236
  else if (action === 'hide') {
125
- change(sections.map((existing) => (existing.id === id ? { ...existing, hidden: true } : existing)));
237
+ change(list.to(list.sections.map((existing) => (existing.id === id ? { ...existing, hidden: true } : existing))));
126
238
  }
127
239
  else if (action === 'show') {
128
- change(sections.map((existing) => (existing.id === id ? { id: existing.id, block: existing.block, data: existing.data } : existing)));
240
+ change(list.to(list.sections.map((existing) => (existing.id === id ? { id: existing.id, block: existing.block, data: existing.data } : existing))));
129
241
  }
130
242
  else if (action === 'remove') {
131
243
  // An empty section is removed at once: there is nothing to lose.
@@ -135,61 +247,146 @@ export function BlocksControl({ field, label, help, value, onChange, path, probl
135
247
  setRemovingId(id);
136
248
  }
137
249
  };
138
- // A button pressed on a section on the page does what the same button does here.
139
- const move = (from, to) => {
140
- const next = [...sections];
250
+ const moveIn = (list, from, to) => {
251
+ const next = [...list.sections];
141
252
  const [moved] = next.splice(from, 1);
142
253
  if (moved)
143
254
  next.splice(to, 0, moved);
144
- change(next);
255
+ change(list.to(next));
145
256
  };
146
- const acting = useRef({ sections, act, move, isFull });
257
+ const acting = useRef({ act, moveIn });
147
258
  useEffect(() => {
148
- acting.current = { sections, act, move, isFull };
259
+ acting.current = { act, moveIn };
149
260
  });
261
+ // A button pressed on a section on the page does what the same button does here.
150
262
  const onActed = focus?.onActed;
151
263
  const show = focus?.open;
152
264
  useEffect(() => onActed?.((id, action) => {
153
- const now = acting.current;
154
- const at = now.sections.findIndex((section) => section.id === id);
155
- if (at < 0)
265
+ const found = locate(held.current.lists, id);
266
+ if (!found)
156
267
  return;
268
+ const { list, at } = found;
157
269
  if (action === 'up' || action === 'down') {
158
270
  const to = action === 'up' ? at - 1 : at + 1;
159
- if (to < 0 || to >= now.sections.length)
271
+ if (to < 0 || to >= list.sections.length)
160
272
  return;
161
- now.move(at, to);
273
+ acting.current.moveIn(list, at, to);
162
274
  // The page is drawn again with the section in its new place, and brings it into view.
163
275
  show?.(id);
164
276
  }
165
277
  else if (action === 'addBefore' || action === 'addAfter') {
166
- if (now.isFull)
278
+ if (isFull(list))
167
279
  return;
168
- setPickAt(action === 'addBefore' ? at : at + 1);
280
+ setPick({ key: list.key, at: action === 'addBefore' ? at : at + 1 });
169
281
  setPicking(true);
170
282
  }
171
- else if (action !== 'duplicate' || !now.isFull) {
172
- now.act(id, action);
283
+ else {
284
+ acting.current.act(id, action);
173
285
  }
174
286
  }), [onActed, show]);
175
- // A section dragged on the page lands where the form would put it.
176
- const onDragged = focus?.onDragged;
177
- useEffect(() => onDragged?.((id, beforeId) => {
178
- const now = acting.current;
179
- const from = now.sections.findIndex((section) => section.id === id);
180
- const before = beforeId === null ? now.sections.length : now.sections.findIndex((section) => section.id === beforeId);
181
- if (from < 0 || before < 0)
287
+ // The button of an empty place inside a section offers what can be added there.
288
+ const onSlotted = focus?.onSlotted;
289
+ useEffect(() => onSlotted?.((id, place) => {
290
+ const list = held.current.lists.find((candidate) => candidate.key === `${id}:${place}`);
291
+ if (!list || isFull(list))
292
+ return;
293
+ setPick({ key: list.key, at: null });
294
+ setPicking(true);
295
+ }), [onSlotted]);
296
+ // A swatch pressed on a section's bar chooses what the same swatch chooses in the form.
297
+ const onSwatched = focus?.onSwatched;
298
+ useEffect(() => onSwatched?.((id, place, chosen) => {
299
+ const now = held.current;
300
+ const found = locate(now.lists, id);
301
+ const swatch = found && now.swatchesOf(found.list, found.section).find((candidate) => candidate.place === place);
302
+ // Only a choice the field has, whatever the page said.
303
+ if (!found || !swatch || swatch.chosen === chosen || !swatch.choices.some((choice) => choice.value === chosen))
182
304
  return;
183
- // Taking it out first moves everything after it one place up.
184
- const to = before > from ? before - 1 : before;
185
- if (to === from)
305
+ now.change(withData(found.list, id, swatch.put(chosen)));
306
+ }), [onSwatched]);
307
+ // The button on a picture of the page opens the window that picture is chosen in.
308
+ const onPictured = focus?.onPictured;
309
+ useEffect(() => onPictured?.((id, place) => setPicturing({ section: id, place })), [onPictured]);
310
+ const pictureAt = useLast(picturing);
311
+ const pictureIn = pictureAt ? locate(lists, pictureAt.section) : undefined;
312
+ const picture = pictureIn && picturesOf(pictureIn.list, pictureIn.section).find((candidate) => candidate.place === pictureAt?.place);
313
+ // A button on an item of a list on the page does what the list's own row does in the form.
314
+ const onItemActed = focus?.onItemActed;
315
+ useEffect(() => onItemActed?.((id, place, at, action) => {
316
+ const now = held.current;
317
+ const found = locate(now.lists, id);
318
+ const list = found && now.itemsOf(found.list, found.section).find((candidate) => candidate.place === place);
319
+ const item = list?.items[at];
320
+ if (!found || !list || at < 0 || at >= list.items.length)
186
321
  return;
187
- now.move(from, to);
322
+ const next = [...list.items];
323
+ if (action === 'remove') {
324
+ next.splice(at, 1);
325
+ }
326
+ else if (action === 'duplicate') {
327
+ if (list.isFull)
328
+ return;
329
+ next.splice(at + 1, 0, copyOf(item));
330
+ }
331
+ else {
332
+ const to = action === 'up' ? at - 1 : at + 1;
333
+ if (to < 0 || to >= next.length)
334
+ return;
335
+ next.splice(at, 1);
336
+ next.splice(to, 0, item);
337
+ }
338
+ now.change(withData(found.list, id, list.put(next)));
339
+ }), [onItemActed]);
340
+ // The button on a link of the page opens a window that holds the link's own field.
341
+ const onLinked = focus?.onLinked;
342
+ useEffect(() => onLinked?.((id, place) => setLinking({ section: id, place })), [onLinked]);
343
+ const linkAt = useLast(linking);
344
+ const linkIn = linkAt ? locate(lists, linkAt.section) : undefined;
345
+ const link = linkIn && linksOf(linkIn.list, linkIn.section).find((candidate) => candidate.place === linkAt?.place);
346
+ // A section dragged on the page lands where the form would put it: among the sections it was
347
+ // with, or in another place that takes the same blocks.
348
+ const onDragged = focus?.onDragged;
349
+ useEffect(() => onDragged?.((id, beforeId, into) => {
350
+ const now = held.current;
351
+ const found = locate(now.lists, id);
352
+ if (!found)
353
+ return;
354
+ const from = found.list;
355
+ if (into === undefined || into === from.key) {
356
+ const before = beforeId === null ? from.sections.length : from.sections.findIndex((section) => section.id === beforeId);
357
+ if (before < 0)
358
+ return;
359
+ // Taking it out first moves everything after it one place up.
360
+ const to = before > found.at ? before - 1 : before;
361
+ if (to === found.at)
362
+ return;
363
+ acting.current.moveIn(from, found.at, to);
364
+ }
365
+ else {
366
+ const target = now.lists.find((candidate) => candidate.key === into);
367
+ // Only into a place that holds the same blocks, has room, and is not inside the section itself.
368
+ if (!target || target.group !== from.group || isFull(target) || target.inside.includes(id))
369
+ return;
370
+ const without = from.to(from.sections.filter((section) => section.id !== id));
371
+ const there = now.listsOf(without).find((candidate) => candidate.key === into);
372
+ if (!there)
373
+ return;
374
+ const before = beforeId === null ? there.sections.length : there.sections.findIndex((section) => section.id === beforeId);
375
+ if (before < 0)
376
+ return;
377
+ // The same block there may go by other ids: the section is held as that place holds it.
378
+ const carried = carriedSection(from.blocks, there.blocks, found.section, controlFor);
379
+ if (!carried)
380
+ return;
381
+ now.change(there.to([...there.sections.slice(0, before), carried, ...there.sections.slice(before)]));
382
+ }
188
383
  show?.(id);
189
384
  }), [onDragged, show]);
190
- // The picker offers what the template describes today, under the names it files them by.
385
+ // The picker offers what the place being added to takes.
386
+ const picking = lists.find((list) => list.key === pick.key) ?? own;
387
+ // It offers what the template describes today, under the names it files them by.
191
388
  const byGroup = new Map();
192
- for (const block of blocks) {
389
+ for (const block of picking?.blocks ?? []) {
193
390
  if (block.retiredAt !== undefined)
194
391
  continue;
195
392
  const name = block.group ?? messages.editor.sectionOtherGroup;
@@ -204,23 +401,27 @@ export function BlocksControl({ field, label, help, value, onChange, path, probl
204
401
  byGroup.set(name, options);
205
402
  }
206
403
  const groups = [...byGroup].map(([name, options]) => ({ id: name, label: name, options }));
207
- const removing = useLast(removingId === null ? null : (sections.find((section) => section.id === removingId) ?? null));
404
+ const removingIn = removingId === null ? undefined : locate(lists, removingId);
405
+ const removingName = useLast(removingIn ? nameOf(blockOf(removingIn.list.blocks, removingIn.section)) : null);
208
406
  return (_jsxs(_Fragment, { children: [_jsx(SectionList, { label: label, ...(help !== undefined && { description: help }), ...(problem !== undefined && { errorMessage: problem }), emptyText: messages.editor.sectionsEmpty, addLabel: messages.editor.sectionAdd, onAdd: () => {
209
- setPickAt(null);
407
+ setPick({ key: '', at: null });
210
408
  setPicking(true);
211
- }, isFull: isFull, openIds: openIds, revealId: pressed?.id, onOpenChange: (id, isOpen) => {
409
+ }, isFull: own !== undefined && isFull(own), openIds: openIds, revealId: pressed?.id, onOpenChange: (id, isOpen) => {
212
410
  setPointed(null);
213
411
  if (isOpen)
214
412
  open(id);
215
413
  else
216
414
  setOpenIds((ids) => ids.filter((candidate) => candidate !== id));
217
- }, onMove: move, onAction: act, moveUpLabel: (position) => messages.editor.sectionMoveUp(nameOf(blockOf(blocks, sections[position - 1])), position), moveDownLabel: (position) => messages.editor.sectionMoveDown(nameOf(blockOf(blocks, sections[position - 1])), position), moreLabel: (position) => messages.editor.sectionMore(nameOf(blockOf(blocks, sections[position - 1])), position), dragLabel: (position) => messages.editor.sectionDrag(nameOf(blockOf(blocks, sections[position - 1])), position), items: sections.map((section, index) => {
415
+ }, onMove: (from, to) => {
416
+ if (own)
417
+ moveIn(own, from, to);
418
+ }, onAction: act, moveUpLabel: (position) => messages.editor.sectionMoveUp(nameOf(blockOf(blocks, sections[position - 1])), position), moveDownLabel: (position) => messages.editor.sectionMoveDown(nameOf(blockOf(blocks, sections[position - 1])), position), moreLabel: (position) => messages.editor.sectionMore(nameOf(blockOf(blocks, sections[position - 1])), position), dragLabel: (position) => messages.editor.sectionDrag(nameOf(blockOf(blocks, sections[position - 1])), position), items: sections.map((section, index) => {
218
419
  const block = blockOf(blocks, section);
219
420
  const isRetired = block === undefined || block.retiredAt !== undefined;
220
421
  const inner = block ? liveFields(block.fields) : [];
221
422
  const prefix = `${path}.${index}.`;
222
423
  const found = [...problems.keys()].filter((key) => key.startsWith(prefix)).length;
223
- const summary = block ? summaryOf(block, section, (inner) => controlFor(inner.type)?.width !== 'compact') : undefined;
424
+ const summary = block ? summaryOf(block, section, (inner) => controlFor(inner.type)?.width !== 'compact' && controlFor(inner.type)?.pictures === undefined) : undefined;
224
425
  const hidden = section.hidden === true;
225
426
  return {
226
427
  id: section.id,
@@ -240,17 +441,28 @@ export function BlocksControl({ field, label, help, value, onChange, path, probl
240
441
  ]),
241
442
  { id: 'remove', label: messages.editor.sectionRemove, icon: Trash2, tone: 'danger' },
242
443
  ],
243
- content: isRetired ? (_jsx(Text, { size: "sm", tone: "muted", children: messages.editor.sectionRetiredNote })) : inner.length === 0 ? (_jsx(Text, { size: "sm", tone: "muted", children: messages.editor.sectionNoFields })) : (_jsx(FieldGrid, { fields: inner, values: section.data, onChange: (child, next) => change(sections.map((existing) => (existing.id === section.id ? { ...existing, data: withValue(existing.data, child.id, next) } : existing))), pathOf: (child) => `${prefix}${child.key}`, problems: problems, spotlight: pointed?.section === section.id ? pointed : {} })),
444
+ content: isRetired ? (_jsx(Text, { size: "sm", tone: "muted", children: messages.editor.sectionRetiredNote })) : inner.length === 0 ? (_jsx(Text, { size: "sm", tone: "muted", children: messages.editor.sectionNoFields })) : (_jsx(PageFocusInside, { children: _jsx(FieldGrid, { fields: inner, values: section.data, onChange: (child, next) => change(sections.map((existing) => (existing.id === section.id ? { ...existing, data: withValue(existing.data, child.id, next) } : existing))), pathOf: (child) => `${prefix}${child.key}`, problems: problems, spotlight: pointed?.section === section.id ? pointed : {} }) })),
244
445
  };
245
446
  }) }), _jsx(OptionPicker, { isOpen: isPicking, onOpenChange: setPicking, title: messages.editor.sectionPickerTitle, closeLabel: messages.editor.close, emptyText: messages.editor.sectionPickerEmpty, groups: groups, onChoose: (blockId) => {
246
447
  const added = { id: crypto.randomUUID(), block: blockId, data: {} };
247
- const at = pickAt ?? sections.length;
248
- change([...sections.slice(0, at), added, ...sections.slice(at)]);
448
+ if (!picking)
449
+ return;
450
+ const at = pick.at ?? picking.sections.length;
451
+ change(picking.to([...picking.sections.slice(0, at), added, ...picking.sections.slice(at)]));
249
452
  open(added.id);
250
- } }), _jsx(ConfirmDialog, { isOpen: removingId !== null, onOpenChange: (isOpen) => {
453
+ } }), pictureIn && picture && (_jsx(SiteImagePicker, { label: messages.editor.pagePicture, value: picture.name, onChange: (name) => change(withData(pictureIn.list, pictureIn.section.id, picture.put(name))), window: {
454
+ isOpen: picturing !== null,
455
+ onOpenChange: (isOpen) => {
456
+ if (!isOpen)
457
+ setPicturing(null);
458
+ },
459
+ } })), linkIn && link && (_jsx(Dialog, { isOpen: linking !== null, onOpenChange: (isOpen) => {
460
+ if (!isOpen)
461
+ setLinking(null);
462
+ }, title: messages.editor.pageLink, closeLabel: messages.editor.close, footer: _jsx(Button, { variant: "primary", onPress: () => setLinking(null), children: messages.editor.pageLinkDone }), children: _jsx(FieldGrid, { fields: [link.field], values: { [link.field.id]: link.link }, onChange: (_field, next) => change(withData(linkIn.list, linkIn.section.id, link.put(next))), pathOf: () => `${linkIn.list.path}.${linkIn.at}.${link.place}`, problems: problems, perRow: 1 }) })), _jsx(ConfirmDialog, { isOpen: removingId !== null, onOpenChange: (isOpen) => {
251
463
  if (!isOpen)
252
464
  setRemovingId(null);
253
- }, title: messages.editor.sectionRemoveTitle(nameOf(blockOf(blocks, removing ?? undefined))), confirmLabel: messages.editor.sectionRemoveConfirm, cancelLabel: messages.editor.cancel, tone: "danger", onConfirm: () => {
465
+ }, title: messages.editor.sectionRemoveTitle(removingName ?? messages.editor.sectionUnknown), confirmLabel: messages.editor.sectionRemoveConfirm, cancelLabel: messages.editor.cancel, tone: "danger", onConfirm: () => {
254
466
  if (removingId !== null)
255
467
  remove(removingId);
256
468
  setRemovingId(null);