trebol 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 (256) hide show
  1. package/README.md +2 -0
  2. package/dist/cli.js +10040 -7519
  3. package/dist/lib/core/boot.js +10 -7
  4. package/dist/lib/core/eval.js +2 -2
  5. package/dist/lib/core/machine/checks.d.ts +1 -0
  6. package/dist/lib/core/machine/checks.js +250 -0
  7. package/dist/lib/core/machine/events.d.ts +1 -0
  8. package/dist/lib/core/machine/events.js +365 -0
  9. package/dist/lib/core/machine/running.d.ts +1 -0
  10. package/dist/lib/core/machine/running.js +180 -0
  11. package/dist/lib/core/machine/shared.d.ts +1 -0
  12. package/dist/lib/core/machine/shared.js +123 -0
  13. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  14. package/dist/lib/core/machine/snapshot.js +244 -0
  15. package/dist/lib/core/machine/spec.d.ts +1 -0
  16. package/dist/lib/core/machine/spec.js +274 -0
  17. package/dist/lib/core/machine/words.d.ts +1 -0
  18. package/dist/lib/core/machine/words.js +98 -0
  19. package/dist/lib/core/machine.d.ts +1 -0
  20. package/dist/lib/core/machine.js +17 -0
  21. package/dist/lib/core/mezz.js +31 -0
  22. package/dist/lib/core/natives/control.js +1 -1
  23. package/dist/lib/core/natives/data.js +6 -1
  24. package/dist/lib/core/natives/series.js +49 -9
  25. package/dist/lib/core/natives/text.js +0 -1
  26. package/dist/lib/host/browser/calendar.d.ts +34 -0
  27. package/dist/lib/host/browser/calendar.js +268 -0
  28. package/dist/lib/host/browser/count.d.ts +15 -0
  29. package/dist/lib/host/browser/count.js +48 -0
  30. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  31. package/dist/lib/host/browser/dialogs.js +443 -0
  32. package/dist/lib/host/browser/drag.d.ts +70 -0
  33. package/dist/lib/host/browser/drag.js +268 -0
  34. package/dist/lib/host/browser/drawings.d.ts +30 -0
  35. package/dist/lib/host/browser/drawings.js +176 -0
  36. package/dist/lib/host/browser/events.d.ts +16 -0
  37. package/dist/lib/host/browser/events.js +125 -0
  38. package/dist/lib/host/browser/faces.d.ts +35 -0
  39. package/dist/lib/host/browser/faces.js +131 -0
  40. package/dist/lib/host/browser/fields.d.ts +16 -0
  41. package/dist/lib/host/browser/fields.js +322 -0
  42. package/dist/lib/host/browser/keys.d.ts +29 -0
  43. package/dist/lib/host/browser/keys.js +151 -0
  44. package/dist/lib/host/browser/lookup.d.ts +25 -0
  45. package/dist/lib/host/browser/lookup.js +275 -0
  46. package/dist/lib/host/browser/menus.d.ts +20 -0
  47. package/dist/lib/host/browser/menus.js +149 -0
  48. package/dist/lib/host/browser/mount.d.ts +92 -0
  49. package/dist/lib/host/browser/mount.js +448 -0
  50. package/dist/lib/host/browser/orbits.d.ts +90 -0
  51. package/dist/lib/host/browser/orbits.js +379 -0
  52. package/dist/lib/host/browser/page.d.ts +13 -0
  53. package/dist/lib/host/browser/page.js +35 -0
  54. package/dist/lib/host/browser/richtext.d.ts +19 -0
  55. package/dist/lib/host/browser/richtext.js +379 -0
  56. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  57. package/dist/lib/host/browser/row-faces.js +74 -0
  58. package/dist/lib/host/browser/screens.d.ts +37 -0
  59. package/dist/lib/host/browser/screens.js +140 -0
  60. package/dist/lib/host/browser/swipe.d.ts +31 -0
  61. package/dist/lib/host/browser/swipe.js +178 -0
  62. package/dist/lib/host/browser/sync.d.ts +24 -0
  63. package/dist/lib/host/browser/sync.js +450 -0
  64. package/dist/lib/host/browser/tables.d.ts +65 -0
  65. package/dist/lib/host/browser/tables.js +505 -0
  66. package/dist/lib/host/browser/times.d.ts +14 -0
  67. package/dist/lib/host/browser/times.js +114 -0
  68. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  69. package/dist/lib/host/browser/toolbars.js +129 -0
  70. package/dist/lib/host/browser/trees.d.ts +20 -0
  71. package/dist/lib/host/browser/trees.js +235 -0
  72. package/dist/lib/host/browser/uploads.d.ts +29 -0
  73. package/dist/lib/host/browser/uploads.js +370 -0
  74. package/dist/lib/host/browser/widget.d.ts +13 -0
  75. package/dist/lib/host/browser/widget.js +105 -0
  76. package/dist/lib/host/browser.d.ts +25 -0
  77. package/dist/lib/host/browser.js +84 -4812
  78. package/dist/lib/host/devtools/console.d.ts +9 -0
  79. package/dist/lib/host/devtools/console.js +104 -0
  80. package/dist/lib/host/devtools/faces.d.ts +25 -0
  81. package/dist/lib/host/devtools/faces.js +169 -0
  82. package/dist/lib/host/devtools/log.d.ts +3 -0
  83. package/dist/lib/host/devtools/log.js +32 -0
  84. package/dist/lib/host/devtools/machines.d.ts +34 -0
  85. package/dist/lib/host/devtools/machines.js +212 -0
  86. package/dist/lib/host/devtools/read.d.ts +13 -0
  87. package/dist/lib/host/devtools/read.js +52 -0
  88. package/dist/lib/host/devtools/state.d.ts +90 -0
  89. package/dist/lib/host/devtools/state.js +123 -0
  90. package/dist/lib/host/devtools.d.ts +15 -19
  91. package/dist/lib/host/devtools.js +39 -427
  92. package/dist/lib/host/fonts.js +4 -3
  93. package/dist/lib/host/icons.js +4 -0
  94. package/dist/lib/host/index.d.ts +3 -1
  95. package/dist/lib/host/index.js +107 -4
  96. package/dist/lib/host/locale.d.ts +18 -0
  97. package/dist/lib/host/locale.js +75 -5
  98. package/dist/lib/host/markdown.d.ts +4 -0
  99. package/dist/lib/host/markdown.js +8 -0
  100. package/dist/lib/host/modules.js +118 -19
  101. package/dist/lib/host/motion.d.ts +32 -0
  102. package/dist/lib/host/motion.js +190 -0
  103. package/dist/lib/host/net.js +5 -2
  104. package/dist/lib/host/palette.d.ts +17 -0
  105. package/dist/lib/host/palette.js +131 -0
  106. package/dist/lib/host/scheduler.d.ts +2 -0
  107. package/dist/lib/host/scheduler.js +4 -0
  108. package/dist/lib/host/share.d.ts +31 -0
  109. package/dist/lib/host/share.js +134 -0
  110. package/dist/lib/host/theme/classic.d.ts +1 -0
  111. package/dist/lib/host/theme/classic.js +120 -0
  112. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  113. package/dist/lib/host/theme/cupertino.js +65 -0
  114. package/dist/lib/host/theme/data.d.ts +1 -0
  115. package/dist/lib/host/theme/data.js +109 -0
  116. package/dist/lib/host/theme/devtools.d.ts +1 -0
  117. package/dist/lib/host/theme/devtools.js +166 -0
  118. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  119. package/dist/lib/host/theme/dialogs.js +63 -0
  120. package/dist/lib/host/theme/fluent.d.ts +1 -0
  121. package/dist/lib/host/theme/fluent.js +64 -0
  122. package/dist/lib/host/theme/forms.d.ts +1 -0
  123. package/dist/lib/host/theme/forms.js +239 -0
  124. package/dist/lib/host/theme/fusion.d.ts +1 -0
  125. package/dist/lib/host/theme/fusion.js +66 -0
  126. package/dist/lib/host/theme/layout.d.ts +1 -0
  127. package/dist/lib/host/theme/layout.js +166 -0
  128. package/dist/lib/host/theme/media.d.ts +1 -0
  129. package/dist/lib/host/theme/media.js +106 -0
  130. package/dist/lib/host/theme/navigation.d.ts +1 -0
  131. package/dist/lib/host/theme/navigation.js +36 -0
  132. package/dist/lib/host/theme/panes.d.ts +1 -0
  133. package/dist/lib/host/theme/panes.js +99 -0
  134. package/dist/lib/host/theme/popups.d.ts +1 -0
  135. package/dist/lib/host/theme/popups.js +149 -0
  136. package/dist/lib/host/theme/responsive.d.ts +1 -0
  137. package/dist/lib/host/theme/responsive.js +103 -0
  138. package/dist/lib/host/theme/toasts.d.ts +1 -0
  139. package/dist/lib/host/theme/toasts.js +62 -0
  140. package/dist/lib/host/theme.js +19 -1384
  141. package/dist/lib/ui/dialect/events.trb +276 -0
  142. package/dist/lib/ui/dialect/faces.trb +135 -0
  143. package/dist/lib/ui/dialect/layout.trb +283 -0
  144. package/dist/lib/ui/dialect/palette.trb +87 -0
  145. package/dist/lib/ui/dialect/problems.trb +174 -0
  146. package/dist/lib/ui/dialect/reactions.trb +198 -0
  147. package/dist/lib/ui/dialect/rows.trb +208 -0
  148. package/dist/lib/ui/dialect/screens.trb +293 -0
  149. package/dist/lib/ui/dialect/steps.trb +203 -0
  150. package/dist/lib/ui/dialect/validation.trb +137 -0
  151. package/dist/lib/ui/dialect/view.trb +110 -0
  152. package/dist/lib/ui/dialect/walking.trb +98 -0
  153. package/dist/lib/ui/dialect/words.trb +70 -0
  154. package/dist/lib/ui/i18n.trb +17 -0
  155. package/dist/lib/ui/test.trb +3 -556
  156. package/dist/lib/ui/testing/actions.trb +421 -0
  157. package/dist/lib/ui/testing/faces.trb +148 -0
  158. package/dist/lib/ui/testing/server.trb +73 -0
  159. package/dist/lib/ui/ui.trb +25 -1675
  160. package/dist/trebol.js +4463 -1891
  161. package/package.json +1 -1
  162. package/src/core/boot.ts +9 -6
  163. package/src/core/eval.ts +2 -2
  164. package/src/core/machine/checks.ts +251 -0
  165. package/src/core/machine/events.ts +366 -0
  166. package/src/core/machine/running.ts +181 -0
  167. package/src/core/machine/shared.ts +124 -0
  168. package/src/core/machine/snapshot.ts +245 -0
  169. package/src/core/machine/spec.ts +275 -0
  170. package/src/core/machine/words.ts +99 -0
  171. package/src/core/machine.ts +20 -0
  172. package/src/core/mezz.ts +31 -0
  173. package/src/core/natives/control.ts +1 -1
  174. package/src/core/natives/data.ts +6 -1
  175. package/src/core/natives/parse.ts +1 -1
  176. package/src/core/natives/series.ts +47 -11
  177. package/src/core/natives/text.ts +0 -1
  178. package/src/host/browser/calendar.ts +264 -0
  179. package/src/host/browser/count.ts +48 -0
  180. package/src/host/browser/dialogs.ts +439 -0
  181. package/src/host/browser/drag.ts +278 -0
  182. package/src/host/browser/drawings.ts +178 -0
  183. package/src/host/browser/events.ts +117 -0
  184. package/src/host/browser/faces.ts +143 -0
  185. package/src/host/browser/fields.ts +315 -0
  186. package/src/host/browser/keys.ts +145 -0
  187. package/src/host/browser/lookup.ts +262 -0
  188. package/src/host/browser/menus.ts +146 -0
  189. package/src/host/browser/mount.ts +420 -0
  190. package/src/host/browser/orbits.ts +418 -0
  191. package/src/host/browser/page.ts +43 -0
  192. package/src/host/browser/richtext.ts +346 -0
  193. package/src/host/browser/row-faces.ts +75 -0
  194. package/src/host/browser/screens.ts +142 -0
  195. package/src/host/browser/swipe.ts +168 -0
  196. package/src/host/browser/sync.ts +354 -0
  197. package/src/host/browser/tables.ts +461 -0
  198. package/src/host/browser/times.ts +115 -0
  199. package/src/host/browser/toolbars.ts +126 -0
  200. package/src/host/browser/trees.ts +205 -0
  201. package/src/host/browser/uploads.ts +367 -0
  202. package/src/host/browser/widget.ts +103 -0
  203. package/src/host/browser.ts +82 -4612
  204. package/src/host/devtools/console.ts +102 -0
  205. package/src/host/devtools/faces.ts +160 -0
  206. package/src/host/devtools/log.ts +34 -0
  207. package/src/host/devtools/machines.ts +208 -0
  208. package/src/host/devtools/read.ts +58 -0
  209. package/src/host/devtools/state.ts +148 -0
  210. package/src/host/devtools.ts +39 -442
  211. package/src/host/fonts.ts +4 -3
  212. package/src/host/icons.ts +4 -0
  213. package/src/host/index.ts +107 -4
  214. package/src/host/locale.ts +71 -5
  215. package/src/host/markdown.ts +9 -0
  216. package/src/host/modules.ts +117 -21
  217. package/src/host/motion.ts +204 -0
  218. package/src/host/net.ts +4 -2
  219. package/src/host/palette.ts +139 -0
  220. package/src/host/scheduler.ts +5 -0
  221. package/src/host/share.ts +168 -0
  222. package/src/host/theme/classic.ts +121 -0
  223. package/src/host/theme/cupertino.ts +66 -0
  224. package/src/host/theme/data.ts +110 -0
  225. package/src/host/theme/devtools.ts +167 -0
  226. package/src/host/theme/dialogs.ts +64 -0
  227. package/src/host/theme/fluent.ts +65 -0
  228. package/src/host/theme/forms.ts +240 -0
  229. package/src/host/theme/fusion.ts +67 -0
  230. package/src/host/theme/layout.ts +167 -0
  231. package/src/host/theme/media.ts +107 -0
  232. package/src/host/theme/navigation.ts +37 -0
  233. package/src/host/theme/panes.ts +100 -0
  234. package/src/host/theme/popups.ts +150 -0
  235. package/src/host/theme/responsive.ts +104 -0
  236. package/src/host/theme/toasts.ts +63 -0
  237. package/src/host/theme.ts +19 -1384
  238. package/src/ui/dialect/events.trb +276 -0
  239. package/src/ui/dialect/faces.trb +135 -0
  240. package/src/ui/dialect/layout.trb +283 -0
  241. package/src/ui/dialect/palette.trb +87 -0
  242. package/src/ui/dialect/problems.trb +174 -0
  243. package/src/ui/dialect/reactions.trb +198 -0
  244. package/src/ui/dialect/rows.trb +208 -0
  245. package/src/ui/dialect/screens.trb +293 -0
  246. package/src/ui/dialect/steps.trb +203 -0
  247. package/src/ui/dialect/validation.trb +137 -0
  248. package/src/ui/dialect/view.trb +110 -0
  249. package/src/ui/dialect/walking.trb +98 -0
  250. package/src/ui/dialect/words.trb +70 -0
  251. package/src/ui/i18n.trb +17 -0
  252. package/src/ui/test.trb +3 -556
  253. package/src/ui/testing/actions.trb +421 -0
  254. package/src/ui/testing/faces.trb +148 -0
  255. package/src/ui/testing/server.trb +73 -0
  256. package/src/ui/ui.trb +25 -1675
