@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,187 @@
|
|
|
1
|
+
export type TableShape = "round" | "square" | "rect" | "bar";
|
|
2
|
+
|
|
3
|
+
export interface Chair {
|
|
4
|
+
cx: number;
|
|
5
|
+
cy: number;
|
|
6
|
+
rotate: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
circle: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface GlyphLayout {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
body: { x: number; y: number; width: number; height: number; round: boolean };
|
|
16
|
+
chairs: Chair[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const UNIT = 100;
|
|
20
|
+
export const CHAIR_DEPTH = 18;
|
|
21
|
+
export const CHAIR_WIDTH = 26;
|
|
22
|
+
export const CHAIR_GAP = 6;
|
|
23
|
+
const INSET = CHAIR_DEPTH + CHAIR_GAP;
|
|
24
|
+
|
|
25
|
+
type Side = "top" | "bottom" | "left" | "right";
|
|
26
|
+
|
|
27
|
+
function spread(count: number, length: number): number[] {
|
|
28
|
+
return Array.from({ length: count }, (_, i) => ((i + 0.5) / count) * length);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function chairWidthFor(count: number, length: number): number {
|
|
32
|
+
if (count === 0) return CHAIR_WIDTH;
|
|
33
|
+
return Math.max(6, Math.min(CHAIR_WIDTH, length / count - CHAIR_GAP));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function sideChair(side: Side, along: number, w: number, h: number, size: number): Chair {
|
|
37
|
+
const horizontal = side === "top" || side === "bottom";
|
|
38
|
+
const half = CHAIR_DEPTH / 2;
|
|
39
|
+
const cx = horizontal ? along : side === "left" ? half : w - half;
|
|
40
|
+
const cy = horizontal ? (side === "top" ? half : h - half) : along;
|
|
41
|
+
return {
|
|
42
|
+
cx,
|
|
43
|
+
cy,
|
|
44
|
+
rotate: horizontal ? 0 : 90,
|
|
45
|
+
width: size,
|
|
46
|
+
height: CHAIR_DEPTH,
|
|
47
|
+
circle: false,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Chairs per side of a square table: one per side up to four seats, then spread evenly with the remainder going top, bottom, left, right. */
|
|
52
|
+
export function squareSideCounts(seats: number): Record<Side, number> {
|
|
53
|
+
const order: Side[] = ["top", "bottom", "left", "right"];
|
|
54
|
+
const counts: Record<Side, number> = { top: 0, bottom: 0, left: 0, right: 0 };
|
|
55
|
+
const base = Math.floor(seats / 4);
|
|
56
|
+
const extra = seats % 4;
|
|
57
|
+
order.forEach((side, i) => {
|
|
58
|
+
counts[side] = base + (i < extra ? 1 : 0);
|
|
59
|
+
});
|
|
60
|
+
return counts;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Chairs of a rect table: one at each end, the rest split between the two long sides (top gets the odd one). */
|
|
64
|
+
export function rectSideCounts(seats: number): Record<Side, number> {
|
|
65
|
+
const ends = Math.min(seats, 2);
|
|
66
|
+
const rest = seats - ends;
|
|
67
|
+
return {
|
|
68
|
+
left: ends >= 1 ? 1 : 0,
|
|
69
|
+
right: ends >= 2 ? 1 : 0,
|
|
70
|
+
top: Math.ceil(rest / 2),
|
|
71
|
+
bottom: Math.floor(rest / 2),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function sidedChairs(counts: Record<Side, number>, w: number, h: number): Chair[] {
|
|
76
|
+
const out: Chair[] = [];
|
|
77
|
+
for (const side of ["top", "bottom", "left", "right"] as const) {
|
|
78
|
+
const horizontal = side === "top" || side === "bottom";
|
|
79
|
+
const length = horizontal ? w - 2 * INSET : h - 2 * INSET;
|
|
80
|
+
const offset = INSET;
|
|
81
|
+
const size = chairWidthFor(counts[side], length);
|
|
82
|
+
for (const along of spread(counts[side], length)) {
|
|
83
|
+
out.push(sideChair(side, offset + along, w, h, size));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function roundLayout(seats: number, w: number, h: number): GlyphLayout {
|
|
90
|
+
const cx = w / 2;
|
|
91
|
+
const cy = h / 2;
|
|
92
|
+
const radius = Math.max(8, Math.min(w, h) / 2 - INSET);
|
|
93
|
+
const orbit = radius + CHAIR_GAP + CHAIR_DEPTH / 2;
|
|
94
|
+
const size = chairWidthFor(seats, 2 * Math.PI * orbit);
|
|
95
|
+
const chairs = Array.from({ length: seats }, (_, i) => {
|
|
96
|
+
const angle = -Math.PI / 2 + (i * 2 * Math.PI) / seats;
|
|
97
|
+
return {
|
|
98
|
+
cx: cx + orbit * Math.cos(angle),
|
|
99
|
+
cy: cy + orbit * Math.sin(angle),
|
|
100
|
+
rotate: (angle * 180) / Math.PI + 90,
|
|
101
|
+
width: size,
|
|
102
|
+
height: CHAIR_DEPTH,
|
|
103
|
+
circle: false,
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
width: w,
|
|
108
|
+
height: h,
|
|
109
|
+
body: { x: cx - radius, y: cy - radius, width: 2 * radius, height: 2 * radius, round: true },
|
|
110
|
+
chairs,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function barLayout(seats: number, w: number, h: number): GlyphLayout {
|
|
115
|
+
const counterHeight = Math.max(14, h * 0.4);
|
|
116
|
+
const stool = Math.min(
|
|
117
|
+
CHAIR_WIDTH * 0.8,
|
|
118
|
+
h - counterHeight - CHAIR_GAP,
|
|
119
|
+
w / Math.max(seats, 1) - 4,
|
|
120
|
+
);
|
|
121
|
+
const diameter = Math.max(6, stool);
|
|
122
|
+
const chairs = spread(seats, w - 2 * CHAIR_GAP).map((x) => ({
|
|
123
|
+
cx: CHAIR_GAP + x,
|
|
124
|
+
cy: counterHeight + CHAIR_GAP + diameter / 2,
|
|
125
|
+
rotate: 0,
|
|
126
|
+
width: diameter,
|
|
127
|
+
height: diameter,
|
|
128
|
+
circle: true,
|
|
129
|
+
}));
|
|
130
|
+
return {
|
|
131
|
+
width: w,
|
|
132
|
+
height: h,
|
|
133
|
+
body: { x: 0, y: 0, width: w, height: counterHeight, round: false },
|
|
134
|
+
chairs,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function transpose(layout: GlyphLayout): GlyphLayout {
|
|
139
|
+
return {
|
|
140
|
+
width: layout.height,
|
|
141
|
+
height: layout.width,
|
|
142
|
+
body: {
|
|
143
|
+
x: layout.body.y,
|
|
144
|
+
y: layout.body.x,
|
|
145
|
+
width: layout.body.height,
|
|
146
|
+
height: layout.body.width,
|
|
147
|
+
round: layout.body.round,
|
|
148
|
+
},
|
|
149
|
+
chairs: layout.chairs.map((c) => ({
|
|
150
|
+
...c,
|
|
151
|
+
cx: c.cy,
|
|
152
|
+
cy: c.cx,
|
|
153
|
+
rotate: c.circle ? 0 : c.rotate === 0 ? 90 : 0,
|
|
154
|
+
})),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function framed(counts: Record<Side, number>, w: number, h: number): GlyphLayout {
|
|
159
|
+
return {
|
|
160
|
+
width: w,
|
|
161
|
+
height: h,
|
|
162
|
+
body: { x: INSET, y: INSET, width: w - 2 * INSET, height: h - 2 * INSET, round: false },
|
|
163
|
+
chairs: sidedChairs(counts, w, h),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Geometry of a table glyph in a `width` x `height` box (100 units per grid cell). Round tables
|
|
169
|
+
* space chairs radially; square ones put a chair per side up to four, then spread; rect tables
|
|
170
|
+
* seat one at each end and split the rest along the long sides; bars line stools up in a row.
|
|
171
|
+
* Rect and bar follow the longer axis of the box.
|
|
172
|
+
*/
|
|
173
|
+
export function glyphLayout(
|
|
174
|
+
shape: TableShape,
|
|
175
|
+
seats: number,
|
|
176
|
+
width: number,
|
|
177
|
+
height: number,
|
|
178
|
+
): GlyphLayout {
|
|
179
|
+
const count = Math.max(0, Math.floor(seats));
|
|
180
|
+
if (shape === "round") return roundLayout(count, width, height);
|
|
181
|
+
if (shape === "square") return framed(squareSideCounts(count), width, height);
|
|
182
|
+
const vertical = height > width;
|
|
183
|
+
const w = vertical ? height : width;
|
|
184
|
+
const h = vertical ? width : height;
|
|
185
|
+
const layout = shape === "rect" ? framed(rectSideCounts(count), w, h) : barLayout(count, w, h);
|
|
186
|
+
return vertical ? transpose(layout) : layout;
|
|
187
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef } from "react";
|
|
2
|
+
import type { ClassNames } from "../../utils/class-names.js";
|
|
3
|
+
import { cn } from "../../utils/cn.js";
|
|
4
|
+
import { glyphLayout, type TableShape, UNIT } from "./layout.js";
|
|
5
|
+
|
|
6
|
+
export type TableGlyphSize = "sm" | "md" | "lg" | "fill";
|
|
7
|
+
|
|
8
|
+
export type TableGlyphPattern = "dashed" | "solid" | "soft" | "strong";
|
|
9
|
+
|
|
10
|
+
export type TableGlyphPart = "root" | "body" | "chair";
|
|
11
|
+
|
|
12
|
+
export interface TableGlyphProps extends Omit<ComponentPropsWithoutRef<"svg">, "children"> {
|
|
13
|
+
shape: TableShape;
|
|
14
|
+
/** Chairs drawn around the table. */
|
|
15
|
+
seats: number;
|
|
16
|
+
/** How many chairs are filled, in drawing order. Clamped to `seats`. */
|
|
17
|
+
occupied?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Height of a one-unit box from the control-height tokens, so it follows density; wider shapes
|
|
20
|
+
* grow from it. `fill` takes the size of the parent instead.
|
|
21
|
+
*/
|
|
22
|
+
size?: TableGlyphSize;
|
|
23
|
+
/** Box in grid units (1 unit is one square cell). Defaults to 1x1, rect 2x1 and bar 3x1. */
|
|
24
|
+
width?: number;
|
|
25
|
+
height?: number;
|
|
26
|
+
/** Table surface: `dashed` outline, `solid` outline on the surface, `soft` or `strong` tint. Defaults to `solid`. */
|
|
27
|
+
pattern?: TableGlyphPattern;
|
|
28
|
+
/** Per-part classes merged with `cn`; consumer classes win. `className` still targets the root and is merged last. */
|
|
29
|
+
classNames?: ClassNames<TableGlyphPart>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const sizeClasses: Record<TableGlyphSize, string> = {
|
|
33
|
+
sm: "h-control w-auto",
|
|
34
|
+
md: "h-control-lg w-auto",
|
|
35
|
+
lg: "h-[calc(var(--control-height-lg)*1.5)] w-auto",
|
|
36
|
+
fill: "size-full",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const patternClasses: Record<TableGlyphPattern, string> = {
|
|
40
|
+
dashed: "fill-transparent",
|
|
41
|
+
solid: "fill-surface",
|
|
42
|
+
soft: "fill-current/10",
|
|
43
|
+
strong: "fill-current/35",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const defaultBox: Record<TableShape, readonly [number, number]> = {
|
|
47
|
+
round: [1, 1],
|
|
48
|
+
square: [1, 1],
|
|
49
|
+
rect: [2, 1],
|
|
50
|
+
bar: [3, 1],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const STROKE = 1.5;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* An SVG table with its chairs placed from `seats` and `shape`: radial for round, one per side
|
|
57
|
+
* (then spread) for square, ends plus long sides for rect, a row of stools for bar. It draws with
|
|
58
|
+
* `currentColor` and the surface token, so it follows the text colour and the theme. Decorative
|
|
59
|
+
* (`aria-hidden`) unless an `aria-label` is given.
|
|
60
|
+
*/
|
|
61
|
+
export function TableGlyph({
|
|
62
|
+
shape,
|
|
63
|
+
seats,
|
|
64
|
+
occupied = 0,
|
|
65
|
+
size = "md",
|
|
66
|
+
width,
|
|
67
|
+
height,
|
|
68
|
+
pattern = "solid",
|
|
69
|
+
className,
|
|
70
|
+
classNames,
|
|
71
|
+
"aria-label": ariaLabel,
|
|
72
|
+
...props
|
|
73
|
+
}: TableGlyphProps) {
|
|
74
|
+
const [defaultW, defaultH] = defaultBox[shape];
|
|
75
|
+
const w = (width ?? defaultW) * UNIT;
|
|
76
|
+
const h = (height ?? defaultH) * UNIT;
|
|
77
|
+
const layout = glyphLayout(shape, seats, w, h);
|
|
78
|
+
const filled = Math.max(0, Math.min(Math.floor(occupied), layout.chairs.length));
|
|
79
|
+
const { body } = layout;
|
|
80
|
+
const labelled = ariaLabel !== undefined;
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<svg
|
|
84
|
+
viewBox={`0 0 ${w} ${h}`}
|
|
85
|
+
role={labelled ? "img" : undefined}
|
|
86
|
+
aria-label={ariaLabel}
|
|
87
|
+
aria-hidden={labelled ? undefined : true}
|
|
88
|
+
focusable="false"
|
|
89
|
+
data-shape={shape}
|
|
90
|
+
data-seats={layout.chairs.length}
|
|
91
|
+
data-occupied={filled}
|
|
92
|
+
data-pattern={pattern}
|
|
93
|
+
preserveAspectRatio="xMidYMid meet"
|
|
94
|
+
className={cn("shrink-0 text-ink-2", sizeClasses[size], classNames?.root, className)}
|
|
95
|
+
{...props}
|
|
96
|
+
>
|
|
97
|
+
{body.round ? (
|
|
98
|
+
<circle
|
|
99
|
+
cx={body.x + body.width / 2}
|
|
100
|
+
cy={body.y + body.height / 2}
|
|
101
|
+
r={body.width / 2}
|
|
102
|
+
data-part="body"
|
|
103
|
+
className={cn("stroke-current", patternClasses[pattern], classNames?.body)}
|
|
104
|
+
strokeWidth={STROKE}
|
|
105
|
+
strokeDasharray={pattern === "dashed" ? "5 4" : undefined}
|
|
106
|
+
vectorEffect="non-scaling-stroke"
|
|
107
|
+
/>
|
|
108
|
+
) : (
|
|
109
|
+
<rect
|
|
110
|
+
x={body.x}
|
|
111
|
+
y={body.y}
|
|
112
|
+
width={body.width}
|
|
113
|
+
height={body.height}
|
|
114
|
+
rx={6}
|
|
115
|
+
data-part="body"
|
|
116
|
+
className={cn("stroke-current", patternClasses[pattern], classNames?.body)}
|
|
117
|
+
strokeWidth={STROKE}
|
|
118
|
+
strokeDasharray={pattern === "dashed" ? "5 4" : undefined}
|
|
119
|
+
vectorEffect="non-scaling-stroke"
|
|
120
|
+
/>
|
|
121
|
+
)}
|
|
122
|
+
{layout.chairs.map((chair, index) => {
|
|
123
|
+
const taken = index < filled;
|
|
124
|
+
const chairClass = cn(
|
|
125
|
+
"stroke-current",
|
|
126
|
+
taken ? "fill-current" : "fill-surface",
|
|
127
|
+
classNames?.chair,
|
|
128
|
+
);
|
|
129
|
+
const key = `${chair.cx.toFixed(2)}-${chair.cy.toFixed(2)}`;
|
|
130
|
+
return chair.circle ? (
|
|
131
|
+
<circle
|
|
132
|
+
key={key}
|
|
133
|
+
cx={chair.cx}
|
|
134
|
+
cy={chair.cy}
|
|
135
|
+
r={chair.width / 2}
|
|
136
|
+
data-part="chair"
|
|
137
|
+
data-occupied={taken ? "" : undefined}
|
|
138
|
+
className={chairClass}
|
|
139
|
+
strokeWidth={STROKE}
|
|
140
|
+
vectorEffect="non-scaling-stroke"
|
|
141
|
+
/>
|
|
142
|
+
) : (
|
|
143
|
+
<rect
|
|
144
|
+
key={key}
|
|
145
|
+
x={chair.cx - chair.width / 2}
|
|
146
|
+
y={chair.cy - chair.height / 2}
|
|
147
|
+
width={chair.width}
|
|
148
|
+
height={chair.height}
|
|
149
|
+
rx={5}
|
|
150
|
+
transform={`rotate(${chair.rotate.toFixed(2)} ${chair.cx.toFixed(2)} ${chair.cy.toFixed(2)})`}
|
|
151
|
+
data-part="chair"
|
|
152
|
+
data-occupied={taken ? "" : undefined}
|
|
153
|
+
className={chairClass}
|
|
154
|
+
strokeWidth={STROKE}
|
|
155
|
+
vectorEffect="non-scaling-stroke"
|
|
156
|
+
/>
|
|
157
|
+
);
|
|
158
|
+
})}
|
|
159
|
+
</svg>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { Box } from "./model.js";
|
|
2
|
+
|
|
3
|
+
export type ResizeHandle = "n" | "s" | "e" | "w" | "ne" | "nw" | "se" | "sw";
|
|
4
|
+
|
|
5
|
+
export interface EditBounds {
|
|
6
|
+
/** Canvas size in grid units. */
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
/** Grid step positions and sizes snap to. */
|
|
10
|
+
snap: number;
|
|
11
|
+
/** Smallest width or height. */
|
|
12
|
+
minSize: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function snapTo(value: number, step: number): number {
|
|
16
|
+
if (step <= 0) return value;
|
|
17
|
+
return Math.round(value / step) * step;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const clean = (value: number) => Math.round(value * 1e6) / 1e6;
|
|
21
|
+
|
|
22
|
+
function contain(box: Box, bounds: EditBounds): Box {
|
|
23
|
+
const width = Math.min(Math.max(box.width, bounds.minSize), bounds.width);
|
|
24
|
+
const height = Math.min(Math.max(box.height, bounds.minSize), bounds.height);
|
|
25
|
+
return {
|
|
26
|
+
x: clean(Math.min(Math.max(box.x, 0), bounds.width - width)),
|
|
27
|
+
y: clean(Math.min(Math.max(box.y, 0), bounds.height - height)),
|
|
28
|
+
width: clean(width),
|
|
29
|
+
height: clean(height),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Moves a box by a delta in grid units, snapped to the grid and kept inside the canvas. */
|
|
34
|
+
export function moveBox(box: Box, dx: number, dy: number, bounds: EditBounds): Box {
|
|
35
|
+
return contain(
|
|
36
|
+
{ ...box, x: snapTo(box.x + dx, bounds.snap), y: snapTo(box.y + dy, bounds.snap) },
|
|
37
|
+
bounds,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Resizes a box from one handle by a delta in grid units; the opposite edges stay put. */
|
|
42
|
+
export function resizeBox(
|
|
43
|
+
box: Box,
|
|
44
|
+
handle: ResizeHandle,
|
|
45
|
+
dx: number,
|
|
46
|
+
dy: number,
|
|
47
|
+
bounds: EditBounds,
|
|
48
|
+
): Box {
|
|
49
|
+
let left = box.x;
|
|
50
|
+
let top = box.y;
|
|
51
|
+
let right = box.x + box.width;
|
|
52
|
+
let bottom = box.y + box.height;
|
|
53
|
+
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
|
|
54
|
+
if (handle.includes("w")) left = clamp(snapTo(left + dx, bounds.snap), 0, right - bounds.minSize);
|
|
55
|
+
if (handle.includes("e")) {
|
|
56
|
+
right = clamp(snapTo(right + dx, bounds.snap), left + bounds.minSize, bounds.width);
|
|
57
|
+
}
|
|
58
|
+
if (handle.includes("n")) top = clamp(snapTo(top + dy, bounds.snap), 0, bottom - bounds.minSize);
|
|
59
|
+
if (handle.includes("s")) {
|
|
60
|
+
bottom = clamp(snapTo(bottom + dy, bounds.snap), top + bounds.minSize, bounds.height);
|
|
61
|
+
}
|
|
62
|
+
return contain({ x: left, y: top, width: right - left, height: bottom - top }, bounds);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Turns a box a quarter by swapping its width and height around its centre, snapped and kept inside the canvas. */
|
|
66
|
+
export function rotateBox(box: Box, bounds: EditBounds): Box {
|
|
67
|
+
const centerX = box.x + box.width / 2;
|
|
68
|
+
const centerY = box.y + box.height / 2;
|
|
69
|
+
return moveBox(
|
|
70
|
+
{
|
|
71
|
+
x: centerX - box.height / 2,
|
|
72
|
+
y: centerY - box.width / 2,
|
|
73
|
+
width: box.height,
|
|
74
|
+
height: box.width,
|
|
75
|
+
},
|
|
76
|
+
0,
|
|
77
|
+
0,
|
|
78
|
+
bounds,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const ARROWS: Record<string, readonly [number, number]> = {
|
|
83
|
+
ArrowLeft: [-1, 0],
|
|
84
|
+
ArrowRight: [1, 0],
|
|
85
|
+
ArrowUp: [0, -1],
|
|
86
|
+
ArrowDown: [0, 1],
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export type EditKeyResult = { box: Box } | undefined;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Keyboard edit of a box: Alt with an arrow nudges by one grid step, Alt and Shift with an arrow
|
|
93
|
+
* grows or shrinks the bottom-right corner, `r` turns it a quarter. Anything else is not an edit.
|
|
94
|
+
*/
|
|
95
|
+
export function editKey(
|
|
96
|
+
box: Box,
|
|
97
|
+
event: { key: string; altKey: boolean; shiftKey: boolean; ctrlKey?: boolean; metaKey?: boolean },
|
|
98
|
+
bounds: EditBounds,
|
|
99
|
+
): EditKeyResult {
|
|
100
|
+
if (event.ctrlKey || event.metaKey) return undefined;
|
|
101
|
+
if (!event.altKey && !event.shiftKey && event.key.toLowerCase() === "r") {
|
|
102
|
+
return { box: rotateBox(box, bounds) };
|
|
103
|
+
}
|
|
104
|
+
const arrow = ARROWS[event.key];
|
|
105
|
+
if (!arrow || !event.altKey) return undefined;
|
|
106
|
+
const dx = arrow[0] * bounds.snap;
|
|
107
|
+
const dy = arrow[1] * bounds.snap;
|
|
108
|
+
return {
|
|
109
|
+
box: event.shiftKey ? resizeBox(box, "se", dx, dy, bounds) : moveBox(box, dx, dy, bounds),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function sameBox(a: Box, b: Box): boolean {
|
|
114
|
+
return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
|
|
115
|
+
}
|