@jcsoftdev/ui 0.7.0 → 0.8.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.
- package/CHANGELOG.md +51 -0
- package/README.md +9 -0
- package/dist/atoms/elapsed-time/elapsed-time.d.ts +29 -0
- package/dist/atoms/elapsed-time/elapsed-time.js +65 -0
- package/dist/atoms/elapsed-time/index.d.ts +2 -0
- package/dist/atoms/elapsed-time/index.js +9 -0
- package/dist/atoms/elapsed-time/model.d.ts +17 -0
- package/dist/atoms/elapsed-time/model.js +37 -0
- package/dist/atoms/elapsed-time/ticker.d.ts +5 -0
- package/dist/atoms/elapsed-time/ticker.js +37 -0
- package/dist/atoms/index.d.ts +2 -0
- package/dist/atoms/index.js +2 -0
- package/dist/atoms/table-glyph/index.d.ts +2 -0
- package/dist/atoms/table-glyph/index.js +14 -0
- package/dist/atoms/table-glyph/layout.d.ts +38 -0
- package/dist/atoms/table-glyph/layout.js +152 -0
- package/dist/atoms/table-glyph/table-glyph.d.ts +32 -0
- package/dist/atoms/table-glyph/table-glyph.js +131 -0
- package/dist/containers/floor-plan/edit-model.d.ts +33 -0
- package/dist/containers/floor-plan/edit-model.js +82 -0
- package/dist/containers/floor-plan/floor-plan.d.ts +83 -0
- package/dist/containers/floor-plan/floor-plan.js +278 -0
- package/dist/containers/floor-plan/index.d.ts +3 -0
- package/dist/containers/floor-plan/index.js +21 -0
- package/dist/containers/floor-plan/model.d.ts +36 -0
- package/dist/containers/floor-plan/model.js +52 -0
- package/dist/containers/floor-plan/use-floor-plan-edit.d.ts +26 -0
- package/dist/containers/floor-plan/use-floor-plan-edit.js +97 -0
- package/dist/containers/index.d.ts +1 -0
- package/dist/containers/index.js +1 -0
- package/dist/i18n/strings.d.ts +48 -0
- package/dist/i18n/strings.js +24 -0
- package/dist/shell/bottom-nav/bottom-nav.d.ts +5 -1
- package/dist/shell/bottom-nav/bottom-nav.js +52 -9
- package/package.json +2 -2
- package/src/atoms/elapsed-time/elapsed-time.tsx +101 -0
- package/src/atoms/elapsed-time/index.ts +6 -0
- package/src/atoms/elapsed-time/model.ts +54 -0
- package/src/atoms/elapsed-time/ticker.ts +39 -0
- package/src/atoms/index.ts +2 -0
- package/src/atoms/table-glyph/index.ts +14 -0
- package/src/atoms/table-glyph/layout.ts +187 -0
- package/src/atoms/table-glyph/table-glyph.tsx +161 -0
- package/src/containers/floor-plan/edit-model.ts +115 -0
- package/src/containers/floor-plan/floor-plan.tsx +393 -0
- package/src/containers/floor-plan/index.ts +20 -0
- package/src/containers/floor-plan/model.ts +81 -0
- package/src/containers/floor-plan/use-floor-plan-edit.ts +135 -0
- package/src/containers/index.ts +1 -0
- package/src/i18n/strings.ts +48 -0
- package/src/shell/bottom-nav/bottom-nav.tsx +78 -8
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { type CSSProperties, type ReactNode, useId, useRef } from "react";
|
|
4
|
+
import type { TableShape } from "../../atoms/table-glyph/layout.js";
|
|
5
|
+
import { TableGlyph, type TableGlyphPattern } from "../../atoms/table-glyph/table-glyph.js";
|
|
6
|
+
import { useUiStrings } from "../../i18n/index.js";
|
|
7
|
+
import { RotateCcw } from "../../icons/index.js";
|
|
8
|
+
import type { ClassNames } from "../../utils/class-names.js";
|
|
9
|
+
import { cn } from "../../utils/cn.js";
|
|
10
|
+
import { useComposedRefs } from "../../utils/compose.js";
|
|
11
|
+
import { useRovingFocus } from "../../utils/roving-focus.js";
|
|
12
|
+
import { customToneStyle, isCustomTone } from "../../utils/tone.js";
|
|
13
|
+
import { useContainerWidth } from "../../utils/use-container-width.js";
|
|
14
|
+
import type { ResizeHandle } from "./edit-model.js";
|
|
15
|
+
import { DEFAULT_PX_PER_UNIT, groupOutlines, labelPlacement, percentBox } from "./model.js";
|
|
16
|
+
import { useFloorPlanEdit } from "./use-floor-plan-edit.js";
|
|
17
|
+
|
|
18
|
+
export type FloorPlanBuiltinTone = "neutral" | "accent" | "success" | "warning" | "danger" | "info";
|
|
19
|
+
|
|
20
|
+
/** A built-in tone, or the name of a tone declared in `tonePalettes`. */
|
|
21
|
+
export type FloorPlanTone = FloorPlanBuiltinTone | (string & {});
|
|
22
|
+
|
|
23
|
+
export type FloorPlanRing = "none" | "late" | "selected";
|
|
24
|
+
|
|
25
|
+
export interface FloorPlanItem {
|
|
26
|
+
id: string;
|
|
27
|
+
/** Position and size in grid units. */
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
shape: TableShape;
|
|
33
|
+
seats: number;
|
|
34
|
+
/** Chairs drawn as taken. */
|
|
35
|
+
occupied?: number | undefined;
|
|
36
|
+
/** Table name; always visible, inside the table or under it on small shapes. */
|
|
37
|
+
label: string;
|
|
38
|
+
/** Text of the state. Pair it with `statusIcon`: the state is never carried by colour alone. */
|
|
39
|
+
statusLabel?: string | undefined;
|
|
40
|
+
/** Decorative glyph shown before `statusLabel`. */
|
|
41
|
+
statusIcon?: ReactNode;
|
|
42
|
+
/** Colour of the table and the status glyph. Defaults to `neutral`. */
|
|
43
|
+
tone?: FloorPlanTone | undefined;
|
|
44
|
+
/** Table surface; see `TableGlyph`. Defaults to `solid`. */
|
|
45
|
+
pattern?: TableGlyphPattern | undefined;
|
|
46
|
+
/** A uniform ring around the whole item: `late` (danger) or `selected` (accent). `selectedId` wins over it. */
|
|
47
|
+
ring?: FloorPlanRing | undefined;
|
|
48
|
+
/** Items sharing a `groupId` (merged tables) get one outline around the group. */
|
|
49
|
+
groupId?: string | undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface FloorPlanLandmark {
|
|
53
|
+
id: string;
|
|
54
|
+
/** What it is (`wall`, `door`, `bar`, `stairs`, `restroom`...); exposed as `data-kind`. */
|
|
55
|
+
kind: string;
|
|
56
|
+
label: string;
|
|
57
|
+
x: number;
|
|
58
|
+
y: number;
|
|
59
|
+
width: number;
|
|
60
|
+
height: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type FloorPlanPart =
|
|
64
|
+
| "root"
|
|
65
|
+
| "canvas"
|
|
66
|
+
| "item"
|
|
67
|
+
| "glyph"
|
|
68
|
+
| "label"
|
|
69
|
+
| "status"
|
|
70
|
+
| "landmark"
|
|
71
|
+
| "group"
|
|
72
|
+
| "handle"
|
|
73
|
+
| "rotate";
|
|
74
|
+
|
|
75
|
+
export interface FloorPlanProps {
|
|
76
|
+
/** Canvas size in grid units. The canvas fills its container and keeps this aspect ratio. */
|
|
77
|
+
width: number;
|
|
78
|
+
height: number;
|
|
79
|
+
items: readonly FloorPlanItem[];
|
|
80
|
+
landmarks?: readonly FloorPlanLandmark[];
|
|
81
|
+
/** Controlled selection; the item gets the selected ring and `aria-pressed`. */
|
|
82
|
+
selectedId?: string | null;
|
|
83
|
+
/** Called with the id of the item or landmark that was picked, by click, keyboard or the start of a drag. */
|
|
84
|
+
onSelect?: (id: string) => void;
|
|
85
|
+
/**
|
|
86
|
+
* Lets people rearrange the plan: drag an item or landmark to move it, drag a corner handle of
|
|
87
|
+
* the selected one to resize, press the rotate button or `r` to turn it a quarter, Alt with an
|
|
88
|
+
* arrow to nudge and Alt and Shift with an arrow to resize. Edits snap to `snap`, stay inside the
|
|
89
|
+
* canvas and are reported through `onChange`; the plan stays controlled by `items` and `landmarks`.
|
|
90
|
+
* Edited boxes are not checked against each other, so overlaps are possible.
|
|
91
|
+
*/
|
|
92
|
+
editable?: boolean;
|
|
93
|
+
/** Receives the full lists with the edited box applied; called once per drop and once per key press. */
|
|
94
|
+
onChange?: (items: FloorPlanItem[], landmarks: FloorPlanLandmark[]) => void;
|
|
95
|
+
/** Grid step in units that positions and sizes snap to while editing. Defaults to 1. */
|
|
96
|
+
snap?: number;
|
|
97
|
+
/** Smallest width or height in units while editing. Defaults to 1. */
|
|
98
|
+
minSize?: number;
|
|
99
|
+
/** Accessible name of the plan. Defaults to the localized "Floor plan". */
|
|
100
|
+
"aria-label"?: string;
|
|
101
|
+
className?: string;
|
|
102
|
+
/** Per-part classes merged with `cn`; consumer classes win. `className` still targets the root and is merged last. */
|
|
103
|
+
classNames?: ClassNames<FloorPlanPart>;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const toneClasses: Record<FloorPlanBuiltinTone, string> = {
|
|
107
|
+
neutral: "text-ink-2",
|
|
108
|
+
accent: "text-accent",
|
|
109
|
+
success: "text-success",
|
|
110
|
+
warning: "text-warning",
|
|
111
|
+
danger: "text-danger",
|
|
112
|
+
info: "text-info",
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const builtinTones = new Set<string>(Object.keys(toneClasses));
|
|
116
|
+
|
|
117
|
+
const ringClasses: Record<FloorPlanRing, string> = {
|
|
118
|
+
none: "",
|
|
119
|
+
late: "ring-2 ring-danger",
|
|
120
|
+
selected: "ring-2 ring-accent",
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const HANDLES: readonly ResizeHandle[] = ["nw", "ne", "sw", "se"];
|
|
124
|
+
|
|
125
|
+
const handleClasses: Partial<Record<ResizeHandle, string>> = {
|
|
126
|
+
nw: "-top-1.5 -left-1.5 cursor-nwse-resize",
|
|
127
|
+
ne: "-top-1.5 -right-1.5 cursor-nesw-resize",
|
|
128
|
+
sw: "-bottom-1.5 -left-1.5 cursor-nesw-resize",
|
|
129
|
+
se: "-right-1.5 -bottom-1.5 cursor-nwse-resize",
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const hatch =
|
|
133
|
+
"bg-[repeating-linear-gradient(135deg,var(--line-strong)_0_1px,transparent_1px_7px)] border border-line-strong";
|
|
134
|
+
|
|
135
|
+
function format(template: string, count: number): string {
|
|
136
|
+
return template.replace("{count}", String(count));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A salon plan on a logical-unit canvas, read-only unless `editable`. The canvas fills its container and keeps the
|
|
141
|
+
* `width` x `height` aspect ratio, and every item is placed in percentages of it, so hit areas
|
|
142
|
+
* always match what is drawn. Items are buttons in array order with roving arrow-key focus. A
|
|
143
|
+
* state shows as an icon plus text (never colour alone), `late` and `selected` as one ring around
|
|
144
|
+
* the whole item, and the label moves under the shape when the table is too small to hold it.
|
|
145
|
+
*/
|
|
146
|
+
export function FloorPlan({
|
|
147
|
+
width,
|
|
148
|
+
height,
|
|
149
|
+
items,
|
|
150
|
+
landmarks = [],
|
|
151
|
+
selectedId,
|
|
152
|
+
onSelect,
|
|
153
|
+
editable = false,
|
|
154
|
+
onChange,
|
|
155
|
+
snap = 1,
|
|
156
|
+
minSize = 1,
|
|
157
|
+
"aria-label": ariaLabel,
|
|
158
|
+
className,
|
|
159
|
+
classNames,
|
|
160
|
+
}: FloorPlanProps) {
|
|
161
|
+
const strings = useUiStrings();
|
|
162
|
+
const canvasRef = useRef<HTMLDivElement>(null);
|
|
163
|
+
const measured = useContainerWidth(canvasRef);
|
|
164
|
+
const pxPerUnit = measured === null ? DEFAULT_PX_PER_UNIT : measured / width;
|
|
165
|
+
const { rootProps, getItemProps } = useRovingFocus({ activeId: selectedId ?? null });
|
|
166
|
+
const rootRef = useComposedRefs(canvasRef, rootProps.ref);
|
|
167
|
+
const selectable = onSelect !== undefined || selectedId !== undefined;
|
|
168
|
+
const hintId = useId();
|
|
169
|
+
const edit = useFloorPlanEdit({
|
|
170
|
+
enabled: editable,
|
|
171
|
+
items,
|
|
172
|
+
landmarks,
|
|
173
|
+
bounds: { width, height, snap, minSize },
|
|
174
|
+
canvasRef,
|
|
175
|
+
onChange,
|
|
176
|
+
onSelect,
|
|
177
|
+
});
|
|
178
|
+
const shownItems = items.map(edit.view);
|
|
179
|
+
const shownLandmarks = landmarks.map(edit.view);
|
|
180
|
+
const outlines = groupOutlines(shownItems);
|
|
181
|
+
const selectedBox = editable
|
|
182
|
+
? [...shownItems, ...shownLandmarks].find((entry) => entry.id === selectedId)
|
|
183
|
+
: undefined;
|
|
184
|
+
const selectedSource = selectedBox
|
|
185
|
+
? [...items, ...landmarks].find((entry) => entry.id === selectedBox.id)
|
|
186
|
+
: undefined;
|
|
187
|
+
|
|
188
|
+
return (
|
|
189
|
+
<div className={cn("@container/floor w-full", classNames?.root, className)}>
|
|
190
|
+
{/* biome-ignore lint/a11y/useSemanticElements: the canvas is a positioned div; a fieldset cannot be laid out as a canvas */}
|
|
191
|
+
<div
|
|
192
|
+
{...rootProps}
|
|
193
|
+
ref={rootRef}
|
|
194
|
+
role="group"
|
|
195
|
+
aria-label={ariaLabel ?? strings.floorPlan}
|
|
196
|
+
style={{
|
|
197
|
+
aspectRatio: `${width} / ${height}`,
|
|
198
|
+
...(editable
|
|
199
|
+
? {
|
|
200
|
+
backgroundImage: "radial-gradient(circle, var(--line-strong) 1px, transparent 1px)",
|
|
201
|
+
backgroundSize: `${100 / width}% ${100 / height}%`,
|
|
202
|
+
}
|
|
203
|
+
: null),
|
|
204
|
+
}}
|
|
205
|
+
data-editable={editable ? "" : undefined}
|
|
206
|
+
className={cn(
|
|
207
|
+
"relative w-full rounded-card border border-line bg-surface-sunken focus-visible:focus-ring",
|
|
208
|
+
classNames?.canvas,
|
|
209
|
+
)}
|
|
210
|
+
>
|
|
211
|
+
{editable ? (
|
|
212
|
+
<p id={hintId} className="sr-only">
|
|
213
|
+
{strings.floorPlanEditHint}
|
|
214
|
+
</p>
|
|
215
|
+
) : null}
|
|
216
|
+
{shownLandmarks.map((landmark) => {
|
|
217
|
+
const common = {
|
|
218
|
+
"data-kind": landmark.kind,
|
|
219
|
+
style: percentBox(landmark, width, height),
|
|
220
|
+
className: cn(
|
|
221
|
+
"absolute flex items-center justify-center overflow-hidden rounded-control p-0.5 text-center text-ink-2 text-meta",
|
|
222
|
+
hatch,
|
|
223
|
+
classNames?.landmark,
|
|
224
|
+
),
|
|
225
|
+
};
|
|
226
|
+
const label = (
|
|
227
|
+
<span className="max-w-full truncate rounded-chip bg-surface px-1">
|
|
228
|
+
{landmark.label}
|
|
229
|
+
</span>
|
|
230
|
+
);
|
|
231
|
+
if (!editable) {
|
|
232
|
+
return (
|
|
233
|
+
<div key={landmark.id} {...common}>
|
|
234
|
+
{label}
|
|
235
|
+
</div>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
const selected = selectedId === landmark.id;
|
|
239
|
+
return (
|
|
240
|
+
<button
|
|
241
|
+
key={landmark.id}
|
|
242
|
+
type="button"
|
|
243
|
+
data-landmark={landmark.id}
|
|
244
|
+
aria-pressed={selectable ? selected : undefined}
|
|
245
|
+
aria-describedby={hintId}
|
|
246
|
+
{...common}
|
|
247
|
+
className={cn(
|
|
248
|
+
common.className,
|
|
249
|
+
"cursor-grab touch-none focus-visible:focus-ring active:cursor-grabbing",
|
|
250
|
+
selected && "ring-2 ring-accent",
|
|
251
|
+
)}
|
|
252
|
+
{...getItemProps(landmark.id, { onKeyDown: edit.onKeyDown(landmark) })}
|
|
253
|
+
{...edit.dragProps(landmark, "move")}
|
|
254
|
+
onClick={() => onSelect?.(landmark.id)}
|
|
255
|
+
>
|
|
256
|
+
<span className="sr-only">{landmark.label}</span>
|
|
257
|
+
<span aria-hidden="true" className="contents">
|
|
258
|
+
{label}
|
|
259
|
+
</span>
|
|
260
|
+
</button>
|
|
261
|
+
);
|
|
262
|
+
})}
|
|
263
|
+
{outlines.map((outline) => (
|
|
264
|
+
<div
|
|
265
|
+
key={outline.groupId}
|
|
266
|
+
aria-hidden="true"
|
|
267
|
+
data-group={outline.groupId}
|
|
268
|
+
style={percentBox(outline, width, height)}
|
|
269
|
+
className={cn(
|
|
270
|
+
"pointer-events-none absolute rounded-card border border-ink-3 border-dashed",
|
|
271
|
+
classNames?.group,
|
|
272
|
+
)}
|
|
273
|
+
/>
|
|
274
|
+
))}
|
|
275
|
+
{shownItems.map((item) => {
|
|
276
|
+
const selected = selectedId === item.id || item.ring === "selected";
|
|
277
|
+
const ring: FloorPlanRing = selected ? "selected" : (item.ring ?? "none");
|
|
278
|
+
const tone = item.tone ?? "neutral";
|
|
279
|
+
const placement = labelPlacement(item, pxPerUnit);
|
|
280
|
+
const hasStatus = item.statusLabel !== undefined || item.statusIcon !== undefined;
|
|
281
|
+
const style: CSSProperties | undefined = customToneStyle(
|
|
282
|
+
tone,
|
|
283
|
+
builtinTones,
|
|
284
|
+
percentBox(item, width, height),
|
|
285
|
+
);
|
|
286
|
+
return (
|
|
287
|
+
<button
|
|
288
|
+
key={item.id}
|
|
289
|
+
type="button"
|
|
290
|
+
data-item={item.id}
|
|
291
|
+
data-shape={item.shape}
|
|
292
|
+
data-tone={tone === "neutral" ? undefined : tone}
|
|
293
|
+
data-ring={ring === "none" ? undefined : ring}
|
|
294
|
+
data-label={placement}
|
|
295
|
+
data-selected={selected ? "" : undefined}
|
|
296
|
+
aria-pressed={selectable ? selected : undefined}
|
|
297
|
+
style={style}
|
|
298
|
+
aria-describedby={editable ? hintId : undefined}
|
|
299
|
+
{...getItemProps(item.id, { onKeyDown: edit.onKeyDown(item) })}
|
|
300
|
+
{...(editable ? edit.dragProps(item, "move") : null)}
|
|
301
|
+
onClick={() => onSelect?.(item.id)}
|
|
302
|
+
className={cn(
|
|
303
|
+
"absolute block rounded-control p-0 text-ink focus-visible:focus-ring",
|
|
304
|
+
editable && "cursor-grab touch-none active:cursor-grabbing",
|
|
305
|
+
isCustomTone(tone, builtinTones)
|
|
306
|
+
? "text-(--tone-c)"
|
|
307
|
+
: (toneClasses[tone as FloorPlanBuiltinTone] ?? toneClasses.neutral),
|
|
308
|
+
ringClasses[ring],
|
|
309
|
+
classNames?.item,
|
|
310
|
+
)}
|
|
311
|
+
>
|
|
312
|
+
<TableGlyph
|
|
313
|
+
shape={item.shape}
|
|
314
|
+
seats={item.seats}
|
|
315
|
+
occupied={item.occupied ?? 0}
|
|
316
|
+
width={item.width}
|
|
317
|
+
height={item.height}
|
|
318
|
+
pattern={item.pattern}
|
|
319
|
+
size="fill"
|
|
320
|
+
className={cn(
|
|
321
|
+
"pointer-events-none absolute inset-0 text-current",
|
|
322
|
+
classNames?.glyph,
|
|
323
|
+
)}
|
|
324
|
+
/>
|
|
325
|
+
<span
|
|
326
|
+
className={cn(
|
|
327
|
+
"pointer-events-none flex flex-col items-center text-center",
|
|
328
|
+
placement === "inside"
|
|
329
|
+
? "absolute inset-0 justify-center"
|
|
330
|
+
: "absolute inset-x-0 top-full pt-0.5",
|
|
331
|
+
classNames?.label,
|
|
332
|
+
)}
|
|
333
|
+
>
|
|
334
|
+
<span className="whitespace-nowrap font-bold text-ink text-meta">{item.label}</span>
|
|
335
|
+
{hasStatus ? (
|
|
336
|
+
<span
|
|
337
|
+
className={cn(
|
|
338
|
+
"inline-flex items-center gap-0.5 whitespace-nowrap text-ink-2 text-count",
|
|
339
|
+
classNames?.status,
|
|
340
|
+
)}
|
|
341
|
+
>
|
|
342
|
+
{item.statusIcon ? (
|
|
343
|
+
<span aria-hidden="true" className="inline-flex shrink-0 text-current">
|
|
344
|
+
{item.statusIcon}
|
|
345
|
+
</span>
|
|
346
|
+
) : null}
|
|
347
|
+
{item.statusLabel}
|
|
348
|
+
</span>
|
|
349
|
+
) : null}
|
|
350
|
+
</span>
|
|
351
|
+
<span className="sr-only">
|
|
352
|
+
{format(item.seats === 1 ? strings.seatsOne : strings.seats, item.seats)}
|
|
353
|
+
{item.ring === "late" ? `, ${strings.late}` : ""}
|
|
354
|
+
</span>
|
|
355
|
+
</button>
|
|
356
|
+
);
|
|
357
|
+
})}
|
|
358
|
+
{selectedBox && selectedSource ? (
|
|
359
|
+
<div
|
|
360
|
+
data-edit-overlay={selectedBox.id}
|
|
361
|
+
style={percentBox(selectedBox, width, height)}
|
|
362
|
+
className="pointer-events-none absolute"
|
|
363
|
+
>
|
|
364
|
+
{HANDLES.map((handle) => (
|
|
365
|
+
<span
|
|
366
|
+
key={handle}
|
|
367
|
+
aria-hidden="true"
|
|
368
|
+
data-handle={handle}
|
|
369
|
+
{...edit.dragProps(selectedSource, handle)}
|
|
370
|
+
className={cn(
|
|
371
|
+
"pointer-events-auto absolute size-2.5 touch-none rounded-chip border border-accent bg-surface before:absolute before:-inset-1.5 before:content-['']",
|
|
372
|
+
handleClasses[handle],
|
|
373
|
+
classNames?.handle,
|
|
374
|
+
)}
|
|
375
|
+
/>
|
|
376
|
+
))}
|
|
377
|
+
<button
|
|
378
|
+
type="button"
|
|
379
|
+
aria-label={strings.rotate}
|
|
380
|
+
onClick={() => edit.rotate(selectedSource)}
|
|
381
|
+
className={cn(
|
|
382
|
+
"pointer-events-auto absolute right-0 bottom-full mb-1 inline-flex size-control-sm items-center justify-center rounded-control border border-line bg-surface text-ink-2 hover:bg-surface-2 focus-visible:focus-ring [&_svg]:size-4",
|
|
383
|
+
classNames?.rotate,
|
|
384
|
+
)}
|
|
385
|
+
>
|
|
386
|
+
<RotateCcw aria-hidden="true" />
|
|
387
|
+
</button>
|
|
388
|
+
</div>
|
|
389
|
+
) : null}
|
|
390
|
+
</div>
|
|
391
|
+
</div>
|
|
392
|
+
);
|
|
393
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type EditBounds,
|
|
3
|
+
editKey,
|
|
4
|
+
moveBox,
|
|
5
|
+
type ResizeHandle,
|
|
6
|
+
resizeBox,
|
|
7
|
+
rotateBox,
|
|
8
|
+
snapTo,
|
|
9
|
+
} from "./edit-model.js";
|
|
10
|
+
export {
|
|
11
|
+
FloorPlan,
|
|
12
|
+
type FloorPlanBuiltinTone,
|
|
13
|
+
type FloorPlanItem,
|
|
14
|
+
type FloorPlanLandmark,
|
|
15
|
+
type FloorPlanPart,
|
|
16
|
+
type FloorPlanProps,
|
|
17
|
+
type FloorPlanRing,
|
|
18
|
+
type FloorPlanTone,
|
|
19
|
+
} from "./floor-plan.js";
|
|
20
|
+
export { type GroupOutline, groupOutlines, type LabelPlacement, labelPlacement } from "./model.js";
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { TableShape } from "../../atoms/table-glyph/layout.js";
|
|
2
|
+
|
|
3
|
+
export interface Box {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface FloorPlanBox extends Box {
|
|
11
|
+
id: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type LabelPlacement = "inside" | "below";
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_PX_PER_UNIT = 32;
|
|
17
|
+
const CHAIR_BAND = 0.24;
|
|
18
|
+
const MIN_INSIDE_WIDTH = 44;
|
|
19
|
+
const MIN_INSIDE_HEIGHT = 30;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether an item's label fits inside the table body. The usable area is the box minus the chair
|
|
23
|
+
* band (and a round body only offers its inscribed square), measured in pixels from the canvas
|
|
24
|
+
* scale; anything smaller puts the label under the shape.
|
|
25
|
+
*/
|
|
26
|
+
export function labelPlacement(
|
|
27
|
+
item: { width: number; height: number; shape: TableShape },
|
|
28
|
+
pxPerUnit: number = DEFAULT_PX_PER_UNIT,
|
|
29
|
+
): LabelPlacement {
|
|
30
|
+
if (item.shape === "bar") return "below";
|
|
31
|
+
const usable = (units: number) => units * pxPerUnit * (1 - 2 * CHAIR_BAND);
|
|
32
|
+
const round = item.shape === "round" ? Math.SQRT1_2 : 1;
|
|
33
|
+
const fits =
|
|
34
|
+
usable(item.width) * round >= MIN_INSIDE_WIDTH &&
|
|
35
|
+
usable(item.height) * round >= MIN_INSIDE_HEIGHT;
|
|
36
|
+
return fits ? "inside" : "below";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function percentBox(box: Box, width: number, height: number) {
|
|
40
|
+
return {
|
|
41
|
+
left: `${(box.x / width) * 100}%`,
|
|
42
|
+
top: `${(box.y / height) * 100}%`,
|
|
43
|
+
width: `${(box.width / width) * 100}%`,
|
|
44
|
+
height: `${(box.height / height) * 100}%`,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface GroupOutline extends Box {
|
|
49
|
+
groupId: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const GROUP_PADDING = 0.2;
|
|
53
|
+
|
|
54
|
+
/** One outline per `groupId` shared by at least two items: their union box plus a small padding. */
|
|
55
|
+
export function groupOutlines(
|
|
56
|
+
items: ReadonlyArray<Box & { groupId?: string | undefined }>,
|
|
57
|
+
): GroupOutline[] {
|
|
58
|
+
const groups = new Map<string, Box[]>();
|
|
59
|
+
for (const item of items) {
|
|
60
|
+
if (item.groupId === undefined) continue;
|
|
61
|
+
const list = groups.get(item.groupId) ?? [];
|
|
62
|
+
list.push(item);
|
|
63
|
+
groups.set(item.groupId, list);
|
|
64
|
+
}
|
|
65
|
+
const outlines: GroupOutline[] = [];
|
|
66
|
+
for (const [groupId, members] of groups) {
|
|
67
|
+
if (members.length < 2) continue;
|
|
68
|
+
const left = Math.min(...members.map((m) => m.x));
|
|
69
|
+
const top = Math.min(...members.map((m) => m.y));
|
|
70
|
+
const right = Math.max(...members.map((m) => m.x + m.width));
|
|
71
|
+
const bottom = Math.max(...members.map((m) => m.y + m.height));
|
|
72
|
+
outlines.push({
|
|
73
|
+
groupId,
|
|
74
|
+
x: left - GROUP_PADDING,
|
|
75
|
+
y: top - GROUP_PADDING,
|
|
76
|
+
width: right - left + 2 * GROUP_PADDING,
|
|
77
|
+
height: bottom - top + 2 * GROUP_PADDING,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return outlines;
|
|
81
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { type PointerEvent, type RefObject, useRef, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
type EditBounds,
|
|
4
|
+
editKey,
|
|
5
|
+
moveBox,
|
|
6
|
+
type ResizeHandle,
|
|
7
|
+
resizeBox,
|
|
8
|
+
sameBox,
|
|
9
|
+
} from "./edit-model.js";
|
|
10
|
+
import type { Box, FloorPlanBox } from "./model.js";
|
|
11
|
+
|
|
12
|
+
type DragMode = "move" | ResizeHandle;
|
|
13
|
+
|
|
14
|
+
interface Drag {
|
|
15
|
+
id: string;
|
|
16
|
+
mode: DragMode;
|
|
17
|
+
startX: number;
|
|
18
|
+
startY: number;
|
|
19
|
+
origin: Box;
|
|
20
|
+
unit: number;
|
|
21
|
+
moved: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DRAG_THRESHOLD_PX = 3;
|
|
25
|
+
|
|
26
|
+
function capturePointer(element: HTMLElement, pointerId: number): boolean {
|
|
27
|
+
try {
|
|
28
|
+
element.setPointerCapture(pointerId);
|
|
29
|
+
return true;
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const pick = ({ x, y, width, height }: Box): Box => ({ x, y, width, height });
|
|
36
|
+
|
|
37
|
+
interface Options<I extends FloorPlanBox, L extends FloorPlanBox> {
|
|
38
|
+
enabled: boolean;
|
|
39
|
+
items: readonly I[];
|
|
40
|
+
landmarks: readonly L[];
|
|
41
|
+
bounds: EditBounds;
|
|
42
|
+
canvasRef: RefObject<HTMLElement | null>;
|
|
43
|
+
onChange?: ((items: I[], landmarks: L[]) => void) | undefined;
|
|
44
|
+
onSelect?: ((id: string) => void) | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Drag, resize, rotate and keyboard edits of floor plan boxes; commits only on release, never mid-drag. */
|
|
48
|
+
export function useFloorPlanEdit<I extends FloorPlanBox, L extends FloorPlanBox>({
|
|
49
|
+
enabled,
|
|
50
|
+
items,
|
|
51
|
+
landmarks,
|
|
52
|
+
bounds,
|
|
53
|
+
canvasRef,
|
|
54
|
+
onChange,
|
|
55
|
+
onSelect,
|
|
56
|
+
}: Options<I, L>) {
|
|
57
|
+
const [draft, setDraft] = useState<{ id: string; box: Box } | null>(null);
|
|
58
|
+
const drag = useRef<Drag | null>(null);
|
|
59
|
+
|
|
60
|
+
const view = <T extends FloorPlanBox>(entry: T): T =>
|
|
61
|
+
draft?.id === entry.id ? { ...entry, ...draft.box } : entry;
|
|
62
|
+
|
|
63
|
+
const commit = (id: string, box: Box) => {
|
|
64
|
+
const current = [...items, ...landmarks].find((entry) => entry.id === id);
|
|
65
|
+
if (!current || sameBox(pick(current), box)) return;
|
|
66
|
+
const apply = <T extends FloorPlanBox>(list: readonly T[]): T[] =>
|
|
67
|
+
list.map((entry) => (entry.id === id ? { ...entry, ...box } : entry));
|
|
68
|
+
onChange?.(apply(items), apply(landmarks));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const dragProps = (entry: FloorPlanBox, mode: DragMode) => ({
|
|
72
|
+
onPointerDown: (event: PointerEvent<HTMLElement>) => {
|
|
73
|
+
if (!enabled || event.button !== 0) return;
|
|
74
|
+
const canvas = canvasRef.current;
|
|
75
|
+
if (!canvas) return;
|
|
76
|
+
event.stopPropagation();
|
|
77
|
+
capturePointer(event.currentTarget, event.pointerId);
|
|
78
|
+
drag.current = {
|
|
79
|
+
id: entry.id,
|
|
80
|
+
mode,
|
|
81
|
+
startX: event.clientX,
|
|
82
|
+
startY: event.clientY,
|
|
83
|
+
origin: pick(entry),
|
|
84
|
+
unit: canvas.getBoundingClientRect().width / bounds.width,
|
|
85
|
+
moved: false,
|
|
86
|
+
};
|
|
87
|
+
onSelect?.(entry.id);
|
|
88
|
+
},
|
|
89
|
+
onPointerMove: (event: PointerEvent<HTMLElement>) => {
|
|
90
|
+
const current = drag.current;
|
|
91
|
+
if (!current || current.id !== entry.id) return;
|
|
92
|
+
const px = Math.hypot(event.clientX - current.startX, event.clientY - current.startY);
|
|
93
|
+
if (!current.moved && px < DRAG_THRESHOLD_PX) return;
|
|
94
|
+
current.moved = true;
|
|
95
|
+
const dx = (event.clientX - current.startX) / current.unit;
|
|
96
|
+
const dy = (event.clientY - current.startY) / current.unit;
|
|
97
|
+
const box =
|
|
98
|
+
current.mode === "move"
|
|
99
|
+
? moveBox(current.origin, dx, dy, bounds)
|
|
100
|
+
: resizeBox(current.origin, current.mode, dx, dy, bounds);
|
|
101
|
+
setDraft((previous) =>
|
|
102
|
+
previous && sameBox(previous.box, box) ? previous : { id: entry.id, box },
|
|
103
|
+
);
|
|
104
|
+
},
|
|
105
|
+
onPointerUp: (event: PointerEvent<HTMLElement>) => {
|
|
106
|
+
const current = drag.current;
|
|
107
|
+
if (!current || current.id !== entry.id) return;
|
|
108
|
+
drag.current = null;
|
|
109
|
+
if (event.currentTarget.hasPointerCapture?.(event.pointerId)) {
|
|
110
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
111
|
+
}
|
|
112
|
+
if (current.moved && draft) commit(entry.id, draft.box);
|
|
113
|
+
setDraft(null);
|
|
114
|
+
},
|
|
115
|
+
onPointerCancel: () => {
|
|
116
|
+
drag.current = null;
|
|
117
|
+
setDraft(null);
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const onKeyDown = (entry: FloorPlanBox) => (event: React.KeyboardEvent<HTMLElement>) => {
|
|
122
|
+
if (!enabled) return;
|
|
123
|
+
const result = editKey(pick(entry), event, bounds);
|
|
124
|
+
if (!result) return;
|
|
125
|
+
event.preventDefault();
|
|
126
|
+
commit(entry.id, result.box);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const rotate = (entry: FloorPlanBox) => {
|
|
130
|
+
const result = editKey(pick(entry), { key: "r", altKey: false, shiftKey: false }, bounds);
|
|
131
|
+
if (result) commit(entry.id, result.box);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
return { view, dragProps, onKeyDown, rotate };
|
|
135
|
+
}
|
package/src/containers/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ export * from "./facts/index.js";
|
|
|
17
17
|
export * from "./feed/index.js";
|
|
18
18
|
export * from "./file-tree/index.js";
|
|
19
19
|
export * from "./filter-bar/index.js";
|
|
20
|
+
export * from "./floor-plan/index.js";
|
|
20
21
|
export * from "./generic-row/index.js";
|
|
21
22
|
export * from "./grid/index.js";
|
|
22
23
|
export * from "./hbars/index.js";
|