@nickpaustian/tela 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +1 -1
  3. package/bin/tela.ts +18 -2
  4. package/lib/index.js +172 -7
  5. package/package.json +5 -2
  6. package/scripts/check-components.ts +20 -0
  7. package/scripts/media-sync.ts +41 -0
  8. package/scripts/republish.ts +24 -0
  9. package/src/api.ts +3 -3
  10. package/src/components/EntryEditor.lsp +174 -11
  11. package/src/components/EntryList.lsp +8 -0
  12. package/src/components/Field.lsp +92 -2
  13. package/src/components/LinkField.lsp +477 -0
  14. package/src/components/MarkdownField.lsp +19 -24
  15. package/src/components/PersonField.lsp +255 -0
  16. package/src/components/ReferenceField.lsp +22 -1
  17. package/src/components/RepeaterField.lsp +141 -13
  18. package/src/components/ToggleField.lsp +63 -0
  19. package/src/config/fields.ts +134 -1
  20. package/src/config/seo.ts +4 -2
  21. package/src/config/types.ts +158 -7
  22. package/src/content/derive-values.ts +14 -3
  23. package/src/content/entries.ts +1 -1
  24. package/src/content/entry.ts +15 -1
  25. package/src/content/export.ts +173 -14
  26. package/src/content/git.ts +1 -1
  27. package/src/content/hooks.ts +75 -0
  28. package/src/content/link-address.ts +168 -0
  29. package/src/content/links.ts +13 -0
  30. package/src/content/person.ts +11 -0
  31. package/src/content/placement.ts +48 -0
  32. package/src/content/references.ts +48 -4
  33. package/src/content/rules.ts +24 -4
  34. package/src/content/shape.ts +192 -0
  35. package/src/content/store.ts +17 -0
  36. package/src/content/url.ts +15 -1
  37. package/src/content/users.ts +32 -0
  38. package/src/content/validate.ts +12 -2
  39. package/src/index.ts +4 -1
  40. package/src/keys.ts +47 -0
  41. package/src/media/index.ts +5 -0
  42. package/src/server/index.ts +168 -34
  43. package/src/server/library-routes.ts +11 -3
