@grida/hud 0.1.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/LICENSE +201 -0
- package/README.md +340 -0
- package/dist/index-CBqCh-ZM.d.mts +734 -0
- package/dist/index-DRBeSiI2.d.ts +734 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +11 -0
- package/dist/index.mjs +2 -0
- package/dist/react.d.mts +73 -0
- package/dist/react.d.ts +73 -0
- package/dist/react.js +124 -0
- package/dist/react.mjs +117 -0
- package/dist/surface-CNlBaEXn.js +1890 -0
- package/dist/surface-hUEeEVdL.mjs +1808 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
# @grida/hud
|
|
2
|
+
|
|
3
|
+
The surface backend for the Grida editor viewport.
|
|
4
|
+
|
|
5
|
+
A self-contained, mathematically grounded HUD overlay: handles, selection chrome, hover, marquee, gesture state, hit-testing — all rendered to a single `<canvas>` from a pure-logic state machine. No DOM overlay, no `data-id` traversal, no per-element React reconciliation in the hot path.
|
|
6
|
+
|
|
7
|
+
## What is the HUD?
|
|
8
|
+
|
|
9
|
+
The HUD is the non-content visual chrome drawn on top of the viewport: selection rectangles, resize handles, hover outlines, marquees, snap guides, measurement lines, pixel grids. Everything the operator sees that isn't part of the document itself.
|
|
10
|
+
|
|
11
|
+
In industry terms: Blender calls this "Overlays", Unity calls it "Gizmos", game engines call it "HUD". We use HUD.
|
|
12
|
+
|
|
13
|
+
## Why a canvas-based surface
|
|
14
|
+
|
|
15
|
+
The DOM approach — positioned `<div>` handles, `data-grida-id` for hit-test, native dblclick — works until it doesn't. Concrete failures we hit before this package existed:
|
|
16
|
+
|
|
17
|
+
- **DOM identity is fragile.** Re-rendering the SVG tree on every state change destroys the live element mid-gesture; native dblclick (keyed by DOM identity) breaks and has to be re-implemented manually.
|
|
18
|
+
- **Hit-test relies on attributes.** `data-grida-id` has to be applied and stripped around export; any path-rewrite invalidates the id index.
|
|
19
|
+
- **State and events are hard to propagate.** Gesture, hover, modifier, selection state scatter across the surface and editor; each one re-renders the world.
|
|
20
|
+
- **It does not scale across backends.** SVG-tied DOM overlays can't be reused by the cg/Rust editor.
|
|
21
|
+
|
|
22
|
+
The fix is to make the HUD a **pure-logic state machine + a canvas renderer**. The state machine takes raw pointer events, owns gesture/hover/modifiers, and emits `Intent`s to the host. The renderer draws a single `HUDDraw` per frame. Both layers are testable in isolation; neither layer touches the DOM.
|
|
23
|
+
|
|
24
|
+
## Architecture
|
|
25
|
+
|
|
26
|
+
Three layers. One-directional dependency: **`primitives/` ← `event/` ← `surface/`**.
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
┌─────────────────────────────────────────────────┐
|
|
30
|
+
│ Host (svg-editor, grida-canvas-react, …) │
|
|
31
|
+
│ - Document, scene, selection │
|
|
32
|
+
│ - Provides: pick, shapeOf, onIntent │
|
|
33
|
+
│ - Pushes pointer events to surface │
|
|
34
|
+
│ - Commits intents (history.preview, commands) │
|
|
35
|
+
└───────────────────────┬─────────────────────────┘
|
|
36
|
+
│
|
|
37
|
+
┌───────────────────────▼─────────────────────────┐
|
|
38
|
+
│ surface/ — the wired class │
|
|
39
|
+
│ - `Surface`: constructor wiring, lifecycle, │
|
|
40
|
+
│ draw loop │
|
|
41
|
+
│ - `chrome`: builds HUDDraw from state │
|
|
42
|
+
└───────────┬───────────────────────┬─────────────┘
|
|
43
|
+
│ │
|
|
44
|
+
┌───────────▼──────────┐ ┌──────────▼─────────────┐
|
|
45
|
+
│ event/ │ │ primitives/ │
|
|
46
|
+
│ the math core │ │ dumb render shapes │
|
|
47
|
+
│ - Gesture state │ │ - HUDCanvas │
|
|
48
|
+
│ - Hit-regions │ │ - HUDDraw primitives │
|
|
49
|
+
│ - Click-tracker │ │ - Snap/measure/lasso │
|
|
50
|
+
│ No canvas. No host. │ │ builders │
|
|
51
|
+
│ No DOM. │ │ No state. No host. │
|
|
52
|
+
└──────────────────────┘ └────────────────────────┘
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The `event/` layer is the testable core. It dispatches plain objects, returns plain objects, has zero I/O. Every gesture transition, hit-region resolution, and click-tracker decision is a pure function over its inputs — runnable under vitest with no canvas, no DOM, no React.
|
|
56
|
+
|
|
57
|
+
## Layer responsibilities
|
|
58
|
+
|
|
59
|
+
| Layer | Owns | Reads from | Writes to |
|
|
60
|
+
| -------------------------- | ------------------------------------------------------------------------------ | ----------------------------- | ---------------------------------------------------------------- |
|
|
61
|
+
| **`primitives/HUDCanvas`** | Canvas2D context, DPR, transform, drawing of `HUDDraw` primitives | — | Canvas |
|
|
62
|
+
| **`event/`** | Gesture, hover, modifiers, cursor, click-tracker, hit-regions, intent builders | — | Returns next state + intents from `dispatch(state, event, deps)` |
|
|
63
|
+
| **`surface/Surface`** | Current `SurfaceState`, the underlying `HUDCanvas`, host providers, draw loop | Host providers, surface state | Calls `onIntent`; calls `HUDCanvas.draw` |
|
|
64
|
+
| **Host** | Document, scene, selection, history | Surface state introspection | Pushes pointer events; pushes selection mirror; commits intents |
|
|
65
|
+
|
|
66
|
+
### What the surface deliberately does NOT own
|
|
67
|
+
|
|
68
|
+
- **Selection.** Host owns; surface holds a read-only mirror via `setSelection(ids)`. Surface emits `select` / `deselect` / `toggle` intents; host commits.
|
|
69
|
+
- **Document / scene.** Surface never reads node data directly. Only callbacks: `pick(point) → NodeId | null`, `shapeOf(id) → SelectionShape | null`.
|
|
70
|
+
- **History.** Host wraps commits with `history.preview` per its own logic. Intents carry `phase: "preview" | "commit"` so the host knows when to wrap.
|
|
71
|
+
- **`<style>` resolution, defs, paint, anything SVG-specific.** Out of scope.
|
|
72
|
+
|
|
73
|
+
### What the surface owns because nothing else needs it
|
|
74
|
+
|
|
75
|
+
Gesture state machine, hover indicator, modifier snapshot, cursor, click-tracker (dblclick). These have no representation outside the surface; making them surface-private is the single-source-of-truth rule.
|
|
76
|
+
|
|
77
|
+
## Public API
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { Surface, HUDCanvas, type HUDDraw, type HUDStyle } from "@grida/hud";
|
|
81
|
+
|
|
82
|
+
const surface = new Surface(canvasElement, {
|
|
83
|
+
// required wiring
|
|
84
|
+
pick: (point_doc) => editor.hitTest(point_doc), // (point) => NodeId | null
|
|
85
|
+
shapeOf: (id) => editor.shapeOf(id), // (id) => SelectionShape | null
|
|
86
|
+
onIntent: (intent) => editor.commitIntent(intent), // surface → host
|
|
87
|
+
|
|
88
|
+
// optional config
|
|
89
|
+
style: { chromeColor: "#2563eb", handleSize: 8 },
|
|
90
|
+
readonly: false,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Lifecycle
|
|
94
|
+
surface.setSize(w, h);
|
|
95
|
+
surface.setTransform(t); // camera (screen ↔ doc)
|
|
96
|
+
surface.setSelection(ids); // read-only mirror from host
|
|
97
|
+
surface.setStyle(partial);
|
|
98
|
+
surface.setReadonly(v);
|
|
99
|
+
surface.dispose();
|
|
100
|
+
|
|
101
|
+
// Input
|
|
102
|
+
const response = surface.dispatch(event);
|
|
103
|
+
// response: { needsRedraw, cursorChanged, hoverChanged }
|
|
104
|
+
// (intents are pushed via onIntent, not returned here)
|
|
105
|
+
|
|
106
|
+
// Frame
|
|
107
|
+
surface.draw(extra?); // merges surface chrome + host extras, one canvas draw
|
|
108
|
+
|
|
109
|
+
// Read-only introspection
|
|
110
|
+
surface.gesture(): SurfaceGesture;
|
|
111
|
+
surface.hover(): NodeId | null;
|
|
112
|
+
surface.cursor(): CursorIcon;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Event types
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
type SurfaceEvent =
|
|
119
|
+
| { kind: "pointer_move"; x: number; y: number; mods: Modifiers }
|
|
120
|
+
| {
|
|
121
|
+
kind: "pointer_down";
|
|
122
|
+
x: number;
|
|
123
|
+
y: number;
|
|
124
|
+
button: PointerButton;
|
|
125
|
+
mods: Modifiers;
|
|
126
|
+
}
|
|
127
|
+
| {
|
|
128
|
+
kind: "pointer_up";
|
|
129
|
+
x: number;
|
|
130
|
+
y: number;
|
|
131
|
+
button: PointerButton;
|
|
132
|
+
mods: Modifiers;
|
|
133
|
+
}
|
|
134
|
+
| { kind: "modifiers"; mods: Modifiers }
|
|
135
|
+
| {
|
|
136
|
+
kind: "wheel";
|
|
137
|
+
x: number;
|
|
138
|
+
y: number;
|
|
139
|
+
dx: number;
|
|
140
|
+
dy: number;
|
|
141
|
+
mods: Modifiers;
|
|
142
|
+
}
|
|
143
|
+
| { kind: "key"; phase: "down" | "up"; code: string; mods: Modifiers };
|
|
144
|
+
|
|
145
|
+
type Modifiers = { shift: boolean; alt: boolean; meta: boolean; ctrl: boolean };
|
|
146
|
+
type PointerButton = "primary" | "secondary" | "middle";
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
All `SurfaceEvent` coordinates are **screen-space CSS pixels relative to the canvas**. The surface owns the camera and converts internally.
|
|
150
|
+
|
|
151
|
+
### Gesture state
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
type SurfaceGesture =
|
|
155
|
+
| { kind: "idle" }
|
|
156
|
+
| { kind: "pan"; dx: number; dy: number }
|
|
157
|
+
| { kind: "marquee"; rect: Rect } // screen-space
|
|
158
|
+
| { kind: "translate"; ids: NodeId[]; dx: number; dy: number }
|
|
159
|
+
| { kind: "resize"; id: NodeId; anchor: ResizeDirection; rect: Rect }
|
|
160
|
+
| { kind: "rotate"; id: NodeId; corner: RotationCorner; angle: number };
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Intents
|
|
164
|
+
|
|
165
|
+
The surface emits `Intent`s through `onIntent`. The host commits — wrapping in `history.preview`, dispatching commands, whatever. The surface itself doesn't mutate the document.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
type Intent =
|
|
169
|
+
| { kind: "select"; ids: NodeId[]; mode: "replace" | "add" | "toggle" }
|
|
170
|
+
| { kind: "deselect_all" }
|
|
171
|
+
| { kind: "translate"; ids: NodeId[]; dx: number; dy: number; phase: Phase }
|
|
172
|
+
| {
|
|
173
|
+
kind: "resize";
|
|
174
|
+
id: NodeId;
|
|
175
|
+
anchor: ResizeDirection;
|
|
176
|
+
rect: Rect;
|
|
177
|
+
phase: Phase;
|
|
178
|
+
}
|
|
179
|
+
| { kind: "rotate"; id: NodeId; angle: number; phase: Phase }
|
|
180
|
+
| { kind: "enter_content_edit"; id: NodeId }
|
|
181
|
+
| { kind: "cancel_gesture" };
|
|
182
|
+
|
|
183
|
+
type Phase = "preview" | "commit";
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
**Why `phase` matters.** During a drag, the surface emits `phase: "preview"` on every frame; on pointer-up, one final `phase: "commit"`. The host wraps `preview` intents in `history.preview()` (apply + revert) and finalizes on `commit`. This removes guesswork from the host — gesture state is not part of the intent contract.
|
|
187
|
+
|
|
188
|
+
### React API
|
|
189
|
+
|
|
190
|
+
A single hook. No provider, no context.
|
|
191
|
+
|
|
192
|
+
```tsx
|
|
193
|
+
import { useHUDSurface } from "@grida/hud/react";
|
|
194
|
+
|
|
195
|
+
function Viewport() {
|
|
196
|
+
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
197
|
+
const surface = useHUDSurface(canvasRef, {
|
|
198
|
+
pick,
|
|
199
|
+
shapeOf,
|
|
200
|
+
onIntent,
|
|
201
|
+
style,
|
|
202
|
+
});
|
|
203
|
+
// surface is the imperative instance — same as `new Surface(...)`
|
|
204
|
+
// wire pointer events as the host wants
|
|
205
|
+
return <canvas ref={canvasRef} />;
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Selection intent
|
|
210
|
+
|
|
211
|
+
The full classification table — every named scenario at pointer-down, what
|
|
212
|
+
commits when, and why DOM event order can't express it cleanly — is an
|
|
213
|
+
**implementation-agnostic working-group spec**, alongside its UX-narrative
|
|
214
|
+
sibling:
|
|
215
|
+
|
|
216
|
+
- Formal classifier: https://grida.co/docs/wg/feat-editor/ux-surface/selection-intent
|
|
217
|
+
- UX-narrative test cases: https://grida.co/docs/wg/feat-editor/ux-surface/selection
|
|
218
|
+
|
|
219
|
+
This package's implementation:
|
|
220
|
+
|
|
221
|
+
- [`event/decision.ts`](./event/decision.ts) — `Scenario` enum + `classifyScenario` +
|
|
222
|
+
dispatch.
|
|
223
|
+
- [`__tests__/decision.test.ts`](./__tests__/decision.test.ts) — one test
|
|
224
|
+
per named scenario.
|
|
225
|
+
|
|
226
|
+
Skim the spec before changing the classifier or its dispatch.
|
|
227
|
+
|
|
228
|
+
## Coordinate model
|
|
229
|
+
|
|
230
|
+
- All `SurfaceEvent` points are **screen-space** (CSS px relative to the canvas).
|
|
231
|
+
- `setTransform(t)` is the camera — one matrix, screen ↔ doc.
|
|
232
|
+
- Hit-test happens in two tiers on `pointer_down`:
|
|
233
|
+
1. **UI layer (screen-space AABB)** — surface's own `HitRegions` registry, populated by chrome builder each frame. Resize handle? Rotation handle? Body region (translate)? Direct path; no host involvement.
|
|
234
|
+
2. **Scene layer (doc-space point)** — if no UI hit, surface converts the point screen→doc and calls `pick(point_doc)`. Host implements this with whatever it has (`elementFromPoint`+`data-id` for SVG-DOM hosts, scene-cache R-tree for cg).
|
|
235
|
+
- `shapeOf(id)` returns a **doc-space** `SelectionShape` (`{ kind: "rect", rect }` for most nodes, `{ kind: "line", p1, p2 }` for vector lines). Surface converts to screen for handle placement and screen-space chrome.
|
|
236
|
+
|
|
237
|
+
Handles are always drawn at a fixed screen-space size regardless of zoom. The primitive layer supports this via `HUDScreenRect` (see below).
|
|
238
|
+
|
|
239
|
+
## Render path (one draw per frame)
|
|
240
|
+
|
|
241
|
+
1. **Surface builds its own `HUDDraw`** from `SurfaceState` + `shapeOf`:
|
|
242
|
+
- hover outline (if `hover()` set)
|
|
243
|
+
- selection outline (from selection mirror)
|
|
244
|
+
- marquee rect (if active gesture is `marquee`)
|
|
245
|
+
- resize / rotate handles (screen-space)
|
|
246
|
+
- size meter pill (selection bounds W×H)
|
|
247
|
+
2. **Host extras merge in** if `draw(extra)` was called with a host-fed `HUDDraw` (snap guides, measurement, custom widgets).
|
|
248
|
+
3. **One call to `HUDCanvas.draw(merged)`** — clears the canvas and renders everything immediately.
|
|
249
|
+
|
|
250
|
+
Layer order within a frame (back-to-front):
|
|
251
|
+
|
|
252
|
+
1. Hover outline
|
|
253
|
+
2. Selection outline
|
|
254
|
+
3. Marquee rect
|
|
255
|
+
4. Resize/rotate handles
|
|
256
|
+
5. Size meter pill
|
|
257
|
+
6. Host-fed extras (always on top)
|
|
258
|
+
|
|
259
|
+
## Primitives — what `HUDCanvas` can draw
|
|
260
|
+
|
|
261
|
+
| Primitive | Coordinate space | Used by |
|
|
262
|
+
| --------------- | ------------------------------------------------------ | --------------------------------------------- |
|
|
263
|
+
| `HUDLine` | doc-space (segment), optional label in screen-space | snap lines, measurement, spacing |
|
|
264
|
+
| `HUDRule` | doc-space offset, full-viewport extent in screen-space | guide snapping, ruler |
|
|
265
|
+
| `HUDRect` | doc-space | selection outline, marquee in doc-space hosts |
|
|
266
|
+
| `HUDPolyline` | doc-space | lasso, complex selections |
|
|
267
|
+
| `HUDScreenRect` | screen-space size at doc-space anchor | resize/rotate handles |
|
|
268
|
+
| Crosshair point | screen-space | snap hit indicators |
|
|
269
|
+
| Label pill | screen-space at doc-space anchor | size meters, snap gap labels |
|
|
270
|
+
|
|
271
|
+
`HUDDraw` is a plain command struct grouping these. Builders (`snapGuideToHUDDraw`, `measurementToHUDDraw`, …) take host state and return a `HUDDraw` — the surface uses the same mechanism internally for its chrome.
|
|
272
|
+
|
|
273
|
+
## Module layout
|
|
274
|
+
|
|
275
|
+
```
|
|
276
|
+
packages/grida-canvas-hud/
|
|
277
|
+
├── README.md
|
|
278
|
+
├── index.ts # Surface, HUDCanvas, public types
|
|
279
|
+
├── react.tsx # useHUDSurface hook
|
|
280
|
+
├── primitives/ # UI — dumb render shapes
|
|
281
|
+
│ ├── canvas.ts # HUDCanvas
|
|
282
|
+
│ ├── types.ts # HUDDraw, HUDLine, HUDRect, HUDPolyline, HUDRule, HUDScreenRect
|
|
283
|
+
│ ├── snap-guide.ts # HUDDraw builder
|
|
284
|
+
│ ├── measurement-guide.ts # HUDDraw builder
|
|
285
|
+
│ ├── marquee.ts # legacy HUDDraw builder (surface emits its own marquee)
|
|
286
|
+
│ └── lasso.ts # HUDDraw builder
|
|
287
|
+
├── event/ # the math core
|
|
288
|
+
│ ├── event.ts # SurfaceEvent, Modifiers, PointerButton
|
|
289
|
+
│ ├── gesture.ts # SurfaceGesture + transitions
|
|
290
|
+
│ ├── hit-regions.ts # screen-space AABB region registry
|
|
291
|
+
│ ├── handles.ts # 8 resize + 4 rotate geometry & hit-test
|
|
292
|
+
│ ├── click-tracker.ts # dblclick / multi-click detection
|
|
293
|
+
│ ├── cursor.ts # CursorIcon, ResizeDirection, RotationCorner
|
|
294
|
+
│ ├── intent.ts # Intent types + builders
|
|
295
|
+
│ ├── transform.ts # screen ↔ doc helpers
|
|
296
|
+
│ └── state.ts # SurfaceState — pure dispatch entry
|
|
297
|
+
└── surface/ # the wired class
|
|
298
|
+
├── surface.ts # Surface class
|
|
299
|
+
├── chrome.ts # builds HUDDraw from SurfaceState + shapeOf
|
|
300
|
+
└── style.ts # HUDStyle defaults + merge
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Naming conventions
|
|
304
|
+
|
|
305
|
+
- File names are **kebab-case `.ts`** throughout.
|
|
306
|
+
- Inside files, **snake_case is acceptable** for non-filename identifiers.
|
|
307
|
+
- Public method names on `Surface` and `HUDCanvas` are **camelCase** (`setSize`, `setTransform`, `setSelection`) — matches the existing primitive renderer API.
|
|
308
|
+
- The top-level class is **`Surface`**, not `HUDSurface`. The package name already says "hud".
|
|
309
|
+
|
|
310
|
+
## Testing
|
|
311
|
+
|
|
312
|
+
`packages/grida-canvas-hud/__tests__/` runs under vitest. No DOM, no canvas mock — the `event/` layer is pure.
|
|
313
|
+
|
|
314
|
+
| File | Tests |
|
|
315
|
+
| ----------------------- | ------------------------------------------------------------------------------------------------- |
|
|
316
|
+
| `transform.test.ts` | screen ↔ doc across translate, scale, DPR |
|
|
317
|
+
| `hit-regions.test.ts` | topmost wins, reverse iteration, clear/empty, AABB containment |
|
|
318
|
+
| `handles.test.ts` | 8 resize + 4 rotate positions; screen-space hit-test; visibility threshold |
|
|
319
|
+
| `click-tracker.test.ts` | single vs double within window; position threshold; multi-button isolation |
|
|
320
|
+
| `gesture.test.ts` | legal transitions (idle↔translate/resize/marquee/cancel; deferred selection) |
|
|
321
|
+
| `state.test.ts` | dispatch sequences: click selects, drag-empty marquees, drag-handle resizes, drag-node translates |
|
|
322
|
+
| `intent.test.ts` | intent stream + `phase` correctness across a full drag (preview\*N → commit) |
|
|
323
|
+
| `chrome.test.ts` | given state + bounds, assert resulting `HUDDraw` shape — primitive counts and coords |
|
|
324
|
+
|
|
325
|
+
Render output (visual canvas correctness) is verified in the browser, not unit-tested.
|
|
326
|
+
|
|
327
|
+
## Anti-goals
|
|
328
|
+
|
|
329
|
+
- **Not a renderer.** `primitives/HUDCanvas` is intentionally minimal Canvas2D. Skia / WebGL backends are not in scope.
|
|
330
|
+
- **Not a scene graph.** Surface never reads node data — only via `pick` / `shapeOf`.
|
|
331
|
+
- **Not a host of plugins.** No widget registry. Custom HUD elements go through host-fed `HUDDraw` extras.
|
|
332
|
+
- **Not undo-aware.** Intents carry `phase`; host owns undo.
|
|
333
|
+
- **Not a selection store.** Host owns selection; surface mirrors.
|
|
334
|
+
- **Not SVG-aware.** No `data-id`, no DOM IR, no `<style>` resolution.
|
|
335
|
+
|
|
336
|
+
## Adoption
|
|
337
|
+
|
|
338
|
+
v1 ships against [`@grida/svg-editor`](../grida-svg-editor/). The svg-editor's `dom.ts` is rewritten as a thin adapter: SVG content on the bottom layer, a `<canvas>` HUD on top, pointer events forwarded to `surface.dispatch`, intents flowing back via `editor.commitIntent`. Selection chrome, handles, and gesture state move out of `dom.ts` entirely.
|
|
339
|
+
|
|
340
|
+
Main-editor migration (`editor/grida-canvas-react/viewport/ui/*`) is tracked separately. The host contract is the same; the only difference is the `pick` implementation (scene-cache R-tree instead of `elementFromPoint`+`data-id`).
|