@pascal-app/viewer 1.1.0 → 1.1.2

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 (224) hide show
  1. package/README.md +157 -0
  2. package/dist/components/viewer/drone-controller.d.ts.map +1 -1
  3. package/dist/components/viewer/drone-controller.js +1 -5
  4. package/dist/components/viewer/first-person/build-collider-world.d.ts +14 -0
  5. package/dist/components/viewer/first-person/build-collider-world.d.ts.map +1 -0
  6. package/dist/components/viewer/first-person/build-collider-world.js +426 -0
  7. package/dist/components/viewer/first-person/ground-holes.d.ts +22 -0
  8. package/dist/components/viewer/first-person/ground-holes.d.ts.map +1 -0
  9. package/dist/components/viewer/first-person/ground-holes.js +116 -0
  10. package/dist/components/viewer/first-person/mechanism-targets.d.ts +27 -0
  11. package/dist/components/viewer/first-person/mechanism-targets.d.ts.map +1 -0
  12. package/dist/components/viewer/first-person/mechanism-targets.js +79 -0
  13. package/dist/components/viewer/first-person/terrain-collider.d.ts +32 -0
  14. package/dist/components/viewer/first-person/terrain-collider.d.ts.map +1 -0
  15. package/dist/components/viewer/first-person/terrain-collider.js +175 -0
  16. package/dist/components/viewer/glb-walkthrough-controller.d.ts +1 -1
  17. package/dist/components/viewer/glb-walkthrough-controller.d.ts.map +1 -1
  18. package/dist/components/viewer/glb-walkthrough-controller.js +3 -4
  19. package/dist/components/viewer/live-walkthrough-controller.d.ts +17 -0
  20. package/dist/components/viewer/live-walkthrough-controller.d.ts.map +1 -0
  21. package/dist/components/viewer/live-walkthrough-controller.js +1449 -0
  22. package/dist/components/viewer/post-processing.d.ts.map +1 -1
  23. package/dist/components/viewer/post-processing.js +22 -1
  24. package/dist/index.d.ts +3 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +3 -0
  27. package/dist/lib/daylight/room-map.d.ts +29 -4
  28. package/dist/lib/daylight/room-map.d.ts.map +1 -1
  29. package/dist/lib/daylight/room-map.js +219 -36
  30. package/dist/lib/daylight/room-scene.d.ts.map +1 -1
  31. package/dist/lib/daylight/room-scene.js +9 -3
  32. package/dist/lib/daylight/rooms.d.ts +15 -6
  33. package/dist/lib/daylight/rooms.d.ts.map +1 -1
  34. package/dist/lib/daylight/rooms.js +49 -9
  35. package/dist/lib/measurement-presentation.d.ts +2 -0
  36. package/dist/lib/measurement-presentation.d.ts.map +1 -1
  37. package/dist/lib/measurement-presentation.js +6 -0
  38. package/dist/lib/typing-target.d.ts +2 -0
  39. package/dist/lib/typing-target.d.ts.map +1 -0
  40. package/dist/lib/typing-target.js +7 -0
  41. package/dist/lib/viewer-config.d.ts +6 -0
  42. package/dist/lib/viewer-config.d.ts.map +1 -1
  43. package/dist/lib/viewer-config.js +2 -0
  44. package/dist/sfx/audio-settings.d.ts +81 -0
  45. package/dist/sfx/audio-settings.d.ts.map +1 -0
  46. package/dist/sfx/audio-settings.js +166 -0
  47. package/dist/sfx/index.d.ts +6 -0
  48. package/dist/sfx/index.d.ts.map +1 -0
  49. package/dist/sfx/index.js +5 -0
  50. package/dist/sfx/interaction-sounds.d.ts +7 -0
  51. package/dist/sfx/interaction-sounds.d.ts.map +1 -0
  52. package/dist/sfx/interaction-sounds.js +39 -0
  53. package/dist/sfx/player.d.ts +31 -0
  54. package/dist/sfx/player.d.ts.map +1 -0
  55. package/dist/sfx/player.js +255 -0
  56. package/dist/sfx/sounds.d.ts +60 -0
  57. package/dist/sfx/sounds.d.ts.map +1 -0
  58. package/dist/sfx/sounds.js +25 -0
  59. package/dist/sfx/use-viewer-ui-sounds.d.ts +8 -0
  60. package/dist/sfx/use-viewer-ui-sounds.d.ts.map +1 -0
  61. package/dist/sfx/use-viewer-ui-sounds.js +42 -0
  62. package/dist/store/use-first-person-hud.d.ts +13 -0
  63. package/dist/store/use-first-person-hud.d.ts.map +1 -0
  64. package/dist/store/use-first-person-hud.js +21 -0
  65. package/dist/systems/roof/roof-system.d.ts.map +1 -1
  66. package/dist/systems/roof/roof-system.js +27 -13
  67. package/dist/ui/components/action-button.d.ts +14 -0
  68. package/dist/ui/components/action-button.d.ts.map +1 -0
  69. package/dist/ui/components/action-button.js +21 -0
  70. package/dist/ui/components/action-menu-placement.d.ts +7 -0
  71. package/dist/ui/components/action-menu-placement.d.ts.map +1 -0
  72. package/dist/ui/components/action-menu-placement.js +10 -0
  73. package/dist/ui/components/floorplan-compass-button.d.ts +10 -0
  74. package/dist/ui/components/floorplan-compass-button.d.ts.map +1 -0
  75. package/dist/ui/components/floorplan-compass-button.js +22 -0
  76. package/dist/ui/components/follow-pascal-toggle.d.ts +6 -0
  77. package/dist/ui/components/follow-pascal-toggle.d.ts.map +1 -0
  78. package/dist/ui/components/follow-pascal-toggle.js +21 -0
  79. package/dist/ui/components/localized-html.d.ts +9 -0
  80. package/dist/ui/components/localized-html.d.ts.map +1 -0
  81. package/dist/ui/components/localized-html.js +16 -0
  82. package/dist/ui/components/render-settings-menu.d.ts +9 -0
  83. package/dist/ui/components/render-settings-menu.d.ts.map +1 -0
  84. package/dist/ui/components/render-settings-menu.js +145 -0
  85. package/dist/ui/components/rooms/room-highlight.d.ts +10 -0
  86. package/dist/ui/components/rooms/room-highlight.d.ts.map +1 -0
  87. package/dist/ui/components/rooms/room-highlight.js +56 -0
  88. package/dist/ui/components/rooms/room-labels.d.ts +278 -0
  89. package/dist/ui/components/rooms/room-labels.d.ts.map +1 -0
  90. package/dist/ui/components/rooms/room-labels.js +1295 -0
  91. package/dist/ui/components/rooms/viewer-rooms.d.ts +14 -0
  92. package/dist/ui/components/rooms/viewer-rooms.d.ts.map +1 -0
  93. package/dist/ui/components/rooms/viewer-rooms.js +123 -0
  94. package/dist/ui/components/scene-loader.d.ts +18 -0
  95. package/dist/ui/components/scene-loader.d.ts.map +1 -0
  96. package/dist/ui/components/scene-loader.js +29 -0
  97. package/dist/ui/components/sky-control.d.ts +37 -0
  98. package/dist/ui/components/sky-control.d.ts.map +1 -0
  99. package/dist/ui/components/sky-control.js +382 -0
  100. package/dist/ui/components/use-viewer-camera-navigation-sync.d.ts +4 -0
  101. package/dist/ui/components/use-viewer-camera-navigation-sync.d.ts.map +1 -0
  102. package/dist/ui/components/use-viewer-camera-navigation-sync.js +117 -0
  103. package/dist/ui/components/viewer-controls-bar.d.ts +20 -0
  104. package/dist/ui/components/viewer-controls-bar.d.ts.map +1 -0
  105. package/dist/ui/components/viewer-controls-bar.js +130 -0
  106. package/dist/ui/components/viewer-hover-label.d.ts +14 -0
  107. package/dist/ui/components/viewer-hover-label.d.ts.map +1 -0
  108. package/dist/ui/components/viewer-hover-label.js +28 -0
  109. package/dist/ui/components/viewer-scene-header.d.ts +34 -0
  110. package/dist/ui/components/viewer-scene-header.d.ts.map +1 -0
  111. package/dist/ui/components/viewer-scene-header.js +92 -0
  112. package/dist/ui/components/viewer-selection-manager.d.ts +7 -0
  113. package/dist/ui/components/viewer-selection-manager.d.ts.map +1 -0
  114. package/dist/ui/components/viewer-selection-manager.js +116 -0
  115. package/dist/ui/components/viewer-stage-modes.d.ts +7 -0
  116. package/dist/ui/components/viewer-stage-modes.d.ts.map +1 -0
  117. package/dist/ui/components/viewer-stage-modes.js +42 -0
  118. package/dist/ui/components/viewer-stage-switcher.d.ts +10 -0
  119. package/dist/ui/components/viewer-stage-switcher.d.ts.map +1 -0
  120. package/dist/ui/components/viewer-stage-switcher.js +16 -0
  121. package/dist/ui/components/viewer-stage.d.ts +25 -0
  122. package/dist/ui/components/viewer-stage.d.ts.map +1 -0
  123. package/dist/ui/components/viewer-stage.js +107 -0
  124. package/dist/ui/components/walkthrough-hud.d.ts +29 -0
  125. package/dist/ui/components/walkthrough-hud.d.ts.map +1 -0
  126. package/dist/ui/components/walkthrough-hud.js +55 -0
  127. package/dist/ui/floorplan/floorplan-camera-sync.d.ts +22 -0
  128. package/dist/ui/floorplan/floorplan-camera-sync.d.ts.map +1 -0
  129. package/dist/ui/floorplan/floorplan-camera-sync.js +157 -0
  130. package/dist/ui/floorplan/floorplan-dimension-renderer.d.ts +61 -0
  131. package/dist/ui/floorplan/floorplan-dimension-renderer.d.ts.map +1 -0
  132. package/dist/ui/floorplan/floorplan-dimension-renderer.js +280 -0
  133. package/dist/ui/floorplan/floorplan-geometry-renderer.d.ts +54 -0
  134. package/dist/ui/floorplan/floorplan-geometry-renderer.d.ts.map +1 -0
  135. package/dist/ui/floorplan/floorplan-geometry-renderer.js +317 -0
  136. package/dist/ui/floorplan/floorplan-label-angle.d.ts +24 -0
  137. package/dist/ui/floorplan/floorplan-label-angle.d.ts.map +1 -0
  138. package/dist/ui/floorplan/floorplan-label-angle.js +60 -0
  139. package/dist/ui/floorplan/floorplan-navigation-sync.d.ts +18 -0
  140. package/dist/ui/floorplan/floorplan-navigation-sync.d.ts.map +1 -0
  141. package/dist/ui/floorplan/floorplan-navigation-sync.js +41 -0
  142. package/dist/ui/floorplan/floorplan-preview-geometry.d.ts +26 -0
  143. package/dist/ui/floorplan/floorplan-preview-geometry.d.ts.map +1 -0
  144. package/dist/ui/floorplan/floorplan-preview-geometry.js +173 -0
  145. package/dist/ui/floorplan/floorplan-preview-navigation.d.ts +22 -0
  146. package/dist/ui/floorplan/floorplan-preview-navigation.d.ts.map +1 -0
  147. package/dist/ui/floorplan/floorplan-preview-navigation.js +41 -0
  148. package/dist/ui/floorplan/floorplan-preview-visibility.d.ts +8 -0
  149. package/dist/ui/floorplan/floorplan-preview-visibility.d.ts.map +1 -0
  150. package/dist/ui/floorplan/floorplan-preview-visibility.js +21 -0
  151. package/dist/ui/floorplan/floorplan-preview.d.ts +19 -0
  152. package/dist/ui/floorplan/floorplan-preview.d.ts.map +1 -0
  153. package/dist/ui/floorplan/floorplan-preview.js +700 -0
  154. package/dist/ui/hooks/use-mobile.d.ts +2 -0
  155. package/dist/ui/hooks/use-mobile.d.ts.map +1 -0
  156. package/dist/ui/hooks/use-mobile.js +14 -0
  157. package/dist/ui/index.d.ts +49 -0
  158. package/dist/ui/index.d.ts.map +1 -0
  159. package/dist/ui/index.js +48 -0
  160. package/dist/ui/lib/below-grade.d.ts +38 -0
  161. package/dist/ui/lib/below-grade.d.ts.map +1 -0
  162. package/dist/ui/lib/below-grade.js +102 -0
  163. package/dist/ui/lib/camera-pose.d.ts +19 -0
  164. package/dist/ui/lib/camera-pose.d.ts.map +1 -0
  165. package/dist/ui/lib/camera-pose.js +113 -0
  166. package/dist/ui/lib/cn.d.ts +3 -0
  167. package/dist/ui/lib/cn.d.ts.map +1 -0
  168. package/dist/ui/lib/cn.js +5 -0
  169. package/dist/ui/lib/level-name.d.ts +10 -0
  170. package/dist/ui/lib/level-name.d.ts.map +1 -0
  171. package/dist/ui/lib/level-name.js +33 -0
  172. package/dist/ui/lib/node-display-label.d.ts +8 -0
  173. package/dist/ui/lib/node-display-label.d.ts.map +1 -0
  174. package/dist/ui/lib/node-display-label.js +36 -0
  175. package/dist/ui/lib/room-area.d.ts +3 -0
  176. package/dist/ui/lib/room-area.d.ts.map +1 -0
  177. package/dist/ui/lib/room-area.js +8 -0
  178. package/dist/ui/lib/room-assembly-overlay.d.ts +46 -0
  179. package/dist/ui/lib/room-assembly-overlay.d.ts.map +1 -0
  180. package/dist/ui/lib/room-assembly-overlay.js +222 -0
  181. package/dist/ui/lib/room-name-messages.d.ts +28 -0
  182. package/dist/ui/lib/room-name-messages.d.ts.map +1 -0
  183. package/dist/ui/lib/room-name-messages.js +112 -0
  184. package/dist/ui/lib/room-selection.d.ts +58 -0
  185. package/dist/ui/lib/room-selection.d.ts.map +1 -0
  186. package/dist/ui/lib/room-selection.js +297 -0
  187. package/dist/ui/lib/scene-theme-name.d.ts +4 -0
  188. package/dist/ui/lib/scene-theme-name.d.ts.map +1 -0
  189. package/dist/ui/lib/scene-theme-name.js +22 -0
  190. package/dist/ui/lib/selection-modifiers.d.ts +20 -0
  191. package/dist/ui/lib/selection-modifiers.d.ts.map +1 -0
  192. package/dist/ui/lib/selection-modifiers.js +15 -0
  193. package/dist/ui/lib/viewer-selection.d.ts +132 -0
  194. package/dist/ui/lib/viewer-selection.d.ts.map +1 -0
  195. package/dist/ui/lib/viewer-selection.js +462 -0
  196. package/dist/ui/lib/zone-content.d.ts +6 -0
  197. package/dist/ui/lib/zone-content.d.ts.map +1 -0
  198. package/dist/ui/lib/zone-content.js +96 -0
  199. package/dist/ui/primitives/button.d.ts +11 -0
  200. package/dist/ui/primitives/button.d.ts.map +1 -0
  201. package/dist/ui/primitives/button.js +35 -0
  202. package/dist/ui/primitives/dropdown-menu.d.ts +26 -0
  203. package/dist/ui/primitives/dropdown-menu.d.ts.map +1 -0
  204. package/dist/ui/primitives/dropdown-menu.js +53 -0
  205. package/dist/ui/primitives/popover.d.ts +8 -0
  206. package/dist/ui/primitives/popover.d.ts.map +1 -0
  207. package/dist/ui/primitives/popover.js +17 -0
  208. package/dist/ui/primitives/switch.d.ts +5 -0
  209. package/dist/ui/primitives/switch.d.ts.map +1 -0
  210. package/dist/ui/primitives/switch.js +8 -0
  211. package/dist/ui/primitives/tooltip.d.ts +8 -0
  212. package/dist/ui/primitives/tooltip.d.ts.map +1 -0
  213. package/dist/ui/primitives/tooltip.js +17 -0
  214. package/dist/ui/store/camera-pose-store.d.ts +17 -0
  215. package/dist/ui/store/camera-pose-store.d.ts.map +1 -0
  216. package/dist/ui/store/camera-pose-store.js +15 -0
  217. package/dist/ui/store/navigation-sync-pose-store.d.ts +26 -0
  218. package/dist/ui/store/navigation-sync-pose-store.d.ts.map +1 -0
  219. package/dist/ui/store/navigation-sync-pose-store.js +16 -0
  220. package/dist/ui/store/use-follow-pascal.d.ts +30 -0
  221. package/dist/ui/store/use-follow-pascal.d.ts.map +1 -0
  222. package/dist/ui/store/use-follow-pascal.js +28 -0
  223. package/dist/ui/theme.css +339 -0
  224. package/package.json +34 -7
