@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
@@ -9,7 +9,7 @@
9
9
 
10
10
  <header class="statusbar" $show="!state.loading && !state.error">
11
11
  <div class="crumb">
12
- <a class="crumb-up" $href="'/collections/' + state.collection">{{ state.plural }}</a>
12
+ <a class="crumb-up" $show="!state.isData" $href="'/collections/' + state.collection">{{ state.plural }}</a>
13
13
  <div class="crumb-title">{{ state.title }}</div>
14
14
  </div>
15
15
 
@@ -61,7 +61,7 @@
61
61
  <button
62
62
  class="btn btn-ghost"
63
63
  type="button"
64
- data-tip="Focus mode: hides the menus so the form and preview get the room. Ctrl+Shift+F, or Esc to leave."
64
+ $data-tip="state.tips.focus"
65
65
  $aria-pressed="state.focus ? 'true' : 'false'"
66
66
  @click="toggleFocus"
67
67
  >
@@ -84,7 +84,7 @@
84
84
  class="btn btn-secondary"
85
85
  type="submit"
86
86
  form="entry-form"
87
- data-tip="Keeps a draft. The website does not change. (Ctrl+S)"
87
+ $data-tip="state.tips.save"
88
88
  $show="state.canEdit && !state.deleted"
89
89
  $disabled="state.saving"
90
90
  >
@@ -322,11 +322,25 @@
322
322
 
323
323
  <aside class="preview" aria-label="Preview" $show="state.preview">
324
324
  <div class="preview-bar">
325
- <span class="preview-name">Preview</span>
325
+ <span class="preview-name" $show="!state.isData">Preview</span>
326
+ <span class="segmented preview-tabs" role="group" aria-label="Preview" $show="state.isData">
327
+ <button type="button" $show="state.previewPages.length" $aria-pressed="state.previewTab === 'page' ? 'true' : 'false'" @click="setPreviewTab('page')">
328
+ <span class="icon icon-m icon-screen" aria-hidden="true"></span><span>Page</span>
329
+ </button>
330
+ <button type="button" $aria-pressed="state.previewTab === 'data' ? 'true' : 'false'" @click="setPreviewTab('data')">Data</button>
331
+ </span>
326
332
  <span class="badge badge-warn">Your draft</span>
327
- <span class="subtle preview-width" aria-live="off">{{ state.previewing ? 'Updating…' : state.frameLabel }}</span>
328
-
329
- <span class="preview-scheme" role="group" aria-label="Colour scheme">
333
+ <select
334
+ class="select preview-page"
335
+ aria-label="Page to preview on"
336
+ $show="state.isData && state.previewTab === 'page' && state.previewPages.length > 1"
337
+ @change="pickPreviewPage($event)"
338
+ >
339
+ <option $for="page in state.previewPages" $key="page" $value="page" $selected="page === state.previewPath">{{ page }}</option>
340
+ </select>
341
+ <span class="subtle preview-width" aria-live="off" $show="!state.onData">{{ state.previewing ? 'Updating…' : state.frameLabel }}</span>
342
+
343
+ <span class="preview-scheme" role="group" aria-label="Colour scheme" $show="!state.onData">
330
344
  <button class="btn btn-ghost btn-sm btn-icon" type="button" aria-label="Follow the computer's setting" data-tip $aria-pressed="state.scheme === 'auto' ? 'true' : 'false'" @click="setScheme('auto')">
331
345
  <span class="icon icon-contrast" aria-hidden="true"></span>
332
346
  </button>
@@ -338,7 +352,7 @@
338
352
  </button>
339
353
  </span>
340
354
 
341
- <span class="preview-sizes" role="group" aria-label="Screen size">
355
+ <span class="preview-sizes" role="group" aria-label="Screen size" $show="!state.onData">
342
356
  <button class="btn btn-ghost btn-sm btn-icon" type="button" aria-label="Fit to pane" data-tip $aria-pressed="state.frame === 'fit' ? 'true' : 'false'" @click="setFrame('fit')">
343
357
  <span class="icon icon-screen" aria-hidden="true"></span>
344
358
  </button>
