@react-x11/components 0.4.0 → 0.6.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 (248) hide show
  1. package/README.md +143 -8
  2. package/dist/charts/components.d.ts +3 -1
  3. package/dist/charts/components.d.ts.map +1 -1
  4. package/dist/charts/components.js +8 -4
  5. package/dist/charts/components.js.map +1 -1
  6. package/dist/charts/node.d.ts +13 -6
  7. package/dist/charts/node.d.ts.map +1 -1
  8. package/dist/charts/node.js +75 -24
  9. package/dist/charts/node.js.map +1 -1
  10. package/dist/charts/render.d.ts +4 -0
  11. package/dist/charts/render.d.ts.map +1 -1
  12. package/dist/charts/render.js +11 -6
  13. package/dist/charts/render.js.map +1 -1
  14. package/dist/code-editor/node.d.ts +47 -16
  15. package/dist/code-editor/node.d.ts.map +1 -1
  16. package/dist/code-editor/node.js +121 -51
  17. package/dist/code-editor/node.js.map +1 -1
  18. package/dist/flow/draw.d.ts +19 -1
  19. package/dist/flow/draw.d.ts.map +1 -1
  20. package/dist/flow/draw.js +64 -35
  21. package/dist/flow/draw.js.map +1 -1
  22. package/dist/flow/index.d.ts.map +1 -1
  23. package/dist/flow/index.js +24 -8
  24. package/dist/flow/index.js.map +1 -1
  25. package/dist/flow/model.d.ts +4 -2
  26. package/dist/flow/model.d.ts.map +1 -1
  27. package/dist/flow/model.js +4 -2
  28. package/dist/flow/model.js.map +1 -1
  29. package/dist/flow/node.d.ts +40 -9
  30. package/dist/flow/node.d.ts.map +1 -1
  31. package/dist/flow/node.js +142 -53
  32. package/dist/flow/node.js.map +1 -1
  33. package/dist/flow/types.d.ts +46 -21
  34. package/dist/flow/types.d.ts.map +1 -1
  35. package/dist/formula/index.d.ts +4 -2
  36. package/dist/formula/index.d.ts.map +1 -1
  37. package/dist/formula/index.js.map +1 -1
  38. package/dist/formula/node.d.ts +5 -1
  39. package/dist/formula/node.d.ts.map +1 -1
  40. package/dist/formula/node.js +19 -3
  41. package/dist/formula/node.js.map +1 -1
  42. package/dist/html/controls.d.ts +4 -1
  43. package/dist/html/controls.d.ts.map +1 -1
  44. package/dist/html/controls.js.map +1 -1
  45. package/dist/html/css/cascade.d.ts +11 -5
  46. package/dist/html/css/cascade.d.ts.map +1 -1
  47. package/dist/html/css/cascade.js +18 -8
  48. package/dist/html/css/cascade.js.map +1 -1
  49. package/dist/html/css/parse.d.ts +15 -10
  50. package/dist/html/css/parse.d.ts.map +1 -1
  51. package/dist/html/css/parse.js +41 -16
  52. package/dist/html/css/parse.js.map +1 -1
  53. package/dist/html/css/style.d.ts +3 -0
  54. package/dist/html/css/style.d.ts.map +1 -1
  55. package/dist/html/css/style.js +3 -1
  56. package/dist/html/css/style.js.map +1 -1
  57. package/dist/html/css/values.d.ts +14 -1
  58. package/dist/html/css/values.d.ts.map +1 -1
  59. package/dist/html/css/values.js +11 -8
  60. package/dist/html/css/values.js.map +1 -1
  61. package/dist/html/index.d.ts.map +1 -1
  62. package/dist/html/index.js +1 -0
  63. package/dist/html/index.js.map +1 -1
  64. package/dist/html/layout/boxes.d.ts +12 -5
  65. package/dist/html/layout/boxes.d.ts.map +1 -1
  66. package/dist/html/layout/boxes.js +7 -4
  67. package/dist/html/layout/boxes.js.map +1 -1
  68. package/dist/html/layout/inline.d.ts.map +1 -1
  69. package/dist/html/layout/inline.js +22 -1
  70. package/dist/html/layout/inline.js.map +1 -1
  71. package/dist/html/node.d.ts +22 -8
  72. package/dist/html/node.d.ts.map +1 -1
  73. package/dist/html/node.js +97 -15
  74. package/dist/html/node.js.map +1 -1
  75. package/dist/html/paint.d.ts +3 -0
  76. package/dist/html/paint.d.ts.map +1 -1
  77. package/dist/html/paint.js +2 -2
  78. package/dist/html/paint.js.map +1 -1
  79. package/dist/index.d.ts +8 -2
  80. package/dist/index.d.ts.map +1 -1
  81. package/dist/index.js +21 -0
  82. package/dist/index.js.map +1 -1
  83. package/dist/maps/clip.d.ts +43 -0
  84. package/dist/maps/clip.d.ts.map +1 -0
  85. package/dist/maps/clip.js +171 -0
  86. package/dist/maps/clip.js.map +1 -0
  87. package/dist/maps/gzip.d.ts +15 -0
  88. package/dist/maps/gzip.d.ts.map +1 -0
  89. package/dist/maps/gzip.js +56 -0
  90. package/dist/maps/gzip.js.map +1 -0
  91. package/dist/maps/index.d.ts +55 -0
  92. package/dist/maps/index.d.ts.map +1 -0
  93. package/dist/maps/index.js +141 -0
  94. package/dist/maps/index.js.map +1 -0
  95. package/dist/maps/labels.d.ts +123 -0
  96. package/dist/maps/labels.d.ts.map +1 -0
  97. package/dist/maps/labels.js +323 -0
  98. package/dist/maps/labels.js.map +1 -0
  99. package/dist/maps/mvt.d.ts +244 -0
  100. package/dist/maps/mvt.d.ts.map +1 -0
  101. package/dist/maps/mvt.js +820 -0
  102. package/dist/maps/mvt.js.map +1 -0
  103. package/dist/maps/node.d.ts +278 -0
  104. package/dist/maps/node.d.ts.map +1 -0
  105. package/dist/maps/node.js +1459 -0
  106. package/dist/maps/node.js.map +1 -0
  107. package/dist/maps/overlay.d.ts +157 -0
  108. package/dist/maps/overlay.d.ts.map +1 -0
  109. package/dist/maps/overlay.js +472 -0
  110. package/dist/maps/overlay.js.map +1 -0
  111. package/dist/maps/paint.d.ts +226 -0
  112. package/dist/maps/paint.d.ts.map +1 -0
  113. package/dist/maps/paint.js +705 -0
  114. package/dist/maps/paint.js.map +1 -0
  115. package/dist/maps/proj.d.ts +281 -0
  116. package/dist/maps/proj.d.ts.map +1 -0
  117. package/dist/maps/proj.js +451 -0
  118. package/dist/maps/proj.js.map +1 -0
  119. package/dist/maps/sources.d.ts +256 -0
  120. package/dist/maps/sources.d.ts.map +1 -0
  121. package/dist/maps/sources.js +210 -0
  122. package/dist/maps/sources.js.map +1 -0
  123. package/dist/maps/style.d.ts +141 -0
  124. package/dist/maps/style.d.ts.map +1 -0
  125. package/dist/maps/style.js +147 -0
  126. package/dist/maps/style.js.map +1 -0
  127. package/dist/maps/styles.d.ts +82 -0
  128. package/dist/maps/styles.d.ts.map +1 -0
  129. package/dist/maps/styles.js +898 -0
  130. package/dist/maps/styles.js.map +1 -0
  131. package/dist/maps/tiles.d.ts +283 -0
  132. package/dist/maps/tiles.d.ts.map +1 -0
  133. package/dist/maps/tiles.js +469 -0
  134. package/dist/maps/tiles.js.map +1 -0
  135. package/dist/maps/types.d.ts +244 -0
  136. package/dist/maps/types.d.ts.map +1 -0
  137. package/dist/maps/types.js +2 -0
  138. package/dist/maps/types.js.map +1 -0
  139. package/dist/markdown/ast.d.ts +80 -6
  140. package/dist/markdown/ast.d.ts.map +1 -1
  141. package/dist/markdown/ast.js +13 -4
  142. package/dist/markdown/ast.js.map +1 -1
  143. package/dist/markdown/expressions.d.ts +27 -0
  144. package/dist/markdown/expressions.d.ts.map +1 -0
  145. package/dist/markdown/expressions.js +203 -0
  146. package/dist/markdown/expressions.js.map +1 -0
  147. package/dist/markdown/index.d.ts +61 -2
  148. package/dist/markdown/index.d.ts.map +1 -1
  149. package/dist/markdown/index.js +101 -2
  150. package/dist/markdown/index.js.map +1 -1
  151. package/dist/markdown/parse.d.ts +1 -1
  152. package/dist/markdown/parse.d.ts.map +1 -1
  153. package/dist/markdown/parse.js +163 -15
  154. package/dist/markdown/parse.js.map +1 -1
  155. package/dist/markdown/spans.d.ts.map +1 -1
  156. package/dist/markdown/spans.js +4 -0
  157. package/dist/markdown/spans.js.map +1 -1
  158. package/dist/markdown/tags.d.ts +35 -0
  159. package/dist/markdown/tags.d.ts.map +1 -0
  160. package/dist/markdown/tags.js +213 -0
  161. package/dist/markdown/tags.js.map +1 -0
  162. package/dist/reorder/hx.d.ts +18 -0
  163. package/dist/reorder/hx.d.ts.map +1 -0
  164. package/dist/reorder/hx.js +10 -0
  165. package/dist/reorder/hx.js.map +1 -0
  166. package/dist/reorder/index.d.ts +314 -0
  167. package/dist/reorder/index.d.ts.map +1 -0
  168. package/dist/reorder/index.js +1539 -0
  169. package/dist/reorder/index.js.map +1 -0
  170. package/dist/reorder/model.d.ts +107 -0
  171. package/dist/reorder/model.d.ts.map +1 -0
  172. package/dist/reorder/model.js +162 -0
  173. package/dist/reorder/model.js.map +1 -0
  174. package/dist/richtext/node.d.ts +15 -5
  175. package/dist/richtext/node.d.ts.map +1 -1
  176. package/dist/richtext/node.js +40 -11
  177. package/dist/richtext/node.js.map +1 -1
  178. package/dist/richtext/runs.d.ts +35 -13
  179. package/dist/richtext/runs.d.ts.map +1 -1
  180. package/dist/richtext/runs.js +83 -27
  181. package/dist/richtext/runs.js.map +1 -1
  182. package/dist/tabs/index.d.ts +17 -0
  183. package/dist/tabs/index.d.ts.map +1 -1
  184. package/dist/tabs/index.js +849 -186
  185. package/dist/tabs/index.js.map +1 -1
  186. package/dist/terminal/vt/fonts.d.ts +45 -15
  187. package/dist/terminal/vt/fonts.d.ts.map +1 -1
  188. package/dist/terminal/vt/fonts.js +89 -19
  189. package/dist/terminal/vt/fonts.js.map +1 -1
  190. package/dist/terminal/vt/node.d.ts +42 -0
  191. package/dist/terminal/vt/node.d.ts.map +1 -1
  192. package/dist/terminal/vt/node.js +95 -11
  193. package/dist/terminal/vt/node.js.map +1 -1
  194. package/dist/terminal/vt/renderer.d.ts.map +1 -1
  195. package/dist/terminal/vt/renderer.js +13 -3
  196. package/dist/terminal/vt/renderer.js.map +1 -1
  197. package/package.json +14 -3
  198. package/src/charts/components.ts +14 -7
  199. package/src/charts/node.ts +81 -26
  200. package/src/charts/render.ts +16 -6
  201. package/src/code-editor/node.ts +147 -53
  202. package/src/flow/draw.ts +104 -35
  203. package/src/flow/index.ts +33 -13
  204. package/src/flow/model.ts +4 -2
  205. package/src/flow/node.ts +158 -59
  206. package/src/flow/types.ts +46 -21
  207. package/src/formula/index.ts +4 -2
  208. package/src/formula/node.ts +24 -5
  209. package/src/html/controls.ts +4 -1
  210. package/src/html/css/cascade.ts +18 -7
  211. package/src/html/css/parse.ts +46 -21
  212. package/src/html/css/style.ts +6 -1
  213. package/src/html/css/values.ts +25 -9
  214. package/src/html/index.ts +1 -0
  215. package/src/html/layout/boxes.ts +19 -9
  216. package/src/html/layout/inline.ts +23 -1
  217. package/src/html/node.ts +110 -17
  218. package/src/html/paint.ts +17 -2
  219. package/src/index.ts +102 -0
  220. package/src/maps/clip.ts +184 -0
  221. package/src/maps/gzip.ts +66 -0
  222. package/src/maps/index.ts +316 -0
  223. package/src/maps/labels.ts +442 -0
  224. package/src/maps/mvt.ts +857 -0
  225. package/src/maps/node.ts +1819 -0
  226. package/src/maps/overlay.ts +639 -0
  227. package/src/maps/paint.ts +978 -0
  228. package/src/maps/proj.ts +641 -0
  229. package/src/maps/sources.ts +425 -0
  230. package/src/maps/style.ts +309 -0
  231. package/src/maps/styles.ts +1040 -0
  232. package/src/maps/tiles.ts +676 -0
  233. package/src/maps/types.ts +260 -0
  234. package/src/markdown/ast.ts +85 -9
  235. package/src/markdown/expressions.ts +223 -0
  236. package/src/markdown/index.ts +190 -5
  237. package/src/markdown/parse.ts +230 -15
  238. package/src/markdown/spans.ts +4 -0
  239. package/src/markdown/tags.ts +240 -0
  240. package/src/reorder/hx.ts +31 -0
  241. package/src/reorder/index.ts +2223 -0
  242. package/src/reorder/model.ts +232 -0
  243. package/src/richtext/node.ts +49 -14
  244. package/src/richtext/runs.ts +115 -30
  245. package/src/tabs/index.ts +1040 -204
  246. package/src/terminal/vt/fonts.ts +128 -36
  247. package/src/terminal/vt/node.ts +105 -12
  248. package/src/terminal/vt/renderer.ts +13 -3
