@nickpaustian/tela 0.1.3 → 0.3.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.
@@ -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
+
@@ -1,4 +1,5 @@
1
- import type { Field } from "./types.ts"
1
+ import type { Block, Collection, Field } from "./types.ts"
2
+ import { acceptedKinds, linkMessage, linkPattern, type LinkKind } from "../content/link-address.ts"
2
3
 
3
4
  /**
4
5
  * Field shapes several collections want, written once.
@@ -131,3 +132,105 @@ export function person(
131
132
  ],
132
133
  }
133
134
  }
135
+
136
+ /**
137
+ * Somewhere to go, chosen the way an editor thinks of it.
138
+ *
139
+ * One box. Typing searches this site's pages, and the library's files where
140
+ * they are accepted; pasting an address keeps it, tidied: an email gets
141
+ * `mailto:`, `example.org` its `https://`, and an address on this site becomes
142
+ * the page it names, or its path.
143
+ *
144
+ * `accepts` narrows what it may go to (see `LINK_KINDS`); by default anything
145
+ * but a file. `collections` narrows which entries count as pages.
146
+ *
147
+ * Stored as `tela:<collection>/<id>` for an entry or a file and as the address
148
+ * otherwise. Published as `tela:<collection>/<slug>` for an entry, which a
149
+ * site resolves as it does a rich-text link; as the file's own address for a
150
+ * file; as written for anything else. A link to something not on the site is
151
+ * left out, as a reference is.
152
+ */
153
+ export function link(
154
+ name: string,
155
+ label: string,
156
+ options: {
157
+ hint?: string
158
+ required?: boolean
159
+ accepts?: LinkKind[]
160
+ collections?: string[]
161
+ placeholder?: string
162
+ } = {},
163
+ ): Field {
164
+ const { required, accepts, collections, ...rest } = options
165
+ const kinds = acceptedKinds(accepts)
166
+ return {
167
+ name,
168
+ label,
169
+ type: "link",
170
+ ...(required ? { required: true } : {}),
171
+ patterns: [{ pattern: linkPattern(kinds, collections), message: linkMessage(kinds) }],
172
+ options: { ...rest, accepts: kinds, ...(collections?.length ? { collections } : {}) },
173
+ }
174
+ }
175
+
176
+ /**
177
+ * A section written for one page or a few: its content and its look are code,
178
+ * and only the parts named in `editable` are the editor's to change.
179
+ *
180
+ * Each editable field shows the code's own value until somebody changes it,
181
+ * and goes back to it when cleared. Facts shared with other pages or apps
182
+ * belong in a data source the block `uses` rather than in its content.
183
+ * The block is offered only on the pages it
184
+ * is `for`, at most `once`, and is `kept` - moved, never removed - unless it
185
+ * says otherwise.
186
+ */
187
+ export function customBlock(
188
+ name: string,
189
+ {
190
+ label,
191
+ description,
192
+ note,
193
+ for: pages,
194
+ uses,
195
+ once = true,
196
+ kept = true,
197
+ content = {},
198
+ editable = [],
199
+ }: {
200
+ label: string
201
+ description?: string
202
+ note?: string
203
+ for: string[]
204
+ uses?: string[]
205
+ once?: boolean
206
+ kept?: boolean
207
+ content?: Record<string, unknown>
208
+ editable?: Field[]
209
+ },
210
+ ): Block {
211
+ return {
212
+ name,
213
+ label,
214
+ description,
215
+ note,
216
+ for: pages,
217
+ ...(uses?.length ? { uses } : {}),
218
+ once,
219
+ kept,
220
+ content,
221
+ fields: editable.map((field) =>
222
+ field.fallback === undefined && content[field.name] !== undefined ? { ...field, fallback: content[field.name] } : field,
223
+ ),
224
+ }
225
+ }
226
+
227
+ /**
228
+ * One record of facts with no page of its own, kept where editors can change
229
+ * it and published once for every page and app that shows it.
230
+ */
231
+ export function dataSource(
232
+ name: string,
233
+ options: Omit<Collection, "name" | "data" | "url" | "publishable" | "fields"> & { fields: Field[] },
234
+ ): Collection {
235
+ return { ...options, name, data: true }
236
+ }
package/src/config/seo.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Collection, Field } from "./types.ts"
2
2
  import { imageUse } from "./fields.ts"
3
+ import { dataFields } from "./components.ts"
3
4
 
4
5
  /**
5
6
  * Search and sharing, as a collection option.
@@ -63,8 +64,9 @@ export function seoGroup(collection: Collection, siteName?: string): Field | nul
63
64
  const options: SeoOptions = collection.seo === true ? {} : collection.seo
64
65
  const at = `Collection "${collection.name}" seo`
65
66
 
67
+ const stored = dataFields(collection.fields)
66
68
  const named = (name: string) => {
67
- const field = collection.fields.find((one) => one.name === name)
69
+ const field = stored.find((one) => one.name === name)
68
70
  if (!field) throw new Error(`${at}: falls back to "${name}", which is not one of its fields.`)
69
71
  return field
70
72
  }
@@ -88,7 +90,7 @@ export function seoGroup(collection: Collection, siteName?: string): Field | nul
88
90
  maxLength: 70,
89
91
  counter: { target: options.title?.target ?? 60, ...(suffix ? { adds: suffix } : {}) },
90
92
  options: {
91
- hint: options.title?.hint ?? `Leave empty to use the ${collection.fields.some((one) => one.name === titleField) ? labelOf(titleField) : "title"}.`,
93
+ hint: options.title?.hint ?? `Leave empty to use the ${stored.some((one) => one.name === titleField) ? labelOf(titleField) : "title"}.`,
92
94
  },
93
95
  },
94
96
  {