@@ -4,14 +4,16 @@ import "./natives.js";
4
4
  import "./net.js";
5
5
  import "./page.js";
6
6
  import "./compress.js";
7
+ import "./share.js";
7
8
  import "./locale.js";
8
9
  import { setLoader, defaultLoader } from "./modules.js";
9
- import { K, makeRuntime, load, bindSeries, doSource, scriptStart, errorMessage, form, mold, LIBRARY_FILES, } from "../core/index.js";
10
+ import { makeRuntime, load, bindSeries, runSync, scriptStart, errorMessage, form, LIBRARY_FILES, } from "../core/index.js";
10
11
  import { makeScheduler, spawn, taskResult } from "./scheduler.js";
11
12
  export { output } from "./natives.js";
12
13
  export { fileHandle } from "./net.js";
14
+ export { setShareEnv } from "./share.js";
13
15
  export { page } from "./page.js";
14
- export { mapAt, formatLocal, activeLanguage, shown, translate, uiText, editLocal, readLocal, readDate, weekStart, fileSize, LOCAL_KINDS } from "./locale.js";
16
+ export { mapAt, formatLocal, activeLanguage, shown, translate, uiText, editLocal, readLocal, readDate, readRange, readTime, weekStart, fileSize, LOCAL_KINDS, amountOf, countStep } from "./locale.js";
15
17
  export * from "./scheduler.js";