@@ -0,0 +1,1295 @@
1
+ 'use client';
2
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { area, containsPoint, DEFAULT_LEVEL_HEIGHT, polygonInteriorPoint, useLocale, } from '@pascal-app/core';
4
+ import { useFrame, useThree } from '@react-three/fiber';
5
+ import { createContext, Fragment, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, } from 'react';
6
+ import { createPortal } from 'react-dom';
7
+ import { Matrix4, Ray, Raycaster, Vector2, Vector3, } from 'three';
8
+ import { timeSpan } from '../../../lib/perf-tracks';
9
+ import useViewer from '../../../store/use-viewer';
10
+ import { formatRoomArea } from '../../lib/room-area';
11
+ import { LocalizedHtml } from '../localized-html';
12
+ const CONTEXT_TYPES = new Set(['site', 'building', 'level']);
13
+ /**
14
+ * Room pills are the resting view of a level: nothing picked but the site,
15
+ * building or level itself, no room, and no unit being looked at. Anything
16
+ * else selected takes them away.
17
+ */
18
+ export function roomLabelsVisible(state) {
19
+ return (!state.zoneId &&
20
+ !state.focusedUnitId &&
21
+ state.selectedTypes.every((type) => type !== undefined && CONTEXT_TYPES.has(type)));
22
+ }
23
+ /** A pill's screen rect: centred on where its anchor lands, at its size. */
24
+ export function pillRect(x, y, [width, height]) {
25
+ return { left: x - width / 2, top: y - height / 2, right: x + width / 2, bottom: y + height / 2 };
26
+ }
27
+ /** The collapsed pill: its colour dot in a small round box. */
28
+ const PILL_DOT_SIZE = 16;
29
+ /** The clear space around a placed pill, and around the detail chip. */
30
+ export const PILL_GAP = 4;
31
+ const NUDGE_STEP = 4;
32
+ const TIERS = ['full', 'name', 'dot'];
33
+ function near(a, b) {
34
+ return (a.left < b.right + PILL_GAP &&
35
+ b.left < a.right + PILL_GAP &&
36
+ a.top < b.bottom + PILL_GAP &&
37
+ b.top < a.bottom + PILL_GAP);
38
+ }
39
+ function tierSize(size, tier) {
40
+ if (tier === 'full')
41
+ return [size[0], size[1]];
42
+ if (tier === 'name')
43
+ return [size[2], size[1]];
44
+ return [PILL_DOT_SIZE, PILL_DOT_SIZE];
45
+ }
46
+ /** The rect a placed pill takes on screen, or null when it is hidden. */
47
+ export function placedRect(pill, { tier, dy }) {
48
+ return tier === 'hidden' ? null : pillRect(pill.x, pill.y + dy, tierSize(pill.size, tier));
49
+ }
50
+ function segmentTouchesRect([ax, ay], [bx, by], rect) {
51
+ if (Math.max(ax, bx) < rect.left ||
52
+ Math.min(ax, bx) > rect.right ||
53
+ Math.max(ay, by) < rect.top ||
54
+ Math.min(ay, by) > rect.bottom)
55
+ return false;
56
+ // Liang–Barsky: narrow the segment's span to each side of the rect in turn.
57
+ let t0 = 0;
58
+ let t1 = 1;
59
+ const clip = (p, q) => {
60
+ if (p === 0)
61
+ return q >= 0;
62
+ const t = q / p;
63
+ if (p < 0) {
64
+ if (t > t1)
65
+ return false;
66
+ if (t > t0)
67
+ t0 = t;
68
+ }
69
+ else {
70
+ if (t < t0)
71
+ return false;
72
+ if (t < t1)
73
+ t1 = t;
74
+ }
75
+ return true;
76
+ };
77
+ return (clip(ax - bx, ax - rect.left) &&
78
+ clip(bx - ax, rect.right - ax) &&
79
+ clip(ay - by, ay - rect.top) &&
80
+ clip(by - ay, rect.bottom - ay));
81
+ }
82
+ function ringTouchesRect(ring, rect) {
83
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
84
+ if (segmentTouchesRect(ring[j], ring[i], rect))
85
+ return true;
86
+ }
87
+ return false;
88
+ }
89
+ /**
90
+ * Whether `rect` lies inside `outline`: no edge of the outline or its holes
91
+ * touches the rect, and the rect's centre is inside.
92
+ */
93
+ function rectInPolygon(rect, outline) {
94
+ if (ringTouchesRect(outline.outer, rect))
95
+ return false;
96
+ if (outline.holes.some((hole) => ringTouchesRect(hole, rect)))
97
+ return false;
98
+ return containsPoint([outline], [(rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2]);
99
+ }
100
+ /**
101
+ * 0, then the nudge the pill took last time (so it keeps to that side), then
102
+ * up and down by growing steps, up to `budget` px either way.
103
+ */
104
+ function* nudges(budget, previous) {
105
+ yield 0;
106
+ if (previous)
107
+ yield previous;
108
+ for (let d = NUDGE_STEP; d <= budget; d += NUDGE_STEP) {
109
+ if (-d !== previous)
110
+ yield -d;
111
+ if (d !== previous)
112
+ yield d;
113
+ }
114
+ }
115
+ function ringBox(ring) {
116
+ const box = { left: Infinity, top: Infinity, right: -Infinity, bottom: -Infinity };
117
+ for (const [x, y] of ring) {
118
+ box.left = Math.min(box.left, x);
119
+ box.top = Math.min(box.top, y);
120
+ box.right = Math.max(box.right, x);
121
+ box.bottom = Math.max(box.bottom, y);
122
+ }
123
+ return box;
124
+ }
125
+ function slotFor(pill, bounds, placed, previous) {
126
+ const { outline } = pill;
127
+ // The pane, narrowed to the room's bounding box: cheap to reject against.
128
+ const box = ringBox(outline.outer);
129
+ box.left = Math.max(box.left, 0);
130
+ box.top = Math.max(box.top, 0);
131
+ box.right = Math.min(box.right, bounds.width);
132
+ box.bottom = Math.min(box.bottom, bounds.height);
133
+ for (const tier of TIERS) {
134
+ const size = tierSize(pill.size, tier);
135
+ // Wider or taller than the room's box: no nudge fits it.
136
+ if (size[0] > box.right - box.left || size[1] > box.bottom - box.top)
137
+ continue;
138
+ const budget = tier === 'dot' ? PILL_DOT_SIZE : pill.size[1];
139
+ for (const dy of nudges(budget, previous?.tier === tier ? previous.dy : undefined)) {
140
+ const rect = pillRect(pill.x, pill.y + dy, size);
141
+ if (rect.left < box.left ||
142
+ rect.top < box.top ||
143
+ rect.right > box.right ||
144
+ rect.bottom > box.bottom)
145
+ continue;
146
+ if (!rectInPolygon(rect, outline))
147
+ continue;
148
+ if (placed.some((other) => near(rect, other)))
149
+ continue;
150
+ placed.push(rect);
151
+ return { tier, dy };
152
+ }
153
+ }
154
+ return { tier: 'hidden', dy: 0 };
155
+ }
156
+ /**
157
+ * Where every pill goes, so that none overlap and each stays inside its room
158
+ * and the pane. Larger rooms are placed first (ties by id, so the order holds
159
+ * while the camera moves). Each pill takes the first slot that fits: its full
160
+ * pill, then its name alone, then its dot, each at its anchor or nudged up or
161
+ * down; with no slot it is hidden. A room's size on screen decides how much of
162
+ * its label shows, so the detail follows the zoom.
163
+ */
164
+ export function placePills(candidates, bounds, previous = new Map()) {
165
+ const placed = [];
166
+ const placements = new Map();
167
+ const order = [...candidates].sort((a, b) => b.area - a.area || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
168
+ for (const pill of order)
169
+ placements.set(pill.id, slotFor(pill, bounds, placed, previous.get(pill.id)));
170
+ return placements;
171
+ }
172
+ /** The placed pills whose rects come within the gap of `chip`. */
173
+ export function suppressedBy(chip, rects) {
174
+ const ids = new Set();
175
+ for (const [id, rect] of rects)
176
+ if (near(chip, rect))
177
+ ids.add(id);
178
+ return ids;
179
+ }
180
+ // Planes closer together than this along the ray (in metres) count as one floor.
181
+ const SAME_FLOOR = 1e-3;
182
+ /**
183
+ * The room under the pointer: of the rooms whose outline, holes excluded,
184
+ * holds the point where the pointer meets their plane, the nearest along the
185
+ * pointer's ray (the floor it reaches first), then the smallest (a zone drawn
186
+ * inside a room, on the same floor). `pointOn` gives that point for each room
187
+ * (in 3D, where the ray meets the room's floor), or null if it never meets it.
188
+ */
189
+ export function roomAt(entries, pointOn) {
190
+ let found = null;
191
+ let foundAt = Number.POSITIVE_INFINITY;
192
+ for (const entry of entries) {
193
+ const hit = pointOn(entry);
194
+ if (!hit)
195
+ continue;
196
+ const [x, y, distance] = hit;
197
+ if (found) {
198
+ const nearer = distance < foundAt - SAME_FLOOR;
199
+ const sameFloor = !nearer && distance <= foundAt + SAME_FLOOR;
200
+ if (!(nearer || (sameFloor && entry.area < found.area)))
201
+ continue;
202
+ }
203
+ if (containsPoint([{ outer: entry.polygon, holes: entry.holes }], [x, y])) {
204
+ found = entry;
205
+ foundAt = distance;
206
+ }
207
+ }
208
+ return found?.zoneId ?? null;
209
+ }
210
+ // Mid-height of a default storey: above furniture, below the ceiling.
211
+ const LABEL_HEIGHT = DEFAULT_LEVEL_HEIGHT / 2;
212
+ // The view has to hold still this long before pills are re-placed; while it
213
+ // moves they keep their places and ride their rooms, so nothing jitters.
214
+ const DECLUTTER_REST_SECONDS = 0.12;
215
+ // Below this the view counts as still: well under a pixel on screen.
216
+ const VIEW_EPSILON = 1e-4;
217
+ /** How long a pill takes to fade, move or collapse. */
218
+ const PILL_FADE_MS = 180;
219
+ /** How long the pointer rests on a room before its detail opens. */
220
+ export const REVEAL_DWELL_MS = 80;
221
+ /** How long the detail stays once the pointer has left its room. */
222
+ export const REVEAL_GRACE_MS = 120;
223
+ // The detail chip stays this far inside its layer.
224
+ const DETAIL_INSET = 8;
225
+ function pillFadeMs() {
226
+ return typeof window !== 'undefined' &&
227
+ window.matchMedia?.('(prefers-reduced-motion: reduce)').matches
228
+ ? 0
229
+ : PILL_FADE_MS;
230
+ }
231
+ /**
232
+ * When a layer re-places its pills: once the view has held still, and again
233
+ * whenever its pills change while it rests. Still means within an epsilon of
234
+ * the view last seen moving — camera damping keeps nudging the camera by
235
+ * fractions of a millimetre for a second or more after it visibly stops, and
236
+ * waiting for it to stop exactly left pills misplaced for that long.
237
+ */
238
+ export class DeclutterClock {
239
+ reference = null;
240
+ movedAt = Number.NEGATIVE_INFINITY;
241
+ settled = false;
242
+ dirty = true;
243
+ /** Whether the view rests and its pills were placed for it. */
244
+ get atRest() {
245
+ return this.settled;
246
+ }
247
+ /** The pills changed (mounted, unmounted, resized): place them again. */
248
+ invalidate() {
249
+ this.dirty = true;
250
+ }
251
+ /** Whether to place pills for this view (any flat list of numbers) at `now` seconds. */
252
+ shouldRead(view, now) {
253
+ const reference = this.reference;
254
+ let moved = !reference || reference.length !== view.length;
255
+ for (let i = 0; !moved && i < view.length; i++) {
256
+ moved = Math.abs(view[i] - reference[i]) > VIEW_EPSILON;
257
+ }
258
+ if (moved) {
259
+ this.reference = Array.from(view);
260
+ this.movedAt = now;
261
+ this.settled = false;
262
+ }
263
+ if (now - this.movedAt < DECLUTTER_REST_SECONDS || (this.settled && !this.dirty))
264
+ return false;
265
+ this.settled = true;
266
+ this.dirty = false;
267
+ return true;
268
+ }
269
+ }
270
+ /** The pills a layer has mounted, by zone id; the layer hears each one come and go. */
271
+ export class PillElements extends Map {
272
+ onMount = null;
273
+ onUnmount = null;
274
+ onMeasure = null;
275
+ measure(id) {
276
+ const element = this.get(id);
277
+ if (element)
278
+ this.onMeasure?.(id, element);
279
+ }
280
+ set(id, element) {
281
+ super.set(id, element);
282
+ this.onMount?.(id, element);
283
+ return this;
284
+ }
285
+ delete(id) {
286
+ const element = this.get(id);
287
+ const removed = super.delete(id);
288
+ if (element)
289
+ this.onUnmount?.(element);
290
+ return removed;
291
+ }
292
+ }
293
+ /** Where each room's pill floats, level-local; moving one has the pills placed again. */
294
+ export class RoomAnchors extends Map {
295
+ onMove = null;
296
+ set(id, position) {
297
+ const moved = !this.get(id)?.equals(position);
298
+ super.set(id, position);
299
+ if (moved)
300
+ this.onMove?.();
301
+ return this;
302
+ }
303
+ }
304
+ /** Read a pill's full border-box and its width without the area, whatever it shows now. */
305
+ export function measureFullPill(element, read = getComputedStyle) {
306
+ const tier = element.dataset.roomLabelTier;
307
+ delete element.dataset.roomLabelTier;
308
+ try {
309
+ const style = read(element);
310
+ const width = Number.parseFloat(style.width);
311
+ const areaLabel = element.querySelector('[data-room-label-area]');
312
+ const areaWidth = areaLabel
313
+ ? Number.parseFloat(read(areaLabel).width) + Number.parseFloat(style.columnGap)
314
+ : 0;
315
+ return [width, Number.parseFloat(style.height), width - areaWidth];
316
+ }
317
+ finally {
318
+ if (tier !== undefined)
319
+ element.dataset.roomLabelTier = tier;
320
+ }
321
+ }
322
+ // Wrapped, not passed as is: browsers throw "Illegal invocation" when the
323
+ // global timers are called as methods of another object.
324
+ const startTimer = (run, ms) => setTimeout(run, ms);
325
+ const stopTimer = (timer) => clearTimeout(timer);
326
+ /**
327
+ * Applies placements to the mounted pills as direct style writes: the nudge
328
+ * as a `translate`, the tier as `data-room-label-tier`, both eased by the
329
+ * pill's transition. A pill placed hidden fades out without taking the
330
+ * pointer, then leaves the DOM (`gone`); one given room again mounts and fades
331
+ * back in. A mounted pill that a pass leaves out (not measured yet, or its
332
+ * anchor off screen) waits hidden: no pill shows that placement has not
333
+ * cleared.
334
+ */
335
+ export class PillPlacer {
336
+ elements;
337
+ onGone;
338
+ schedule;
339
+ cancel;
340
+ gone = new Set();
341
+ /** The last pass's placements: a remounting pill takes its own at once. */
342
+ placements = new Map();
343
+ fading = new Map();
344
+ suppressed = new Set();
345
+ open = null;
346
+ shown = true;
347
+ constructor(elements, onGone, schedule = startTimer, cancel = stopTimer) {
348
+ this.elements = elements;
349
+ this.onGone = onGone;
350
+ this.schedule = schedule;
351
+ this.cancel = cancel;
352
+ }
353
+ apply(placements, fadeMs = pillFadeMs()) {
354
+ this.placements = placements;
355
+ for (const [id, element] of this.elements) {
356
+ const placement = placements.get(id);
357
+ if (placement && placement.tier !== 'hidden') {
358
+ const timer = this.fading.get(id);
359
+ if (timer !== undefined)
360
+ this.cancel(timer);
361
+ this.fading.delete(id);
362
+ this.show(element, placement);
363
+ continue;
364
+ }
365
+ element.dataset.roomLabelHidden = '';
366
+ if (!placement || this.fading.has(id))
367
+ continue;
368
+ this.fading.set(id, this.schedule(() => {
369
+ this.fading.delete(id);
370
+ this.setGone(new Set(this.gone).add(id));
371
+ }, fadeMs));
372
+ }
373
+ const kept = [...this.gone].filter((id) => (placements.get(id)?.tier ?? 'hidden') === 'hidden');
374
+ if (kept.length !== this.gone.size)
375
+ this.setGone(new Set(kept));
376
+ }
377
+ /** A pill mounting takes its last placement at once, or waits hidden for one. */
378
+ mount(id, element) {
379
+ const placement = this.placements.get(id);
380
+ if (placement && placement.tier !== 'hidden')
381
+ this.show(element, placement);
382
+ else
383
+ element.dataset.roomLabelHidden = '';
384
+ this.mark(id, element);
385
+ }
386
+ /**
387
+ * Fade the pills under the detail chip out of its way, and every other back.
388
+ * The open room's own pill fades too but still takes the pointer: the chip
389
+ * over it is read-only, so the pill stays the control.
390
+ */
391
+ suppress(ids, open) {
392
+ this.suppressed = ids;
393
+ this.open = open;
394
+ for (const [id, element] of this.elements)
395
+ this.mark(id, element);
396
+ }
397
+ /** Remount hidden pills invisibly so changed text or fonts can be measured. */
398
+ remeasure() {
399
+ if (this.gone.size)
400
+ this.setGone(new Set());
401
+ }
402
+ /** Fade every mounted pill out (the layer is leaving), or the placed ones back in. */
403
+ showAll(shown) {
404
+ this.shown = shown;
405
+ for (const [id, element] of this.elements) {
406
+ const placement = this.placements.get(id);
407
+ if (shown && placement && placement.tier !== 'hidden')
408
+ delete element.dataset.roomLabelHidden;
409
+ else
410
+ element.dataset.roomLabelHidden = '';
411
+ }
412
+ }
413
+ dispose() {
414
+ for (const timer of this.fading.values())
415
+ this.cancel(timer);
416
+ this.fading.clear();
417
+ }
418
+ show(element, { tier, dy }) {
419
+ const translate = dy ? `0 ${dy}px` : '';
420
+ if (element.style.translate !== translate)
421
+ element.style.translate = translate;
422
+ if (tier === 'full')
423
+ delete element.dataset.roomLabelTier;
424
+ else if (element.dataset.roomLabelTier !== tier)
425
+ element.dataset.roomLabelTier = tier;
426
+ if (this.shown)
427
+ delete element.dataset.roomLabelHidden;
428
+ else
429
+ element.dataset.roomLabelHidden = '';
430
+ }
431
+ mark(id, element) {
432
+ const { dataset } = element;
433
+ if (id === this.open)
434
+ dataset.roomLabelOpen = '';
435
+ else
436
+ delete dataset.roomLabelOpen;
437
+ if (id !== this.open && this.suppressed.has(id))
438
+ dataset.roomLabelSuppressed = '';
439
+ else
440
+ delete dataset.roomLabelSuppressed;
441
+ }
442
+ setGone(gone) {
443
+ this.gone = gone;
444
+ this.onGone(gone);
445
+ }
446
+ }
447
+ /**
448
+ * Which room's detail chip is open. A pill under the pointer, or a pill with
449
+ * keyboard focus, opens at once; when both are set, whichever moved last
450
+ * wins. Otherwise the room under the pointer opens once the pointer has
451
+ * rested on it for a moment, and stays a moment after the pointer leaves, so
452
+ * crossing a wall hands over from one room to the next without a gap and
453
+ * passing over a room opens nothing. Escape closes it until the pointer or
454
+ * focus moves on to another room.
455
+ */
456
+ export class LabelReveal {
457
+ onChange;
458
+ schedule;
459
+ cancel;
460
+ active = null;
461
+ activeFocused = false;
462
+ hovered = null;
463
+ focused = null;
464
+ picked = null;
465
+ keyboardLast = false;
466
+ dismissed = null;
467
+ pending = null;
468
+ constructor(onChange, schedule = startTimer, cancel = stopTimer) {
469
+ this.onChange = onChange;
470
+ this.schedule = schedule;
471
+ this.cancel = cancel;
472
+ }
473
+ /** The pill under the pointer. */
474
+ hover(id) {
475
+ if (id !== null && id !== this.hovered)
476
+ this.keyboardLast = false;
477
+ this.hovered = id;
478
+ this.update();
479
+ }
480
+ /** The pill with keyboard focus. */
481
+ focus(id) {
482
+ if (id !== null)
483
+ this.keyboardLast = true;
484
+ this.focused = id;
485
+ this.update();
486
+ }
487
+ /** The room under the pointer. */
488
+ pick(id) {
489
+ this.picked = id;
490
+ this.update();
491
+ }
492
+ /** Close what is open, and keep it closed while the pointer and focus stay on it. */
493
+ dismiss() {
494
+ if (this.active === null)
495
+ return;
496
+ this.dismissed = this.active;
497
+ this.stop();
498
+ this.set(null);
499
+ }
500
+ /**
501
+ * Close at once and forget the pointer: the view moved, or the layer hid.
502
+ * Focus is kept, so a focused pill opens again once the view rests.
503
+ */
504
+ clear() {
505
+ this.hovered = null;
506
+ this.picked = null;
507
+ this.stop();
508
+ this.set(null);
509
+ }
510
+ update() {
511
+ const direct = this.keyboardLast
512
+ ? (this.focused ?? this.hovered)
513
+ : (this.hovered ?? this.focused);
514
+ if (this.dismissed !== null) {
515
+ if ((direct ?? this.picked) === this.dismissed) {
516
+ this.stop();
517
+ return;
518
+ }
519
+ this.dismissed = null;
520
+ }
521
+ if (direct) {
522
+ this.stop();
523
+ this.set(direct);
524
+ return;
525
+ }
526
+ const next = this.picked;
527
+ if (next === this.active) {
528
+ this.stop();
529
+ this.set(next);
530
+ return;
531
+ }
532
+ if (this.pending && this.pending.id === next)
533
+ return;
534
+ this.stop();
535
+ this.pending = {
536
+ id: next,
537
+ timer: this.schedule(() => {
538
+ this.pending = null;
539
+ this.set(next);
540
+ }, next ? REVEAL_DWELL_MS : REVEAL_GRACE_MS),
541
+ };
542
+ }
543
+ stop() {
544
+ if (this.pending)
545
+ this.cancel(this.pending.timer);
546
+ this.pending = null;
547
+ }
548
+ set(id) {
549
+ const focused = id !== null && this.focused === id;
550
+ if (id === this.active && focused === this.activeFocused)
551
+ return;
552
+ this.active = id;
553
+ this.activeFocused = focused;
554
+ this.onChange(id, focused);
555
+ }
556
+ }
557
+ /** True while `shown`, and through the fade once it turns false; then false, so the pills unmount. */
558
+ export function useFadePresence(shown, onGone) {
559
+ const [present, setPresent] = useState(shown);
560
+ useEffect(() => {
561
+ if (shown) {
562
+ setPresent(true);
563
+ return;
564
+ }
565
+ const timer = setTimeout(() => setPresent(false), pillFadeMs());
566
+ return () => clearTimeout(timer);
567
+ }, [shown]);
568
+ const goneRef = useRef(onGone);
569
+ goneRef.current = onGone;
570
+ const isPresent = shown || present;
571
+ useEffect(() => {
572
+ if (!isPresent)
573
+ goneRef.current?.();
574
+ }, [isPresent]);
575
+ return isPresent;
576
+ }
577
+ /**
578
+ * One layer's placement and reveal, shared by its frame loop, its pointer
579
+ * listeners and its pills: the pills it has mounted, the size each last
580
+ * rendered at (kept for pills that left the DOM), the last placement pass, and
581
+ * which room's detail is open.
582
+ */
583
+ class PillLayer {
584
+ elements = new PillElements();
585
+ clock = new DeclutterClock();
586
+ sizes = new Map();
587
+ placer;
588
+ reveal;
589
+ container = () => null;
590
+ surface = () => null;
591
+ /** The pointer over the host in client px while no button is down, null off it. */
592
+ pointer = null;
593
+ pointerMoved = false;
594
+ candidates = new Map();
595
+ rects = new Map();
596
+ bounds = { width: 0, height: 0 };
597
+ chip = null;
598
+ // The open detail, a store of its own: see `OpenRoomLabelDetail`.
599
+ detail = null;
600
+ detailListeners = new Set();
601
+ constructor(onGone) {
602
+ this.placer = new PillPlacer(this.elements, onGone);
603
+ this.reveal = new LabelReveal((id, focused) => this.openDetail(id, focused));
604
+ }
605
+ subscribeDetail = (listener) => {
606
+ this.detailListeners.add(listener);
607
+ return () => {
608
+ this.detailListeners.delete(listener);
609
+ };
610
+ };
611
+ getDetail = () => this.detail;
612
+ place(candidates, bounds) {
613
+ const placements = placePills(candidates, bounds, this.placer.placements);
614
+ this.placer.apply(placements);
615
+ this.bounds = { width: bounds.width, height: bounds.height };
616
+ this.candidates = new Map(candidates.map((candidate) => [candidate.id, candidate]));
617
+ this.rects = new Map();
618
+ for (const candidate of candidates) {
619
+ const rect = placedRect(candidate, placements.get(candidate.id));
620
+ if (rect)
621
+ this.rects.set(candidate.id, rect);
622
+ }
623
+ // An open chip follows its pill to its new place.
624
+ if (this.detail?.open)
625
+ this.openDetail(this.detail.id, this.detail.focused);
626
+ this.suppress();
627
+ }
628
+ /**
629
+ * After the frame's placement, if any: close the detail while the view
630
+ * moves; once it rests, follow the room under the pointer, again after each
631
+ * placement so a wheel zoom opens the room left under the cursor.
632
+ */
633
+ settle(placed, pick) {
634
+ if (!this.clock.atRest) {
635
+ this.reveal.clear();
636
+ return;
637
+ }
638
+ if (!(placed || this.pointerMoved))
639
+ return;
640
+ this.pointerMoved = false;
641
+ this.reveal.pick(this.pointer && pick(this.pointer));
642
+ }
643
+ /** Where the detail chip now sits, or null once it closes or unmounts. */
644
+ placeDetail = (rect) => {
645
+ this.chip = rect;
646
+ this.suppress();
647
+ };
648
+ suppress() {
649
+ if (!this.chip) {
650
+ this.placer.suppress(new Set(), null);
651
+ return;
652
+ }
653
+ const open = this.detail?.open ? this.detail.id : null;
654
+ this.placer.suppress(suppressedBy(this.chip, this.rects), open);
655
+ }
656
+ openDetail(id, focused) {
657
+ const at = id ? this.locate(id) : null;
658
+ this.detail =
659
+ id && at ? { id, open: true, focused, at } : this.detail && { ...this.detail, open: false };
660
+ for (const listener of this.detailListeners)
661
+ listener();
662
+ }
663
+ locate(id) {
664
+ const bounds = this.bounds;
665
+ const rect = this.rects.get(id);
666
+ const dot = rect && this.elements.get(id)?.querySelector('[data-room-label-dot]');
667
+ if (rect && dot) {
668
+ // From the placed rect, not the pill's live box: it may still be easing in.
669
+ const x = rect.left + dot.offsetLeft + dot.offsetWidth / 2;
670
+ return { x, y: (rect.top + rect.bottom) / 2, onDot: true, bounds };
671
+ }
672
+ const anchor = this.candidates.get(id);
673
+ if (anchor)
674
+ return { x: anchor.x, y: anchor.y, onDot: false, bounds };
675
+ const box = this.container()?.getBoundingClientRect();
676
+ if (!(box && this.pointer))
677
+ return null;
678
+ return { x: this.pointer.x - box.left, y: this.pointer.y - box.top, onDot: false, bounds };
679
+ }
680
+ }
681
+ const samePx = (a, b) => Math.abs(a - b) < 0.5;
682
+ /**
683
+ * The state of one layer of pills, kept in step with them: measured as they
684
+ * mount (and observed, so a web font arriving, a rename or a unit change
685
+ * places them again), shown and hidden with the layer, and its detail opened
686
+ * by keyboard focus or the pointer over `host`: over the `surface` (the canvas
687
+ * or the plan) or the layer's own labels, not the UI over them.
688
+ */
689
+ function usePillLayer(shown, entries, host, container, surface) {
690
+ const [gone, setGone] = useState(() => new Set());
691
+ const [layer] = useState(() => new PillLayer(setGone));
692
+ layer.container = container;
693
+ layer.surface = surface;
694
+ const unit = useViewer((s) => s.unit);
695
+ const shownRef = useRef(shown);
696
+ shownRef.current = shown;
697
+ useEffect(() => {
698
+ const { clock, elements, placer, sizes } = layer;
699
+ elements.onMeasure = (id, element) => {
700
+ const size = measureFullPill(element);
701
+ if (!(size[0] > 0 && size[1] > 0 && size[2] > 0))
702
+ return;
703
+ const previous = sizes.get(id);
704
+ if (previous?.every((value, i) => samePx(value, size[i])))
705
+ return;
706
+ sizes.set(id, size);
707
+ clock.invalidate();
708
+ };
709
+ const observer = typeof ResizeObserver === 'undefined'
710
+ ? null
711
+ : new ResizeObserver((records) => {
712
+ for (const record of records) {
713
+ const element = record.target;
714
+ const id = element.dataset.roomLabel;
715
+ const box = record.borderBoxSize?.[0];
716
+ if (!(id && box && box.inlineSize > 0))
717
+ continue;
718
+ // A new tier resizes the pill to a size already known; anything
719
+ // else (a dot back on screen with text changed while its wrapper
720
+ // was display:none) is measured again.
721
+ const tier = element.dataset.roomLabelTier;
722
+ const size = sizes.get(id);
723
+ const expected = size && tierSize(size, tier === 'name' || tier === 'dot' ? tier : 'full');
724
+ if (expected &&
725
+ samePx(expected[0], box.inlineSize) &&
726
+ samePx(expected[1], box.blockSize))
727
+ continue;
728
+ elements.measure(id);
729
+ }
730
+ });
731
+ elements.onMount = (id, element) => {
732
+ placer.mount(id, element);
733
+ elements.measure(id);
734
+ observer?.observe(element);
735
+ clock.invalidate();
736
+ };
737
+ elements.onUnmount = (element) => {
738
+ observer?.unobserve(element);
739
+ clock.invalidate();
740
+ };
741
+ for (const [id, element] of elements)
742
+ elements.onMount(id, element);
743
+ const fontsChanged = () => {
744
+ placer.remeasure();
745
+ for (const id of elements.keys())
746
+ elements.measure(id);
747
+ };
748
+ document.fonts?.addEventListener('loadingdone', fontsChanged);
749
+ return () => {
750
+ elements.onMount = null;
751
+ elements.onUnmount = null;
752
+ elements.onMeasure = null;
753
+ document.fonts?.removeEventListener('loadingdone', fontsChanged);
754
+ observer?.disconnect();
755
+ placer.dispose();
756
+ layer.reveal.clear();
757
+ };
758
+ }, [layer]);
759
+ // biome-ignore lint/correctness/useExhaustiveDependencies: Hidden pills must remount to measure changed entries or units.
760
+ useEffect(() => {
761
+ layer.placer.remeasure();
762
+ layer.clock.invalidate();
763
+ }, [entries, layer, unit]);
764
+ useEffect(() => {
765
+ layer.placer.showAll(shown);
766
+ if (shown)
767
+ layer.clock.invalidate();
768
+ else
769
+ layer.reveal.clear();
770
+ }, [layer, shown]);
771
+ useEffect(() => {
772
+ if (!host)
773
+ return;
774
+ const { reveal } = layer;
775
+ const labelAt = (target) => {
776
+ const label = target instanceof Element ? target.closest('[data-room-label]') : null;
777
+ if (!(label && layer.container()?.contains(label)))
778
+ return null;
779
+ return label.dataset.roomLabel ?? null;
780
+ };
781
+ const move = (event) => {
782
+ const label = labelAt(event.target);
783
+ const over = event.buttons === 0 &&
784
+ event.pointerType !== 'touch' &&
785
+ (label !== null ||
786
+ (event.target instanceof Node && !!layer.surface()?.contains(event.target)));
787
+ layer.pointer = over ? { x: event.clientX, y: event.clientY } : null;
788
+ layer.pointerMoved = true;
789
+ if (shownRef.current && layer.clock.atRest)
790
+ reveal.hover(over ? label : null);
791
+ };
792
+ const leave = () => {
793
+ layer.pointer = null;
794
+ reveal.hover(null);
795
+ reveal.pick(null);
796
+ };
797
+ const focusIn = (event) => {
798
+ const id = labelAt(event.target);
799
+ if (id && shownRef.current && event.target.matches(':focus-visible'))
800
+ reveal.focus(id);
801
+ };
802
+ const focusOut = () => reveal.focus(null);
803
+ // The canvas and the plan pan while Space is held; on a pill, Space presses it.
804
+ const keepSpace = (event) => {
805
+ if (event.code === 'Space' && labelAt(event.target))
806
+ event.stopPropagation();
807
+ };
808
+ const dismiss = (event) => {
809
+ if (event.key === 'Escape')
810
+ reveal.dismiss();
811
+ };
812
+ // Captured: a plan or canvas handler stopping the move must not blind the layer.
813
+ host.addEventListener('pointermove', move, { capture: true, passive: true });
814
+ // A drag ending with the pointer held still picks the room under it once the view rests.
815
+ host.addEventListener('pointerup', move, { capture: true, passive: true });
816
+ host.addEventListener('pointerleave', leave);
817
+ host.addEventListener('focusin', focusIn);
818
+ host.addEventListener('focusout', focusOut);
819
+ host.addEventListener('keydown', keepSpace);
820
+ host.addEventListener('keyup', keepSpace);
821
+ document.addEventListener('keydown', dismiss);
822
+ return () => {
823
+ host.removeEventListener('pointermove', move, { capture: true });
824
+ host.removeEventListener('pointerup', move, { capture: true });
825
+ host.removeEventListener('pointerleave', leave);
826
+ host.removeEventListener('focusin', focusIn);
827
+ host.removeEventListener('focusout', focusOut);
828
+ host.removeEventListener('keydown', keepSpace);
829
+ host.removeEventListener('keyup', keepSpace);
830
+ document.removeEventListener('keydown', dismiss);
831
+ };
832
+ }, [host, layer]);
833
+ return { layer, gone };
834
+ }
835
+ /**
836
+ * The pills of one level: every detected room, and every drawn zone that
837
+ * bounds none. A smaller one inside a larger one (a zone drawn in a room) is
838
+ * a hole in the larger: the label there, and the room under the pointer
839
+ * there, are the smaller one's.
840
+ */
841
+ export function roomLabelEntries(rooms, zones) {
842
+ const hiddenIds = new Set(zones.filter((zone) => zone.visible === false).map((zone) => zone.id));
843
+ const roomIds = new Set(rooms.map((room) => room.zoneId));
844
+ const entries = [
845
+ ...rooms
846
+ .filter((room) => !hiddenIds.has(room.zoneId))
847
+ .map((room) => ({
848
+ zoneId: room.zoneId,
849
+ polygon: room.polygon,
850
+ holes: room.holes,
851
+ area: room.area,
852
+ })),
853
+ ...zones
854
+ .filter((zone) => zone.visible !== false && !roomIds.has(zone.id) && zone.polygon.length >= 3)
855
+ .map((zone) => ({
856
+ zoneId: zone.id,
857
+ polygon: zone.polygon,
858
+ holes: zone.holes ?? [],
859
+ area: area([{ outer: zone.polygon, holes: zone.holes ?? [] }]),
860
+ })),
861
+ ];
862
+ const insides = entries.map((entry) => polygonInteriorPoint({ polygon: entry.polygon, holes: entry.holes }));
863
+ return entries.map((entry) => {
864
+ const outline = [{ outer: entry.polygon, holes: entry.holes }];
865
+ const nested = entries.filter((other, i) => other.area < entry.area && containsPoint(outline, insides[i]));
866
+ return nested.length === 0
867
+ ? entry
868
+ : { ...entry, holes: [...entry.holes, ...nested.map((other) => other.polygon)] };
869
+ });
870
+ }
871
+ /** A label's colour dot, name and area: a pill hides what its tier leaves out, the detail shows all. */
872
+ function LabelParts({ color, name, areaLabel, full, }) {
873
+ return (_jsxs(_Fragment, { children: [_jsx("span", { className: "h-2 w-2 shrink-0 rounded-full", "data-room-label-dot": "", style: { backgroundColor: color } }), _jsx("span", { className: full
874
+ ? 'min-w-0 font-medium [overflow-wrap:anywhere]'
875
+ : 'max-w-40 truncate font-medium [[data-room-label-tier=dot]>&]:hidden', children: name }), _jsx("span", { className: "shrink-0 whitespace-nowrap font-mono text-white/60 tabular-nums [[data-room-label-tier]>&]:hidden", "data-room-label-area": "", children: areaLabel })] }));
876
+ }
877
+ /**
878
+ * The pill itself, a button: a colour dot, the name (cut at 160 px) and the
879
+ * area in the active unit, as much of it as its placement shows.
880
+ */
881
+ export function RoomPillChip({ zoneId, view, color, name, areaM2, elements, onClick, onHover, onLeave, }) {
882
+ const unit = useViewer((s) => s.unit);
883
+ const locale = useLocale();
884
+ const areaLabel = formatRoomArea(areaM2, unit, locale);
885
+ // biome-ignore lint/correctness/useExhaustiveDependencies: A dot's border box does not resize when its hidden text changes.
886
+ useLayoutEffect(() => {
887
+ if (elements instanceof PillElements)
888
+ elements.measure(zoneId);
889
+ }, [areaM2, elements, locale, name, unit, zoneId]);
890
+ const register = useCallback((element) => {
891
+ if (element)
892
+ elements.set(zoneId, element);
893
+ else
894
+ elements.delete(zoneId);
895
+ }, [elements, zoneId]);
896
+ return (_jsx("button", { "aria-label": `${name}, ${areaLabel}`,
897
+ // Fades in once placed, and out while hidden (then out of the tab order)
898
+ // or under the detail chip (taking no pointer unless it is the open
899
+ // room's); eases into its nudged place and its tier.
900
+ className: "pointer-events-auto flex cursor-pointer select-none items-center gap-1.5 whitespace-nowrap rounded-full border border-white/10 bg-neutral-950/75 px-2.5 py-1 text-white text-xs shadow-sm backdrop-blur-sm transition-[opacity,translate,visibility] duration-180 starting:opacity-0 focus-visible:outline-2 focus-visible:outline-white focus-visible:outline-offset-2 focus-visible:ring-6 focus-visible:ring-neutral-950/85 data-room-label-hidden:pointer-events-none data-room-label-suppressed:pointer-events-none data-room-label-hidden:invisible data-room-label-hidden:opacity-0 data-room-label-open:opacity-0 data-room-label-suppressed:opacity-0 data-room-label-open:duration-150 data-room-label-suppressed:duration-150 data-[room-label-tier=dot]:gap-0 data-[room-label-tier=dot]:p-[3px] motion-reduce:transition-none", "data-room-label": zoneId, "data-room-label-view": view, onClick: (event) => {
901
+ event.stopPropagation();
902
+ onClick(event);
903
+ }, onPointerDown: (event) => event.stopPropagation(),
904
+ // Move as well as enter: leaving the canvas for the pill ends the canvas
905
+ // hover after the pill's enter has run.
906
+ onPointerEnter: onHover, onPointerLeave: onLeave, onPointerMove: onHover, ref: register, type: "button", children: _jsx(LabelParts, { areaLabel: areaLabel, color: color, full: false, name: name }) }));
907
+ }
908
+ /**
909
+ * The detail chip: the open room's whole label, wrapping to fit its layer.
910
+ * It only reads: the pill under it is the control, and the pointer goes
911
+ * through it to the rooms it covers.
912
+ */
913
+ export function RoomPillDetail({ zoneId, view, color, name, areaM2, }) {
914
+ const unit = useViewer((s) => s.unit);
915
+ const locale = useLocale();
916
+ return (_jsx("div", { "aria-hidden": true, className: "flex select-none items-center gap-1.5 rounded-[13px] border border-white/10 bg-neutral-950/90 px-2.5 py-1 text-white text-xs shadow-elevation-3 backdrop-blur-sm", "data-room-label-detail": zoneId, "data-room-label-view": view, children: _jsx(LabelParts, { areaLabel: formatRoomArea(areaM2, unit, locale), color: color, full: true, name: name }) }));
917
+ }
918
+ /**
919
+ * The open room's whole label over its layer: opened with its colour dot on
920
+ * the pill's, or centred where a hidden room's label would sit, and kept 8 px
921
+ * inside the layer. The pills it covers fade out of its way; nothing moves.
922
+ * One chip serves every room, so a handoff moves it without fading it again.
923
+ */
924
+ function RoomLabelDetail({ detail, onPlaced, children, }) {
925
+ const ref = useRef(null);
926
+ const { open, at } = detail;
927
+ useLayoutEffect(() => {
928
+ const frame = ref.current;
929
+ if (!(frame && open)) {
930
+ onPlaced(null);
931
+ return;
932
+ }
933
+ const place = () => {
934
+ const width = frame.offsetWidth;
935
+ const height = frame.offsetHeight;
936
+ const dot = frame.querySelector('[data-room-label-dot]');
937
+ const x = at.onDot && dot ? at.x - dot.offsetLeft - dot.offsetWidth / 2 : at.x - width / 2;
938
+ const left = Math.max(DETAIL_INSET, Math.min(x, at.bounds.width - DETAIL_INSET - width));
939
+ const top = Math.max(DETAIL_INSET, Math.min(at.y - height / 2, at.bounds.height - DETAIL_INSET - height));
940
+ frame.style.left = `${left}px`;
941
+ frame.style.top = `${top}px`;
942
+ onPlaced({ left, top, right: left + width, bottom: top + height });
943
+ };
944
+ place();
945
+ // Placed again when its text reflows: a rename, a unit change, a font arriving.
946
+ const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(place);
947
+ observer?.observe(frame);
948
+ return () => observer?.disconnect();
949
+ }, [at, onPlaced, open]);
950
+ // Gone with its room: the pills it covered come back.
951
+ useLayoutEffect(() => () => onPlaced(null), [onPlaced]);
952
+ return (_jsx("div", { className: "pointer-events-none absolute z-30 w-max origin-left rounded-[13px] transition-[opacity,scale] duration-150 ease-[cubic-bezier(0.22,1,0.36,1)] starting:scale-[0.98] starting:opacity-0 data-closed:scale-[0.98] data-closed:opacity-0 data-closed:duration-50 data-focused:outline-2 data-focused:outline-white data-focused:outline-offset-2 data-focused:ring-6 data-focused:ring-neutral-950/85 motion-reduce:transition-none", "data-closed": open ? undefined : '', "data-focused": open && detail.focused ? '' : undefined, ref: ref, style: { maxWidth: at.bounds.width - 2 * DETAIL_INSET }, children: children }));
953
+ }
954
+ /**
955
+ * The open room's detail, read from the layer's own store where it renders,
956
+ * so a reveal re-renders the chip alone, not the canvas tree or every pill.
957
+ */
958
+ function OpenRoomLabelDetail({ layer, entries, renderDetail, }) {
959
+ const detail = useSyncExternalStore(layer.subscribeDetail, layer.getDetail, layer.getDetail);
960
+ const entry = detail && entries.find((candidate) => candidate.zoneId === detail.id);
961
+ if (!(detail && entry))
962
+ return null;
963
+ return (_jsx(RoomLabelDetail, { detail: detail, onPlaced: layer.placeDetail, children: renderDetail(entry) }));
964
+ }
965
+ const toScreen = new Matrix4();
966
+ /** Where a level-local point lands in the canvas, in px, with its depth; null behind the camera. */
967
+ function screenPoint(x, y, z, size) {
968
+ const e = toScreen.elements;
969
+ const w = e[3] * x + e[7] * y + e[11] * z + e[15];
970
+ if (!(w > 0))
971
+ return null;
972
+ return [
973
+ (((e[0] * x + e[4] * y + e[8] * z + e[12]) / w + 1) / 2) * size.width,
974
+ ((1 - (e[1] * x + e[5] * y + e[9] * z + e[13]) / w) / 2) * size.height,
975
+ (e[2] * x + e[6] * y + e[10] * z + e[14]) / w,
976
+ ];
977
+ }
978
+ // How far in front of the camera an outline is cut off (in clip-space w, the
979
+ // depth along the view for a perspective camera): past it, points project
980
+ // ever further out and then flip to the other side of the screen.
981
+ const NEAR_W = 1e-3;
982
+ /**
983
+ * A level-local ring at height `y` on screen: cut to the part in front of the
984
+ * camera (Sutherland–Hodgman against w = NEAR_W, which is linear in the
985
+ * level's coordinates), then projected. Null when none of it is in front.
986
+ */
987
+ function screenRing(points, y, size) {
988
+ const e = toScreen.elements;
989
+ const w = ([x, z]) => e[3] * x + e[7] * y + e[11] * z + e[15];
990
+ const ring = [];
991
+ const add = (x, z) => {
992
+ const point = screenPoint(x, y, z, size);
993
+ if (point)
994
+ ring.push([point[0], point[1]]);
995
+ };
996
+ for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
997
+ const a = points[j];
998
+ const b = points[i];
999
+ const wa = w(a);
1000
+ const wb = w(b);
1001
+ if (wa >= NEAR_W !== wb >= NEAR_W) {
1002
+ const t = (wa - NEAR_W) / (wa - wb);
1003
+ add(a[0] + t * (b[0] - a[0]), a[1] + t * (b[1] - a[1]));
1004
+ }
1005
+ if (wb >= NEAR_W)
1006
+ add(b[0], b[1]);
1007
+ }
1008
+ return ring.length >= 3 ? ring : null;
1009
+ }
1010
+ /** Where the pills of a level land on screen, each with the part of its room in front of the camera. */
1011
+ export function candidates3D(entries, anchors, sizes, camera, level, size) {
1012
+ toScreen.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
1013
+ toScreen.multiply(level.matrixWorld);
1014
+ const candidates = [];
1015
+ for (const entry of entries) {
1016
+ const anchor = anchors.get(entry.zoneId);
1017
+ const pill = sizes.get(entry.zoneId);
1018
+ if (!(anchor && pill))
1019
+ continue;
1020
+ const at = screenPoint(anchor.x, anchor.y, anchor.z, size);
1021
+ // Behind the camera or past its far plane: not on screen to collide.
1022
+ if (!at || Math.abs(at[2]) > 1)
1023
+ continue;
1024
+ // The room's outline at its pill's height, where the pill has to fit.
1025
+ const outer = screenRing(entry.polygon, anchor.y, size);
1026
+ if (!outer)
1027
+ continue;
1028
+ const holes = [];
1029
+ for (const hole of entry.holes) {
1030
+ const ring = screenRing(hole, anchor.y, size);
1031
+ if (ring)
1032
+ holes.push(ring);
1033
+ }
1034
+ candidates.push({
1035
+ id: entry.zoneId,
1036
+ x: at[0],
1037
+ y: at[1],
1038
+ size: pill,
1039
+ area: entry.area,
1040
+ outline: { outer, holes },
1041
+ });
1042
+ }
1043
+ return candidates;
1044
+ }
1045
+ const pickRaycaster = new Raycaster();
1046
+ const pickPointer = new Vector2();
1047
+ const levelInverse = new Matrix4();
1048
+ const levelRay = new Ray();
1049
+ /** The room whose floor the pointer's ray meets first, testing each room at its own floor. */
1050
+ function pick3D(entries, anchors, pointer, camera, level, size) {
1051
+ pickPointer.set(((pointer.x - size.left) / size.width) * 2 - 1, -((pointer.y - size.top) / size.height) * 2 + 1);
1052
+ pickRaycaster.setFromCamera(pickPointer, camera);
1053
+ levelRay.copy(pickRaycaster.ray).applyMatrix4(levelInverse.copy(level.matrixWorld).invert());
1054
+ const { origin, direction } = levelRay;
1055
+ return roomAt(entries, (entry) => {
1056
+ const anchor = anchors.get(entry.zoneId);
1057
+ if (!anchor)
1058
+ return null;
1059
+ const t = (anchor.y - LABEL_HEIGHT - origin.y) / direction.y;
1060
+ return t > 0 && Number.isFinite(t)
1061
+ ? [origin.x + t * direction.x, origin.z + t * direction.z, t]
1062
+ : null;
1063
+ });
1064
+ }
1065
+ // The detail chip's own Html sits at its layer's top-left corner and places
1066
+ // the chip itself, so drei must never hide it: the object it rides is not
1067
+ // where it shows.
1068
+ const atLayerOrigin = () => [0, 0];
1069
+ const keepShown = () => { };
1070
+ /**
1071
+ * The pills of one level in 3D, riding the level's object (so they follow
1072
+ * level stacking) and placed from where their anchors land on screen. They
1073
+ * live in their own stacking layer clipped to the canvas, so every piece of
1074
+ * UI over the canvas stays above them; the open room's chip, the label being
1075
+ * read, has a layer of its own above the canvas's flags.
1076
+ */
1077
+ export function RoomPillsLayer3D({ entries, levelObject, renderLabel, renderDetail, shown = true, }) {
1078
+ const ref = useRef(null);
1079
+ const [anchors] = useState(() => new RoomAnchors());
1080
+ const view = useRef(new Float64Array(50));
1081
+ const host = useThree((s) => (s.events.connected ?? s.gl.domElement.parentNode));
1082
+ const [portals] = useState(() => typeof document === 'undefined'
1083
+ ? null
1084
+ : {
1085
+ pills: { current: document.createElement('div') },
1086
+ detail: { current: document.createElement('div') },
1087
+ });
1088
+ const canvas = useThree((s) => s.gl.domElement);
1089
+ const { layer, gone } = usePillLayer(shown, entries, host, () => portals?.pills.current ?? null, () => canvas);
1090
+ useEffect(() => {
1091
+ if (!(host && portals))
1092
+ return;
1093
+ const { pills, detail } = portals;
1094
+ pills.current.className = 'pointer-events-none absolute inset-0 isolate z-0 overflow-hidden';
1095
+ // Over the flags and guides on the canvas (z-index 25 at most), unlike the pills.
1096
+ detail.current.className = 'pointer-events-none absolute inset-0 z-30';
1097
+ host.append(pills.current, detail.current);
1098
+ return () => {
1099
+ pills.current.remove();
1100
+ detail.current.remove();
1101
+ };
1102
+ }, [host, portals]);
1103
+ useEffect(() => {
1104
+ anchors.onMove = () => layer.clock.invalidate();
1105
+ return () => {
1106
+ anchors.onMove = null;
1107
+ };
1108
+ }, [anchors, layer]);
1109
+ useEffect(() => {
1110
+ const ids = new Set(entries.map((entry) => entry.zoneId));
1111
+ for (const id of anchors.keys())
1112
+ if (!ids.has(id))
1113
+ anchors.delete(id);
1114
+ layer.clock.invalidate();
1115
+ }, [anchors, entries, layer]);
1116
+ useFrame(({ camera, clock: frameClock, size }) => {
1117
+ if (!ref.current)
1118
+ return;
1119
+ const level = levelObject();
1120
+ ref.current.visible = !!level;
1121
+ if (!level)
1122
+ return;
1123
+ level.updateWorldMatrix(true, false);
1124
+ ref.current.matrix.copy(level.matrixWorld);
1125
+ if (!shown)
1126
+ return;
1127
+ const flat = view.current;
1128
+ flat.set(camera.matrixWorld.elements, 0);
1129
+ flat.set(camera.projectionMatrix.elements, 16);
1130
+ flat.set(level.matrixWorld.elements, 32);
1131
+ flat[48] = size.width;
1132
+ flat[49] = size.height;
1133
+ const placed = layer.clock.shouldRead(flat, frameClock.elapsedTime);
1134
+ if (placed) {
1135
+ timeSpan('room-labels', () => layer.place(candidates3D(entries, anchors, layer.sizes, camera, level, size), size));
1136
+ }
1137
+ layer.settle(placed, (pointer) => pick3D(entries, anchors, pointer, camera, level, size));
1138
+ });
1139
+ if (entries.length === 0 || !portals)
1140
+ return null;
1141
+ return (_jsx(PillPortalContext.Provider, { value: portals.pills, children: _jsx(HiddenPillsContext.Provider, { value: gone, children: _jsxs("group", { matrixAutoUpdate: false, ref: ref, children: [entries.map((entry) => (_jsx(Fragment, { children: renderLabel(entry, anchors, layer.elements) }, entry.zoneId))), _jsx(LocalizedHtml, { calculatePosition: atLayerOrigin, onOcclude: keepShown, portal: portals.detail, style: { pointerEvents: 'none' }, children: _jsx(OpenRoomLabelDetail, { entries: entries, layer: layer, renderDetail: renderDetail }) })] }) }) }));
1142
+ }
1143
+ const PillPortalContext = createContext(null);
1144
+ const HiddenPillsContext = createContext(new Set());
1145
+ /** Where a room's pill floats: over its interior, at mid-storey above its floor. */
1146
+ export function RoomLabelAnchor3D({ entry, floorY, anchors, children, }) {
1147
+ const portal = useContext(PillPortalContext);
1148
+ const gone = useContext(HiddenPillsContext);
1149
+ const position = useMemo(() => {
1150
+ const [x, z] = polygonInteriorPoint({ polygon: entry.polygon, holes: entry.holes });
1151
+ return new Vector3(x, floorY + LABEL_HEIGHT, z);
1152
+ }, [entry.polygon, entry.holes, floorY]);
1153
+ // Hidden HTML still needs a live anchor so moving a room can give its pill
1154
+ // room again. The layer drops anchors of rooms that left.
1155
+ useEffect(() => {
1156
+ anchors.set(entry.zoneId, position);
1157
+ }, [anchors, entry.zoneId, position]);
1158
+ if (gone.has(entry.zoneId))
1159
+ return null;
1160
+ return (_jsx(LocalizedHtml, { center: true, portal: portal ?? undefined, position: position,
1161
+ // The wrapper lets the canvas keep everything but the pill itself.
1162
+ style: { pointerEvents: 'none' }, zIndexRange: [20, 0], children: children }));
1163
+ }
1164
+ /**
1165
+ * The same pills over a 2D plan, mounted inside the plan's SVG: screen-positioned
1166
+ * from the plan's transform, re-placed as in 3D once the plan rests, and clipped
1167
+ * to the plan's pane in the plan's own stacking layer, under the UI over it.
1168
+ */
1169
+ export function RoomPillsLayer2D({ entries, renderLabel, renderDetail, shown, }) {
1170
+ const ref = useRef(null);
1171
+ const paneRef = useRef(null);
1172
+ const [host, setHost] = useState(null);
1173
+ const [nodes] = useState(() => new PillElements());
1174
+ const { layer, gone } = usePillLayer(shown, entries, host, () => paneRef.current, () => ref.current?.ownerSVGElement);
1175
+ const shownRef = useRef(shown);
1176
+ shownRef.current = shown;
1177
+ const anchors = useMemo(() => new Map(entries.map((entry) => [
1178
+ entry.zoneId,
1179
+ polygonInteriorPoint({ polygon: entry.polygon, holes: entry.holes }),
1180
+ ])), [entries]);
1181
+ useEffect(() => {
1182
+ setHost(ref.current?.ownerSVGElement?.parentElement ?? null);
1183
+ }, []);
1184
+ useEffect(() => {
1185
+ if (entries.length === 0)
1186
+ return;
1187
+ let raf = 0;
1188
+ let lastKey = '';
1189
+ const view = new Float64Array(10);
1190
+ // A pill wrapper that mounts is placed on the next frame.
1191
+ nodes.onMount = () => {
1192
+ lastKey = '';
1193
+ };
1194
+ layer.clock.invalidate();
1195
+ const tick = (now) => {
1196
+ raf = requestAnimationFrame(tick);
1197
+ const group = ref.current;
1198
+ const svg = group?.ownerSVGElement;
1199
+ const ctm = group?.getScreenCTM();
1200
+ const paneElement = paneRef.current;
1201
+ if (!(svg && ctm && paneElement))
1202
+ return;
1203
+ const pane = svg.getBoundingClientRect();
1204
+ const origin = paneElement.offsetParent?.getBoundingClientRect() ?? { left: 0, top: 0 };
1205
+ const key = `${ctm.a},${ctm.b},${ctm.c},${ctm.d},${ctm.e},${ctm.f},${pane.left},${pane.top},${pane.width},${pane.height},${origin.left},${origin.top}`;
1206
+ // A plan point in the pane's px.
1207
+ const place = ([x, y]) => [
1208
+ ctm.a * x + ctm.c * y + ctm.e - pane.left,
1209
+ ctm.b * x + ctm.d * y + ctm.f - pane.top,
1210
+ ];
1211
+ const inside = ([x, y]) => x >= 0 && x <= pane.width && y >= 0 && y <= pane.height;
1212
+ if (key !== lastKey) {
1213
+ lastKey = key;
1214
+ Object.assign(paneElement.style, {
1215
+ left: `${pane.left - origin.left}px`,
1216
+ top: `${pane.top - origin.top}px`,
1217
+ width: `${pane.width}px`,
1218
+ height: `${pane.height}px`,
1219
+ });
1220
+ for (const [id, element] of nodes) {
1221
+ const anchor = anchors.get(id);
1222
+ if (!anchor)
1223
+ continue;
1224
+ const at = place(anchor);
1225
+ element.style.left = `${at[0]}px`;
1226
+ element.style.top = `${at[1]}px`;
1227
+ element.style.display = inside(at) ? '' : 'none';
1228
+ }
1229
+ }
1230
+ view[0] = ctm.a;
1231
+ view[1] = ctm.b;
1232
+ view[2] = ctm.c;
1233
+ view[3] = ctm.d;
1234
+ view[4] = ctm.e;
1235
+ view[5] = ctm.f;
1236
+ view[6] = pane.left;
1237
+ view[7] = pane.top;
1238
+ view[8] = pane.width;
1239
+ view[9] = pane.height;
1240
+ if (!shownRef.current)
1241
+ return;
1242
+ const placed = layer.clock.shouldRead(view, now / 1000);
1243
+ if (placed) {
1244
+ timeSpan('room-labels', () => {
1245
+ const candidates = [];
1246
+ for (const entry of entries) {
1247
+ const anchor = anchors.get(entry.zoneId);
1248
+ const pill = layer.sizes.get(entry.zoneId);
1249
+ if (!(anchor && pill))
1250
+ continue;
1251
+ const at = place(anchor);
1252
+ if (!inside(at))
1253
+ continue;
1254
+ candidates.push({
1255
+ id: entry.zoneId,
1256
+ x: at[0],
1257
+ y: at[1],
1258
+ size: pill,
1259
+ area: entry.area,
1260
+ outline: {
1261
+ outer: entry.polygon.map(place),
1262
+ holes: entry.holes.map((hole) => hole.map(place)),
1263
+ },
1264
+ });
1265
+ }
1266
+ layer.place(candidates, pane);
1267
+ });
1268
+ }
1269
+ layer.settle(placed, (pointer) => {
1270
+ const inverse = ctm.inverse();
1271
+ // The plan is one plane, met at the same distance for every room: the smallest wins.
1272
+ const point = [
1273
+ inverse.a * pointer.x + inverse.c * pointer.y + inverse.e,
1274
+ inverse.b * pointer.x + inverse.d * pointer.y + inverse.f,
1275
+ 0,
1276
+ ];
1277
+ return roomAt(entries, () => point);
1278
+ });
1279
+ };
1280
+ raf = requestAnimationFrame(tick);
1281
+ return () => {
1282
+ cancelAnimationFrame(raf);
1283
+ nodes.onMount = null;
1284
+ };
1285
+ }, [anchors, entries, layer, nodes]);
1286
+ return (_jsx("g", { ref: ref, children: host &&
1287
+ createPortal(_jsxs("div", { className: "pointer-events-none absolute isolate z-0 overflow-hidden", ref: paneRef, children: [entries
1288
+ .filter((entry) => !gone.has(entry.zoneId))
1289
+ .map((entry) => (_jsx("div", { className: "pointer-events-none absolute -translate-x-1/2 -translate-y-1/2", ref: (element) => {
1290
+ if (element)
1291
+ nodes.set(entry.zoneId, element);
1292
+ else
1293
+ nodes.delete(entry.zoneId);
1294
+ }, style: { display: 'none' }, children: renderLabel(entry, layer.elements) }, entry.zoneId))), _jsx(OpenRoomLabelDetail, { entries: entries, layer: layer, renderDetail: renderDetail })] }), host) }));
1295
+ }