trebol 0.1.1 → 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 (300) hide show
  1. package/README.md +12 -1
  2. package/dist/cli.js +17097 -12238
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/boot.js +12 -8
  12. package/dist/lib/core/eval.js +2 -2
  13. package/dist/lib/core/func.d.ts +4 -0
  14. package/dist/lib/core/machine/checks.d.ts +1 -0
  15. package/dist/lib/core/machine/checks.js +250 -0
  16. package/dist/lib/core/machine/events.d.ts +1 -0
  17. package/dist/lib/core/machine/events.js +365 -0
  18. package/dist/lib/core/machine/running.d.ts +1 -0
  19. package/dist/lib/core/machine/running.js +180 -0
  20. package/dist/lib/core/machine/shared.d.ts +1 -0
  21. package/dist/lib/core/machine/shared.js +123 -0
  22. package/dist/lib/core/machine/snapshot.d.ts +1 -0
  23. package/dist/lib/core/machine/snapshot.js +244 -0
  24. package/dist/lib/core/machine/spec.d.ts +1 -0
  25. package/dist/lib/core/machine/spec.js +274 -0
  26. package/dist/lib/core/machine/words.d.ts +1 -0
  27. package/dist/lib/core/machine/words.js +98 -0
  28. package/dist/lib/core/machine.d.ts +1 -0
  29. package/dist/lib/core/machine.js +17 -0
  30. package/dist/lib/core/mezz.js +31 -0
  31. package/dist/lib/core/natives/control.js +2 -2
  32. package/dist/lib/core/natives/data.js +6 -1
  33. package/dist/lib/core/natives/series.js +59 -9
  34. package/dist/lib/core/natives/text.js +3 -1
  35. package/dist/lib/host/browser/calendar.d.ts +34 -0
  36. package/dist/lib/host/browser/calendar.js +268 -0
  37. package/dist/lib/host/browser/count.d.ts +15 -0
  38. package/dist/lib/host/browser/count.js +48 -0
  39. package/dist/lib/host/browser/dialogs.d.ts +39 -0
  40. package/dist/lib/host/browser/dialogs.js +443 -0
  41. package/dist/lib/host/browser/drag.d.ts +70 -0
  42. package/dist/lib/host/browser/drag.js +268 -0
  43. package/dist/lib/host/browser/drawings.d.ts +30 -0
  44. package/dist/lib/host/browser/drawings.js +176 -0
  45. package/dist/lib/host/browser/events.d.ts +16 -0
  46. package/dist/lib/host/browser/events.js +125 -0
  47. package/dist/lib/host/browser/faces.d.ts +35 -0
  48. package/dist/lib/host/browser/faces.js +131 -0
  49. package/dist/lib/host/browser/fields.d.ts +16 -0
  50. package/dist/lib/host/browser/fields.js +322 -0
  51. package/dist/lib/host/browser/keys.d.ts +29 -0
  52. package/dist/lib/host/browser/keys.js +151 -0
  53. package/dist/lib/host/browser/lookup.d.ts +25 -0
  54. package/dist/lib/host/browser/lookup.js +275 -0
  55. package/dist/lib/host/browser/menus.d.ts +20 -0
  56. package/dist/lib/host/browser/menus.js +149 -0
  57. package/dist/lib/host/browser/mount.d.ts +92 -0
  58. package/dist/lib/host/browser/mount.js +448 -0
  59. package/dist/lib/host/browser/orbits.d.ts +90 -0
  60. package/dist/lib/host/browser/orbits.js +379 -0
  61. package/dist/lib/host/browser/page.d.ts +13 -0
  62. package/dist/lib/host/browser/page.js +35 -0
  63. package/dist/lib/host/browser/richtext.d.ts +19 -0
  64. package/dist/lib/host/browser/richtext.js +379 -0
  65. package/dist/lib/host/browser/row-faces.d.ts +5 -0
  66. package/dist/lib/host/browser/row-faces.js +74 -0
  67. package/dist/lib/host/browser/screens.d.ts +37 -0
  68. package/dist/lib/host/browser/screens.js +140 -0
  69. package/dist/lib/host/browser/swipe.d.ts +31 -0
  70. package/dist/lib/host/browser/swipe.js +178 -0
  71. package/dist/lib/host/browser/sync.d.ts +24 -0
  72. package/dist/lib/host/browser/sync.js +450 -0
  73. package/dist/lib/host/browser/tables.d.ts +65 -0
  74. package/dist/lib/host/browser/tables.js +505 -0
  75. package/dist/lib/host/browser/times.d.ts +14 -0
  76. package/dist/lib/host/browser/times.js +114 -0
  77. package/dist/lib/host/browser/toolbars.d.ts +17 -0
  78. package/dist/lib/host/browser/toolbars.js +129 -0
  79. package/dist/lib/host/browser/trees.d.ts +20 -0
  80. package/dist/lib/host/browser/trees.js +235 -0
  81. package/dist/lib/host/browser/uploads.d.ts +29 -0
  82. package/dist/lib/host/browser/uploads.js +370 -0
  83. package/dist/lib/host/browser/widget.d.ts +13 -0
  84. package/dist/lib/host/browser/widget.js +105 -0
  85. package/dist/lib/host/browser.d.ts +25 -0
  86. package/dist/lib/host/browser.js +129 -3964
  87. package/dist/lib/host/devlog.d.ts +11 -0
  88. package/dist/lib/host/devlog.js +13 -0
  89. package/dist/lib/host/devtools/console.d.ts +9 -0
  90. package/dist/lib/host/devtools/console.js +104 -0
  91. package/dist/lib/host/devtools/faces.d.ts +25 -0
  92. package/dist/lib/host/devtools/faces.js +169 -0
  93. package/dist/lib/host/devtools/log.d.ts +3 -0
  94. package/dist/lib/host/devtools/log.js +32 -0
  95. package/dist/lib/host/devtools/machines.d.ts +34 -0
  96. package/dist/lib/host/devtools/machines.js +212 -0
  97. package/dist/lib/host/devtools/read.d.ts +13 -0
  98. package/dist/lib/host/devtools/read.js +52 -0
  99. package/dist/lib/host/devtools/state.d.ts +90 -0
  100. package/dist/lib/host/devtools/state.js +123 -0
  101. package/dist/lib/host/devtools.d.ts +19 -0
  102. package/dist/lib/host/devtools.js +238 -0
  103. package/dist/lib/host/fonts/OFL.txt +93 -0
  104. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  105. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  106. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  107. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  108. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  109. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  110. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  111. package/dist/lib/host/fonts.d.ts +11 -0
  112. package/dist/lib/host/fonts.js +30 -0
  113. package/dist/lib/host/icons.js +4 -0
  114. package/dist/lib/host/index.d.ts +5 -1
  115. package/dist/lib/host/index.js +110 -5
  116. package/dist/lib/host/js.d.ts +5 -0
  117. package/dist/lib/host/js.js +130 -0
  118. package/dist/lib/host/locale.d.ts +18 -0
  119. package/dist/lib/host/locale.js +75 -5
  120. package/dist/lib/host/markdown.d.ts +6 -0
  121. package/dist/lib/host/markdown.js +18 -0
  122. package/dist/lib/host/modules.d.ts +4 -0
  123. package/dist/lib/host/modules.js +157 -20
  124. package/dist/lib/host/motion.d.ts +32 -0
  125. package/dist/lib/host/motion.js +190 -0
  126. package/dist/lib/host/net.js +14 -3
  127. package/dist/lib/host/orbit.d.ts +121 -0
  128. package/dist/lib/host/orbit.js +374 -0
  129. package/dist/lib/host/palette.d.ts +17 -0
  130. package/dist/lib/host/palette.js +131 -0
  131. package/dist/lib/host/rows.d.ts +14 -0
  132. package/dist/lib/host/rows.js +29 -0
  133. package/dist/lib/host/scheduler.d.ts +8 -0
  134. package/dist/lib/host/scheduler.js +64 -36
  135. package/dist/lib/host/share.d.ts +31 -0
  136. package/dist/lib/host/share.js +134 -0
  137. package/dist/lib/host/theme/classic.d.ts +1 -0
  138. package/dist/lib/host/theme/classic.js +120 -0
  139. package/dist/lib/host/theme/cupertino.d.ts +1 -0
  140. package/dist/lib/host/theme/cupertino.js +65 -0
  141. package/dist/lib/host/theme/data.d.ts +1 -0
  142. package/dist/lib/host/theme/data.js +109 -0
  143. package/dist/lib/host/theme/devtools.d.ts +1 -0
  144. package/dist/lib/host/theme/devtools.js +166 -0
  145. package/dist/lib/host/theme/dialogs.d.ts +1 -0
  146. package/dist/lib/host/theme/dialogs.js +63 -0
  147. package/dist/lib/host/theme/fluent.d.ts +1 -0
  148. package/dist/lib/host/theme/fluent.js +64 -0
  149. package/dist/lib/host/theme/forms.d.ts +1 -0
  150. package/dist/lib/host/theme/forms.js +239 -0
  151. package/dist/lib/host/theme/fusion.d.ts +1 -0
  152. package/dist/lib/host/theme/fusion.js +66 -0
  153. package/dist/lib/host/theme/layout.d.ts +1 -0
  154. package/dist/lib/host/theme/layout.js +166 -0
  155. package/dist/lib/host/theme/media.d.ts +1 -0
  156. package/dist/lib/host/theme/media.js +106 -0
  157. package/dist/lib/host/theme/navigation.d.ts +1 -0
  158. package/dist/lib/host/theme/navigation.js +36 -0
  159. package/dist/lib/host/theme/panes.d.ts +1 -0
  160. package/dist/lib/host/theme/panes.js +99 -0
  161. package/dist/lib/host/theme/popups.d.ts +1 -0
  162. package/dist/lib/host/theme/popups.js +149 -0
  163. package/dist/lib/host/theme/responsive.d.ts +1 -0
  164. package/dist/lib/host/theme/responsive.js +103 -0
  165. package/dist/lib/host/theme/toasts.d.ts +1 -0
  166. package/dist/lib/host/theme/toasts.js +62 -0
  167. package/dist/lib/host/theme.js +19 -1179
  168. package/dist/lib/ui/dialect/events.trb +276 -0
  169. package/dist/lib/ui/dialect/faces.trb +135 -0
  170. package/dist/lib/ui/dialect/layout.trb +283 -0
  171. package/dist/lib/ui/dialect/palette.trb +87 -0
  172. package/dist/lib/ui/dialect/problems.trb +174 -0
  173. package/dist/lib/ui/dialect/reactions.trb +198 -0
  174. package/dist/lib/ui/dialect/rows.trb +208 -0
  175. package/dist/lib/ui/dialect/screens.trb +293 -0
  176. package/dist/lib/ui/dialect/steps.trb +203 -0
  177. package/dist/lib/ui/dialect/validation.trb +137 -0
  178. package/dist/lib/ui/dialect/view.trb +110 -0
  179. package/dist/lib/ui/dialect/walking.trb +98 -0
  180. package/dist/lib/ui/dialect/words.trb +70 -0
  181. package/dist/lib/ui/i18n.trb +23 -0
  182. package/dist/lib/ui/test.trb +12 -0
  183. package/dist/lib/ui/testing/actions.trb +421 -0
  184. package/dist/lib/ui/testing/faces.trb +148 -0
  185. package/dist/lib/ui/testing/server.trb +73 -0
  186. package/dist/lib/ui/ui.trb +25 -1234
  187. package/dist/trebol.js +4882 -1088
  188. package/editors/vscode/README.md +15 -0
  189. package/editors/vscode/language-configuration.json +28 -0
  190. package/editors/vscode/package.json +42 -0
  191. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  192. package/package.json +7 -3
  193. package/src/core/boot.ts +10 -7
  194. package/src/core/eval.ts +2 -2
  195. package/src/core/func.ts +4 -0
  196. package/src/core/machine/checks.ts +251 -0
  197. package/src/core/machine/events.ts +366 -0
  198. package/src/core/machine/running.ts +181 -0
  199. package/src/core/machine/shared.ts +124 -0
  200. package/src/core/machine/snapshot.ts +245 -0
  201. package/src/core/machine/spec.ts +275 -0
  202. package/src/core/machine/words.ts +99 -0
  203. package/src/core/machine.ts +20 -0
  204. package/src/core/mezz.ts +31 -0
  205. package/src/core/natives/control.ts +2 -2
  206. package/src/core/natives/data.ts +6 -1
  207. package/src/core/natives/parse.ts +1 -1
  208. package/src/core/natives/series.ts +55 -11
  209. package/src/core/natives/text.ts +2 -1
  210. package/src/host/browser/calendar.ts +264 -0
  211. package/src/host/browser/count.ts +48 -0
  212. package/src/host/browser/dialogs.ts +439 -0
  213. package/src/host/browser/drag.ts +278 -0
  214. package/src/host/browser/drawings.ts +178 -0
  215. package/src/host/browser/events.ts +117 -0
  216. package/src/host/browser/faces.ts +143 -0
  217. package/src/host/browser/fields.ts +315 -0
  218. package/src/host/browser/keys.ts +145 -0
  219. package/src/host/browser/lookup.ts +262 -0
  220. package/src/host/browser/menus.ts +146 -0
  221. package/src/host/browser/mount.ts +420 -0
  222. package/src/host/browser/orbits.ts +418 -0
  223. package/src/host/browser/page.ts +43 -0
  224. package/src/host/browser/richtext.ts +346 -0
  225. package/src/host/browser/row-faces.ts +75 -0
  226. package/src/host/browser/screens.ts +142 -0
  227. package/src/host/browser/swipe.ts +168 -0
  228. package/src/host/browser/sync.ts +354 -0
  229. package/src/host/browser/tables.ts +461 -0
  230. package/src/host/browser/times.ts +115 -0
  231. package/src/host/browser/toolbars.ts +126 -0
  232. package/src/host/browser/trees.ts +205 -0
  233. package/src/host/browser/uploads.ts +367 -0
  234. package/src/host/browser/widget.ts +103 -0
  235. package/src/host/browser.ts +128 -3747
  236. package/src/host/devlog.ts +16 -0
  237. package/src/host/devtools/console.ts +102 -0
  238. package/src/host/devtools/faces.ts +160 -0
  239. package/src/host/devtools/log.ts +34 -0
  240. package/src/host/devtools/machines.ts +208 -0
  241. package/src/host/devtools/read.ts +58 -0
  242. package/src/host/devtools/state.ts +148 -0
  243. package/src/host/devtools.ts +238 -0
  244. package/src/host/fonts/OFL.txt +93 -0
  245. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  246. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  247. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  248. package/src/host/fonts/inter-greek.woff2 +0 -0
  249. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  250. package/src/host/fonts/inter-latin.woff2 +0 -0
  251. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  252. package/src/host/fonts.ts +40 -0
  253. package/src/host/icons.ts +4 -0
  254. package/src/host/index.ts +110 -5
  255. package/src/host/js.ts +127 -0
  256. package/src/host/locale.ts +71 -5
  257. package/src/host/markdown.ts +18 -0
  258. package/src/host/modules.ts +150 -22
  259. package/src/host/motion.ts +204 -0
  260. package/src/host/net.ts +13 -3
  261. package/src/host/orbit.ts +432 -0
  262. package/src/host/palette.ts +139 -0
  263. package/src/host/rows.ts +30 -0
  264. package/src/host/scheduler.ts +67 -36
  265. package/src/host/share.ts +168 -0
  266. package/src/host/theme/classic.ts +121 -0
  267. package/src/host/theme/cupertino.ts +66 -0
  268. package/src/host/theme/data.ts +110 -0
  269. package/src/host/theme/devtools.ts +167 -0
  270. package/src/host/theme/dialogs.ts +64 -0
  271. package/src/host/theme/fluent.ts +65 -0
  272. package/src/host/theme/forms.ts +240 -0
  273. package/src/host/theme/fusion.ts +67 -0
  274. package/src/host/theme/layout.ts +167 -0
  275. package/src/host/theme/media.ts +107 -0
  276. package/src/host/theme/navigation.ts +37 -0
  277. package/src/host/theme/panes.ts +100 -0
  278. package/src/host/theme/popups.ts +150 -0
  279. package/src/host/theme/responsive.ts +104 -0
  280. package/src/host/theme/toasts.ts +63 -0
  281. package/src/host/theme.ts +19 -1179
  282. package/src/ui/dialect/events.trb +276 -0
  283. package/src/ui/dialect/faces.trb +135 -0
  284. package/src/ui/dialect/layout.trb +283 -0
  285. package/src/ui/dialect/palette.trb +87 -0
  286. package/src/ui/dialect/problems.trb +174 -0
  287. package/src/ui/dialect/reactions.trb +198 -0
  288. package/src/ui/dialect/rows.trb +208 -0
  289. package/src/ui/dialect/screens.trb +293 -0
  290. package/src/ui/dialect/steps.trb +203 -0
  291. package/src/ui/dialect/validation.trb +137 -0
  292. package/src/ui/dialect/view.trb +110 -0
  293. package/src/ui/dialect/walking.trb +98 -0
  294. package/src/ui/dialect/words.trb +70 -0
  295. package/src/ui/i18n.trb +23 -0
  296. package/src/ui/test.trb +12 -0
  297. package/src/ui/testing/actions.trb +421 -0
  298. package/src/ui/testing/faces.trb +148 -0
  299. package/src/ui/testing/server.trb +73 -0
  300. package/src/ui/ui.trb +25 -1234
