@7365admin1/layer-common 4.31.0 → 4.32.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.
@@ -0,0 +1,233 @@
1
+ <template>
2
+ <!--
3
+ Map editor (README §4 "Edit mode"): Wall · Zone · Door · Lift shaft · Move
4
+ · Delete | Undo ⌃Z | Save, one floor at a time on a flat 2D plan. A click
5
+ places the tool's shape; Move = click a shape, then its destination;
6
+ Delete = click a shape. Keyboard: Tab to the plan, arrows move the
7
+ cursor, Enter places. Save checks in plain words first, then writes the
8
+ building map through the API (versioned; a stale save is refused).
9
+ -->
10
+ <div ref="root" class="hero-editor" :class="{ 'hero-editor--fs': isFs }">
11
+ <div class="hero-editor__bar">
12
+ <div class="hero-seg" role="toolbar" aria-label="Map editor tools">
13
+ <button v-for="t in MAP_TOOLS" :key="t.id" type="button" class="hero-seg__opt" :aria-pressed="tool === t.id" @click="pickTool(t.id)">{{ t.label }}</button>
14
+ <button type="button" class="hero-seg__opt hero-seg__opt--undo" @click="state = editorReduce(state, { type: 'undo' })">Undo <span class="hero-seg__kbd">⌃Z</span></button>
15
+ <button type="button" class="hero-seg__opt hero-seg__opt--save" :disabled="saving" @click="save">{{ saving ? "Saving…" : "Save" }}</button>
16
+ </div>
17
+ <select v-model.number="floor" class="hero-input hero-select hero-editor__floor" aria-label="Floor">
18
+ <option v-for="f in floors" :key="f" :value="f">{{ floorName(f) }}</option>
19
+ </select>
20
+ <input
21
+ v-if="tool === 'zone'"
22
+ v-model="zoneName"
23
+ class="hero-input hero-editor__zone"
24
+ aria-label="Zone name"
25
+ placeholder="Zone name (required to save)"
26
+ @input="state = editorReduce(state, { type: 'rename', name: zoneName })"
27
+ />
28
+ <span class="hero-editor__hint">{{ TOOL_HINTS[tool] }}</span>
29
+ <v-btn variant="outlined" class="hero-editor__fs" @click="fullscreen">
30
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M8 3H5a2 2 0 0 0-2 2v3m18 0V5a2 2 0 0 0-2-2h-3m0 18h3a2 2 0 0 0 2-2v-3M3 16v3a2 2 0 0 0 2 2h3" /></svg>
31
+ Full screen
32
+ </v-btn>
33
+ </div>
34
+
35
+ <div v-if="state.notice" class="hero-editor__notice" :class="{ 'hero-editor__notice--ok': state.notice.ok }" role="status" aria-live="polite">{{ state.notice.text }}</div>
36
+
37
+ <div class="hero-editor__split">
38
+ <div
39
+ ref="plan"
40
+ class="hero-editor__plan"
41
+ tabindex="0"
42
+ role="application"
43
+ :aria-label="`Floor plan ${floorName(floor)}. Arrow keys move the cursor, Enter places.`"
44
+ @click="onPlanClick"
45
+ @keydown="onKey"
46
+ @blur="keyboard = false"
47
+ >
48
+ <span class="hero-editor__room" style="left: 8%; top: 10%; width: 34%; height: 30%" aria-hidden="true" />
49
+ <span class="hero-editor__room" style="left: 58%; top: 10%; width: 34%; height: 44%" aria-hidden="true" />
50
+ <span class="hero-editor__room" style="left: 8%; top: 62%; width: 60%; height: 28%" aria-hidden="true" />
51
+ <svg class="hero-editor__svg" viewBox="0 0 100 100" preserveAspectRatio="none" aria-hidden="true">
52
+ <template v-for="s in visible" :key="s.id">
53
+ <polygon v-if="s.kind === 'zone'" class="hero-editor__zone-shape" :class="{ 'hero-editor__sel': state.moving === s.id }" :points="pts(s)" vector-effect="non-scaling-stroke" />
54
+ <line v-else-if="s.kind === 'wall'" class="hero-editor__wall" :class="{ 'hero-editor__sel': state.moving === s.id }" :x1="s.points[0]!.x" :y1="s.points[0]!.y" :x2="s.points[1]!.x" :y2="s.points[1]!.y" />
55
+ </template>
56
+ </svg>
57
+ <template v-for="s in visible" :key="`l-${s.id}`">
58
+ <div v-if="s.kind === 'zone'" class="hero-editor__label" :style="boxStyle(s)" :title="shapeTitle(s)">{{ s.name || "Unnamed zone" }}</div>
59
+ <div v-else-if="s.kind === 'lift'" class="hero-editor__lift" :class="{ 'hero-editor__sel': state.moving === s.id }" :style="boxStyle(s)" :title="shapeTitle(s)">Lift</div>
60
+ <div v-else-if="s.kind === 'door'" class="hero-editor__door" :class="{ 'hero-editor__sel': state.moving === s.id }" :style="{ left: `${shapeAnchor(s).x}%`, top: `${shapeAnchor(s).y}%` }" :title="shapeTitle(s)" />
61
+ </template>
62
+ <span v-if="keyboard" class="hero-editor__cursor" :style="{ left: `${cursor.x}%`, top: `${cursor.y}%` }" aria-hidden="true" />
63
+ </div>
64
+
65
+ <aside class="hero-editor__aside">
66
+ <div class="hero-aside__title hero-editor__asidetitle">On {{ floorName(floor) }}</div>
67
+ <div v-for="s in visible" :key="`r-${s.id}`" class="hero-editor__row">
68
+ <span>{{ shapeTitle(s) }}</span><span class="hero-editor__pos">{{ shapeAnchor(s).x }}, {{ shapeAnchor(s).y }}</span>
69
+ </div>
70
+ <div v-if="!visible.length" class="hero-muted">Nothing drawn yet. Pick a tool and click the plan.</div>
71
+ <div class="hero-editor__note">The plan image comes from the robot; everything here is drawn on top of it. Keyboard: Tab to the plan, arrow keys move the cursor, Enter places.</div>
72
+ </aside>
73
+ </div>
74
+ </div>
75
+ </template>
76
+
77
+ <script setup lang="ts">
78
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
79
+ import { floorName, MAP_TOOLS } from "../../utils/robot-display";
80
+ import {
81
+ editorProblem,
82
+ editorReduce,
83
+ emptyEditor,
84
+ saveErrorText,
85
+ savedNotice,
86
+ shapeAnchor,
87
+ shapeBox,
88
+ shapesFromMap,
89
+ shapesOnFloor,
90
+ shapeTitle,
91
+ toMapBody,
92
+ TOOL_HINTS,
93
+ type TEditorShape,
94
+ type TEditorState,
95
+ type TEditorTool,
96
+ } from "../../utils/robot-building";
97
+
98
+ const props = withDefaults(
99
+ defineProps<{
100
+ /** The site's saved building map, or null (the first save creates it). */
101
+ map?: Record<string, any> | null;
102
+ floors: number[];
103
+ siteName?: string;
104
+ /** The robot a first save is attached to - maps are stored per robot. */
105
+ ownerRobotId?: string;
106
+ /** The floor to open on. */
107
+ startFloor?: number | null;
108
+ }>(),
109
+ { map: null, siteName: "", ownerRobotId: "", startFloor: null },
110
+ );
111
+ const emit = defineEmits<{ saved: [] }>();
112
+ const { createBuildingMap, updateBuildingMap } = useRobots();
113
+
114
+ const state = ref<TEditorState>(emptyEditor(shapesFromMap(props.map)));
115
+ /** A newer map (after our own save, or another tab's) replaces the shapes only while nothing is unsaved. */
116
+ watch(
117
+ () => props.map,
118
+ (m) => {
119
+ if (!state.value.dirty) state.value = { ...emptyEditor(shapesFromMap(m)), notice: state.value.notice };
120
+ },
121
+ );
122
+
123
+ const tool = ref<TEditorTool>("wall");
124
+ const zoneName = ref("");
125
+ const floor = ref<number>(props.startFloor !== null && props.floors.includes(props.startFloor) ? props.startFloor : props.floors[0] ?? 1);
126
+ watch(() => props.floors, (f) => {
127
+ if (!f.includes(floor.value)) floor.value = f[0] ?? 1;
128
+ });
129
+ const visible = computed(() => shapesOnFloor(state.value.shapes, floor.value));
130
+
131
+ function pickTool(id: TEditorTool) {
132
+ tool.value = id;
133
+ state.value = editorReduce(state.value, { type: "tool" });
134
+ }
135
+
136
+ /* ---- the plan: a click (or Enter at the keyboard cursor) ---- */
137
+ const plan = ref<HTMLElement | null>(null);
138
+ const cursor = ref({ x: 50, y: 50 });
139
+ const keyboard = ref(false);
140
+
141
+ /** The topmost shape under a point (walls and doors get a little slack). */
142
+ function hit(x: number, y: number): TEditorShape | null {
143
+ for (const s of [...visible.value].reverse()) {
144
+ const b = shapeBox(s);
145
+ const pad = s.kind === "wall" || s.kind === "door" ? 1.8 : 0;
146
+ if (x >= b.x - pad && x <= b.x + b.w + pad && y >= b.y - pad && y <= b.y + b.h + pad) return s;
147
+ }
148
+ return null;
149
+ }
150
+ function act(x: number, y: number) {
151
+ const target = hit(x, y);
152
+ // Move with a shape picked: this point is its destination, even over another shape.
153
+ if (target && !(tool.value === "move" && state.value.moving)) {
154
+ state.value = editorReduce(state.value, { type: "pick", id: target.id, tool: tool.value, zoneName: zoneName.value });
155
+ if (tool.value === "move" && target.kind === "zone") zoneName.value = target.name ?? "";
156
+ return;
157
+ }
158
+ state.value = editorReduce(state.value, { type: "place", tool: tool.value, floorNo: floor.value, x, y, zoneName: zoneName.value, floors: props.floors });
159
+ }
160
+ function onPlanClick(e: MouseEvent) {
161
+ const r = (e.currentTarget as HTMLElement).getBoundingClientRect();
162
+ const clamp = (v: number) => Math.max(0, Math.min(98, v));
163
+ act(clamp(((e.clientX - r.left) / r.width) * 100), clamp(((e.clientY - r.top) / r.height) * 100));
164
+ }
165
+ function onKey(e: KeyboardEvent) {
166
+ const step = e.shiftKey ? 10 : 2;
167
+ const move: Record<string, [number, number]> = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step] };
168
+ if (move[e.key]) {
169
+ e.preventDefault();
170
+ keyboard.value = true;
171
+ const [dx, dy] = move[e.key]!;
172
+ cursor.value = { x: Math.max(0, Math.min(98, cursor.value.x + dx)), y: Math.max(0, Math.min(98, cursor.value.y + dy)) };
173
+ } else if (e.key === "Enter" || e.key === " ") {
174
+ e.preventDefault();
175
+ keyboard.value = true;
176
+ act(cursor.value.x, cursor.value.y);
177
+ }
178
+ }
179
+
180
+ /** Ctrl/Cmd+Z anywhere in the editor except while typing a name. */
181
+ function onUndoKey(e: KeyboardEvent) {
182
+ if (!(e.ctrlKey || e.metaKey) || e.key.toLowerCase() !== "z" || e.shiftKey) return;
183
+ if ((e.target as HTMLElement)?.tagName === "INPUT") return;
184
+ e.preventDefault();
185
+ state.value = editorReduce(state.value, { type: "undo" });
186
+ }
187
+ onMounted(() => window.addEventListener("keydown", onUndoKey));
188
+ onBeforeUnmount(() => window.removeEventListener("keydown", onUndoKey));
189
+
190
+ /* ---- save ---- */
191
+ const saving = ref(false);
192
+ async function save() {
193
+ const problem = editorProblem(state.value);
194
+ if (problem) return void (state.value = editorReduce(state.value, { type: "notice", text: problem, ok: false }));
195
+ if (!props.map?._id && !props.ownerRobotId) {
196
+ return void (state.value = editorReduce(state.value, {
197
+ type: "notice",
198
+ text: "Can't save yet: this site has no robot to keep the map with. Add a robot to the site first, then draw the map.",
199
+ ok: false,
200
+ }));
201
+ }
202
+ const name = String(props.map?.name || `${props.siteName || "Site"} building`).slice(0, 60);
203
+ const body = toMapBody(state.value.shapes, name, props.map);
204
+ saving.value = true;
205
+ try {
206
+ if (props.map?._id) await updateBuildingMap(String(props.map._id), { ...body, version: Number(props.map.version) || 1 });
207
+ else await createBuildingMap({ ...body, robotId: props.ownerRobotId });
208
+ state.value = editorReduce(state.value, { type: "saved", text: savedNotice(new Date(), state.value.shapes.length, props.siteName) });
209
+ emit("saved");
210
+ } catch (e) {
211
+ state.value = editorReduce(state.value, { type: "notice", text: saveErrorText(e), ok: false });
212
+ } finally {
213
+ saving.value = false;
214
+ }
215
+ }
216
+
217
+ /* ---- drawing helpers + full screen ---- */
218
+ const pts = (s: TEditorShape) => s.points.map((p) => `${p.x},${p.y}`).join(" ");
219
+ function boxStyle(s: TEditorShape) {
220
+ const b = shapeBox(s);
221
+ return { left: `${b.x}%`, top: `${b.y}%`, width: `${b.w}%`, height: `${b.h}%` };
222
+ }
223
+ const root = ref<HTMLElement | null>(null);
224
+ const isFs = ref(false);
225
+ const onFs = () => (isFs.value = document.fullscreenElement === root.value);
226
+ onMounted(() => document.addEventListener("fullscreenchange", onFs));
227
+ onBeforeUnmount(() => document.removeEventListener("fullscreenchange", onFs));
228
+ function fullscreen() {
229
+ if (document.fullscreenElement) document.exitFullscreen?.();
230
+ else root.value?.requestFullscreen?.();
231
+ }
232
+ defineExpose({ root });
233
+ </script>
@@ -15,7 +15,7 @@
15
15
  :key="`tag-${t.key}`"