@@ -0,0 +1,260 @@
1
+ // The public vocabulary: what `<Map>` takes, what it hands back, and what
2
+ // its events carry.
3
+ //
4
+ // Separate from `index.ts` for the reason `src/flow/types.ts` is: the
5
+ // element, the component and the tests all name these, and a types module
6
+ // with no runtime in it can be imported by any of them without dragging the
7
+ // others in.
8
+ import type { Style } from 'react-x11/style';
9
+ import type { ReactNode, Ref } from 'react';
10
+
11
+ import type { LngLat, LngLatBounds, MapCamera, TileId } from './proj.js';
12
+ import type { MapSource } from './sources.js';
13
+ import type { MapStyle } from './style.js';
14
+ import type { MapMarker, MapOverlay } from './overlay.js';
15
+
16
+ export type { LngLat, LngLatBounds, MapCamera };
17
+ export type { MapSource, TileData, TileRequest } from './sources.js';
18
+ export type { MapMarker, MapOverlay, OverlayPalette } from './overlay.js';
19
+ export type {
20
+ MapStyle,
21
+ MapStyleLayer,
22
+ MapFilter,
23
+ FillLayer,
24
+ LineLayer,
25
+ CircleLayer,
26
+ SymbolLayer,
27
+ Zoomed,
28
+ } from './style.js';
29
+
30
+ /** Where a pointer event happened, in every space that could be wanted. */
31
+ export interface MapPointerEvent {
32
+ /** Geographic. */
33
+ lngLat: LngLat;
34
+ /** Pane-local logical pixels. */
35
+ x: number;
36
+ y: number;
37
+ /** The marker under the pointer, when there was one. */
38
+ marker: MapMarker | null;
39
+ /** Modifier state, as the synthetic event reported it. */
40
+ shiftKey: boolean;
41
+ ctrlKey: boolean;
42
+ altKey: boolean;
43
+ metaKey: boolean;
44
+ /** `1` left, `2` middle, `3` right. */
45
+ button: number;
46
+ }
47
+
48
+ /** What one frame cost — for a performance HUD, and what
49
+ * `scripts/bench/maps.ts` reads. */
50
+ export interface MapFrameStats {
51
+ /** Milliseconds spent rasterizing tiles into their surfaces this frame.
52
+ * Bounded by `rasterBudgetMs` except for the one run that crossed it. */
53
+ rasterMs: number;
54
+ /** …and compositing them, drawing the labels, overlays and markers. */
55
+ drawMs: number;
56
+ /** Tiles the cover asked for. */
57
+ tiles: number;
58
+ /** …of which were composited from their own finished surface. Under
59
+ * `progressive` this also counts the half-drawn ones. */
60
+ ready: number;
61
+ /** …and how many were drawn from a coarser ancestor instead, scaled up.
62
+ * What covers a hole while zooming *in*. */
63
+ fromAncestor: number;
64
+ /** …and how many from their finer descendants, scaled down. What covers
65
+ * a hole while zooming *out*, where there is no ancestor to borrow. */
66
+ fromDescendant: number;
67
+ /** Tiles still to rasterize — non-zero means the map is still sharpening
68
+ * and another frame is already scheduled. */
69
+ pending: number;
70
+ /** Labels placed, and labels drawn. */
71
+ labels: number;
72
+ /**
73
+ * Tiles in the cover whose load failed and are waiting on a retry.
74
+ *
75
+ * Here because a map whose tiles all fail looks exactly like a map that
76
+ * is still loading — an empty background and nothing else — and the
77
+ * difference is not something a user can see.
78
+ */
79
+ errors: number;
80
+ /** Bytes of rendered surfaces the cache is holding. */
81
+ surfaceBytes: number;
82
+ /**
83
+ * What this pass repainted, in device pixels, or `null` for a full one.
84
+ *
85
+ * The number to watch when a map looks busy: a frame that is only
86
+ * continuing a rasterization should claim almost nothing, because the
87
+ * tile being drawn is a second surface nobody is looking at. A run of
88
+ * full-pane damage while tiles are still landing means something is
89
+ * asking for repaints it does not need — which on a backend that paints
90
+ * many frames a second reads as flashing.
91
+ */
92
+ damage: { x: number; y: number; width: number; height: number } | null;
93
+ /** What the rasterizer did, summed over the tiles drawn this frame. */
94
+ draw: {
95
+ features: number;
96
+ vertices: number;
97
+ decimated: number;
98
+ culled: number;
99
+ batches: number;
100
+ };
101
+ }
102
+
103
+ /** Options for framing a box. */
104
+ export interface FitBoundsOptions {
105
+ /** Logical pixels kept clear on every side. 24 by default. */
106
+ padding?: number;
107
+ maxZoom?: number;
108
+ }
109
+
110
+ /** The imperative surface, through `ref`. */
111
+ export interface MapHandle {
112
+ getCamera(): MapCamera;
113
+ setCamera(camera: Partial<MapCamera>): void;
114
+ panBy(dx: number, dy: number): void;
115
+ zoomIn(step?: number): void;
116
+ zoomOut(step?: number): void;
117
+ zoomTo(zoom: number): void;
118
+ /** Frame a box. */
119
+ fitBounds(bounds: LngLatBounds, options?: FitBoundsOptions): void;
120
+ /** Frame every marker, or the ones named. */
121
+ fitMarkers(ids?: readonly string[], options?: FitBoundsOptions): void;
122
+ /** What the pane can see. `west > east` across the antimeridian. */
123
+ getBounds(): LngLatBounds;
124
+ /** Geography to pane-local logical pixels, and back. */
125
+ project(position: LngLat): { x: number; y: number };
126
+ unproject(x: number, y: number): LngLat;
127
+ /** The marker at a pane-local point, or null. */
128
+ markerAt(x: number, y: number): MapMarker | null;
129
+ /** Throw away every rendered tile — after a style edit an application
130
+ * made in place, which the map cannot see. */
131
+ refresh(): void;
132
+ /** What the last frame cost. */
133
+ stats(): MapFrameStats | null;
134
+ }
135
+
136
+ /** What the `<mapview>` element takes. `<Map>` adds the uncontrolled
137
+ * camera and the box around it. */
138
+ export interface MapViewProps {
139
+ /**
140
+ * Where tiles come from, drawn in order — a basemap, then an overlay
141
+ * pyramid over it. Empty draws the style's background and nothing else,
142
+ * which is what a map with only markers on it wants.
143
+ */
144
+ sources?: readonly MapSource[];
145
+ /** How to draw them. Defaults to {@link shortbreadStyle} in the theme's
146
+ * light or dark palette. */
147
+ mapStyle?: MapStyle;
148
+ /**
149
+ * The camera, controlled. Leave it out and the **element** owns it —
150
+ * which is not just a convenience: an element-owned camera means a pan is
151
+ * a blit and a claim, with no React render in the loop at all.
152
+ */
153
+ camera?: MapCamera;
154
+ /** Where an element-owned camera starts. Read once, at construction. */
155
+ defaultCamera?: MapCamera;
156
+ onCameraChange?: (camera: MapCamera) => void;
157
+ /** Fired when a gesture ends and the camera has settled — the moment to
158
+ * fetch what is now on screen. */
159
+ onMoveEnd?: (camera: MapCamera) => void;
160
+ minZoom?: number;
161
+ maxZoom?: number;
162
+ markers?: readonly MapMarker[];
163
+ overlays?: readonly MapOverlay[];
164
+ onMapClick?: (event: MapPointerEvent) => void;
165
+ onMarkerClick?: (marker: MapMarker, event: MapPointerEvent) => void;
166
+ /**
167
+ * The marker under the pointer, or null when it left every marker.
168
+ *
169
+ * `event` is null for the leave that comes from the pointer leaving the
170
+ * map altogether: there is no position on the map to report, and
171
+ * inventing one would be worse than saying so.
172
+ */
173
+ onMarkerHover?: (
174
+ marker: MapMarker | null,
175
+ event: MapPointerEvent | null,
176
+ ) => void;
177
+ /** `false` freezes the camera: no drag, no wheel, no keys. The map still
178
+ * draws and still reports clicks. */
179
+ interactive?: boolean;
180
+ /**
181
+ * Show a tile as it is drawn rather than when it is finished.
182
+ *
183
+ * `false` by default, which is what every other map client does: a tile
184
+ * appears whole, and until it does the coarser one already in the cache
185
+ * is scaled up in its place. `true` composites a tile's surface as soon
186
+ * as it exists — so a dense tile arrives as water, then landuse, then
187
+ * road casings, then roads, across a dozen frames — which is honest about
188
+ * what the renderer is doing and does not look like a map.
189
+ *
190
+ * The one thing the default costs is a transition that invalidates every
191
+ * surface at once: a `mapStyle` change, `refresh()`, or a display-scale
192
+ * change leaves no finished tile *and* no finished ancestor, so the map
193
+ * drops to its background colour until the new tiles land. `true` shows
194
+ * it repainting instead.
195
+ */
196
+ progressive?: boolean;
197
+ /**
198
+ * Milliseconds a frame may spend rasterizing tiles. 8 by default, which
199
+ * leaves the rest of a 60 Hz frame for everything else; a tile that takes
200
+ * longer than the budget is finished over the frames after it. `0`
201
+ * suspends rasterization, which is what a gesture does on its own.
202
+ */
203
+ rasterBudgetMs?: number;
204
+ /**
205
+ * Device pixels per logical pixel for the tile surfaces, if not the
206
+ * display's.
207
+ *
208
+ * Lowering it to 1 on a retina panel makes rasterization about 1.6×
209
+ * quicker and the basemap correspondingly softer; labels, markers and
210
+ * overlays are unaffected, since none of them goes through a tile
211
+ * surface. Worth having on a slow machine, and worth knowing about before
212
+ * reaching for it.
213
+ */
214
+ rasterScale?: number;
215
+ /** Bytes of rendered tile surfaces to keep. 128 MB by default. */
216
+ surfaceBudget?: number;
217
+ /**
218
+ * Vertices after which the rasterizer flushes its path. 12,000 by
219
+ * default.
220
+ *
221
+ * A real trade on X11, where a fill becomes one coverage-mask upload and
222
+ * a bigger path is fewer of them over the same pixels. It used to be a
223
+ * per-backend trade — `CGContextStrokePath` was quadratic in the number
224
+ * of subpaths, so the Cocoa backend wanted the opposite value and this
225
+ * element probed for it — and react-x11 2.6.1 chunks that stroke inside
226
+ * the backend instead (react-x11#457). Set it only with a profile in
227
+ * hand.
228
+ */
229
+ batchVertices?: number;
230
+ /** What the licence requires, drawn in the corner. Taken from the
231
+ * sources when they carry one; `''` removes it, which is the
232
+ * application saying it has put the attribution somewhere else. */
233
+ attribution?: string;
234
+ /** Called once per painted frame with what it cost. */
235
+ onFrame?: (stats: MapFrameStats) => void;
236
+ /**
237
+ * Called once per failed tile load, with whatever the source threw.
238
+ *
239
+ * Nothing is drawn for a failed tile, so without this a source that is
240
+ * misconfigured, rate-limited or down is indistinguishable from one that
241
+ * is slow. The tile is retried on a backoff (0.5 s doubling to 30 s), so
242
+ * this fires again for each retry rather than once and forever.
243
+ */
244
+ onTileError?: (error: unknown, tile: TileId & { sourceId: string }) => void;
245
+ style?: Style;
246
+ role?: string;
247
+ 'aria-label'?: string;
248
+ 'data-testname'?: string;
249
+ ref?: Ref<unknown>;
250
+ }
251
+
252
+ /** What `<Map>` takes. */
253
+ export interface MapProps extends Omit<MapViewProps, 'ref'> {
254
+ ref?: Ref<MapHandle>;
255
+ /** Anything absolutely positioned over the map — a legend, a control
256
+ * panel. Laid out as siblings of the drawn pane rather than inside it,
257
+ * because a registered element paints its children *before* its own
258
+ * drawing and anything inside would be painted over. */
259
+ children?: ReactNode;
260
+ }
@@ -1,9 +1,10 @@
1
1
  // The markdown AST `parse.ts` produces and `index.ts` renders. Deliberately