@@ -0,0 +1,255 @@
1
+ <view>
2
+ <Field $field="props.field" $path="props.path" $controlId="'f-' + props.path">
3
+ <div class="person">
4
+ <div class="person-picked" $show="state.picked">
5
+ <span class="person-chip">
6
+ <span class="person-initials" aria-hidden="true">{{ state.initials }}</span>
7
+ <span class="person-name">{{ state.pickedName }}</span>
8
+ <span class="one-warning" $show="state.gone">No longer has an account. Choose again or type a name.</span>
9
+ </span>
10
+ <button
11
+ type="button"
12
+ class="btn btn-ghost btn-sm btn-icon"
13
+ $show="!state.readonly"
14
+ $aria-label="'Remove ' + state.pickedName"
15
+ @click="unpick"
16
+ >
17
+ <span class="icon icon-s icon-x" aria-hidden="true"></span>
18
+ </button>
19
+ </div>
20
+
21
+ <div class="person-entry" $show="!state.picked">
22
+ <input
23
+ class="input"
24
+ type="text"
25
+ role="combobox"
26
+ autocomplete="off"
27
+ aria-autocomplete="list"
28
+ $id="'f-' + props.path"
29
+ $aria-expanded="state.open ? 'true' : 'false'"
30
+ $aria-controls="'f-' + props.path + '-people'"
31
+ $aria-activedescendant="state.active >= 0 ? 'f-' + props.path + '-person-' + state.active : null"
32
+ $placeholder="state.placeholder"
33
+ $readonly="state.readonly"
34
+ $maxLength="200"
35
+ $model="state.typed"
36
+ @input="typed"
37
+ @keydown="key"
38
+ @blur="blurred"
39
+ />
40
+ <ul
41
+ class="menu person-options"
42
+ role="listbox"
43
+ $id="'f-' + props.path + '-people'"
44
+ $show="state.open && state.results.length > 0"
45
+ >
46
+ <li
47
+ $for="one in state.results"
48
+ $key="one.id"
49
+ role="option"
50
+ class="menu-item"
51
+ $class="{ active: one.index === state.active }"
52
+ $id="'f-' + props.path + '-person-' + one.index"
53
+ $aria-selected="one.index === state.active ? 'true' : 'false'"
54
+ @pointerdown="choose($event, one)"
55
+ >{{ one.name }}</li>
56
+ </ul>
57
+ <p class="person-note" $show="state.typed && !state.open">Not one of the CMS's people, so it shows as typed.</p>
58
+ </div>
59
+ </div>
60
+ </Field>
61
+ </view>
62
+
63
+ <logic lang="ts">
64
+ import { defineProps, onMounted, reactive, shared } from '@lightspeed-js/core'
65
+ import { api } from '../api.ts'
66
+ import { initialsOf } from '../content/person.ts'
67
+
68
+ /**
69
+ * Somebody named on an entry: one of the CMS's people, or a name as typed.
70
+ *
71
+ * One box. Typing asks `/api/people` for matching names - ids and names only,
72
+ * which any signed-in account may ask for - and picking one stores the
73
+ * account. Anything not picked is kept as typed. The two are never both set,
74
+ * so a site reads the account's name where there is one and the words where
75
+ * there is not.
76
+ */
77
+ const props = defineProps({
78
+ field: { default: {} },
79
+ path: { default: '' },
80
+ })
81
+
82
+ const user = shared('entry:value:' + props.path + '.user')
83
+ const name = shared('entry:value:' + props.path + '.name')
84
+ const readonly = shared('entry:readonly')
85
+
86
+ const state = reactive({
87
+ picked: false,
88
+ pickedName: '',
89
+ initials: '',
90
+ gone: false,
91
+ typed: '',
92
+ results: [] as { id: string; name: string; index: number }[],
93
+ open: false,
94
+ active: -1,
95
+ readonly: false,
96
+ placeholder: props.field.options?.placeholder || 'Start typing a name',
97
+ })
98
+
99
+ let asked = ''
100
+ let timer: ReturnType<typeof setTimeout> | undefined
101
+
102
+ /** What the cells hold, shown: a picked account by its name, or the words. */
103
+ async function show() {
104
+ state.readonly = Boolean(props.field.readOnly || readonly.value)
105
+ const id = typeof user.value === 'string' ? user.value : ''
106
+ if (!id) {
107
+ state.picked = false
108
+ state.typed = typeof name.value === 'string' ? name.value : ''
109
+ return
110
+ }
111
+ state.picked = true
112
+ if (id === asked) return
113
+ asked = id
114
+ const result = await api.get<{ people: { id: string; name: string }[] }>('/api/people?ids=' + encodeURIComponent(id))
115
+ if (asked !== id) return
116
+ const found = result.ok ? result.data.people[0] : undefined
117
+ state.gone = !found
118
+ state.pickedName = found?.name ?? 'Somebody'
119
+ state.initials = found ? initialsOf({ name: found.name }) : '?'
120
+ }
121
+
122
+ function typed() {
123
+ user.value = undefined
124
+ name.value = state.typed.trim() ? state.typed : undefined
125
+ clearTimeout(timer)
126
+ const words = state.typed.trim()
127
+ if (!words) {
128
+ close()
129
+ return
130
+ }
131
+ timer = setTimeout(() => search(words), 150)
132
+ }
133
+
134
+ async function search(words: string) {
135
+ const result = await api.get<{ people: { id: string; name: string }[] }>(
136
+ '/api/people?limit=8&q=' + encodeURIComponent(words),
137
+ )
138
+ if (state.typed.trim() !== words) return
139
+ state.results = result.ok ? result.data.people.map((one, index) => ({ ...one, index })) : []
140
+ state.active = -1
141
+ state.open = state.results.length > 0
142
+ }
143
+
144
+ function pick(one: { id: string; name: string }) {
145
+ asked = one.id
146
+ user.value = one.id
147
+ name.value = undefined
148
+ state.picked = true
149
+ state.gone = false
150
+ state.pickedName = one.name
151
+ state.initials = initialsOf({ name: one.name })
152
+ state.typed = ''
153
+ close()
154
+ }
155
+
156
+ /** Before the input loses focus, or the click would land on nothing. */
157
+ function choose(event: PointerEvent, one: { id: string; name: string }) {
158
+ event.preventDefault()
159
+ pick(one)
160
+ }
161
+
162
+ function unpick() {
163
+ asked = ''
164
+ user.value = undefined
165
+ state.picked = false
166
+ state.pickedName = ''
167
+ state.typed = ''
168
+ requestAnimationFrame(() => document.getElementById('f-' + props.path)?.focus())
169
+ }
170
+
171
+ function key(event: KeyboardEvent) {
172
+ if (!state.open) return
173
+ const last = state.results.length - 1
174
+ if (event.key === 'ArrowDown') {
175
+ event.preventDefault()
176
+ state.active = state.active >= last ? 0 : state.active + 1
177
+ } else if (event.key === 'ArrowUp') {
178
+ event.preventDefault()
179
+ state.active = state.active <= 0 ? last : state.active - 1
180
+ } else if (event.key === 'Enter' && state.active >= 0) {
181
+ event.preventDefault()
182
+ pick(state.results[state.active]!)
183
+ } else if (event.key === 'Escape') {
184
+ event.preventDefault()
185
+ close()
186
+ }
187
+ }
188
+
189
+ function blurred() {
190
+ close()
191
+ }
192
+
193
+ function close() {
194
+ state.open = false
195
+ state.active = -1
196
+ }
197
+
198
+ onMounted(() => {
199
+ user.subscribe(show)
200
+ name.subscribe(() => {
201
+ if (!state.picked && name.value !== state.typed) show()
202
+ })
203
+ readonly.subscribe(show)
204
+ show()
205
+ })
206
+ </logic>
207
+
208
+ <style>
209
+ .person {
210
+ position: relative;
211
+ }
212
+
213
+ .person-picked {
214
+ display: flex;
215
+ gap: 0.5rem;
216
+ align-items: center;
217
+ }
218
+
219
+ .person-chip {
220
+ display: inline-flex;
221
+ flex-wrap: wrap;
222
+ gap: 0.5rem;
223
+ align-items: center;
224
+ }
225
+
226
+ .person-initials {
227
+ display: inline-grid;
228
+ place-items: center;
229
+ inline-size: 1.75rem;
230
+ block-size: 1.75rem;
231
+ border-radius: 50%;
232
+ background: var(--surface-2);
233
+ font-size: 0.75rem;
234
+ font-weight: 600;
235
+ }
236
+
237
+ .person-options {
238
+ position: absolute;
239
+ inset-inline: 0;
240
+ z-index: 10;
241
+ margin: 0.25rem 0 0;
242
+ padding: 0.25rem;
243
+ list-style: none;
244
+ }
245
+
246
+ .person-options .active {
247
+ background: var(--surface-hover);
248
+ }
249
+
250
+ .person-note {
251
+ margin: 0.25rem 0 0;
252
+ color: var(--text-2);
253
+ font-size: 0.8125rem;
254
+ }
255
+ </style>
@@ -26,7 +26,8 @@
26
26
  </li>
