trebol 0.1.0 → 0.1.1

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 (106) hide show
  1. package/README.md +8 -2
  2. package/dist/cli.js +55 -20
  3. package/dist/lib/core/access.d.ts +10 -0
  4. package/dist/lib/core/access.js +210 -0
  5. package/dist/lib/core/base.d.ts +4 -0
  6. package/dist/lib/core/base.js +50 -0
  7. package/dist/lib/core/bitset.d.ts +9 -0
  8. package/dist/lib/core/bitset.js +75 -0
  9. package/dist/lib/core/boot.d.ts +18 -0
  10. package/dist/lib/core/boot.js +72 -0
  11. package/dist/lib/core/compare.d.ts +9 -0
  12. package/dist/lib/core/compare.js +156 -0
  13. package/dist/lib/core/ctx.d.ts +36 -0
  14. package/dist/lib/core/ctx.js +251 -0
  15. package/dist/lib/core/dec.d.ts +28 -0
  16. package/dist/lib/core/dec.js +158 -0
  17. package/dist/lib/core/errors.d.ts +15 -0
  18. package/dist/lib/core/errors.js +44 -0
  19. package/dist/lib/core/escapes.d.ts +14 -0
  20. package/dist/lib/core/escapes.js +80 -0
  21. package/dist/lib/core/eval.d.ts +220 -0
  22. package/dist/lib/core/eval.js +666 -0
  23. package/dist/lib/core/func.d.ts +64 -0
  24. package/dist/lib/core/func.js +129 -0
  25. package/dist/lib/core/hash.d.ts +5 -0
  26. package/dist/lib/core/hash.js +155 -0
  27. package/dist/lib/core/index.d.ts +18 -0
  28. package/dist/lib/core/index.js +20 -0
  29. package/dist/lib/core/int64.d.ts +22 -0
  30. package/dist/lib/core/int64.js +113 -0
  31. package/dist/lib/core/kinds.d.ts +67 -0
  32. package/dist/lib/core/kinds.js +134 -0
  33. package/dist/lib/core/lexer.d.ts +13 -0
  34. package/dist/lib/core/lexer.js +702 -0
  35. package/dist/lib/core/mezz.d.ts +1 -0
  36. package/dist/lib/core/mezz.js +360 -0
  37. package/dist/lib/core/mold.d.ts +20 -0
  38. package/dist/lib/core/mold.js +434 -0
  39. package/dist/lib/core/natives/control.d.ts +1 -0
  40. package/dist/lib/core/natives/control.js +374 -0
  41. package/dist/lib/core/natives/csv.d.ts +15 -0
  42. package/dist/lib/core/natives/csv.js +315 -0
  43. package/dist/lib/core/natives/data.d.ts +11 -0
  44. package/dist/lib/core/natives/data.js +367 -0
  45. package/dist/lib/core/natives/define.d.ts +17 -0
  46. package/dist/lib/core/natives/define.js +47 -0
  47. package/dist/lib/core/natives/encode.d.ts +3 -0
  48. package/dist/lib/core/natives/encode.js +187 -0
  49. package/dist/lib/core/natives/json.d.ts +5 -0
  50. package/dist/lib/core/natives/json.js +273 -0
  51. package/dist/lib/core/natives/math.d.ts +3 -0
  52. package/dist/lib/core/natives/math.js +500 -0
  53. package/dist/lib/core/natives/parse.d.ts +1 -0
  54. package/dist/lib/core/natives/parse.js +600 -0
  55. package/dist/lib/core/natives/series.d.ts +4 -0
  56. package/dist/lib/core/natives/series.js +509 -0
  57. package/dist/lib/core/natives/text.d.ts +1 -0
  58. package/dist/lib/core/natives/text.js +484 -0
  59. package/dist/lib/core/natives/xml.d.ts +9 -0
  60. package/dist/lib/core/natives/xml.js +242 -0
  61. package/dist/lib/core/series.d.ts +26 -0
  62. package/dist/lib/core/series.js +73 -0
  63. package/dist/lib/core/symbols.d.ts +9 -0
  64. package/dist/lib/core/symbols.js +44 -0
  65. package/dist/lib/core/values.d.ts +206 -0
  66. package/dist/lib/core/values.js +223 -0
  67. package/dist/lib/host/browser.d.ts +4 -0
  68. package/dist/lib/host/browser.js +4081 -0
  69. package/dist/lib/host/chart.d.ts +26 -0
  70. package/dist/lib/host/chart.js +158 -0
  71. package/dist/lib/host/compress.d.ts +1 -0
  72. package/dist/lib/host/compress.js +37 -0
  73. package/dist/lib/host/draw.d.ts +31 -0
  74. package/dist/lib/host/draw.js +348 -0
  75. package/dist/lib/host/icons.d.ts +3 -0
  76. package/dist/lib/host/icons.js +97 -0
  77. package/dist/lib/host/index.d.ts +38 -0
  78. package/dist/lib/host/index.js +190 -0
  79. package/dist/lib/host/locale.d.ts +40 -0
  80. package/dist/lib/host/locale.js +241 -0
  81. package/dist/lib/host/markdown.d.ts +28 -0
  82. package/dist/lib/host/markdown.js +151 -0
  83. package/dist/lib/host/modules.d.ts +10 -0
  84. package/dist/lib/host/modules.js +262 -0
  85. package/dist/lib/host/natives.d.ts +6 -0
  86. package/dist/lib/host/natives.js +98 -0
  87. package/dist/lib/host/net.d.ts +12 -0
  88. package/dist/lib/host/net.js +541 -0
  89. package/dist/lib/host/page.d.ts +9 -0
  90. package/dist/lib/host/page.js +55 -0
  91. package/dist/lib/host/rows.d.ts +15 -0
  92. package/dist/lib/host/rows.js +80 -0
  93. package/dist/lib/host/scheduler.d.ts +45 -0
  94. package/dist/lib/host/scheduler.js +173 -0
  95. package/dist/lib/host/theme.d.ts +58 -0
  96. package/dist/lib/host/theme.js +1462 -0
  97. package/dist/lib/ui/i18n.trb +111 -0
  98. package/dist/lib/ui/ui.trb +1237 -0
  99. package/dist/trebol.js +123 -56
  100. package/package.json +17 -5
  101. package/src/host/browser.ts +274 -33
  102. package/src/host/index.ts +2 -2
  103. package/src/host/modules.ts +8 -1
  104. package/src/host/theme.ts +63 -4
  105. package/src/ui/i18n.trb +4 -0
  106. package/src/ui/ui.trb +48 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trebol",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Trebol: a Rebol-derived language for building beautiful web UIs without HTML or CSS",