2
2
  // small: it is the GFM constructs this component draws, not a general
3
3
  // mdast — but it is shaped so that growing it stays additive. In particular
4
- // `component` is reserved for the MDX direction: a parser extension can emit
5
- // one without the renderer, the selection model or the caches changing shape
6
- // (see "MDX" in index.ts).
4
+ // `component` is the MDX direction: `ComponentBlock` is parsed and rendered
5
+ // (docs/prd-mdx.md, M1), and `ComponentInline` is still reserved see the
6
+ // note on it for why the inline half needs machinery `<richtext>` does not
7
+ // have yet.
7
8
 
8
9
  /** Inline content, inside a paragraph, heading, list item or table cell. */
9
10
  export type InlineNode =
@@ -12,7 +13,19 @@ export type InlineNode =
12
13
  | EmphasisInline
13
14
  | LinkInline
14
15
  | BreakInline
15
- | ComponentInline;
16
+ | ComponentInline
17
+ | ExpressionInline;
18
+
19
+ /**
20
+ * `{count}` in the middle of a sentence: unevaluated source, which the
21
+ * renderer compiles against its `scope` and turns into text. Only emitted
22
+ * when `ParseOptions.expressions` says so, so `{braces}` in an ordinary
23
+ * document remain the characters they have always been.
24
+ */
25
+ export interface ExpressionInline {
26
+ type: 'expression';
27
+ src: string;
28
+ }
16
29
 