16
18
  /** Host mezzanines: the Trebol side of the host natives. */
17
19
  const HOST_MEZZANINES = String.raw `
@@ -24,8 +26,106 @@ fetch: func [
24
26
  url [url! string!]
25
27
  /with "method, headers, body, form, multipart, timeout, full"
26
28
  options [block! object!]
29
+ /local answer
27
30
  ][
28
- http-request url either with [either block? options [make object! options] [options]] [none]
31
+ if all [with block? options] [options: make object! options]
32
+ answer: http-request url either with [options] [none]
33
+ ; what was kept of the same path is old now
34
+ if all [with find [post put patch delete] attempt [to word! select options 'method]] [ui-cache-touched url]
35
+ answer
36
+ ]
37
+
38
+ ; --- answers kept: cached and refresh ---------------------------------------------
39
+ ;
40
+ ; Each url read with cached has an entry; a reaction that reads cached %/x has
41
+ ; the entry's version among its sources, so it runs again when an answer comes.
42
+ ; ui-cache-news counts every answer, for reactions whose url is not written out.
43
+
44
+ ui-cache-entries: copy []
45
+ ui-cache-news: make object! [version: 0]
46
+
47
+ ui-cache-entry: func ["The entry of what cached keeps for a url, made the first time." url /local key e] [
48
+ key: form url
49
+ unless e: select/case ui-cache-entries key [
50
+ e: make object! [url: none answer: none version: 0 stale: true asking: false again: false task: none]
51
+ e/url: url
52
+ append ui-cache-entries reduce [key e]
53
+ ]
54
+ e
55
+ ]
56
+
57
+ ; The path of a url, without the server, the query or the fragment.
58
+ ui-cache-path: func ["The path of a url, without the server, the query or the fragment." url /local s i] [
59
+ s: form url
60
+ if i: find s "#" [s: copy/part s i]
61
+ if i: find s "?" [s: copy/part s i]
62
+ if i: find s "://" [s: any [find skip i 3 "/" copy "/"]]
63
+ s
64
+ ]
65
+
66
+ ; Asks again behind; one request at a time per url, and one more if asked meanwhile.
67
+ ui-cache-ask: func ["Asks again behind for the answer of an entry of cached." e] [
68
+ e/stale: false
69
+ either e/asking [e/again: true] [
70
+ e/asking: true
71
+ e/task: spawn compose [ui-cache-answer (e)]
72
+ ]
73
+ ]
74
+
75
+ ui-cache-answer: func ["Asks for the answer of an entry of cached, and keeps it." e /local r] [
76
+ forever [
77
+ e/again: false
78
+ r: try [fetch e/url]
79
+ either error? :r [
80
+ ; the last answer stays; the next read asks again
81
+ e/stale: true
82
+ print ["cached:" form e/url "--" r/message]
83
+ ] [
84
+ e/answer: :r
85
+ e/version: e/version + 1
86
+ ui-cache-news/version: ui-cache-news/version + 1
87
+ ]
88
+ unless e/again [break]
89
+ ]
90
+ e/asking: false
91
+ ]
92
+
93
+ ; Every url kept is asked again: the page came back into view.
94
+ ui-caches-again: func ["Asks again for every answer cached keeps." /local key e] [
95
+ foreach [key e] ui-cache-entries [ui-cache-ask e]
96
+ ]
97
+
98
+ ; Every url kept is old: it is asked again the next time it is read (a screen is entered).
99
+ ui-caches-old: func ["Marks every answer cached keeps as old: the next read asks again." /local key e] [
100
+ foreach [key e] ui-cache-entries [e/stale: true]
101
+ ]
102
+
103
+ ; A post, put, patch or delete to url: what was kept of its path, or of a path
104
+ ; above or under it (/api/clients for /api/clients/7), is asked again.
105
+ ui-cache-touched: func ["Asks again for what cached keeps of the path of url, or above or under it." url /local key e a b] [
106
+ a: ui-cache-path url
107
+ foreach [key e] ui-cache-entries [
108
+ b: ui-cache-path e/url
109
+ if any [a = b find/match a join b "/" find/match b join a "/"] [ui-cache-ask e]
110
+ ]
111
+ ]
112
+
113
+ cached: func [
114
+ "The last answer to a GET of url, at once (none the first time), and asks again behind: a reaction (is, react) that reads it runs again when the answer comes."
115
+ url [url! string!]
116
+ /local e
117
+ ][
118
+ e: ui-cache-entry url
119
+ if e/stale [ui-cache-ask e]
120
+ e/answer
121
+ ]
122
+
123
+ refresh: func [
124
+ "Asks again for an answer kept by cached, now; reactions that read it run again when it comes."
125
+ url [url! string!]
126
+ ][
127
+ ui-cache-ask ui-cache-entry url
128
+ exit
29
129
  ]
30
130
 
31
131
  show-progress: func [
@@ -162,7 +262,10 @@ export function startHost(opts = {}) {
162
262
  const onError = opts.onError ?? (e => console.error(report(e)));
163
263
  const rt = makeRuntime();
164
264
  setLoader(rt, opts.loader ?? defaultLoader());
165
- const r = doSource(rt, HOST_MEZZANINES, 'host');
265
+ // in lib, as the core's mezzanines: a word a program sets never takes the place of one of them
266
+ const code = load(HOST_MEZZANINES, { file: 'host' });
267
+ bindSeries(code.s, rt.lib, true);
268
+ const r = runSync(rt, code);
166
269
  // Host mezzanines failing to load is a bug in Trebol itself.
167
270
  if (r.k === 'error')
168
271
  throw new Error(`host mezzanines failed: ${errorMessage(r.e)}`);
@@ -1,4 +1,11 @@
1
1
  import { type Cell, type MapCell, type Runtime } from '../core/index.ts';
2
+ /** How much a number, a percent or an amount is, as a JavaScript number; null for anything else. */
3
+ export declare function amountOf(c: Cell): number | null;
4
+ /**
5
+ * What a number that counts shows at x on its way to `to` (a text with `count`): a value of the kind of `to`,
6
+ * with no more digits than it has, so the run reads as the number it ends on.
7
+ */
8
+ export declare function countStep(to: Cell, x: number): Cell;
2
9
  /** The kinds `localize` formats; anything else shows as `form` gives it. */
3
10
  export declare const LOCAL_KINDS: ReadonlySet<number>;
4
11
  /** A value in the format of a language tag (`es`, `en-GB`); null when it has no local format. */
@@ -17,6 +24,17 @@ export declare function readLocal(s: string, lang: string): string | null;
17
24
  * reads in every language, and a two-digit year is in this century.
18
25
  */
19
26
  export declare function readDate(s: string, lang: string): string | null;
27
+ /**
28
+ * Two dates typed for a range, as `2026-11-03 2026-11-10` with the start first, or null. They are
29
+ * written apart by a dash with spaces around it, as `3/11/2026 – 10/11/2026` or `3/11/2026 - 10/11/2026`,
30
+ * or by a space alone; a range typed end first is turned round.
31
+ */
32
+ export declare function readRange(s: string, lang: string): string | null;
33
+ /**
34
+ * A time typed in any language, as `09:30`, or null: `9:30`, `9.30`, `9h30`, `930`, `21`, and with am or pm,
35
+ * `9:30 pm` or `9pm`. Minutes go from 00 to 59 and hours from 0 to 23, or 1 to 12 with am or pm.
36
+ */
37
+ export declare function readTime(s: string): string | null;
20
38
  /** The first day of the week in a language, 1 for Monday to 7 for Sunday. */
21
39
  export declare function weekStart(lang: string): number;
22
40
  /** The active language: `i18n/language` of the user context, `en` if there is none. */
@@ -1,7 +1,39 @@
1
1
  // Values in the format of a language: dates, times, numbers, percents and
2
2
  // money, through Intl. It works wherever Intl does (browser or Node); the
3
3
  // core keeps its own language-neutral form/mold.
4
- import { K, native, value, string, form, text, formatDec, spell, ctxFind, intern, } from "../core/index.js";
4
+ import { K, native, value, string, form, text, formatDec, spell, ctxFind, intern, integer, decimal, percent, money, dec, decToNumber, I64, } from "../core/index.js";
5
+ /** How much a number, a percent or an amount is, as a JavaScript number; null for anything else. */
6
+ export function amountOf(c) {
7
+ switch (c.t) {
8
+ case K.Integer: return I64.toNumber(c.v);
9
+ case K.Decimal:
10
+ case K.Percent: return c.v;
11
+ case K.Money: return decToNumber(c.v);
12
+ default: return null;
13
+ }
14
+ }
15
+ /** The digits after the point a decimal number is written with. */
16
+ function fractionDigits(n) {
17
+ const m = /\.(\d+)|e-(\d+)/.exec(String(n));
18
+ return m === null ? 0 : m[1] !== undefined ? m[1].length : Number(m[2]);
19
+ }
20
+ /**
21
+ * What a number that counts shows at x on its way to `to` (a text with `count`): a value of the kind of `to`,
22
+ * with no more digits than it has, so the run reads as the number it ends on.
23
+ */
24
+ export function countStep(to, x) {
25
+ switch (to.t) {
26
+ case K.Integer: return integer(Math.round(x));
27
+ case K.Money: return money(dec(BigInt(Math.round(x * 10 ** to.v.e)), to.v.e), to.cur);
28
+ case K.Decimal:
29
+ case K.Percent: {
30
+ const k = 10 ** fractionDigits(to.v);
31
+ const v = Math.round(x * k) / k;
32
+ return to.t === K.Decimal ? decimal(v) : percent(v);
33
+ }
34
+ default: return to;
35
+ }
36
+ }
5
37
  /** The kinds `localize` formats; anything else shows as `form` gives it. */
6
38
  export const LOCAL_KINDS = new Set([K.Date, K.Time, K.Integer, K.Decimal, K.Percent, K.Money]);
7
39
  const NS_PER_MS = 1e6;
@@ -128,6 +160,44 @@ export function readDate(s, lang) {
128
160
  return null;
129
161
  return `${y}-${String(m).padStart(2, '0')}-${String(d).padStart(2, '0')}`;
130
162
  }
163
+ /**
164
+ * Two dates typed for a range, as `2026-11-03 2026-11-10` with the start first, or null. They are
165
+ * written apart by a dash with spaces around it, as `3/11/2026 – 10/11/2026` or `3/11/2026 - 10/11/2026`,
166
+ * or by a space alone; a range typed end first is turned round.
167
+ */
168
+ export function readRange(s, lang) {
169
+ let parts = s.trim().split(/\s*[\u2013\u2014]\s*|\s+-\s+/);
170
+ if (parts.length === 1)
171
+ parts = s.trim().split(/\s+/);
172
+ if (parts.length !== 2)
173
+ return null;
174
+ const [a, b] = parts.map(p => readDate(p, lang));
175
+ if (a == null || b == null)
176
+ return null;
177
+ return a <= b ? `${a} ${b}` : `${b} ${a}`;
178
+ }
179
+ /**
180
+ * A time typed in any language, as `09:30`, or null: `9:30`, `9.30`, `9h30`, `930`, `21`, and with am or pm,
181
+ * `9:30 pm` or `9pm`. Minutes go from 00 to 59 and hours from 0 to 23, or 1 to 12 with am or pm.
182
+ */
183
+ export function readTime(s) {
184
+ const t = /^(\d{1,2})(?:\s*[:.h]\s*(\d{2})|(\d{2}))?\s*h?\s*(?:([ap])\.?\s*m\.?)?$/i.exec(s.trim());
185
+ if (t === null)
186
+ return null;
187
+ let h = Number(t[1]);
188
+ const m = Number(t[2] ?? t[3] ?? 0);
189
+ const half = t[4]?.toLowerCase();
190
+ if (m > 59)
191
+ return null;
192
+ if (half !== undefined) {
193
+ if (h < 1 || h > 12)
194
+ return null;
195
+ h = (h % 12) + (half === 'p' ? 12 : 0);
196
+ }
197
+ else if (h > 23)
198
+ return null;
199
+ return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`;
200
+ }
131
201
  /** The first day of the week in a language, 1 for Monday to 7 for Sunday. */