@@ -350,16 +364,30 @@
350
364
  </button>
351
365
  </span>
352
366
 
353
- <button class="btn btn-ghost btn-sm btn-icon" type="button" aria-label="Open preview in a new window" data-tip @click="popOut">
367
+ <button class="btn btn-ghost btn-sm btn-icon" type="button" aria-label="Open preview in a new window" data-tip $show="!state.onData" @click="popOut">
354
368
  <span class="icon icon-external" aria-hidden="true"></span>
355
369
  </button>
356
370
  </div>
357
371
 
358
372
  <!-- Edge to edge: the pane is the viewport, so the site's own
359
373
  breakpoints answer to the pane's width. -->
360
- <div class="preview-stage">
374
+ <div class="preview-stage" $show="!state.onData">
361
375
  <iframe class="preview-frame" title="Preview" $style="state.frameStyle" $srcdoc="state.previewHtml"></iframe>
362
376
  </div>
377
+
378
+ <!-- A data source as anything reading it will get it: the file, and
379
+ where it can be read if the project serves it somewhere. -->
380
+ <div class="preview-data" $show="state.onData">
381
+ <p class="preview-data-where">
382
+ <span>Published to <span class="preview-code">{{ state.dataFile }}</span></span>
383
+ <span $show="state.dataAddress">Read it at <a $href="state.dataAddress" target="_blank" rel="noopener">{{ state.dataAddress }}</a></span>
384
+ <span $show="state.isData && !state.previewPages.length && state.pagesKnown">No page on the site shows this yet.</span>
385
+ <span $show="state.pageProblem">The page preview isn't available: {{ state.pageProblem }}</span>
386
+ <button class="btn btn-ghost btn-sm" type="button" @click="copyData()"><span class="icon icon-copy" aria-hidden="true"></span>Copy</button>
387
+ </p>
388
+ <!-- A div, not a pre: Lightspeed leaves pre and code verbatim, bindings and all. -->
389
+ <div class="preview-json preview-code">{{ state.dataJson }}</div>
390
+ </div>
363
391
  </aside>
364
392
  </div>
365
393
 
@@ -527,6 +555,7 @@ import { reactive, onMounted, shared } from '@lightspeed-js/core'
527
555
  // and the save check come from one definition instead of two.
528
556
  import { checkAll, serverRules } from '../content/rules.ts'
529
557
  import { WAITING, fetchWaiting } from '../waiting.ts'