@@ -0,0 +1,418 @@
1
+ // --- orbits -------------------------------------------------------------------------------
2
+
3
+ import { NONE, TRUE, FALSE, type Cell } from '../../core/index.ts';
4
+ import { translate } from '../index.ts';
5
+ import { readOrbit, layoutOrbit, shownEdges, edgeLabel, bubble, gradients, edgePath, ease, fitCamera, MANY, type OrbitSpec, type Role, type Place, type Placed, type Box } from '../orbit.ts';
6
+ import { get, str, own, flag } from './faces.ts';
7
+ import { type Mounted, host, fire, el } from './mount.ts';
8
+ import { SVG_NS, svgNode } from './drawings.ts';
9
+
10
+ export interface OrbitVis {
11
+ key: string;
12
+ /** Moved and faded; `inner`, inside it, is scaled by the radius. Chrome draws a faded group of tiny units blurred, so the two stay apart. */
13
+ g: SVGGElement;
14
+ inner: SVGGElement;
15
+ role: Role;
16
+ cur: Place;
17
+ from: Place;
18
+ to: Place;
19
+ dying: boolean;
20
+ }
21
+
22
+ export interface OrbitLine { a: string; b: string; path: SVGPathElement; text: SVGTextElement | null; curve: number }
23
+
24
+ export interface OrbitState {
25
+ svg: SVGSVGElement;
26
+ defs: SVGDefsElement;
27
+ world: SVGGElement;
28
+ lines: SVGGElement;
29
+ bubbles: SVGGElement;
30
+ vis: Map<string, OrbitVis>;
31
+ edges: OrbitLine[];
32
+ /** What it shows: the spec as read, and a signature of it that tells when it changed. */
33
+ spec: OrbitSpec;
34
+ signature: string;
35
+ /** The page of neighbours on show, and the focus it is a page of. */
36
+ page: number;
37
+ pageOf: string | null;
38
+ box: Box;
39
+ /** The bubbles of the last layout, and how many neighbours were left off its page. */
40
+ placed: Placed[];
41
+ left: number;
42
+ cam: { x: number; y: number; s: number };
43
+ camFrom: { x: number; y: number; s: number } | null;
44
+ camTo: { x: number; y: number; s: number } | null;
45
+ anim: number;
46
+ /** The node the person picked last: new neighbours grow from it. */
47
+ picked: string | null;
48
+ /** The pick came from the keyboard: once redrawn, the focus goes back to the orbit. */
49
+ byKey: boolean;
50
+ dragged: boolean;
51
+ gradients: Map<string, [string, string]>;
52
+ width: number;
53
+ height: number;
54
+ }
55
+
56
+ export let orbitCount = 0;
57
+
58
+ /** An orbit: an SVG the size of the face, a world the camera moves, edges under the bubbles. */
59
+ export function buildOrbit(m: Mounted): HTMLElement {
60
+ const node = el('div', 'tb-orbit');
61
+ const svg = document.createElementNS(SVG_NS, 'svg') as SVGSVGElement;
62
+ svg.setAttribute('role', 'group');
63
+ svg.setAttribute('tabindex', '-1');
64
+ const defs = document.createElementNS(SVG_NS, 'defs') as SVGDefsElement;
65
+ const world = document.createElementNS(SVG_NS, 'g') as SVGGElement;
66
+ const lines = document.createElementNS(SVG_NS, 'g') as SVGGElement;
67
+ const bubbles = document.createElementNS(SVG_NS, 'g') as SVGGElement;
68
+ world.append(lines, bubbles);
69
+ svg.append(defs, world);
70
+ node.append(svg);
71
+ const st: OrbitState = {
72
+ svg, defs, world, lines, bubbles, vis: new Map(), edges: [], spec: readOrbit(NONE).spec, signature: '', page: 0, pageOf: null,
73
+ box: { x0: -100, y0: -100, x1: 100, y1: 100 }, placed: [], left: 0, cam: { x: 0, y: 0, s: 1 }, camFrom: null, camTo: null, anim: 0, picked: null, byKey: false, dragged: false,
74
+ gradients: new Map(), width: 0, height: 0,
75
+ };
76
+ m.kids = st;
77
+ orbitCamera(m, st);
78
+ orbitKeys(m, st);
79
+ if (typeof ResizeObserver !== 'undefined') {
80
+ new ResizeObserver(() => {
81
+ if (svg.clientWidth === st.width && svg.clientHeight === st.height) return;
82
+ st.width = svg.clientWidth;
83
+ st.height = svg.clientHeight;
84
+ // a move under way ends where the new size fits; titles take the new zoom
85
+ const fit = fitCamera(st.width, st.height, st.box);
86
+ if (st.camTo !== null) st.camTo = fit;
87
+ else st.cam = fit;
88
+ orbitDressAll(m, st, fit.s);
89
+ orbitApplyCam(st);
90
+ orbitDrawEdges(st);
91
+ }).observe(svg);
92
+ }
93
+ return node;
94
+ }
95
+
96
+ /** Drag to pan, Ctrl and the wheel (or a pinch) to zoom about the pointer, double click to fit again. */
97
+ export function orbitCamera(m: Mounted, st: OrbitState): void {
98
+ const { svg } = st;
99
+ let drag: { x: number; y: number; cx: number; cy: number; id: number } | null = null;
100
+ svg.addEventListener('pointerdown', ev => {
101
+ if (ev.button !== 0) return;
102
+ drag = { x: ev.clientX, y: ev.clientY, cx: st.cam.x, cy: st.cam.y, id: ev.pointerId };
103
+ st.dragged = false;
104
+ });
105
+ svg.addEventListener('pointermove', ev => {
106
+ if (drag === null || ev.pointerId !== drag.id) return;
107
+ const dx = ev.clientX - drag.x;
108
+ const dy = ev.clientY - drag.y;
109
+ if (!st.dragged && Math.hypot(dx, dy) > 4) {
110
+ st.dragged = true;
111
+ svg.setPointerCapture(ev.pointerId);
112
+ svg.classList.add('tb-orbit-panning');
113
+ }
114
+ if (st.dragged) {
115
+ st.cam = { ...st.cam, x: drag.cx + dx, y: drag.cy + dy };
116
+ orbitApplyCam(st);
117
+ }
118
+ });
119
+ const end = (): void => {
120
+ drag = null;
121
+ svg.classList.remove('tb-orbit-panning');
122
+ // the click that ends a drag picks nothing
123
+ setTimeout(() => { st.dragged = false; }, 0);
124
+ };
125
+ svg.addEventListener('pointerup', end);
126
+ svg.addEventListener('pointercancel', end);
127
+ svg.addEventListener('wheel', ev => {
128
+ // the page scrolls past an orbit; with Ctrl (or a pinch), or when it fills the window, the wheel zooms
129
+ if (!ev.ctrlKey && !ev.metaKey && !flag(m.face, 'fit')) return;
130
+ ev.preventDefault();
131
+ const box = svg.getBoundingClientRect();
132
+ const ox = box.width / 2 + st.cam.x;
133
+ const oy = box.height / 2 + st.cam.y;
134
+ const s = Math.max(0.15, Math.min(4, st.cam.s * Math.exp(-ev.deltaY * (ev.ctrlKey ? 0.01 : 0.0015))));
135
+ const px = ev.clientX - box.left;
136
+ const py = ev.clientY - box.top;
137
+ const wx = (px - ox) / st.cam.s;
138
+ const wy = (py - oy) / st.cam.s;
139
+ st.cam = { x: px - box.width / 2 - wx * s, y: py - box.height / 2 - wy * s, s };
140
+ orbitApplyCam(st);
141
+ }, { passive: false });
142
+ svg.addEventListener('dblclick', ev => {
143
+ if ((ev.target as Element).closest('.tb-orbit-node') !== null) return;
144
+ st.camFrom = { ...st.cam };
145
+ st.camTo = fitCamera(st.width, st.height, st.box);
146
+ orbitAnimate(st, 500);
147
+ });
148
+ }
149
+
150
+ /** Tab goes through the neighbours, Enter or Space picks one, Escape or Backspace steps back. */
151
+ export function orbitKeys(m: Mounted, st: OrbitState): void {
152
+ st.svg.addEventListener('keydown', ev => {
153
+ const g = (ev.target as Element).closest('.tb-orbit-node');
154
+ if ((ev.key === 'Enter' || ev.key === ' ') && g !== null) {
155
+ ev.preventDefault();
156
+ orbitPick(m, st, (g as SVGGElement).dataset['key']!, true);
157
+ } else if ((ev.key === 'Escape' || ev.key === 'Backspace') && st.spec.trail.length > 0) {
158
+ const back = [...st.spec.trail].reverse().find(k => st.spec.nodes.has(k));
159
+ if (back === undefined) return;
160
+ ev.preventDefault();
161
+ ev.stopPropagation();
162
+ orbitPick(m, st, back, true);
163
+ }
164
+ });
165
+ }
166
+
167
+ /** The person picked a bubble: a neighbour enters, the trail steps back, "+N more" turns the page. */
168
+ export function orbitPick(m: Mounted, st: OrbitState, key: string, byKey: boolean): void {
169
+ if (flag(m.face, 'disabled')) return;
170
+ const v = st.vis.get(key);
171
+ if (v === undefined || v.dying) return;
172
+ if (v.role === 'more') {
173
+ st.page++;
174
+ st.picked = st.spec.focus;
175
+ orbitRender(m, st);
176
+ return;
177
+ }
178
+ const node = st.spec.nodes.get(key);
179
+ if (node === undefined || v.role === 'focus') return;
180
+ st.picked = key;
181
+ st.byKey = byKey;
182
+ void fire('ui-orbit', m.face, node.id, v.role === 'trail' ? TRUE : FALSE);
183
+ }
184
+
185
+ export function syncOrbit(m: Mounted): void {
186
+ const st = m.kids as OrbitState;
187
+ const { spec, error } = readOrbit(get(m.face, 'data'));
188
+ if (error !== null) console.warn(`Trebol: ${error}`);
189
+ const t = str(m.face, 'text');
190
+ const focus = spec.focus === null ? undefined : spec.nodes.get(spec.focus);
191
+ const focusTitle = focus === undefined ? '' : translate(focus.title, host.rt);
192
+ st.svg.setAttribute('aria-label', t === null ? focusTitle : focusTitle === '' ? t : `${t}: ${focusTitle}`);
193
+ // a repaint of the same thing in the same language keeps what is on screen, moving or not
194
+ const signature = orbitSignature(spec);
195
+ if (signature === st.signature) return;
196
+ st.signature = signature;
197
+ st.spec = spec;
198
+ if (spec.focus !== st.pageOf) {
199
+ st.page = 0;
200
+ st.pageOf = spec.focus;
201
+ }
202
+ orbitRender(m, st);
203
+ }
204
+
205
+ export function orbitSignature(spec: OrbitSpec): string {
206
+ const tx = (c: Cell | null): string => (c === null ? '' : translate(c, host.rt));
207
+ const nodes = [...spec.nodes.values()].map(n => [n.key, tx(n.title), n.color, n.count, n.many, n.dashed, n.steps, n.at].join('\u0002'));
208
+ const edges = spec.edges.map(e => [e.from, e.to, tx(e.label), e.look].join('\u0002'));
209
+ return [spec.focus ?? '', nodes.join('\u0001'), edges.join('\u0001'), spec.trail.join('\u0001')].join('\u0000');
210
+ }
211
+
212
+ /** The ids of the gradients of a color, made the first time it is used. */
213
+ export function orbitGradients(st: OrbitState, color: string): [string, string] {
214
+ let ids = st.gradients.get(color);
215
+ if (ids === undefined) {
216
+ const n = `tb-orbit${orbitCount++}`;
217
+ ids = [`${n}-body`, `${n}-halo`];
218
+ st.gradients.set(color, ids);
219
+ st.defs.append(...gradients(color, ids[0], ids[1]).map(svgNode));
220
+ }
221
+ return ids;
222
+ }
223
+
224
+ /** Moves from what is on screen to what the spec says: bubbles that stay move, new ones grow from the one picked, the rest fade. */
225
+ export function orbitRender(m: Mounted, st: OrbitState): void {
226
+ const { spec } = st;
227
+ const lay = layoutOrbit(spec, st.page);
228
+ st.box = lay.box;
229
+ const want = new Map(lay.placed.map(p => [p.key, p]));
230
+ for (const [k, v] of [...st.vis]) {
231
+ if (v.dying) continue;
232
+ const p = want.get(k);
233
+ if (p === undefined || p.role !== v.role || p.role === 'more') {
234
+ // gone, or something else now: it fades where it is, and a new one takes its key
235
+ v.dying = true;
236
+ v.from = { ...v.cur };
237
+ v.to = { ...v.cur, r: v.cur.r * (p === undefined ? 0.4 : 1), o: 0 };
238
+ v.g.setAttribute('tabindex', '-1');
239
+ v.g.setAttribute('aria-hidden', 'true');
240
+ st.vis.delete(k);
241
+ st.vis.set(`${k}\u0000old${orbitCount++}`, v);
242
+ if (p !== undefined) st.vis.set(k, orbitBubble(m, st, p, { ...v.cur }));
243
+ } else {
244
+ v.from = { ...v.cur };
245
+ v.to = p;
246
+ }
247
+ }
248
+ const was = st.picked === null ? undefined : [...st.vis.values()].find(v => v.key === st.picked);
249
+ const spawn = was !== undefined ? was.cur : { x: 0, y: 0 };
250
+ for (const p of lay.placed) {
251
+ if (st.vis.has(p.key)) continue;
252
+ st.vis.set(p.key, orbitBubble(m, st, p, { x: spawn.x, y: spawn.y, r: p.r * 0.2, o: 0 }));
253
+ }
254
+ st.width = st.svg.clientWidth;
255
+ st.height = st.svg.clientHeight;
256
+ st.camFrom = { ...st.cam };
257
+ st.camTo = fitCamera(st.width || 600, st.height || 480, lay.box);
258
+ st.placed = lay.placed;
259
+ st.left = lay.left;
260
+ orbitDressAll(m, st, st.camTo.s);
261
+ st.bubbles.append(...lay.placed.map(p => st.vis.get(p.key)!.g));
262
+ st.picked = null;
263
+ orbitAnimate(st, matchMedia('(prefers-reduced-motion: reduce)').matches ? 0 : 900);
264
+ if (st.byKey) {
265
+ st.byKey = false;
266
+ st.svg.focus();
267
+ }
268
+ }
269
+
270
+ /** Every bubble on show drawn anew (its title, hints and size may have changed), and the edges, for the camera's zoom. */
271
+ export function orbitDressAll(m: Mounted, st: OrbitState, scale: number): void {
272
+ for (const p of st.placed) {
273
+ const v = st.vis.get(p.key);
274
+ if (v !== undefined) orbitDress(m, st, v, p, st.left, scale);
275
+ }
276
+ orbitEdges(st, st.placed, scale);
277
+ }
278
+
279
+ export function orbitBubble(m: Mounted, st: OrbitState, p: Placed, start: Place): OrbitVis {
280
+ const g = document.createElementNS(SVG_NS, 'g') as SVGGElement;
281
+ const inner = document.createElementNS(SVG_NS, 'g') as SVGGElement;
282
+ g.append(inner);
283
+ g.dataset['key'] = p.key;
284
+ g.addEventListener('click', ev => {
285
+ if (st.dragged) return;
286
+ ev.stopPropagation();
287
+ orbitPick(m, st, p.key, false);
288
+ });
289
+ // with many neighbours, the labels of a bubble's edges show while the pointer is on it
290
+ g.addEventListener('pointerenter', () => orbitHover(st, p.key, true));
291
+ g.addEventListener('pointerleave', () => orbitHover(st, p.key, false));
292
+ g.addEventListener('focus', () => orbitHover(st, p.key, true));
293
+ g.addEventListener('blur', () => orbitHover(st, p.key, false));
294
+ st.bubbles.append(g);
295
+ return { key: p.key, g, inner, role: p.role, cur: start, from: { ...start }, to: p, dying: false };
296
+ }
297
+
298
+ /** What a bubble shows and how it reads: a link to a neighbour, back to the trail, or the next page. */
299
+ export function orbitDress(m: Mounted, st: OrbitState, v: OrbitVis, p: Placed, left: number, scale: number): void {
300
+ const node = st.spec.nodes.get(p.key) ?? null;
301
+ const title = p.role === 'more' ? (left > 0 ? own('orbit-more', String(left)) : own('orbit-first')) : node === null ? '' : translate(node.title, host.rt);
302
+ const color = p.role === 'more' ? 'var(--tb-muted)' : node?.color ?? 'var(--tb-primary)';
303
+ const [body, halo] = orbitGradients(st, color);
304
+ const label = p.role === 'near' ? orbitEdgeText(st.spec, p.key) : '';
305
+ const said = p.role === 'trail' ? own('orbit-back-to', title) : label === '' ? title : `${title}, ${label}`;
306
+ v.g.setAttribute('class', `tb-orbit-node tb-orbit-${p.role}`);
307
+ const tip = document.createElementNS(SVG_NS, 'title');
308
+ tip.textContent = said;
309
+ v.g.replaceChildren(tip, v.inner);
310
+ v.inner.replaceChildren(...bubble({ role: p.role, r: p.r, scale, above: p.role !== 'trail' && p.y < -1, title, color, body, halo, node }).map(svgNode));
311
+ if (p.role === 'focus') {
312
+ // Chrome stops Tab on an SVG group with a click handler unless told not to
313
+ v.g.setAttribute('tabindex', '-1');
314
+ v.g.removeAttribute('role');
315
+ v.g.removeAttribute('aria-label');
316
+ v.g.setAttribute('aria-hidden', 'true');
317
+ } else {
318
+ v.g.setAttribute('tabindex', '0');
319
+ v.g.setAttribute('role', p.role === 'more' ? 'button' : 'link');
320
+ v.g.setAttribute('aria-label', said);
321
+ v.g.removeAttribute('aria-hidden');
322
+ }
323
+ if (flag(m.face, 'disabled')) v.g.setAttribute('tabindex', '-1');
324
+ }
325
+
326
+ export function orbitEdgeText(spec: OrbitSpec, key: string): string {
327
+ const c = edgeLabel(spec, key);
328
+ return c === null ? '' : translate(c, host.rt);
329
+ }
330
+
331
+ export function orbitEdges(st: OrbitState, placed: Placed[], scale: number): void {
332
+ const many = placed.filter(p => p.role === 'near').length > MANY;
333
+ st.edges = shownEdges(st.spec, placed).map(e => {
334
+ const path = document.createElementNS(SVG_NS, 'path') as SVGPathElement;
335
+ path.setAttribute('class', `tb-orbit-edge tb-orbit-edge-${e.trail ? 'trail' : e.look}`);
336
+ let text: SVGTextElement | null = null;
337
+ if (e.label !== null) {
338
+ text = document.createElementNS(SVG_NS, 'text') as SVGTextElement;
339
+ text.setAttribute('class', many ? 'tb-orbit-label tb-orbit-later' : 'tb-orbit-label');
340
+ // the size of the page's small text once the camera fits
341
+ text.style.fontSize = `${11 / scale}px`;
342
+ text.style.strokeWidth = `${3 / scale}px`;
343
+ text.textContent = translate(e.label, host.rt);
344
+ }
345
+ return { a: e.from, b: e.to, path, text, curve: e.curve };
346
+ });
347
+ st.lines.replaceChildren(...st.edges.flatMap(e => (e.text === null ? [e.path] : [e.path, e.text])));
348
+ st.lines.setAttribute('aria-hidden', 'true');
349
+ }
350
+
351
+ export function orbitHover(st: OrbitState, key: string, on: boolean): void {
352
+ for (const e of st.edges) if (e.text !== null && (e.a === key || e.b === key)) e.text.classList.toggle('tb-orbit-shown', on);
353
+ }
354
+
355
+ export function orbitApplyCam(st: OrbitState): void {
356
+ const w = st.svg.clientWidth || st.width;
357
+ const h = st.svg.clientHeight || st.height;
358
+ st.world.setAttribute('transform', `translate(${w / 2 + st.cam.x},${h / 2 + st.cam.y}) scale(${st.cam.s})`);
359
+ }
360
+
361
+ /** Tweens every bubble and the camera from where they are to where they go; 0 ms puts them there at once. */
362
+ export function orbitAnimate(st: OrbitState, ms: number): void {
363
+ cancelAnimationFrame(st.anim);
364
+ const t0 = performance.now();
365
+ const step = (now: number): void => {
366
+ const k = ms === 0 ? 1 : ease(Math.min(1, (now - t0) / ms));
367
+ for (const v of st.vis.values()) {
368
+ if (v.from !== v.to || v.dying) {
369
+ const f = v.from;
370
+ const t = v.to;
371
+ v.cur = { x: f.x + (t.x - f.x) * k, y: f.y + (t.y - f.y) * k, r: f.r + (t.r - f.r) * k, o: f.o + (t.o - f.o) * k };
372
+ }
373
+ v.g.setAttribute('transform', `translate(${v.cur.x},${v.cur.y})`);
374
+ v.inner.setAttribute('transform', `scale(${Math.max(v.cur.r, 0.001)})`);
375
+ v.g.style.opacity = String(v.cur.o);
376
+ v.g.style.pointerEvents = v.cur.o < 0.05 ? 'none' : '';
377
+ }
378
+ if (st.camFrom !== null && st.camTo !== null) {
379
+ const a = st.camFrom;
380
+ const b = st.camTo;
381
+ st.cam = { x: a.x + (b.x - a.x) * k, y: a.y + (b.y - a.y) * k, s: a.s + (b.s - a.s) * k };
382
+ }
383
+ orbitApplyCam(st);
384
+ orbitDrawEdges(st);
385
+ if (k < 1) st.anim = requestAnimationFrame(step);
386
+ else {
387
+ st.camFrom = st.camTo = null;
388
+ for (const [k2, v] of st.vis) {
389
+ if (v.dying) {
390
+ v.g.remove();
391
+ st.vis.delete(k2);
392
+ }
393
+ }
394
+ }
395
+ };
396
+ if (ms === 0) step(t0);
397
+ else st.anim = requestAnimationFrame(step);
398
+ }
399
+
400
+ export function orbitDrawEdges(st: OrbitState): void {
401
+ for (const e of st.edges) {
402
+ const a = st.vis.get(e.a);
403
+ const b = st.vis.get(e.b);
404
+ if (a === undefined || b === undefined) {
405
+ e.path.setAttribute('d', '');
406
+ continue;
407
+ }
408
+ const at = edgePath(a.cur, b.cur, e.curve);
409
+ e.path.setAttribute('d', at.d);
410
+ const o = String(Math.min(a.cur.o, b.cur.o));
411
+ e.path.style.opacity = o;
412
+ if (e.text !== null) {
413
+ e.text.setAttribute('transform', `translate(${at.x},${at.y}) rotate(${at.angle}) translate(0,-5)`);
414
+ e.text.style.opacity = o;
415
+ }
416
+ }
417
+ }
418
+
@@ -0,0 +1,43 @@
1
+ // --- page -----------------------------------------------------------------------
2
+ //
3
+ // What the page shows as a whole: its root element, the view and title on show, and the style sheets of the theme.
4
+
5
+ import type { ObjectCell } from '../../core/index.ts';
6
+ import { themeCss, type ThemeParams } from '../theme.ts';
7
+ import { themeMoved } from '../motion.ts';
8
+
9
+ export let root: HTMLElement | null = null;
10
+ export function setRoot(v: HTMLElement | null): void {
11
+ root = v;
12
+ }
13
+ /** The view on show, whose palette Ctrl+K opens. */
14
+ export let rootFace: ObjectCell | null = null;
15
+ export function setRootFace(v: ObjectCell | null): void {
16
+ rootFace = v;
17
+ }
18
+ /** The face whose text is the page title: the view's title, or the screen shown. */
19
+ export let titleFace: ObjectCell | null = null;
20
+ export function setTitleFace(v: ObjectCell | null): void {
21
+ titleFace = v;
22
+ }
23
+
24
+
25
+ // The host's own CSS goes in constructed style sheets, not <style> elements: a page whose
26
+ // Content-Security-Policy says style-src 'self' drops a <style> made by script, but adopts these.
27
+ export const sheets = new Map<string, CSSStyleSheet>();
28
+
29
+ export function setSheet(id: string, css: string): void {
30
+ let sheet = sheets.get(id);
31
+ if (sheet === undefined) {
32
+ sheet = new CSSStyleSheet();
33
+ sheets.set(id, sheet);
34
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
35
+ }
36
+ sheet.replaceSync(css);
37
+ }
38
+
39
+ export function setTheme(p: ThemeParams): void {
40
+ setSheet('theme', themeCss(p));
41
+ themeMoved();
42
+ }
43
+