132
202
  export function weekStart(lang) {
133
203
  try {
@@ -144,8 +214,8 @@ export function weekStart(lang) {
144
214
  }
145
215
  /** The active language: `i18n/language` of the user context, `en` if there is none. */
146
216
  export function activeLanguage(rt) {
147
- const i = ctxFind(rt.user, intern('i18n'));
148
- const obj = i < 0 ? null : rt.user.vals[i];
217
+ const i = ctxFind(rt.lib, intern('i18n'));
218
+ const obj = i < 0 ? null : rt.lib.vals[i];
149
219
  if (obj === null || obj.t !== K.Object)
150
220
  return 'en';
151
221
  const o = obj;
@@ -159,8 +229,8 @@ export function activeLanguage(rt) {
159
229
  }
160
230
  /** A field of the user's `i18n` object, or null. */
161
231
  function i18nField(rt, name) {
162
- const i = ctxFind(rt.user, intern('i18n'));
163
- const obj = i < 0 ? null : rt.user.vals[i];
232
+ const i = ctxFind(rt.lib, intern('i18n'));
233
+ const obj = i < 0 ? null : rt.lib.vals[i];
164
234
  if (obj === null || obj.t !== K.Object)
165
235
  return null;
166
236
  const j = ctxFind(obj.ctx, intern(name));
@@ -28,3 +28,7 @@ export declare function printMd(blocks: Block[]): string;
28
28
  export declare function appendText(src: string, text: string): string;
29
29
  /** Only web and mail links open; anything else (javascript:...) is shown as plain text. */
30
30
  export declare function safeHref(href: string): boolean;
31
+ /** What a link face may open: web, mail and phone addresses and addresses with no scheme (a path,
32
+ * a #screen); anything else (javascript:, data:...) is null. Spaces before it and tabs or line
33
+ * breaks inside it are dropped first, as the browser does, so "java\tscript:" is caught too. */
34
+ export declare function linkHref(href: string): string | null;
@@ -159,3 +159,11 @@ export function appendText(src, text) {
159
159
  export function safeHref(href) {
160
160
  return /^(https?:|mailto:)/i.test(href.trim());
161
161
  }
162
+ /** What a link face may open: web, mail and phone addresses and addresses with no scheme (a path,
163
+ * a #screen); anything else (javascript:, data:...) is null. Spaces before it and tabs or line
164
+ * breaks inside it are dropped first, as the browser does, so "java\tscript:" is caught too. */
165
+ export function linkHref(href) {
166
+ const plain = href.replace(/[\t\n\r]/g, '').replace(/^[\x00-\x20]+/, '');
167
+ const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(plain);
168
+ return scheme === null || /^(https?|mailto|tel)$/i.test(scheme[1]) ? href : null;
169
+ }
@@ -11,6 +11,13 @@
11
11
  //
12
12
  // A `.js` file is a JavaScript module instead (js.ts).
13
13
  //
14
+ // `import 'trebol-es` imports a package by its name: the first node_modules/trebol-es/
15
+ // found going up from the importing file (from the page or the working directory when
16
+ // no file imports it), as Node finds packages. Its package.json says which file is the
17
+ // module, `"trebol": "es.trb"`. The same loader reads it, so this works on a disk and
18
+ // over http alike; `trebol build` puts the package's files inside the page where the
19
+ // page finds them, under node_modules/.
20
+ //
14
21
  // `do %file` runs a script in the user context each time; `read` gives a
15
22
  // file's text and `load %file` its values.
16
23
  //
@@ -23,7 +30,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
23
30
  }
24
31
  return path;
25
32
  };
26
- import { K, NONE, UNSET, native, value, text, load, mold, string, context, collectSetWords, bindSeries, bindSelf, copyDeep, setWord, getWord, splice, currentFile, throwError, makeError, scriptStart, LoadError, T, canon, intern, } from "../core/index.js";
33
+ import { K, NONE, UNSET, native, value, text, load, mold, string, context, collectSetWords, bindSeries, bindSelf, copyDeep, setWord, getWord, splice, currentFile, throwError, makeError, scriptStart, LoadError, T, canon, intern, spell, LIBRARY_FILES, blockOf, } from "../core/index.js";
27
34
  import { jsModule } from "./js.js";
28
35
  const loaders = new WeakMap();
29
36
  const modules = new WeakMap();
@@ -55,6 +62,7 @@ const EXPORTS = canon(intern('exports'));
55
62
  const EXPORT = canon(intern('export'));
56
63
  const TYPE = canon(intern('type'));
57
64
  const MODULE = canon(intern('module'));
65
+ const PARTS = canon(intern('parts'));
58
66
  /** The value after `name:` in a script header, or null. */
59
67
  function headerField(header, name) {
60
68
  if (header === null)
@@ -152,14 +160,70 @@ native('do-file', '["Runs the script in a file, as do does; a module is imported
152
160
  bindSeries(code.s, t.rt.user, true);
153
161
  return { k: 'do', s: code.s, i: start };
154
162
  });
155
- native('import', '["Runs a module and gives its exports." source [file! url! string!]]', (t, f, v) => {
163
+ /** Package names as npm takes them, without a scope (a scope does not fit in a word). */
164
+ const PACKAGE_NAME = /^[a-z0-9][a-z0-9._-]*$/;
165
+ /** The href of a package's module: the "trebol" file of the first node_modules/<name>/ going up from `from`. */
166
+ async function packageModule(loader, from, name) {
167
+ let dir = new URL('./', from);
168
+ for (;;) {
169
+ const at = new URL(`node_modules/${name}/`, dir).href;
170
+ const json = builtIn.get(`${at}package.json`) ?? await loader.read(`${at}package.json`).catch(() => null);
171
+ if (json !== null) {
172
+ let main;
173
+ try {
174
+ main = JSON.parse(json).trebol;
175
+ }
176
+ catch {
177
+ throw makeError('access', 'cannot-open', `the package.json of ${name} is not JSON`);
178
+ }
179
+ if (typeof main !== 'string')
180
+ throw makeError('access', 'cannot-open', `${name} is not a Trebol package: its package.json has no "trebol" file`);
181
+ return string(new URL(main, at).href);
182
+ }
183
+ if (dir.pathname === '/' || dir.protocol !== 'file:' && dir.protocol !== 'http:' && dir.protocol !== 'https:')
184
+ break;
185
+ dir = new URL('../', dir);
186
+ }
187
+ throw makeError('access', 'cannot-open', `cannot find the package ${name}: no node_modules/${name}/package.json`);
188
+ }
189
+ /** Binds the bodies of a module (its parts, then its own code) to one context and runs the first. */
190
+ function startModule(rt, f, st, bodies) {
191
+ for (const b of bodies)
192
+ st.exports.push(...takeExports(b));
193
+ const ctx = context();
194
+ ctx.self = true;
195
+ for (const b of bodies)
196
+ collectSetWords(b, 0, ctx);
197
+ // Trebol's own modules (the UI dialect) see lib, as R3's modules do, so a word a program sets,
198
+ // form: or can?, never takes the place of one they use; their exports go to lib too
199
+ for (const b of bodies) {
200
+ bindSeries(b, st.lib ? rt.lib : rt.user, true);
201
+ bindSeries(b, ctx);
202
+ bindSelf(b, ctx);
203
+ }
204
+ st.ctx = ctx;
205
+ st.bodies = bodies;
206
+ st.at = 0;
207
+ f.phase = 2;
208
+ f.trap = 1 << T.Error;
209
+ return { k: 'do', s: bodies[0], i: 0 };
210
+ }
211
+ native('import', '["Runs a module and gives its exports; a word is a package by its name, from node_modules." source [file! url! string! word!]]', (t, f, v) => {
156
212
  const rt = t.rt;
157
213
  const loader = loaders.get(rt);
158
214
  const mods = modules.get(rt);
159
215
  if (loader === undefined || mods === undefined)
160
216
  return throwError('access', 'no-loader', 'this host cannot load modules');
161
- if (f.phase === 0) {
162
- const href = hrefOf(t, loader, f.args[0]);
217
+ // a package: where its module is comes first, then it is imported as a file is
218
+ if (f.phase === 0 && f.args[0].t === K.Word) {
219
+ const name = spell(f.args[0].sym);
220
+ if (!PACKAGE_NAME.test(name))
221
+ return throwError('script', 'bad-module', `${name} is not the name of a package`);
222
+ f.phase = 5;
223
+ return { k: 'await', p: packageModule(loader, resolve(loader.base, currentFile(t), './'), name) };
224
+ }
225
+ if (f.phase === 0 || f.phase === 5) {
226
+ const href = f.phase === 5 ? text(v) : hrefOf(t, loader, f.args[0]);
163
227
  const known = mods.get(href);
164
228
  if (known !== undefined) {
165
229
  if (known.obj !== null)
@@ -215,10 +279,9 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
215
279
  mods.delete(st.href);
216
280
  st.entry.settle(false, e);
217
281
  };
218
- if (f.phase === 1) {
219
- let code;
282
+ const loadIt = (src, file) => {
220
283
  try {
221
- code = load(text(v), { file: st.href });
284
+ return load(text(src), { file });
222
285
  }
223
286
  catch (e) {
224
287
  // LoadError is the one exception on the normal path; it becomes a value here.
@@ -229,22 +292,56 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
229
292
  failed(step.e.v);
230
293
  return step;
231
294
  }
295
+ };
296
+ if (f.phase === 1) {
297
+ const code = loadIt(v, st.href);
298
+ if ('k' in code)
299
+ return code;
232
300
  const { header, start } = scriptStart(code);
233
301
  const listed = headerField(header, EXPORTS);
234
302
  if (listed !== null && listed.t === K.Block)
235
303
  st.exports = listed.s.data.filter(w => w.t === K.Word);
236
- const body = copyDeep(code.s, start);
237
- st.exports.push(...takeExports(body));
238
- const ctx = context();
239
- ctx.self = true;
240
- collectSetWords(body, 0, ctx);
241
- bindSeries(body, rt.user, true);
242
- bindSeries(body, ctx);
243
- bindSelf(body, ctx);
244
- st.ctx = ctx;
245
- f.phase = 2;
246
- f.trap = 1 << T.Error;
247
- return { k: 'do', s: body, i: 0 };
304
+ st.lib = LIBRARY_FILES.has(st.href);
305
+ // parts: [%a.trb %b.trb] are files of the same module, read before it runs
306
+ const parts = headerField(header, PARTS);
307
+ if (parts !== null) {
308
+ const files = parts.t === K.Block ? parts.s.data.slice(parts.i) : [];
309
+ if (files.length === 0 || files.some(x => x.t !== K.File)) {
310
+ const step = throwError('script', 'bad-module', 'parts: takes a block of files');
311
+ if (step.k === 'throw')
312
+ failed(step.e.v);
313
+ return step;
314
+ }
315
+ st.code = code;
316
+ st.start = start;
317
+ st.parts = files.map(x => new URL(text(x), st.href).href);
318
+ // the parts of Trebol's own modules are Trebol's own code too
319
+ if (st.lib)
320
+ for (const h of st.parts)
321
+ LIBRARY_FILES.add(h);
322
+ f.phase = 6;
323
+ const loader = loaders.get(rt);
324
+ return { k: 'await', p: Promise.all(st.parts.map((h, i) => readText(loader, h, files[i]))).then(texts => blockOf(K.Block, texts)) };
325
+ }
326
+ return startModule(rt, f, st, [copyDeep(code.s, start)]);
327
+ }
328
+ if (f.phase === 6) {
329
+ const bodies = [];
330
+ const texts = v.s.data;
331
+ for (let i = 0; i < texts.length; i++) {
332
+ const part = loadIt(texts[i], st.parts[i]);
333
+ if ('k' in part)
334
+ return part;
335
+ bodies.push(copyDeep(part.s, scriptStart(part).start));
336
+ }
337
+ bodies.push(copyDeep(st.code.s, st.start));
338
+ st.code = undefined;
339
+ return startModule(rt, f, st, bodies);
340
+ }
341
+ // a part ran: the next one, or the module's own body
342
+ if (f.phase === 2 && f.thrown === null && st.at + 1 < st.bodies.length) {
343
+ st.at++;
344
+ return { k: 'do', s: st.bodies[st.at], i: 0 };
248
345
  }
249
346
  // phase 2: the module body ran
250
347
  if (f.thrown !== null) {
@@ -262,6 +359,8 @@ native('import', '["Runs a module and gives its exports." source [file! url! str
262
359
  failed(step.e.v);
263
360
  return step;
264
361
  }
362
+ if (st.lib)
363
+ setWord({ t: K.Word, sym: w.sym, bind: rt.lib }, st.ctx.vals[i]);
265
364
  setWord({ t: K.Word, sym: w.sym, bind: rt.user }, st.ctx.vals[i]);
266
365
  }
267
366
  st.entry.obj = obj;
@@ -0,0 +1,32 @@
1
+ /** The theme changed: read its time again. */
2
+ export declare function themeMoved(): void;
3
+ /** Whether the page may move now: the browser can, the user has not asked for less, the page is on show. */
4
+ export declare function mayMove(): boolean;
5
+ export declare function travelled(way: 'on' | 'back'): void;
6
+ /**
7
+ * Puts the screen coming in place of the one on show: the browser takes a picture of
8
+ * the page, swap changes it, and the old screen slides out as the new one slides in,
9
+ * the way the user went. Faces given the same `morph` in both become one another.
10
+ * swap is called once, now or at the browser's next frame; it may decide not to.
11
+ */
12
+ export declare function changeScreen(box: HTMLElement, coming: HTMLElement, swap: () => void): void;
13
+ /** A name as the browser takes it: letters, digits, - and _, anything else spelled by its code. */
14
+ export declare function morphIdent(name: string): string;
15
+ /** Where each row stood before the rows changed. */
16
+ export declare function rowPlaces(rows: Iterable<HTMLElement>): Map<HTMLElement, DOMRect>;
17
+ /**
18
+ * After the rows changed: those that stayed glide from where they were to where
19
+ * they are, the new ones fade in.
20
+ */
21
+ export declare function moveRows(rows: Iterable<HTMLElement>, was: Map<HTMLElement, DOMRect>): void;
22
+ /**
23
+ * A row that went fades where it stood, out of the flow so the others close the gap:
24
+ * its cells keep their widths, as it no longer lies in its table.
25
+ */
26
+ export declare function leaveRow(r: HTMLElement, at: DOMRect): void;
27
+ /** Whether a face is folding away now. */
28
+ export declare function isFolding(e: HTMLElement): boolean;
29
+ /** A face shown again unfolds: from no height, if it lies among others one above another, and from clear. */
30
+ export declare function unfold(e: HTMLElement): void;
31
+ /** A face hidden folds away, and then done hides it. */
32
+ export declare function fold(e: HTMLElement, done: () => void): void;