558
+ import { keyLabel } from '../keys.ts'
530
559
  import {
531
560
  derived,
532
561
  derivedFields,
@@ -640,6 +669,25 @@ function setUi(path, patch) {
640
669
  }
641
670
 
642
671
  const state = reactive({
672
+ /** Tooltips naming shortcuts the way this keyboard labels them. */
673
+ tips: {
674
+ focus: 'Focus mode: hides the menus so the form and preview get the room. ' + keyLabel('Mod+Shift+F') + ', or Esc to leave.',
675
+ save: 'Keeps a draft. The website does not change. (' + keyLabel('Mod+S') + ')',
676
+ },
677
+ /** A data source: one record, called what its collection is called. */
678
+ isData: false,
679
+ /** Its preview shows a page that uses it, or the data itself. */
680
+ previewTab: 'page',
681
+ /** Whether the data tab is the one on screen. */
682
+ onData: false,
683
+ previewPages: [],
684
+ previewPath: '',
685
+ pagesKnown: false,
686
+ /** Why a page that shows the data could not be drawn, when it is not that none does. */
687
+ pageProblem: '',
688
+ dataJson: '',
689
+ dataFile: '',
690
+ dataAddress: '',
643
691
  changes: [],
644
692
  collection: params.collection ?? '',
645
693
  // `/posts/new` is the create form: the path always has an id segment, so
@@ -789,6 +837,7 @@ onMounted(async () => {
789
837
  state.singular = singularOf(state.pluralLower)
790
838
  state.titleField = collection.titleField ?? 'title'
791
839
  state.publishable = collection.publishable !== false
840
+ state.isData = collection.data === true
792
841
 
793
842
  if (state.id) {
794
843
  const entry = await window.telaApi.get(
@@ -1666,6 +1715,7 @@ function refreshPreview() {
1666
1715
  // somebody most wants to look at before committing to it.
1667
1716
  const watching = state.preview || (previewWindow && !previewWindow.closed)
1668
1717
  if (!watching) return
1718
+ if (state.isData) return refreshDataPreview()
1669
1719
 
1670
1720
  clearTimeout(previewTimer)
1671
1721
  previewTimer = setTimeout(async () => {
@@ -1693,6 +1743,85 @@ function refreshPreview() {
1693
1743
  }, 200)
1694
1744
  }
1695
1745
 
1746
+ /**
1747
+ * A data source's preview: the data as it would be published, always, and a
1748
+ * page that shows it when the site has one. Which pages those are is the
1749
+ * site's answer, so a source no page shows yet opens on its data.
1750
+ */
1751
+ function refreshDataPreview() {
1752
+ clearTimeout(previewTimer)
1753
+ previewTimer = setTimeout(async () => {
1754
+ const seq = ++previewSeq
1755
+ state.previewing = true
1756
+ const sent = { entryId: state.id || null, data: values() }
1757
+
1758
+ const published = await window.telaApi.post('/api/collections/' + state.collection + '/published', sent)
1759
+ if (seq !== previewSeq) return
1760
+ if (published.ok) {
1761
+ state.dataJson = JSON.stringify(published.data.data, null, 2)
1762
+ state.dataFile = published.data.file
1763
+ state.dataAddress = published.data.address || ''
1764
+ }
1765
+
1766
+ if (state.previewTab === 'page' || !state.pagesKnown) {
1767
+ const result = await window.telaApi.raw('/api/collections/' + state.collection + '/preview', {
1768
+ ...sent,
1769
+ page: state.previewPath || undefined,
1770
+ })
1771
+ if (seq !== previewSeq) return
1772
+ let pages = []
1773
+ try {
1774
+ pages = JSON.parse(result.headers.get('x-preview-pages') || '[]')
1775
+ } catch {
1776
+ pages = []
1777
+ }
1778
+ state.previewPages = Array.isArray(pages) ? pages : []
1779
+ state.pagesKnown = result.ok || result.status === 404
1780
+ state.pageProblem = state.pagesKnown ? '' : problemIn(result.body)
1781
+ state.previewPath = result.headers.get('x-preview-page') || state.previewPages[0] || ''
1782
+ previewPage = result.ok
1783
+ ? result.body
1784
+ : '<p style="font:14px system-ui;padding:1rem;color:#c0392b">' + (result.body || 'Preview failed') + '</p>'
1785
+ if (!state.previewPages.length) state.previewTab = 'data'
1786
+ showPreview()
1787
+ }
1788
+
1789
+ state.onData = state.previewTab === 'data'
1790
+ state.previewing = false
1791
+ }, 200)
1792
+ }
1793
+
1794
+ /** The message in a failed answer, which is JSON from Tela or plain words from the site. */
1795
+ function problemIn(body) {
1796
+ try {
1797
+ const said = JSON.parse(body)
1798
+ if (said && typeof said.error === 'string') return said.error
1799
+ } catch {
1800
+ // Not JSON: the words themselves.
1801
+ }
1802
+ return body || 'it did not answer.'
1803
+ }
1804
+
1805
+ function setPreviewTab(tab) {
1806
+ state.previewTab = tab
1807
+ state.onData = tab === 'data'
1808
+ refreshPreview()
1809
+ }
1810
+
1811
+ function pickPreviewPage(event) {
1812
+ state.previewPath = event.target.value
1813
+ refreshPreview()
1814
+ }
1815
+
1816
+ async function copyData() {
1817
+ try {
1818
+ await navigator.clipboard.writeText(state.dataJson)
1819
+ window.telaUi.toast('Copied the data')
1820
+ } catch {
1821
+ window.telaUi.toast('Could not copy. Select the text and copy it instead.')
1822
+ }
1823
+ }
1824
+
1696
1825
  /** The page as the site last drew it, before the chosen scheme is applied. */
1697
1826
  let previewPage = ''
1698
1827
 
@@ -1840,7 +1969,7 @@ function refreshStatus() {
1840
1969
  state.title =
1841
1970
  typeof named === 'string' && named.trim()
1842
1971
  ? named.trim()
1843
- : person || (state.id ? 'Untitled ' : 'New ') + state.singular
1972
+ : person || (state.isData ? state.plural : (state.id ? 'Untitled ' : 'New ') + state.singular)
1844
1973
 
1845
1974
  const at = state.savedAt ? ui.clockTime(state.savedAt) : ''
1846
1975
  const count = state.changes.length
@@ -2385,6 +2514,40 @@ window.addEventListener('keydown', (event) => {
2385
2514
  </logic>
2386
2515
 
2387
2516
  <style>
2517
+ .preview-data {
2518
+ display: flex;
2519
+ flex-direction: column;
2520
+ gap: var(--sp-2);
2521
+ min-height: 0;
2522
+ padding: var(--sp-3);
2523
+ overflow: auto;
2524
+ }
2525
+
2526
+ .preview-data-where {
2527
+ display: flex;
2528
+ flex-wrap: wrap;
2529
+ align-items: center;
2530
+ gap: var(--sp-2) var(--sp-3);
2531
+ margin: 0;
2532
+ font-size: var(--fs-s);
2533
+ color: var(--text-2);
2534
+ }
2535
+
2536
+ .preview-code {
2537
+ font-family: var(--font-mono);
2538
+ }
2539
+
2540
+ .preview-json {
2541
+ margin: 0;
2542
+ padding: var(--sp-3);
2543
+ border: 1px solid var(--border);
2544
+ border-radius: var(--r-m);
2545
+ background: var(--surface);
2546
+ font-size: var(--fs-s);
2547
+ white-space: pre;
2548
+ overflow: auto;
2549
+ }
2550
+
2388
2551
  .component-id {
2389
2552
  font-family: var(--font-mono);
2390
2553
  font-size: 0.9em;
@@ -403,6 +403,14 @@ async function load() {
403
403
  return
404
404
  }
405
405
 
406
+ // A data source is one record: there is no list to show, only the record.
407
+ if (schema.data.collection.data) {
408
+ const one = await window.telaApi.get('/api/entries/' + state.collection + '?limit=1')
409
+ const id = one.ok && one.data.entries && one.data.entries[0] ? one.data.entries[0].id : 'new'
410
+ window.location.replace('/collections/' + state.collection + '/' + id)
411
+ return
412
+ }
413
+
406
414
  state.label = schema.data.collection.label ?? state.collection
407
415
  state.plural = state.label.toLowerCase()
408
416
  state.singular = /ies$/.test(state.plural) ? state.plural.replace(/ies$/, 'y') : state.plural.replace(/s$/, '')
@@ -16,7 +16,22 @@
16
16
  <!-- The control. Slot content keeps the *parent's* scope and bindings
17
17
  (ADR-004), so a field component writes its own `$model` and its own
18
18
  styles here and this component never has to know what it wrapped. -->
19
- <slot></slot>
19
+ <div class="field-default" $show="state.defaulted">
20
+ <p class="field-default-text">{{ state.fallbackText }}</p>
21
+ <p class="field-default-bar">
22
+ <span>Using the default</span>
23
+ <button class="btn btn-ghost btn-sm" type="button" $show="!state.readonly" @click="change()">Change</button>
24
+ </p>
25
+ </div>
26
+
27
+ <div class="field-control" $show="!state.defaulted">
28
+ <slot></slot>
29
+ </div>
30
+
31
+ <p class="field-default-bar" $show="state.changed">
32
+ <span>Changed from the default</span>
33
+ <button class="btn btn-ghost btn-sm" type="button" $show="!state.readonly" @click="reset()">Back to default</button>
34
+ </p>
20
35
 
21
36
  <CharacterCount $if="state.counted" $field="props.field" $path="props.path" />
22
37
 
@@ -99,12 +114,14 @@ const props = defineProps({
99
114
  const issues = shared('entry:issues', {})
100
115
 
101
116
  const options = props.field.options || {}
117
+ const hasFallback = props.field.fallback !== undefined
102
118
 
103
119
  const state = reactive({
104
120
  status: '',
105
121
  parts: [],
106
122
  shown: true,
107
- note: options.note || (props.field.required || props.field.readOnly || props.path.startsWith('filter.') ? '' : 'Optional'),
123
+ // A switch is always on or off, so it is never "Optional".
124
+ note: options.note || (hasFallback || props.field.required || props.field.readOnly || props.field.type === 'boolean' || props.path.startsWith('filter.') ? '' : 'Optional'),
108
125
  hint: options.hint || '',
109
126
  counted: Boolean(counterOf(props.field)),
110
127
  adviceTone: '',
@@ -112,6 +129,10 @@ const state = reactive({
112
129
  adviceId: 'a-' + props.path,
113
130
  hintId: 'h-' + props.path,
114
131
  errorId: 'e-' + props.path,
132
+ defaulted: false,
133
+ changed: false,
134
+ readonly: false,
135
+ fallbackText: fallbackText(props.field.fallback),
115
136
  })
116
137
 
117
138
  watchShown(props.field, props.path, shared, (shown) => {
@@ -163,6 +184,47 @@ function advised() {
163
184
  advised()
164
185
  advice.subscribe(advised)
165
186
  onMounted(describe)
187
+
188
+ /**
189
+ * A field with a `fallback` shows it, greyed, until somebody changes it, and
190
+ * offers the way back. Cleared is the same as never set: the default again.
191
+ */
192
+ function fallbackText(fallback) {
193
+ if (typeof fallback === 'string') return fallback.replace(/^\s*[-*] /gm, '• ').replace(/[*_]{1,2}([^*_]+)[*_]{1,2}/g, '$1')
194
+ if (Array.isArray(fallback) && fallback.every((one) => typeof one === 'string')) return fallback.join('\n')
195
+ return 'Set by the site'
196
+ }
197
+
198
+ const unset = (value) => value === undefined || value === null || value === ''
199
+ const value = shared('entry:value:' + props.path)
200
+ const readonly = shared('entry:readonly')
201
+
202
+ function readDefault() {
203
+ state.readonly = Boolean(props.field.readOnly || readonly.value)
204
+ state.defaulted = hasFallback && unset(value.value)
205
+ state.changed = hasFallback && !state.defaulted
206
+ }
207
+
208
+ function change() {
209
+ value.set(structuredClone(props.field.fallback))
210
+ readDefault()
211
+ setTimeout(() => {
212
+ const control = document.querySelector('[data-field-path="' + props.path + '"] .field-control')
213
+ const editable = control && control.querySelector('input, textarea, select, [contenteditable="true"]')
214
+ if (editable) editable.focus()
215
+ }, 40)
216
+ }
217
+
218
+ function reset() {
219
+ value.set(undefined)
220
+ readDefault()
221
+ }
222
+
223
+ if (hasFallback) {
224
+ readDefault()
225
+ value.subscribe(readDefault)
226
+ readonly.subscribe(readDefault)
227
+ }
166
228
  </logic>
167
229
 
168
230
  <style>
@@ -188,6 +250,34 @@ onMounted(describe)
188
250
  color: var(--text-3);
189
251
  }
190
252
 
253
+ .field-control {
254
+ display: contents;
255
+ }
256
+
257
+ .field-default {
258
+ display: flex;
259
+ flex-direction: column;
260
+ gap: var(--sp-1);
261
+ padding: var(--sp-2) var(--sp-3);
262
+ border: 1px dashed var(--border);
263
+ border-radius: var(--r-m);
264
+ }
265
+
266
+ .field-default-text {
267
+ margin: 0;
268
+ color: var(--text-3);
269
+ white-space: pre-line;
270
+ }
271
+
272
+ .field-default-bar {
273
+ display: flex;
274
+ align-items: center;
275
+ gap: var(--sp-2);
276
+ margin: 0;
277
+ font-size: var(--fs-s);
278
+ color: var(--text-3);
279
+ }
280
+
191
281
  .field-hint {
192
282
  margin: 0;
193
283
  font-size: var(--fs-s);