17
30
  /** A run of plain text. Softbreaks arrive collapsed to spaces. */
18
31
  export interface TextInline {
@@ -51,14 +64,41 @@ export interface BreakInline {
51
64
  }
52
65
 
53
66
  /**
54
- * Reserved for MDX: `<Chart data={…} />` would parse to one of these. The
55
- * current parser never emits it; the renderer maps unknown names to plain
56
- * text so a future parser upgrade cannot crash an older renderer.
67
+ * An attribute's value. `literal` is what an attribute string or a `{json}`
68
+ * attribute resolves to at parse time; `expression` holds unevaluated source
69
+ * for the rung that compiles it, so that parsing stays pure and one AST can
70
+ * be rendered against two scopes (docs/prd-mdx.md, M2). The parser emits
71
+ * only `literal` today.
72
+ */
73
+ export type AttributeValue =
74
+ { kind: 'literal'; value: unknown } | { kind: 'expression'; src: string };
75
+
76
+ /**
77
+ * A spread (`{...props}`) is an attribute with no name, so it is keyed
78
+ * `...0`, `...1`, … in the order it appeared. An attribute name cannot start
79
+ * with `.`, so these cannot collide with one; a renderer walking
80
+ * `Object.entries` in insertion order applies each spread where it was
81
+ * written, which is what decides whether it overrides `height` or `height`
82
+ * overrides it.
83
+ */
84
+ export const SPREAD_PREFIX = '...';
85
+
86
+ /**
87
+ * **Still reserved.** `<Badge/>` in the middle of a sentence parses to one of
88
+ * these — except that nothing emits them yet, because nothing can render
89
+ * them: a paragraph is laid out as a single `<richtext>`, and a `TextRun`
90
+ * has no way to say "reserve this much advance width for an element someone
91
+ * else paints". Giving it one is the inline half of MDX and is its own piece
92
+ * of work (baseline alignment, wrapping around the element, and what a
93
+ * selection dragged across it should copy).
94
+ *
95
+ * Kept in the union so the shape stays stable, and so a renderer written
96
+ * against it today cannot be broken by the parser learning to emit them.
57
97
  */
58
98
  export interface ComponentInline {
59
99
  type: 'component';
60
100
  name: string;
61
- attributes: Record<string, string>;
101
+ attributes: Record<string, AttributeValue>;
62
102
  children: InlineNode[];
63
103
  }
64
104
 
@@ -70,7 +110,22 @@ export type BlockNode =
70
110
  | QuoteBlock
71
111
  | ListBlock
72
112
  | TableBlock
73
- | RuleBlock;
113
+ | RuleBlock
114
+ | ComponentBlock;
115
+
116
+ /**
117
+ * A component standing where a paragraph would: `<Chart data={…} />` on its
118
+ * own line, or `<Callout>` … `</Callout>` around blocks.
119
+ *
120
+ * Only emitted when `ParseOptions.isComponent` says the name is one — which
121
+ * is what keeps every document that renders today rendering the same way.
122
+ */
123
+ export interface ComponentBlock {
124
+ type: 'component';
125
+ name: string;
126
+ attributes: Record<string, AttributeValue>;
127
+ children: BlockNode[];
128
+ }
74
129
 
75
130
  export interface ParagraphBlock {
76
131
  type: 'paragraph';
@@ -160,4 +215,25 @@ export interface ParseOptions {
160
215
  * underline or a rule) is held back. Pass false for text that is final.
161
216
  */
162
217
  partial?: boolean;
218
+ /**
219
+ * Whether `name` is a component this document may use. Absent — the
220
+ * default — means no tag is a component and `<Chart/>` stays the literal
221
+ * text it has always been, so turning MDX on is opt-in and turning it off
222
+ * is byte-for-byte the old behaviour.
223
+ *
224
+ * `<Markdown>` supplies one from its `components` prop. A dotted name
225
+ * (`Card.Header`) is passed through whole; resolving it is the caller's.
226
+ */
227
+ isComponent?: (name: string) => boolean;
228
+ /**
229
+ * Whether `{…}` may hold a JavaScript expression — in an attribute, as a
230
+ * `{...spread}`, or bare in the prose. Off by default, and off is the
231
+ * whole of rung 1: a `{…}` attribute must be JSON, and a brace in a
232
+ * paragraph is a brace.
233
+ *
234
+ * `<Markdown>` turns this on when it is given a `scope`, which is the prop
235
+ * that says the application accepts a document running code. Parsing still
236
+ * evaluates nothing — an expression is kept as source.
237
+ */
238
+ expressions?: boolean;
163
239
  }
@@ -0,0 +1,223 @@
1
+ // Compiling `{…}` — the rung where a document runs code (docs/prd-mdx.md,
2
+ // M2), and the one place in this component that calls `new Function`.
3
+ //
4
+ // It is a small file on purpose. Everything hard about MDX expressions was
5
+ // decided elsewhere: the parser holds an expression as *source* rather than
6
+ // a value, so a parse is pure and one AST can be rendered against two
7
+ // scopes; and `<Markdown>` only turns any of this on when the application
8
+ // passes a `scope`, which is the prop that means "I accept that this
9
+ // document may compute". What is left here is compiling the source once, and
10
+ // not letting a half-typed expression take down a document that is still
11
+ // being typed.
12
+ //
13
+ // **There is no sandbox.** `new Function` runs in this process with this
14
+ // process's authority. That is why the gate is a prop, and why the docs say
15
+ // in as few words as they can: do not pass `scope` alongside a document you
16
+ // did not write.
17
+
18
+ import type { BlockNode, Document, InlineNode } from './ast.js';
19
+
20
+ /** A compiled expression, or null if it would not compile at all. */
21
+ type Compiled = ((...args: unknown[]) => unknown) | null;
22
+
23
+ /**
24
+ * Keyed on the scope's shape *and* the source, because a compiled function
25
+ * closes over its parameter names. Bounded the way `richtext/node.ts` bounds
26
+ * its layout cache: a streaming document recompiles its tail on every chunk,
27
+ * and an unbounded map would hold every intermediate spelling of an
28
+ * expression that was being typed.
29
+ */
30
+ const CACHE = new Map<string, Compiled>();
31
+ const LIMIT = 256;
32
+
33
+ let warned = false;
34
+
35
+ /**
36
+ * Say so, once, when an expression does not work out. A document that
37
+ * silently loses a value should not be a mystery — and saying it on every
38
+ * frame of a streaming render would be worse than saying nothing.
39
+ *
40
+ * `process` and `console` come off `globalThis` because `src/` compiles with
41
+ * `types: []` — a Node global that wandered in would fail the build rather
42
+ * than become an implicit `@types/node` dependency.
43
+ */
44
+ function warnFailed(src: string, error: unknown): void {
45
+ if (warned) return;
46
+ warned = true;
47
+ const g = globalThis as {
48
+ process?: { env?: Record<string, string | undefined> };
49
+ console?: { warn(message: string, ...rest: unknown[]): void };
50
+ };
51
+ if (g.process?.env?.NODE_ENV === 'production') return;
52
+ g.console?.warn(
53
+ '@react-x11/components: a markdown expression did not evaluate, so it ' +
54
+ `rendered as nothing: {${src}}`,
55
+ error,
56
+ );
57
+ }
58
+
59
+ function compile(src: string, keys: readonly string[]): Compiled {
60
+ const key = `${keys.join(',')} ${src}`;
61
+ const hit = CACHE.get(key);
62
+ if (hit !== undefined) return hit;
63
+ let compiled: Compiled = null;
64
+ try {
65
+ // `return (…)` rather than a bare body: an expression is what the braces
66
+ // promised, and the parentheses keep an object literal an object literal
67
+ // rather than a block.
68
+ compiled = new Function(...keys, `"use strict"; return (${src});`) as (
69
+ ...args: unknown[]
70
+ ) => unknown;
71
+ } catch (error) {
72
+ // A syntax error is the ordinary state of an expression being typed.
73
+ warnFailed(src, error);
74
+ }
75
+ if (CACHE.size >= LIMIT) CACHE.clear();
76
+ CACHE.set(key, compiled);
77
+ return compiled;
78
+ }
79
+
80
+ /**
81
+ * What a renderer calls: source in, value out, `undefined` if it did not
82
+ * work. Never throws — a document being edited is full of expressions that
83
+ * do not work yet.
84
+ */
85
+ export type Evaluate = (src: string) => unknown;
86
+
87
+ /**
88
+ * An evaluator over `scope`. The keys are read once, so a scope mutated in
89
+ * place after this is called keeps its old shape — the same contract every
90
+ * other seam here has, and the reason the prop doc asks for a stable object.
91
+ */
92
+ export function evaluator(scope: Record<string, unknown>): Evaluate {
93
+ const keys = Object.keys(scope);
94
+ const values = keys.map((k) => scope[k]);
95
+ return (src) => {
96
+ const fn = compile(src, keys);
97
+ if (!fn) return undefined;
98
+ try {
99
+ return fn(...values);
100
+ } catch (error) {
101
+ warnFailed(src, error);
102
+ return undefined;
103
+ }
104
+ };
105
+ }
106
+
107
+ /**
108
+ * Only the tests need to reach in: a compile cache that survived between
109
+ * them would make one test's expression another's, and the warn-once flag
110
+ * would hide the second failure anyone asserted on.
111
+ */
112
+ export function clearExpressionCache(): void {
113
+ CACHE.clear();
114
+ warned = false;
115
+ }
116
+
117
+ // --- resolving a parsed document ------------------------------------------
118
+
119
+ /**
120
+ * A copy of `nodes` with every `expression` replaced by the text it
121
+ * evaluates to. Primitives stringify; anything else — an object, an array, a
122
+ * React element — renders as nothing, because a paragraph is text and there
123
+ * is nowhere in a text run to put an element (see "The inline half" in
124
+ * docs/prd-mdx.md). `null` and `undefined` are nothing on purpose, so
125
+ * `{maybe}` reads as absent rather than as the word "undefined".
126
+ */
127
+ function resolveInline(nodes: InlineNode[], evaluate: Evaluate): InlineNode[] {
128
+ let changed = false;
129
+ const out: InlineNode[] = [];
130
+ for (const node of nodes) {
131
+ if (node.type === 'expression') {
132
+ changed = true;
133
+ const value = evaluate(node.src);
134
+ const text =
135
+ value == null ||
136
+ typeof value === 'object' ||
137
+ typeof value === 'function'
138
+ ? ''
139
+ : String(value);
140
+ if (text) out.push({ type: 'text', text });
141
+ continue;
142
+ }
143
+ if ('children' in node) {
144
+ const children = resolveInline(node.children, evaluate);
145
+ if (children !== node.children) {
146
+ changed = true;
147
+ out.push({ ...node, children });
148
+ continue;
149
+ }
150
+ }
151
+ out.push(node);
152
+ }
153
+ return changed ? out : nodes;
154
+ }
155
+
156
+ function resolveBlocks(blocks: BlockNode[], evaluate: Evaluate): BlockNode[] {
157
+ let changed = false;
158
+ const out = blocks.map((block): BlockNode => {
159
+ switch (block.type) {
160
+ case 'paragraph':
161
+ case 'heading': {
162
+ const children = resolveInline(block.children, evaluate);
163
+ if (children === block.children) return block;
164
+ changed = true;
165
+ return { ...block, children };
166
+ }
167
+ case 'quote':
168
+ case 'component': {
169
+ const children = resolveBlocks(block.children, evaluate);
170
+ if (children === block.children) return block;
171
+ changed = true;
172
+ return { ...block, children };
173
+ }
174
+ case 'list': {
175
+ let itemsChanged = false;
176
+ const items = block.items.map((item) => {
177
+ const children = resolveBlocks(item.children, evaluate);
178
+ if (children === item.children) return item;
179
+ itemsChanged = true;
180
+ return { ...item, children };
181
+ });
182
+ if (!itemsChanged) return block;
183
+ changed = true;
184
+ return { ...block, items };
185
+ }
186
+ case 'table': {
187
+ let rowsChanged = false;
188
+ const rows = block.rows.map((row) =>
189
+ row.map((cell) => {
190
+ const next = resolveInline(cell, evaluate);
191
+ if (next !== cell) rowsChanged = true;
192
+ return next;
193
+ }),
194
+ );
195
+ const header = block.header.map((cell) => {
196
+ const next = resolveInline(cell, evaluate);
197
+ if (next !== cell) rowsChanged = true;
198
+ return next;
199
+ });
200
+ if (!rowsChanged) return block;
201
+ changed = true;
202
+ return { ...block, header, rows };
203
+ }
204
+ default:
205
+ return block;
206
+ }
207
+ });
208
+ return changed ? out : blocks;
209
+ }
210
+
211
+ /**
212
+ * The document with its expressions evaluated. `raws` is carried over
213
+ * untouched: it is the block cache's key, and what changed is the values an
214
+ * expression produced, not the text that produced them — a change of `scope`
215
+ * invalidates that cache through its own identity instead.
216
+ */
217
+ export function resolveExpressions(
218
+ doc: Document,
219
+ evaluate: Evaluate,
220
+ ): Document {
221
+ const blocks = resolveBlocks(doc.blocks, evaluate);
222
+ return blocks === doc.blocks ? doc : { blocks, raws: doc.raws };
223
+ }