Commit c3d8c7a
refactor(shapes): register all geo shapes through GeoTypeDefinition (tldraw#8730)
In order to test whether the `GeoTypeDefinition` extension API is
general enough to act as the canonical registration system for geo
shapes, this PR routes every built-in geo type (rectangle, ellipse,
cloud, etc.) through the same registry that powers `customGeoTypes`.
Built off of tldraw#8543's follow-up branch (`mime/config-geo-followup`).
The same registry now drives path generation, handle snapping, the style
panel picker, and creation defaults — so consumer-defined custom types
and built-ins go through identical code paths. No extension API changes;
built-ins just opt in to the existing one.
### Concepts
| Term | Type | Meaning |
|------|------|---------|
| `defaultGeoTypeDefinitions` | `Record<string, GeoTypeDefinition>` |
Registry of every built-in geo type's path/snap/icon/size behavior |
| `getGeoTypeDefinition(name, customGeoTypes?)` | function | Lookup
helper that prefers custom types, falls back to built-ins |
### What this collapses
| Before | After |
|--------|-------|
| `switch` over each `geo` value in `_getGeoPath` | `def.getPath(w, h,
shape, sw)` |
| Polygon/blobby `switch` in `getHandleSnapGeometry` | `def.snapType ===
'blobby' ? center : vertices+center` |
| Hardcoded `star`/`cloud` size cases in `Pointing.complete()` |
`def.defaultSize ?? { w: 200, h: 200 }` |
| `STYLES.geo` array merged with `customItems` in style panel |
`Object.entries({ ...defaultGeoTypeDefinitions, ...customGeoTypes })` |
| Collision check against `GeoShapeGeoStyle.values` in `configure()` |
Collision check against `defaultGeoTypeDefinitions` keys |
### Example
A consumer defining a custom geo type uses the same shape as a built-in:
```ts
// built-in (now lives in defaultGeoTypeDefinitions)
rectangle: {
snapType: 'polygon',
icon: 'geo-rectangle',
getPath: (w, h, shape) => new PathBuilder()
.moveTo(0, 0, { geometry: { isFilled: shape.props.fill !== 'none' } })
.lineTo(w, 0).lineTo(w, h).lineTo(0, h).close(),
}
// consumer-defined (via GeoShapeUtil.configure({ customGeoTypes }))
'rounded-rect': {
snapType: 'polygon',
icon: 'geo-rounded-rect',
getPath: (w, h, shape) => /* ... */,
}
```
### Change type
- [x] `improvement`
### Test plan
1. `yarn dev` and exercise every built-in geo type (rectangle, ellipse,
triangle, diamond, star, pentagon, hexagon, octagon, rhombus, rhombus-2,
oval, trapezoid, arrow-left/up/down/right, cloud, x-box, check-box,
heart) — creation by click, creation by drag, label editing,
fill/dash/color, snapping.
2. Confirm star and cloud still create at their special default sizes
(200x190 and 300x180).
3. Confirm alt+double-click still toggles rectangle ↔ check-box.
4. Open the `Custom geo types` example and confirm both built-ins and
custom types render in the style panel picker.
5. Confirm `customGeoTypes` collision warning still fires when a
consumer registers a key that matches a built-in.
- [x] Unit tests (existing geo tests pass — `GeoShapeUtil.test.tsx`,
`GeoShapeTool.test.ts`)
- [ ] End to end tests
### Release notes
- The built-in geo types are now registered through the same
`GeoTypeDefinition` system as `customGeoTypes`. Public-facing behavior
is unchanged.
### API changes
- Added `defaultGeoTypeDefinitions` (public) — the registry of built-in
geo type definitions.
- Added `getGeoTypeDefinition(name, customGeoTypes?)` (public) — lookup
helper.
- Removed the internal `getCustomGeoType` helper (replaced by
`getGeoTypeDefinition`).
### Code changes
| Section | LOC change |
| --------------- | ---------- |
| Core code | +319 / -225 |
| Automated files | +115 / -0 |
---------
Co-authored-by: Steve Ruiz <steveruizok@gmail.com>1 parent 5405c3c commit c3d8c7a
8 files changed
Lines changed: 433 additions & 228 deletions
File tree
- packages
- tldraw
- src
- lib
- shapes/geo
- toolStates
- ui/components/StylePanel
- tlschema/src/shapes
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1149 | 1149 | | |
1150 | 1150 | | |
1151 | 1151 | | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
1152 | 1264 | | |
1153 | 1265 | | |
1154 | 1266 | | |
| |||
2240 | 2352 | | |
2241 | 2353 | | |
2242 | 2354 | | |
| 2355 | + | |
| 2356 | + | |
| 2357 | + | |
2243 | 2358 | | |
2244 | 2359 | | |
2245 | 2360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
215 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
216 | 220 | | |
217 | 221 | | |
218 | 222 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
59 | 64 | | |
60 | 65 | | |
61 | 66 | | |
| |||
90 | 95 | | |
91 | 96 | | |
92 | 97 | | |
93 | | - | |
| 98 | + | |
94 | 99 | | |
95 | 100 | | |
96 | 101 | | |
| |||
326 | 331 | | |
327 | 332 | | |
328 | 333 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
364 | 341 | | |
| 342 | + | |
365 | 343 | | |
366 | 344 | | |
367 | 345 | | |
| |||
738 | 716 | | |
739 | 717 | | |
740 | 718 | | |
741 | | - | |
742 | | - | |
743 | | - | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
744 | 722 | | |
745 | 723 | | |
746 | 724 | | |
| |||
0 commit comments