16
16
  class="hero-routetag"
17
17
  :class="{ 'hero-routetag--lg': large }"
18
- :style="{ left: t.left, top: t.top }"
18
+ :style="{ left: insideLeft(t.left, large ? `${t.name} · ${t.count} pts` : t.name, large ? 6.4 : 5.8, large ? 14 : 12), top: t.top }"
19
19
  >
20
20
  {{ t.name }}<template v-if="large"> · {{ t.count }} pts</template>
21
21
  </div>
@@ -28,12 +28,12 @@
28
28
  >
29
29
  {{ m.n }}
30
30
  </div>
31
- <div class="hero-cp__label" :class="{ 'hero-cp__label--lg': large }" :style="{ left: m.left, top: m.top }">{{ m.label }}</div>
31
+ <div class="hero-cp__label" :class="{ 'hero-cp__label--lg': large }" :style="{ left: insideLeft(m.left, m.label, large ? 6.2 : 5.6, 4), top: m.top }">{{ m.label }}</div>
32
32
  </template>
33
33
  </template>
34
34
 
35
35
  <script setup lang="ts">
36
- import { routeMarks, routePathD, routeTags, type TPatrolRouteView } from "../../utils/robot-display";
36
+ import { insideLeft, routeMarks, routePathD, routeTags, type TPatrolRouteView } from "../../utils/robot-display";
37
37
 