5
5
  "keywords": [
6
6
  "rebol",
@@ -16,12 +16,24 @@
16
16
  },
17
17
  "type": "module",
18
18
  "exports": {
19
- "./core": "./src/core/index.ts",
20
- "./host": "./src/host/index.ts",
21
- "./browser": "./src/host/browser.ts"
19
+ "./core": {
20
+ "types": "./dist/lib/core/index.d.ts",
21
+ "source": "./src/core/index.ts",
22
+ "default": "./dist/lib/core/index.js"
23
+ },
24
+ "./host": {
25
+ "types": "./dist/lib/host/index.d.ts",
26
+ "source": "./src/host/index.ts",
27
+ "default": "./dist/lib/host/index.js"
28
+ },
29
+ "./browser": {
30
+ "types": "./dist/lib/host/browser.d.ts",
31
+ "source": "./src/host/browser.ts",
32
+ "default": "./dist/lib/host/browser.js"
33
+ }
22
34
  },
23
35
  "bin": {
24
- "trebol": "./dist/cli.js"
36
+ "trebol": "dist/cli.js"
25
37
  },
26
38
  "license": "Apache-2.0",
27
39
  "files": [
@@ -20,7 +20,7 @@ import { logic,
20
20
  type Cell, type MapCell, type ObjectCell, type BlockCell, type WordCell, type StringCell, type Ctx, type Sym,
21
21
  } from '../core/index.ts';
22
22
  import { mapAt, startHost, startUi, runSource, report, shown, translate, uiText, activeLanguage, editLocal, readLocal, readDate, weekStart, fileSize, LOCAL_KINDS, type Host } from './index.ts';
23
- import { spawn, taskResult } from './scheduler.ts';
23
+ import { spawn, taskResult, handleOf } from './scheduler.ts';
24
24
  import { themeCss, DEFAULT_THEME, LOOKS, THEME_WORDS, type ThemeParams } from './theme.ts';
25
25
  import { iconNode } from './icons.ts';
26
26
  import { parseMd, printMd, safeHref, type Block, type Inline } from './markdown.ts';
@@ -244,6 +244,8 @@ function build(face: ObjectCell): HTMLElement {
244
244
  node = buildRadio(face, m);
245
245
  } else if (style === 'upload') {
246
246
  node = buildUpload(face, m);
247
+ } else if (style === 'photo') {
248
+ node = buildPhoto(face, m);
247
249
  } else if (style === 'lookup') {
248
250
  node = buildLookup(face, m);
249
251
  } else if (style === 'rich') {
@@ -319,6 +321,20 @@ function build(face: ObjectCell): HTMLElement {
319
321
  }
320
322
  case 'progress': node = buildProgress(m); break;
321
323
  case 'image': node = el('img', 'tb-image'); break;
324
+ case 'video': case 'audio': {
325
+ const media = el(style, `tb-${style}`) as HTMLMediaElement;
326
+ media.controls = true;
327
+ media.preload = 'metadata';
328
+ if (media instanceof HTMLVideoElement) media.playsInline = true;
329
+ node = media;
330
+ break;
331
+ }
332
+ case 'backdrop': {
333
+ // decoration behind its container: screen readers skip it
334
+ node = el('div', 'tb-backdrop');
335
+ node.setAttribute('aria-hidden', 'true');
336
+ break;
337
+ }
322
338
  case 'canvas': node = buildCanvas(m); break;
323
339
  case 'chart': node = buildChart(m); break;
324
340
  case 'icon': node = el('span', 'tb-icon-face'); break;
@@ -389,6 +405,7 @@ function listen(m: Mounted, style: string): void {
389
405
  node.addEventListener('pointerleave', () => fireEvent(f, 'on-over', [['away?', TRUE]]));
390
406
  }
391
407
  if (handlerFor(f, 'on-dbl-click')) node.addEventListener('dblclick', () => fireEvent(f, 'on-dbl-click'));
408
+ if (handlerFor(f, 'on-end')) node.addEventListener('ended', () => fireEvent(f, 'on-end'));
392
409
  if (handlerFor(f, 'on-key')) {
393
410
  if (style === 'view') viewKeys = f;
394
411
  else node.addEventListener('keydown', ev => {
@@ -1352,24 +1369,41 @@ function buildUpload(face: ObjectCell, m: Mounted): HTMLElement {
1352
1369
  error.setAttribute('aria-live', 'polite');
1353
1370
  choose.setAttribute('aria-describedby', error.id);
1354
1371
  const list = el('ul', 'tb-upload-list');
1355
- // how far the files went up, while a fetch with progress: the upload sends them
1356
- label.id = `${id}-label`;
1357
- const track = el('div', 'tb-progress tb-upload-progress');
1358
- track.setAttribute('role', 'progressbar');
1359
- track.setAttribute('aria-valuemin', '0');
1360
- track.setAttribute('aria-valuemax', '100');
1361
- track.setAttribute('aria-labelledby', label.id);
1362
- track.hidden = true;
1363
- track.append(el('div', 'tb-progress-bar'));
1372
+ const track = sentBar(label, id);
1364
1373
  choose.addEventListener('click', () => picker.click());
1365
1374
  picker.addEventListener('change', () => {
1366
1375
  const files = [...(picker.files ?? [])];
1367
1376
  picker.value = '';
1368
1377
  if (files.length > 0) chooseFiles(face, files);
1369
1378
  });
1379
+ dropFiles(face, zone, choose);
1380
+ zone.append(choose, hint, picker);
1381
+ control.append(zone, list, track, error);
1382
+ wrap.append(label, control);
1383
+ m.label = label;
1384
+ m.error = error;
1385
+ m.button = choose;
1386
+ m.box = list;
1387
+ return wrap;
1388
+ }
1389
+
1390
+ /** The accept list of an upload: extensions ("pdf") and types ("image/*"); a photo takes images unless told. */
1391
+ function acceptList(face: ObjectCell): string[] {
1392
+ const a = get(face, 'accept');
1393
+ if (a.t !== K.Block && word(face, 'style') === 'photo') return ['image/*'];
1394
+ return a.t === K.Block ? a.s.data.slice(a.i).map(x => form(x).trim().toLowerCase()).filter(x => x !== '') : [];
1395
+ }
1396
+
1397
+ /** The picker's accept attribute for an accept list. */
1398
+ function pickerAccept(list: string[]): string {
1399
+ return list.map(x => (x.includes('/') || x.startsWith('.') ? x : `.${x}`)).join(',');
1400
+ }
1401
+
1402
+ /** Lets a zone take files dropped on it, unless its button is disabled. */
1403
+ function dropFiles(face: ObjectCell, zone: HTMLElement, button: HTMLButtonElement): void {
1370
1404
  const hasFiles = (ev: DragEvent): boolean => ev.dataTransfer?.types.includes('Files') === true;
1371
1405
  zone.addEventListener('dragover', ev => {
1372
- if (!hasFiles(ev) || choose.disabled) return;
1406
+ if (!hasFiles(ev) || button.disabled) return;
1373
1407
  ev.preventDefault();
1374
1408
  ev.dataTransfer!.dropEffect = 'copy';
1375
1409
  zone.classList.add('tb-drop-over');
@@ -1379,24 +1413,196 @@ function buildUpload(face: ObjectCell, m: Mounted): HTMLElement {
1379
1413
  });
1380
1414
  zone.addEventListener('drop', ev => {
1381
1415
  zone.classList.remove('tb-drop-over');
1382
- if (!hasFiles(ev) || choose.disabled) return;
1416
+ if (!hasFiles(ev) || button.disabled) return;
1383
1417
  ev.preventDefault();
1384
1418
  chooseFiles(face, [...ev.dataTransfer!.files]);
1385
1419
  });
1386
- zone.append(choose, hint, picker);
1387
- control.append(zone, list, track, error);
1420
+ }
1421
+
1422
+ /** The bar of how far the files went up, while a fetch with progress sends them. */
1423
+ function sentBar(label: HTMLElement, id: string): HTMLElement {
1424
+ label.id = `${id}-label`;
1425
+ const track = el('div', 'tb-progress tb-upload-progress');
1426
+ track.setAttribute('role', 'progressbar');
1427
+ track.setAttribute('aria-valuemin', '0');
1428
+ track.setAttribute('aria-valuemax', '100');
1429
+ track.setAttribute('aria-labelledby', label.id);
1430
+ track.hidden = true;
1431
+ track.append(el('div', 'tb-progress-bar'));
1432
+ return track;
1433
+ }
1434
+
1435
+ function syncSent(m: Mounted): void {
1436
+ const track = m.el.querySelector<HTMLElement>('.tb-upload-progress')!;
1437
+ const sent = get(m.face, 'sent');
1438
+ const part = fraction(sent) ?? 0;
1439
+ track.hidden = sent.t === K.None;
1440
+ track.setAttribute('aria-valuenow', String(Math.round(part * 100)));
1441
+ (track.firstChild as HTMLElement).style.width = `${part * 100}%`;
1442
+ }
1443
+
1444
+ // A photo: the image itself, a button to choose one and another to take it away. The
1445
+ // image chosen shows from the browser's memory; one kept elsewhere, from its address.
1446
+
1447
+ // A video or a sound: the browser's own player. Captions are one track per language;
1448
+ // the active language's is the one shown, and it follows set-language.
1449
+
1450
+ /** A video's captions as [language, address] pairs: a file alone is in the active language. */
1451
+ function captionTracks(f: ObjectCell): [string, string][] {
1452
+ const c = get(f, 'captions');
1453
+ if (c.t === K.File || c.t === K.Url || c.t === K.String) return [[activeLanguage(host.rt), form(c)]];
1454
+ if (c.t !== K.Block) return [];
1455
+ const d = c.s.data.slice(c.i);
1456
+ const out: [string, string][] = [];
1457
+ for (let i = 0; i + 1 < d.length; i += 2) out.push([form(d[i]!), form(d[i + 1]!)]);
1458
+ return out;
1459
+ }
1460
+
1461
+ function syncMedia(m: Mounted): void {
1462
+ const f = m.face;
1463
+ const media = m.el as HTMLMediaElement;
1464
+ const src = str(f, 'value') ?? '';
1465
+ if (media.getAttribute('src') !== src) media.src = src;
1466
+ const t = str(f, 'text');
1467
+ if (t !== null) media.setAttribute('aria-label', t);
1468
+ else media.removeAttribute('aria-label');
1469
+ media.loop = flag(f, 'looping');
1470
+ // browsers only let what plays on its own do so without sound
1471
+ const auto = flag(f, 'autoplay');
1472
+ media.muted = flag(f, 'silent') || auto;
1473
+ media.autoplay = auto;
1474
+ if (!(media instanceof HTMLVideoElement)) return;
1475
+ const poster = str(f, 'poster') ?? '';
1476
+ if (media.getAttribute('poster') !== poster) media.poster = poster;
1477
+ const lang = activeLanguage(host.rt);
1478
+ const tracks = captionTracks(f);
1479
+ const names = new Intl.DisplayNames([lang], { type: 'language' });
1480
+ const now = Array.from(media.querySelectorAll('track'));
1481
+ if (now.map(x => `${x.srclang} ${x.getAttribute('src')}`).join('|') !== tracks.map(([l, s]) => `${l} ${s}`).join('|')) {
1482
+ media.replaceChildren(...tracks.map(([l, s]) => {
1483
+ const track = el('track') as HTMLTrackElement;
1484
+ track.kind = 'captions';
1485
+ track.srclang = l;
1486
+ track.src = s;
1487
+ return track;
1488
+ }));
1489
+ }
1490
+ // the captions shown, if any, follow the active language
1491
+ const showing = Array.from(media.textTracks).some(x => x.mode === 'showing');
1492
+ const pick = tracks.findIndex(([l]) => l === lang);
1493
+ media.querySelectorAll('track').forEach((track, i) => {
1494
+ track.label = names.of(track.srclang) ?? track.srclang;
1495
+ track.default = i === Math.max(0, pick);
1496
+ if (showing) track.track.mode = i === Math.max(0, pick) ? 'showing' : 'disabled';
1497
+ });
1498
+ }
1499
+
1500
+ /** The address that shows each chosen file, made once and freed when the photo shows another. */
1501
+ const shownUrls = new WeakMap<HTMLElement, { blob: Blob; url: string }>();
1502
+
1503
+ function buildPhoto(face: ObjectCell, m: Mounted): HTMLElement {
1504
+ const id = `tb-${nextId++}`;
1505
+ const wrap = el('div', 'tb-field');
1506
+ const label = el('label', 'tb-label');
1507
+ label.htmlFor = id;
1508
+ const control = el('div', 'tb-control');
1509
+ const zone = el('div', 'tb-photo');
1510
+ // the frame opens the picker for a pointer; the keyboard has the button
1511
+ const frame = el('div', 'tb-photo-frame');
1512
+ frame.addEventListener('click', () => { if (!choose.disabled) picker.click(); });
1513
+ const picker = el('input');
1514
+ picker.type = 'file';
1515
+ picker.hidden = true;
1516
+ picker.tabIndex = -1;
1517
+ const actions = el('div', 'tb-photo-actions');
1518
+ const choose = el('button', 'tb-button');
1519
+ choose.type = 'button';
1520
+ choose.id = id;
1521
+ const svg = icon('upload');
1522
+ if (svg !== null) choose.append(svg);
1523
+ choose.append(el('span'));
1524
+ const remove = el('button', 'tb-button tb-photo-remove');
1525
+ remove.type = 'button';
1526
+ remove.addEventListener('click', () => {
1527
+ choose.focus();
1528
+ void fire('ui-remove-file', face, int(1));
1529
+ });
1530
+ const error = el('div', 'tb-error');
1531
+ error.id = `${id}-error`;
1532
+ error.setAttribute('aria-live', 'polite');
1533
+ choose.setAttribute('aria-describedby', error.id);
1534
+ choose.addEventListener('click', () => picker.click());
1535
+ picker.addEventListener('change', () => {
1536
+ const files = [...(picker.files ?? [])];
1537
+ picker.value = '';
1538
+ if (files.length > 0) chooseFiles(face, files);
1539
+ });
1540
+ dropFiles(face, zone, choose);
1541
+ actions.append(choose, remove);
1542
+ zone.append(frame, actions, picker);
1543
+ control.append(zone, sentBar(label, id), error);
1388
1544
  wrap.append(label, control);
1389
1545
  m.label = label;
1390
1546
  m.error = error;
1391
1547
  m.button = choose;
1392
- m.box = list;
1548
+ m.box = frame;
1393
1549
  return wrap;
1394
1550
  }
1395
1551
 
1396
- /** The accept list of an upload: extensions ("pdf") and types ("image/*"). */
1397
- function acceptList(face: ObjectCell): string[] {
1398
- const a = get(face, 'accept');
1399
- return a.t === K.Block ? a.s.data.slice(a.i).map(x => form(x).trim().toLowerCase()).filter(x => x !== '') : [];
1552
+ /** What a photo shows: the image chosen, or the address of one kept elsewhere; null for none. */
1553
+ function photoSrc(frame: HTMLElement, v: Cell): string | null {
1554
+ const kept = shownUrls.get(frame);
1555
+ const file = v.t === K.Object ? handleOf<Blob>(get(v as ObjectCell, 'file'), 'file') : null;
1556
+ if (file !== null && kept !== undefined && kept.blob === file) return kept.url;
1557
+ if (kept !== undefined) {
1558
+ URL.revokeObjectURL(kept.url);
1559
+ shownUrls.delete(frame);
1560
+ }
1561
+ if (file !== null) {
1562
+ const url = URL.createObjectURL(file);
1563
+ shownUrls.set(frame, { blob: file, url });
1564
+ return url;
1565
+ }
1566
+ return v.t === K.String || v.t === K.Url || v.t === K.File ? form(v) : null;
1567
+ }
1568
+
1569
+ function syncPhoto(m: Mounted, err: string | null): void {
1570
+ const f = m.face;
1571
+ const t = str(f, 'text') ?? '';
1572
+ m.label!.textContent = t;
1573
+ m.label!.classList.toggle('tb-required', required(f));
1574
+ const readonly = flag(f, 'readonly');
1575
+ const choose = m.button!;
1576
+ choose.disabled = flag(f, 'disabled') || readonly;
1577
+ choose.setAttribute('aria-invalid', err !== null ? 'true' : 'false');
1578
+ const frame = m.box!;
1579
+ frame.classList.toggle('tb-round', flag(f, 'round'));
1580
+ frame.classList.toggle('tb-photo-pick', !choose.disabled);
1581
+ const src = photoSrc(frame, get(f, 'value'));
1582
+ let img = frame.querySelector('img');
1583
+ if (src === null) {
1584
+ if (img !== null || frame.firstChild === null) {
1585
+ const svg = icon(flag(f, 'round') ? 'user' : 'image');
1586
+ frame.replaceChildren(...(svg !== null ? [svg] : []));
1587
+ }
1588
+ } else {
1589
+ if (img === null) {
1590
+ img = el('img') as HTMLImageElement;
1591
+ frame.replaceChildren(img);
1592
+ }
1593
+ if (img.getAttribute('src') !== src) img.src = src;
1594
+ img.alt = t;
1595
+ }
1596
+ (choose.lastChild as HTMLElement).textContent = own(src === null ? 'choose-photo' : 'change-photo');
1597
+ const remove = m.el.querySelector<HTMLButtonElement>('.tb-photo-remove')!;
1598
+ remove.textContent = own('remove-photo');
1599
+ remove.hidden = src === null || readonly;
1600
+ remove.disabled = choose.disabled;
1601
+ // read only, there is nothing to choose: only the photo is left
1602
+ (m.el.querySelector('.tb-photo-actions') as HTMLElement).hidden = readonly;
1603
+ (m.el.querySelector('input[type="file"]') as HTMLInputElement).accept = pickerAccept(acceptList(f));
1604
+ m.error!.textContent = err ?? '';
1605
+ syncSent(m);
1400
1606
  }
1401
1607
 
1402
1608
  function accepts(file: File, list: string[]): boolean {
@@ -1445,7 +1651,7 @@ function syncUpload(m: Mounted, err: string | null): void {
1445
1651
  (zone.querySelector('.tb-upload-hint') as HTMLElement).textContent = own(many ? 'drop-files' : 'drop-file');
1446
1652
  const picker = zone.querySelector('input') as HTMLInputElement;
1447
1653
  picker.multiple = many;
1448
- picker.accept = acceptList(f).map(x => (x.includes('/') || x.startsWith('.') ? x : `.${x}`)).join(',');
1654
+ picker.accept = pickerAccept(acceptList(f));
1449
1655
  m.error!.textContent = err ?? '';
1450
1656
  const v = get(f, 'value');
1451
1657
  const files = v.t === K.Block ? v.s.data.slice(v.i).filter(x => x.t === K.Object) as ObjectCell[] : [];
@@ -1490,11 +1696,7 @@ function syncUpload(m: Mounted, err: string | null): void {
1490
1696
  });
1491
1697
  m.box!.replaceChildren(...items);
1492
1698
  m.box!.hidden = items.length === 0;
1493
- const track = m.el.querySelector<HTMLElement>('.tb-upload-progress')!;
1494
- const sent = fraction(get(f, 'sent'));
1495
- track.hidden = get(f, 'sent').t === K.None;
1496
- track.setAttribute('aria-valuenow', String(Math.round((sent ?? 0) * 100)));
1497
- (track.firstChild as HTMLElement).style.width = `${(sent ?? 0) * 100}%`;
1699
+ syncSent(m);
1498
1700
  }
1499
1701
 
1500
1702
  // A lookup: a field where the user types and chooses among what on-search finds, as in
@@ -2342,7 +2544,8 @@ function syncRich(m: Mounted, err: string | null): void {
2342
2544
 
2343
2545
  // --- tables and lists ---------------------------------------------------------------
2344
2546
 
2345
- interface Column { sym: Sym; word: Cell; label: string }
2547
+ /** A column; `image` when it shows each row's picture, `round` when cut to a circle. */
2548
+ interface Column { sym: Sym; word: Cell; label: string; image: boolean; round: boolean }
2346
2549
 
2347
2550
  function columns(face: ObjectCell): Column[] {
2348
2551
  const c = get(face, 'columns');
@@ -2351,11 +2554,32 @@ function columns(face: ObjectCell): Column[] {
2351
2554
  const d = c.s.data;
2352
2555
  for (let i = c.i; i + 1 < d.length; i += 2) {
2353
2556
  const w = d[i]!;
2354
- if (w.t === K.Word) out.push({ sym: (w as WordCell).sym, word: w, label: translate(d[i + 1]!, host.rt) });
2557
+ if (w.t !== K.Word) continue;
2558
+ // a column of images keeps its label in a block with what it is: ["Photo" image round]
2559
+ const spec = d[i + 1]!;
2560
+ const parts = spec.t === K.Block ? (spec as BlockCell).s.data.slice((spec as BlockCell).i) : [spec];
2561
+ const has = (name: string): boolean => parts.some(p => p.t === K.Word && canon((p as WordCell).sym) === canon(intern(name)));
2562
+ out.push({ sym: (w as WordCell).sym, word: w, label: translate(parts[0] ?? NONE, host.rt), image: has('image'), round: has('round') });
2355
2563
  }
2356
2564
  return out;
2357
2565
  }
2358
2566
 
2567
+ /** The picture of a row in a column of images, from an address or a file; without one, an empty place for it. */
2568
+ function thumb(box: Element, v: Cell, round: boolean): void {
2569
+ const src = v.t === K.String || v.t === K.Url || v.t === K.File ? form(v) : '';
2570
+ const old = box.querySelector<HTMLElement>(':scope > .tb-thumb');
2571
+ let pic = old;
2572
+ if (src === '' ? pic?.tagName !== 'SPAN' : pic?.tagName !== 'IMG') {
2573
+ // decoration: the row's text says who or what it is
2574
+ pic = el(src === '' ? 'span' : 'img', 'tb-thumb');
2575
+ if (src !== '') (pic as HTMLImageElement).alt = '';
2576
+ if (old !== null) old.replaceWith(pic);
2577
+ else box.prepend(pic);
2578
+ }
2579
+ if (src !== '' && pic!.getAttribute('src') !== src) (pic as HTMLImageElement).src = src;
2580
+ pic!.classList.toggle('tb-round', round);
2581
+ }
2582
+
2359
2583
  function sameRow(a: Cell, b: Cell): boolean {
2360
2584
  if (a.t === K.Map && b.t === K.Map) return a.s === b.s;
2361
2585
  if (a.t === K.Object && b.t === K.Object) return a.ctx === b.ctx;
@@ -2578,7 +2802,9 @@ function newTableRow(cols: Column[]): HTMLElement {
2578
2802
 
2579
2803
  function newListItem(): HTMLElement {
2580
2804
  const li = el('li');
2581
- li.append(el('span', 'tb-list-primary'), el('span', 'tb-list-secondary'));
2805
+ const texts = el('span', 'tb-list-texts');
2806
+ texts.append(el('span', 'tb-list-primary'), el('span', 'tb-list-secondary'));
2807
+ li.append(texts);
2582
2808
  return li;
2583
2809
  }
2584
2810
 
@@ -2590,15 +2816,23 @@ function fillRow(item: HTMLElement, row: Cell, cols: Column[], table: boolean):
2590
2816
  cols.forEach((c, i) => {
2591
2817
  const v = rowField(row, c.sym);
2592
2818
  const td = item.children[i]!;
2819
+ if (c.image) {
2820
+ td.classList.add('tb-thumb-cell');
2821
+ return thumb(td, v, c.round);
2822
+ }
2593
2823
  set(td, cellText(v));
2594
2824
  td.classList.toggle('tb-num', NUMERIC.has(v.t));
2595
2825
  });
2596
2826
  return;
2597
2827
  }
2598
- // a list shows its first column, then the others on a quieter line
2599
- const [first, ...rest] = cols.map(c => cellText(rowField(row, c.sym)));
2600
- set(item.children[0]!, first ?? '');
2601
- set(item.children[1]!, rest.filter(Boolean).join(' · '));
2828
+ // a list shows its picture first, then its first column, and the others on a quieter line
2829
+ const pic = cols.find(c => c.image);
2830
+ if (pic !== undefined) thumb(item, rowField(row, pic.sym), pic.round);
2831
+ else item.querySelector(':scope > .tb-thumb')?.remove();
2832
+ const [first, ...rest] = cols.filter(c => !c.image).map(c => cellText(rowField(row, c.sym)));
2833
+ const texts = item.querySelector(':scope > .tb-list-texts')!;
2834
+ set(texts.children[0]!, first ?? '');
2835
+ set(texts.children[1]!, rest.filter(Boolean).join(' · '));
2602
2836
  }
2603
2837
 
2604
2838
  function handlerFor(face: ObjectCell, name: string): boolean {
@@ -3206,6 +3440,7 @@ function sync(m: Mounted): void {
3206
3440
 
3207
3441
  if (style === 'radio') return syncRadio(m, err);
3208
3442
  if (style === 'upload') return syncUpload(m, err);
3443
+ if (style === 'photo') return syncPhoto(m, err);
3209
3444
  if (style === 'lookup') return syncLookup(m, err);
3210
3445
  if (style === 'rich') return syncRich(m, err);
3211
3446
  if (m.input !== null) {
@@ -3281,11 +3516,17 @@ function sync(m: Mounted): void {
3281
3516
  case 'canvas': syncCanvas(m); return;
3282
3517
  case 'chart': syncChart(m); return;
3283
3518
  case 'icon': syncIcon(m); return;
3519
+ case 'video': case 'audio': syncMedia(m); return;
3284
3520
  case 'image': {
3285
3521
  const img = m.el as HTMLImageElement;
3286
3522
  const src = str(f, 'value') ?? '';
3287
3523
  if (img.getAttribute('src') !== src) img.src = src;
3288
3524
  img.alt = t ?? '';
3525
+ img.classList.toggle('tb-round', flag(f, 'round'));
3526
+ // a size, as in Red: the image fills it, cut to fit
3527
+ const ext = get(f, 'extent');
3528
+ img.style.width = ext.t === K.Pair ? `${ext.x}px` : '';
3529
+ img.style.height = ext.t === K.Pair ? `${ext.y}px` : '';
3289
3530
  return;
3290
3531
  }
3291
3532
  case 'table': case 'list': syncRows(m); return;
@@ -3297,7 +3538,7 @@ function sync(m: Mounted): void {
3297
3538
  else m.el.removeAttribute('aria-label');
3298
3539
  break;
3299
3540
  }
3300
- case 'desktop': {
3541
+ case 'desktop': case 'backdrop': {
3301
3542
  const src = str(f, 'value');
3302
3543
  m.el.style.backgroundImage = src !== null ? `url(${JSON.stringify(src)})` : '';
3303
3544
  break;
package/src/host/index.ts CHANGED
@@ -36,14 +36,14 @@ fetch: func [
36
36
  ]
37
37
 
38
38
  show-progress: func [
39
- "Shows how far a request went on a face: a progress bar's value, or the bar of an upload."
39
+ "Shows how far a request went on a face: a progress bar's value, or the bar of an upload or a photo."
40
40
  face [object!]
41
41
  done [integer! none!] "Bytes done; none once the request is over"
42
42
  total [integer! none!] "Bytes in all, when known"
43
43
  /local part
44
44
  ][
45
45
  part: either all [done total total > 0] [done / to decimal! total] [none]
46
- either 'upload = select face 'style [face/sent: part] [if done [face/value: part]]
46
+ either find [upload photo] select face 'style [face/sent: part] [if done [face/value: part]]
47
47
  ]
48
48
 
49
49
  save: func [
@@ -42,10 +42,17 @@ export function setLoader(rt: Runtime, loader: Loader): void {
42
42
  }
43
43
 
44
44
  export function defaultLoader(): Loader {
45
- const g = globalThis as { location?: { href: string } };
45
+ type ReadFile = (path: URL, encoding: 'utf8') => Promise<string>;
46
+ const g = globalThis as {
47
+ location?: { href: string };
48
+ process?: { getBuiltinModule?: (name: string) => { readFile: ReadFile } | undefined };
49
+ };
46
50
  return {
47
51
  base: g.location?.href ?? 'file:///',
48
52
  read: async href => {
53
+ // in Node, fetch opens no file: URL; asking Node for its module keeps a browser bundle free of it
54
+ const fs = href.startsWith('file:') ? g.process?.getBuiltinModule?.('node:fs/promises') : undefined;
55
+ if (fs !== undefined) return fs.readFile(new URL(href), 'utf8');
49
56
  const res = await fetch(href);
50
57
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
51
58
  return res.text();
package/src/host/theme.ts CHANGED
@@ -213,6 +213,34 @@ export function contrast(x: Oklch, y: Oklch): number {
213
213
  return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
214
214
  }
215
215
 
216
+ const encode = (x: number): number => (x <= 0.0031308 ? 12.92 * x : 1.055 * x ** (1 / 2.4) - 0.055);
217
+ const decode = (x: number): number => (x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4);
218
+
219
+ /** Luminance of `c` at `alpha` over a pixel of gray `px` (0 black, 1 white), blended as browsers do. */
220
+ export function luminanceOver(c: Oklch, alpha: number, px: number): number {
221
+ const [r, g, b] = toLinearRgb(c).map(x => decode(alpha * encode(x) + (1 - alpha) * px)) as [number, number, number];
222
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
223
+ }
224
+
225
+ /** The worst contrast of `fg` on the background at `alpha` over any image: a black or a white one. */
226
+ export function contrastOverImage(fg: Oklch, bg: Oklch, alpha: number): number {
227
+ const t = luminance(fg);
228
+ return Math.min(...[0, 1].map(px => {
229
+ const b = luminanceOver(bg, alpha, px);
230
+ return (Math.max(t, b) + 0.05) / (Math.min(t, b) + 0.05);
231
+ }));
232
+ }
233
+
234
+ /**
235
+ * The alpha of the veil over a backdrop: the least that keeps text AA on any image. Muted text would
236
+ * need a veil almost opaque, so on a backdrop it takes the color of the text.
237
+ */
238
+ export function scrimAlpha(p: Record<Role, Oklch>): number {
239
+ let alpha = 0.5;
240
+ while (alpha < 1 && contrastOverImage(p.text, p.bg, alpha) < 4.6) alpha = Math.round((alpha + 0.01) * 100) / 100;
241
+ return alpha;
242
+ }
243
+
216
244
  const css = (c: Oklch, alpha?: number): string =>
217
245
  `oklch(${(c.l * 100).toFixed(1)}% ${c.c.toFixed(3)} ${c.h.toFixed(1)}${alpha === undefined ? '' : ` / ${alpha}`})`;
218
246
 
@@ -245,6 +273,7 @@ function colorVars(t: ThemeParams, dark: boolean): string {
245
273
  lines.push(` --tb-danger-soft: ${css(p.danger, dark ? 0.16 : 0.08)};`);
246
274
  lines.push(` --tb-primary-soft: ${css(p.primary, dark ? 0.16 : 0.08)};`);
247
275
  lines.push(` --tb-overlay: ${css(n0(p), dark ? 0.6 : 0.4)};`);
276
+ lines.push(` --tb-scrim: ${css(p.bg, scrimAlpha(p))};`);
248
277
  const shadow = dark ? '0 0 0 1px oklch(100% 0 0 / 0.06)' : `0 1px 2px ${css(n0(p), 0.06)}`;
249
278
  const k = t.depth === 'lifted' ? 2 : 1;
250
279
  // a flat look has no shadows, but what floats over the page still needs one
@@ -363,6 +392,12 @@ body {
363
392
  background-size: cover;
364
393
  background-position: center;
365
394
  }
395
+ /* an image behind what holds it, under a veil that keeps text on it AA; muted text needs the text's color there */
396
+ :has(> .tb-backdrop) { position: relative; isolation: isolate; --tb-muted: var(--tb-text); }
397
+ .tb-backdrop { position: absolute; inset: 0; z-index: -1; border-radius: inherit; background-size: cover; background-position: center; pointer-events: none; }
398
+ .tb-backdrop::after { content: ""; position: absolute; inset: 0; border-radius: inherit; background: var(--tb-scrim); }
399
+ /* the backdrop of the view or of a screen is the window's */
400
+ :is(.tb-view, .tb-screen) > .tb-backdrop { position: fixed; border-radius: 0; }
366
401
  .tb-tile {
367
402
  font: inherit;
368
403
  font-size: 13px;
@@ -692,7 +727,15 @@ button.tb-crumb:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--tb-fo
692
727
  .tb-scroll.tb-large { max-height: 640px; }
693
728
  .tb-scroll:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--tb-focus-ring); }
694
729
 
695
- .tb-image { display: block; max-width: 100%; height: auto; border-radius: var(--tb-radius); }
730
+ .tb-image { display: block; max-width: 100%; height: auto; border-radius: var(--tb-radius); object-fit: cover; }
731
+ .tb-image.tb-round { aspect-ratio: 1; border-radius: 50%; }
732
+ .tb-video { display: block; width: 100%; max-width: 100%; height: auto; aspect-ratio: auto 16 / 9; border-radius: var(--tb-radius); }
733
+ .tb-audio { display: block; width: 100%; max-width: 480px; }
734
+ :is(.tb-video, .tb-audio).tb-small { width: 320px; }
735
+ :is(.tb-video, .tb-audio).tb-medium { width: 480px; }
736
+ .tb-video.tb-large { width: 720px; }
737
+ :is(.tb-video, .tb-audio).tb-fill { max-width: none; }
738
+ :is(.tb-video, .tb-audio):focus-visible { outline: none; box-shadow: 0 0 0 3px var(--tb-focus-ring); }
696
739
  .tb-canvas { display: block; max-width: 100%; }
697
740
  .tb-canvas > svg { display: block; max-width: 100%; height: auto; overflow: visible; }
698
741
  .tb-canvas.tb-fill > svg { width: 100%; }
@@ -715,6 +758,16 @@ button.tb-crumb:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--tb-fo
715
758
  .tb-progress-label[hidden] { display: none; }
716
759
  .tb-progress { height: 8px; border-radius: 4px; background: var(--tb-border); overflow: hidden; }
717
760
  .tb-upload-progress { height: 6px; }
761
+ .tb-photo { display: flex; flex-wrap: wrap; align-items: center; gap: var(--tb-space-4); margin: calc(-1 * var(--tb-space-2)); padding: var(--tb-space-2); border: 1px dashed transparent; border-radius: var(--tb-radius-lg); transition: border-color var(--tb-fast) var(--tb-ease), background var(--tb-fast) var(--tb-ease); }
762
+ .tb-photo.tb-drop-over { border-color: var(--tb-primary); background: var(--tb-primary-soft); }
763
+ .tb-photo-frame { flex: none; width: 96px; height: 96px; display: grid; place-items: center; overflow: hidden; border: var(--tb-line) solid var(--tb-border); border-radius: var(--tb-radius-lg); background: var(--tb-bg); color: var(--tb-muted); }
764
+ .tb-photo-frame.tb-photo-pick { cursor: pointer; }
765
+ .tb-photo-frame.tb-round { border-radius: 50%; }
766
+ .tb-photo-frame > img { display: block; width: 100%; height: 100%; object-fit: cover; }
767
+ .tb-photo-frame > .tb-icon { width: 32px; height: 32px; }
768
+ .tb-invalid .tb-photo-frame { border-color: var(--tb-danger); }
769
+ .tb-photo-actions { display: flex; flex-wrap: wrap; gap: var(--tb-space-2); }
770
+ .tb-photo-actions[hidden], .tb-photo-remove[hidden] { display: none; }
718
771
  .tb-upload-progress[hidden] { display: none; }
719
772
  .tb-progress-bar { height: 100%; width: 0; border-radius: inherit; background: var(--tb-primary); transition: width 300ms var(--tb-ease); }
720
773
  .tb-success .tb-progress-bar { background: var(--tb-success); }
@@ -802,15 +855,19 @@ button.tb-crumb:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--tb-fo
802
855
  .tb-list:empty { display: none; }
803
856
  .tb-list > li {
804
857
  display: flex;
805
- flex-direction: column;
806
- justify-content: center;
807
- gap: 2px;
858
+ align-items: center;
859
+ gap: var(--tb-space-3);
808
860
  min-height: var(--tb-control);
809
861
  padding: var(--tb-space-3) var(--tb-space-4);
810
862
  border-bottom: var(--tb-line) solid var(--tb-border);
811
863
  }
812
864
  .tb-list > li:last-child { border-bottom: 0; }
865
+ .tb-list-texts { display: flex; flex-direction: column; justify-content: center; gap: 2px; min-width: 0; }
813
866
  .tb-list-primary { font-weight: 550; }
867
+ /* the picture of a row, in a column of images or at the start of a list's item */
868
+ .tb-thumb { display: block; flex: none; width: 40px; height: 40px; object-fit: cover; border-radius: var(--tb-radius); background: var(--tb-bg); }
869
+ .tb-thumb.tb-round { border-radius: 50%; }
870
+ .tb-table td.tb-thumb-cell { width: 1%; padding-top: var(--tb-space-1); padding-bottom: var(--tb-space-1); }
814
871
  .tb-list-secondary { color: var(--tb-muted); font-size: 13px; }
815
872
  .tb-list-secondary:empty { display: none; }
816
873
 
@@ -1121,6 +1178,8 @@ button.tb-crumb:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--tb-fo
1121
1178
  text-align: right;
1122
1179
  }
1123
1180
  .tb-table td::before { content: attr(data-label); color: var(--tb-muted); font-weight: 500; text-align: left; }
1181
+ .tb-table td.tb-thumb-cell { width: auto; justify-content: flex-start; }
1182
+ .tb-table td.tb-thumb-cell::before { content: none; }
1124
1183
  .tb-table tr.tb-group-head { padding: var(--tb-space-3) 0 0; background: none; border: 0; box-shadow: none; }
1125
1184
  .tb-table .tb-group-head > th, .tb-table .tb-group-head.tb-level-2 > th { display: block; padding: 0; background: none; border: 0; }
1126
1185
  .tb-table .tb-level-2 > :first-child { padding-left: 0; }
package/src/ui/i18n.trb CHANGED
@@ -22,6 +22,7 @@ i18n: make object! [
22
22
  amount #(en: "Enter an amount" es: "Introduce un importe")
23
23
  date #(en: "Enter a valid date" es: "Introduce una fecha válida")
24
24
  accept #(en: "Only #1 files" es: "Solo archivos #1")
25
+ images #(en: "Only images" es: "Solo imágenes")
25
26
  min #(en: "Must be at least #1" es: "Debe ser como mínimo #1")
26
27
  max #(en: "Must be at most #1" es: "Debe ser como máximo #1")
27
28
  length #(en: "Must be between #1 and #2 characters" es: "Debe tener entre #1 y #2 caracteres")
@@ -53,6 +54,9 @@ i18n: make object! [
53
54
  drop-file #(en: "or drop it here" es: "o suéltalo aquí")
54
55
  drop-files #(en: "or drop them here" es: "o suéltalos aquí")
55
56
  remove-file #(en: "Remove #1" es: "Quitar #1")
57
+ choose-photo #(en: "Choose a photo" es: "Elegir una foto")
58
+ change-photo #(en: "Change" es: "Cambiar")
59
+ remove-photo #(en: "Remove" es: "Quitar")
56
60
  add-row #(en: "Add row" es: "Añadir fila")
57
61
  remove-row #(en: "Remove row" es: "Quitar fila")
58
62
  bold #(en: "Bold" es: "Negrita")