27
27
  </ul>
28
28
 
29
- <div class="search" $show="state.searchable && !state.readonly">
29
+ <p class="full" $show="state.full && !state.readonly">{{ state.fullNote }}</p>
30
+ <div class="search" $show="state.searchable && !state.readonly && !state.full">
30
31
  <button
31
32
  type="button"
32
33
  class="btn open"
@@ -127,6 +128,8 @@ const state = reactive({
127
128
  total: 0,
128
129
  searchable: true,
129
130
  single: !props.field.many,
131
+ full: false,
132
+ fullNote: '',
130
133
  missing: false,
131
134
  readonly: Boolean(props.field.readOnly),
132
135
  openLabel: 'Choose',
@@ -199,6 +202,10 @@ function refreshShape() {
199
202
 
200
203
  state.single = !props.field.many
201
204
  state.missing = state.chosen.some((one) => one.missing)
205
+ const most = props.field.many ? props.field.maxRows : undefined
206
+ state.full = most !== undefined && state.chosen.length >= most
207
+ state.fullNote = state.full ? 'That is the most there can be (' + most + '). Remove one to choose another.' : ''
208
+ if (state.full) close()
202
209
  state.placeholder = 'Search ' + (props.field.to || 'entries')
203
210
 
204
211
  if (state.chosen.length) state.openLabel = state.single ? 'Change' : 'Add'
@@ -340,6 +347,8 @@ function toggle() {
340
347
  /** Whatever was found, minus what is already chosen. */
341
348
  function show(entries) {
342
349
  const taken = new Set(currentIds())
350
+ const route = shared('route').value?.params ?? {}
351
+ if (route.collection === props.field.to && route.id) taken.add(route.id)
343
352
  state.results = entries.filter((entry) => !taken.has(entry.id))
344
353
  state.note = noteFor()
345
354
  }
@@ -404,6 +413,12 @@ function close() {
404
413
  function pick(result) {
405
414
  const ids = currentIds()
406
415
  writeIds(props.field.many ? [...ids, result.id] : [result.id])
416
+ const most = props.field.many ? props.field.maxRows : undefined
417
+ if (most !== undefined && ids.length + 1 >= most) {
418
+ close()
419
+ loadTitles()
420
+ return
421
+ }
407
422
 
408
423
  // A list stays open for a `many` field - choosing three artists should be
409
424
  // three clicks, not three trips through the same menu.
@@ -579,6 +594,12 @@ onUpdate(() => {
579
594
  font-weight: var(--fw-medium);
580
595
  }
581
596
 
597
+ .full {
598
+ margin: var(--sp-2) 0 0;
599
+ color: var(--text-3);
600
+ font-size: var(--fs-s);
601
+ }
602
+
582
603
  .note {
583
604
  margin: 0;
584
605
  padding: var(--sp-2) 12px;
@@ -128,22 +128,22 @@
128
128
 
129
129
  <div class="menu sec-menu" popover role="menu" $id="row.menuId" $aria-label="row.optionsLabel">
130
130
  <button class="menu-item sec-move-up" type="button" role="menuitem" $disabled="row.first" @click="moveUp(row, $event)">
131
- <span class="icon icon-arrow-up" aria-hidden="true"></span><span class="menu-text">Move up</span><span class="kbd">Alt ↑</span>
131
+ <span class="icon icon-arrow-up" aria-hidden="true"></span><span class="menu-text">Move up</span><span class="kbd">{{ state.keys.up }}</span>
132
132
  </button>
133
133
  <button class="menu-item sec-move-down" type="button" role="menuitem" $disabled="row.last" @click="moveDown(row, $event)">
134
- <span class="icon icon-arrow-down" aria-hidden="true"></span><span class="menu-text">Move down</span><span class="kbd">Alt ↓</span>
134
+ <span class="icon icon-arrow-down" aria-hidden="true"></span><span class="menu-text">Move down</span><span class="kbd">{{ state.keys.down }}</span>
135
135
  </button>
136
136
  <button class="menu-item sec-rename-item" type="button" role="menuitem" $show="state.canLabel" @click="startRename(row, $event)">
137
137
  <span class="icon icon-write" aria-hidden="true"></span><span class="menu-text">{{ row.label ? 'Rename' : 'Name this ' + state.singular }}</span>
138
138
  </button>
139
- <button class="menu-item sec-duplicate" type="button" role="menuitem" $show="state.canAdd && !row.choosing" @click="duplicate(row, $event)">
139
+ <button class="menu-item sec-duplicate" type="button" role="menuitem" $show="state.canAdd && !row.choosing && !row.once" @click="duplicate(row, $event)">
140
140
  <span class="icon icon-copy" aria-hidden="true"></span><span class="menu-text">Duplicate</span>
141
141
  </button>
142
142
  <button class="menu-item sec-add-below" type="button" role="menuitem" $show="state.canAdd" @click="addBelow(row, $event)">
143
143
  <span class="icon icon-plus" aria-hidden="true"></span><span class="menu-text">{{ state.addLabel }} below</span>
144
144
  </button>
145
- <hr class="menu-divider" $show="state.canRemove" />
146
- <button class="menu-item danger sec-remove" type="button" role="menuitem" $show="state.canRemove" @click="remove(row, $event)">
145
+ <hr class="menu-divider" $show="row.removable" />
146
+ <button class="menu-item danger sec-remove" type="button" role="menuitem" $show="row.removable" @click="remove(row, $event)">
147
147
  <span class="icon icon-trash" aria-hidden="true"></span>
148
148
  <span><b>{{ state.removeLabel }}</b><span class="subtle">You can undo this until you leave the page</span></span>
149
149
  </button>
@@ -153,13 +153,29 @@
153
153
  <!-- Not chosen yet. The row exists, holds its place in the order, and
154
154
  says what it could become. -->
155
155
  <div class="repeater-picker" $show="row.choosing">
156
+ <p class="repeater-picker-note" $show="state.hereChoices.length">Made for this page</p>
157
+
158
+ <div class="repeater-choices" $show="state.hereChoices.length">
159
+ <button
160
+ class="repeater-choice pick"
161
+ type="button"
162
+ $for="choice in state.hereChoices"
163
+ $key="choice.name"
164
+ $disabled="state.readOnly"
165
+ @click="choose(row, choice.name)"
166
+ >
167
+ <span class="repeater-choice-name">{{ choice.label }}</span>
168
+ <span class="repeater-choice-note" $show="choice.description">{{ choice.description }}</span>
169
+ </button>
170
+ </div>
171
+
156
172
  <p class="repeater-picker-note">What kind of section is this?</p>
157
173
 
158
174
  <div class="repeater-choices">
159
175
  <button
160
176
  class="repeater-choice pick"
161
177
  type="button"
162
- $for="choice in state.choices"
178
+ $for="choice in state.libraryChoices"
163
179
  $key="choice.name"
164
180
  $disabled="state.readOnly"
165
181
  @click="choose(row, choice.name)"
@@ -173,6 +189,12 @@
173
189
  <!-- The same field registry, one level down: a row is a group of fields,
174
190
  so it renders exactly the way the form does. That is what makes a
175
191
  block editor a repeater whose rows are field groups. -->
192
+ <p class="sec-note" $show="row.note && !row.choosing">{{ row.note }}</p>
193
+ <p class="sec-note sec-sources" $show="row.sources.length && !row.choosing">
194
+ <span>Shows what's in</span>
195
+ <a $for="source in row.sources" $key="source.name" $href="source.href" target="_blank" rel="noopener">{{ source.label }}</a>
196
+ </p>
197
+
176
198
  <div class="repeater-fields" $show="!row.choosing">
177
199
  <Dynamic
178
200
  $for="inner in row.fields"
@@ -214,12 +236,34 @@
214
236
  </div>
215
237
 
216
238
  <div class="dialog-body">
239
+ <p class="picks-group" $show="state.hereChoices.length">Made for this page</p>
240
+ <div class="picks" $show="state.hereChoices.length" role="radiogroup" aria-label="Made for this page">
241
+ <button
242
+ class="repeater-choice pick"
243
+ type="button"
244
+ role="radio"
245
+ $for="choice in state.hereChoices"
246
+ $key="choice.name"
247
+ $data-pick="choice.name"
248
+ $aria-checked="choice.name === state.picked ? 'true' : 'false'"
249
+ $tabindex="choice.name === state.picked ? 0 : -1"
250
+ @click="pick(choice.name)"
251
+ @dblclick="confirmPick()"
252
+ >
253
+ <span class="mini" $data-kind="choice.name" aria-hidden="true"><i></i><i></i><i></i><i></i><i></i></span>
254
+ <span class="pick-words">
255
+ <b class="repeater-choice-name">{{ choice.label }}</b>
256
+ <span class="subtle repeater-choice-note" $show="choice.description">{{ choice.description }}</span>
257
+ </span>
258
+ </button>
259
+ </div>
260
+ <p class="picks-group" $show="state.hereChoices.length">Every page</p>
217
261
  <div class="picks" role="radiogroup" aria-label="Section type">
218
262
  <button
219
263
  class="repeater-choice pick"
220
264
  type="button"
221
265
  role="radio"
222
- $for="choice in state.choices"
266
+ $for="choice in state.libraryChoices"
223
267
  $key="choice.name"
224
268
  $data-pick="choice.name"
225
269
  $aria-checked="choice.name === state.picked ? 'true' : 'false'"
@@ -251,6 +295,8 @@ import { blockOf, blocksOf, componentFor, tagsRows } from '../config/components.
251
295
  import { ROWS, VALUE, gatherAt, seedAt } from '../content/entry.ts'
252
296
  import { watchShown } from '../components/visibility.ts'
253
297
  import { LANDING_MS, createReorder } from '../components/reorder.ts'
298
+ import { offeredBlocks } from '../content/placement.ts'
299
+ import { keyLabel } from '../keys.ts'
254
300
 
255
301
  /**
256
302
  * A list of field groups.
@@ -321,6 +367,10 @@ const state = reactive({
321
367
  dialogId: 'repeater-' + props.path + '-picker',
322
368
  rows: [],
323
369
  choices: [],
370
+ /** Shortcuts the way this keyboard labels them. */
371
+ keys: { up: keyLabel('Alt+ArrowUp'), down: keyLabel('Alt+ArrowDown') },
372
+ hereChoices: [],
373
+ libraryChoices: [],
324
374
  addLabel: 'Add a row',
325
375
  removeLabel: 'Remove row',
326
376
  countLabel: '',
@@ -346,6 +396,8 @@ let pickAt = 0
346
396
 
347
397
  /** What a row is called: "section" where rows choose a shape, else the label's singular. */
348
398
  function noun() {
399
+ const said = props.field.options && props.field.options.singular
400
+ if (typeof said === 'string' && said.trim()) return said.trim().toLowerCase()
349
401
  if (tagsRows(props.field)) return 'section'
350
402
  return (props.field.label || 'row').toLowerCase().replace(/s$/, '')
351
403
  }
@@ -409,11 +461,15 @@ function newKey() {
409
461
  function rebuild() {
410
462
  const shapes = blocksOf(props.field)
411
463
 
412
- state.choices = shapes.map((block) => ({
464
+ const choice = (block) => ({
413
465
  name: block.name,
414
466
  label: block.label || block.name,
415
467
  description: block.description || '',
416
- }))
468
+ })
469
+ const offered = offeredBlocks(shapes, pagePath, rowsOf().map((row) => row.block).filter(Boolean))
470
+ state.hereChoices = offered.here.map(choice)
471
+ state.libraryChoices = offered.library.map(choice)
472
+ state.choices = state.hereChoices.concat(state.libraryChoices)
417
473
 
418
474
  const singular = noun()
419
475
  state.addLabel = 'Add ' + article(singular) + ' ' + singular
@@ -428,7 +484,7 @@ function rebuild() {
428
484
  // Remove at the floor. The rules still check on save - a browser is not
429
485
  // where content is decided.
430
486
  const count = rowsOf().length
431
- state.canAdd = props.field.maxRows === undefined || count < props.field.maxRows
487
+ state.canAdd = (props.field.maxRows === undefined || count < props.field.maxRows) && (!tagsRows(props.field) || state.choices.length > 0)
432
488
  state.canRemove = props.field.minRows === undefined || count > props.field.minRows
433
489
 
434
490
  const rows = rowsOf()
@@ -508,6 +564,12 @@ function rebuild() {
508
564
  })),
509
565
  choosing,
510
566
  blockLabel,
567
+ note: block && block.note ? block.note : '',
568
+ sources: block && block.uses
569
+ ? block.uses.map((name) => ({ name, label: sourceLabels[name] || name, href: '/collections/' + name }))
570
+ : [],
571
+ once: Boolean(block && block.once),
572
+ removable: state.canRemove && !(block && block.kept),
511
573
  first: index === 0,
512
574
  last: index === rows.length - 1,
513
575
  invalid,
@@ -685,7 +747,43 @@ function addText() {
685
747
  insert(rowList().length, { block: 'text', data: {} })
686
748
  }
687
749
 
688
- function addAt(at) {
750
+ /**
751
+ * This entry's address, for the blocks made for particular pages. Asked of the
752
+ * server, which knows the URL rules, when the list loads and again before the
753
+ * picker opens, since the slug or the parent may have changed in between.
754
+ */
755
+ let pagePath = null
756
+
757
+ /** What the data sources blocks use are called, for the links to them. */
758
+ const sourceLabels = {}
759
+
760
+ async function nameSources() {
761
+ const used = blocksOf(props.field).some((block) => block.uses && block.uses.length)
762
+ if (!used || !window.telaApi) return
763
+ const result = await window.telaApi.get('/api/collections')
764
+ if (!result.ok) return
765
+ for (const one of result.data.collections || []) sourceLabels[one.name] = one.label || one.name
766
+ rebuild()
767
+ }
768
+
769
+ async function locate() {
770
+ const placed = blocksOf(props.field).some((block) => block.for && block.for.length)
771
+ const collection = (shared('route').value?.params ?? {}).collection ?? ''
772
+ if (!placed || !collection || !window.telaApi) return
773
+ const ask = (data) => window.telaApi.post('/api/collections/' + collection + '/address', { data })
774
+ const first = await ask({})
775
+ if (!first.ok) return
776
+ const data = {}
777
+ for (const name of first.data.uses || []) data[name] = readCell(VALUE + name)
778
+ const found = await ask(data)
779
+ const path = found.ok && typeof found.data.path === 'string' ? found.data.path : null
780
+ if (path === pagePath) return
781
+ pagePath = path
782
+ rebuild()
783
+ }
784
+
785
+ async function addAt(at) {
786
+ await locate()
689
787
  if (!state.canAdd) return
690
788
 
691
789
  // One shape is never in question, so nothing is asked. A field written with
@@ -793,7 +891,7 @@ function remove(row, event) {
793
891
  closeMenus()
794
892
  stop(event)
795
893
  const at = state.rows.indexOf(row)
796
- if (at < 0 || !state.canRemove) return
894
+ if (at < 0 || !row.removable) return
797
895
 
798
896
  const rows = rowsAsData()
799
897
  const [removed] = rows.splice(at, 1)
@@ -834,7 +932,7 @@ function duplicate(row, event) {
834
932
  closeMenus()
835
933
  stop(event)
836
934
  const at = state.rows.indexOf(row)
837
- if (at < 0 || !state.canAdd) return
935
+ if (at < 0 || !state.canAdd || row.once) return
838
936
 
839
937
  const source = rowsAsData()[at]
840
938
  insert(at + 1, JSON.parse(JSON.stringify(source)))
@@ -1068,6 +1166,8 @@ function reveal(event) {
1068
1166
  onMounted(() => {
1069
1167
  seed()
1070
1168
  rebuild()
1169
+ locate()
1170
+ nameSources()
1071
1171
  window.addEventListener('tela:reveal', reveal)
1072
1172
  })
1073
1173
 
@@ -1388,6 +1488,34 @@ readOnly.subscribe(rebuild)
1388
1488
  margin-right: auto;
1389
1489
  }
1390
1490
 
1491
+ .picks-group {
1492
+ margin: 0 0 var(--sp-2);
1493
+ font-size: var(--fs-s);
1494
+ font-weight: var(--fw-semibold);
1495
+ color: var(--text-2);
1496
+ }
1497
+
1498
+ .picks + .picks-group {
1499
+ margin-top: var(--sp-4);
1500
+ }
1501
+
1502
+ .sec-sources {
1503
+ display: flex;
1504
+ flex-wrap: wrap;
1505
+ gap: var(--sp-2);
1506
+ }
1507
+
1508
+ .sec-sources a {
1509
+ color: var(--accent);
1510
+ font-weight: var(--fw-medium);
1511
+ }
1512
+
1513
+ .sec-note {
1514
+ margin: 0 0 var(--sp-3);
1515
+ font-size: var(--fs-s);
1516
+ color: var(--text-2);
1517
+ }
1518
+
1391
1519
  .picks {
1392
1520
  display: grid;
1393
1521
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
@@ -0,0 +1,63 @@
1
+ <view>
2
+ <Field $field="props.field" $path="props.path" $controlId="'f-' + props.path">
3
+ <label class="switch-row">
4
+ <input
5
+ class="sr-only"
6
+ type="checkbox"
7
+ $id="'f-' + props.path"
8
+ $checked="state.on"
9
+ $disabled="state.readonly"
10
+ $aria-describedby="state.hint ? 'h-' + props.path : null"
11
+ @change="flip()"
12
+ />
13
+ <span class="switch" aria-hidden="true"></span>
14
+ <span class="toggle-words">{{ state.on ? state.yes : state.no }}</span>
15
+ </label>
16
+ </Field>
17
+ </view>
18
+
19
+ <logic lang="ts">
20
+ import { defineProps, onMounted, reactive, shared } from '@lightspeed-js/core'
21
+
22
+ /**
23
+ * One yes-or-no, stored as a boolean: a switch that says what each side means.
24
+ *
25
+ * `options.on` and `options.off` are the words beside it, "Shown" and
26
+ * "Hidden" rather than a bare yes or no, so the editor reads the outcome
27
+ * rather than working it out. A real checkbox underneath, so a keyboard and a
28
+ * screen reader have it for free.
29
+ */
30
+ const props = defineProps({
31
+ field: { default: {} },
32
+ path: { default: '' },
33
+ })
34
+
35
+ const cell = shared('entry:value:' + props.path)
36
+ const readonly = shared('entry:readonly')
37
+ const options = props.field.options || {}
38
+
39
+ const state = reactive({
40
+ on: cell.value === true,
41
+ yes: options.on || 'Yes',
42
+ no: options.off || 'No',
43
+ hint: Boolean(options.hint),
44
+ readonly: false,
45
+ })
46
+
47
+ function read() {
48
+ state.on = cell.value === true
49
+ state.readonly = Boolean(props.field.readOnly || readonly.value)
50
+ }
51
+
52
+ function flip() {
53
+ if (state.readonly) return
54
+ cell.value = !(cell.value === true)
55
+ }
56
+
57
+ onMounted(() => {
58
+ cell.subscribe(read)
59
+ readonly.subscribe(read)
60
+ read()
61
+ })
62
+ </logic>
63
+