38
38
  withDefaults(
39
39
  defineProps<{
@@ -2,7 +2,8 @@
2
2
  <!--
3
3
  One estate's robots in the HERO look: the shared shell header (hidden when
4
4
  the host draws its own, e.g. the console's drill-down), then the site
5
- fleet overview, or one robot's detail when `robotId` is set. Every app
5
+ fleet overview, the building map or the lift queue (`view`), or one
6
+ robot's detail when `robotId` is set. Every app
6
7
  passes only its scope (org, site, the robot types it presents) and what
7
8
  this user's role may do; the API narrows again on its own side.
8
9
  -->
@@ -34,6 +35,35 @@
34
35
  @removed="onRemoved"
35
36
  @status="(s) => (connected = s.connected)"
36
37
  />
38
+ <RobotBuildingMap
39
+ v-else-if="view === 'map'"
40
+ :key="`map:${site}:${type}`"
41
+ :organization="organization"
42
+ :site="site"
43
+ :site-name="siteName"
44
+ :client-name="clientName"
45
+ :type="type"
46
+ :can-edit-map="canSetup && canEdit"
47
+ :back-to-network="backToNetwork"
48
+ @open="(id) => emit('open', id)"
49
+ @network="emit('network')"
50
+ @status="onFleetStatus"
51
+ />
52
+ <RobotLiftQueue
53
+ v-else-if="view === 'lifts'"
54
+ :key="`lifts:${site}:${type}`"
55
+ :organization="organization"
56
+ :site="site"
57
+ :site-name="siteName"
58
+ :client-name="clientName"
59
+ :type="type"
60
+ :can-command="canCommand"
61
+ :can-edit-map="canSetup && canEdit"
62
+ :back-to-network="backToNetwork"
63
+ @open="(id) => emit('open', id)"
64
+ @network="emit('network')"
65
+ @status="onFleetStatus"
66
+ />
37
67
  <RobotSiteFleet
38
68
  v-else
39
69
  :organization="organization"
@@ -71,6 +101,8 @@ import RobotHero from "./Hero.vue";
71
101
  import RobotSiteHeader from "./SiteHeader.vue";
72
102
  import RobotSiteFleet from "./SiteFleet.vue";
73
103
  import RobotDetail from "./Detail.vue";
104
+ import RobotBuildingMap from "./BuildingMap.vue";
105
+ import RobotLiftQueue from "./LiftQueue.vue";
74
106
  import RobotForm from "./Form.vue";
75
107
  import RobotToast from "./Toast.vue";
76
108
  import { roleLabel, type TRobotType } from "../../utils/robot-display";
@@ -91,6 +123,8 @@ const props = withDefaults(
91
123
  type?: TRobotType;
92
124
  /** One robot open, or none (the fleet). */
93
125
  robotId?: string;
126
+ /** Which screen: the fleet (default), the 3D building map, or the lift queue. A robot open wins. */
127
+ view?: "fleet" | "map" | "lifts";
94
128
  canSetup?: boolean;
95
129
  canEdit?: boolean;
96
130
  canCommand?: boolean;
@@ -107,6 +141,7 @@ const props = withDefaults(
107
141
  types: () => ["cleaning"],
108
142
  type: undefined,
109
143
  robotId: "",
144
+ view: "fleet",
110
145
  canSetup: false,
111
146
  canEdit: false,
112
147
  canCommand: false,
@@ -0,0 +1,66 @@
1
+ import { computed, ref, watch, type Ref } from "vue";
2
+
3
+ /**
4
+ * One site's building for the HERO building map and lift queue: the robots
5
+ * at the site this app may see (the API narrows by app), the ones of the type
6
+ * on screen, and the site's saved building map - the newest map saved on any
7
+ * of those robots (maps are stored per robot; the building is one).
8
+ *
9
+ * ponytail: the first 50 robots at the site and one map read per robot; a
10
+ * site-level map endpoint when a site holds more robots than that.
11
+ */
12
+ export default function useRobotBuilding(
13
+ organization: Ref<string>,
14
+ site: Ref<string>,
15
+ type: Ref<"cleaning" | "security">,
16
+ ) {
17
+ const { getSiteFleet, getSiteSummary, listBuildingMaps, getBuildingMap } = useRobots();
18
+
19
+ /** Every robot at the site this app may see, both types where the app shows both. */
20
+ const all = ref<Array<Record<string, any>>>([]);
21
+ const map = ref<Record<string, any> | null>(null);
22
+ const names = ref({ siteName: "", clientName: "" });
23
+ const loading = ref(true);
24
+ const error = ref("");
25
+
26
+ /** The robots of the type on screen (scope isolation: the other type is never drawn). */
27
+ const rows = computed(() => all.value.filter((r) => r.type === type.value));
28
+
29
+ async function loadMap(robots: Array<Record<string, any>>) {
30
+ const latest = await Promise.allSettled(robots.map((r) => listBuildingMaps(String(r._id), { limit: 1 })));
31
+ const summaries = latest
32
+ .flatMap((p) => (p.status === "fulfilled" ? p.value?.items ?? [] : []))
33
+ .sort((a: any, b: any) => new Date(b.updatedAt ?? 0).getTime() - new Date(a.updatedAt ?? 0).getTime());
34
+ return summaries[0] ? await getBuildingMap(String(summaries[0]._id)) : null;
35
+ }
36
+
37
+ let run = 0;
38
+ async function load() {
39
+ const mine = ++run;
40
+ loading.value = true;
41
+ error.value = "";
42
+ try {
43
+ const [fleet, summary] = await Promise.all([
44
+ getSiteFleet(organization.value, site.value, { page: 1, limit: 50 }),
45
+ getSiteSummary(organization.value, site.value).catch(() => null),
46
+ ]);
47
+ if (mine !== run) return;
48
+ all.value = fleet?.items ?? [];
49
+ names.value = { siteName: summary?.site?.name ?? "", clientName: summary?.client?.name ?? "" };
50
+ const found = await loadMap(all.value);
51
+ if (mine !== run) return;
52
+ map.value = found;
53
+ } catch (e: any) {
54
+ if (mine !== run) return;
55
+ all.value = [];
56
+ map.value = null;
57
+ error.value = e?.data?.message || "Could not load this building.";
58
+ } finally {
59
+ if (mine === run) loading.value = false;
60
+ }
61
+ }
62
+
63
+ watch([organization, site], load, { immediate: true });
64
+
65
+ return { all, rows, map, names, loading, error, reload: load };
66
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "4.31.0",
5
+ "version": "4.32.0",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
@@ -36,7 +36,7 @@
36
36
  "build": "nuxt build .playground",
37
37
  "generate": "nuxt generate .playground",
38
38
  "preview": "nuxt preview .playground",
39
- "test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && esbuild utils/client-invite-modules.ts --bundle --platform=node --format=esm --outfile=test/.build/client-invite-modules.mjs --log-level=error && esbuild utils/robot-display.ts --bundle --platform=node --format=esm --outfile=test/.build/robot-display.mjs --log-level=error && esbuild utils/building-map.ts --bundle --platform=node --format=esm --outfile=test/.build/building-map.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
39
+ "test": "esbuild composables/useVisitorSocket.ts --format=esm --outfile=test/.build/useVisitorSocket.mjs --log-level=error && esbuild composables/usePlateToastWindow.ts --format=esm --outfile=test/.build/usePlateToastWindow.mjs --log-level=error && esbuild utils/client-invite-modules.ts --bundle --platform=node --format=esm --outfile=test/.build/client-invite-modules.mjs --log-level=error && esbuild utils/robot-display.ts --bundle --platform=node --format=esm --outfile=test/.build/robot-display.mjs --log-level=error && esbuild utils/building-map.ts --bundle --platform=node --format=esm --outfile=test/.build/building-map.mjs --log-level=error && esbuild utils/robot-building.ts --bundle --platform=node --format=esm --outfile=test/.build/robot-building.mjs --log-level=error && node --experimental-strip-types --test \"test/*.test.mjs\" && yarn test:units",
40
40
  "test:units": "node --experimental-strip-types --test \"utils/*.test.ts\"",
41
41
  "release": "yarn run build && changeset publish"
42
42
  },