-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
421 lines (357 loc) · 11.9 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { z } from "zod";
import type { zInfer } from "~/lib/utils";
import {
LngLatTupleSchema,
MultiLineStringGeometrySchema,
PointGeometrySchema,
} from "../shared/types";
const BaseFeltElementSchema = z.object({
id: z.string(),
groupId: z.string().nullable(),
color: z.string(),
name: z.union([z.string(), z.null()]),
description: z.union([z.string(), z.null()]),
attributes: z.record(z.string(), z.unknown()),
});
const Geographic = z.object({
imageUrl: z.string().nullable(),
});
const Strokable = z.object({
strokeOpacity: z.number(),
strokeWidth: z.number(),
strokeStyle: z.enum(["solid", "dashed", "dotted"]),
});
const DistanceUnit = z.enum(["meter", "kilometer", "foot", "mile"]);
const PlaceElementSchema = BaseFeltElementSchema.extend(
Geographic.shape,
).extend({
type: z.literal("Place"),
coordinates: PointGeometrySchema.shape.coordinates,
symbol: z.string(),
frame: z.enum(["frame-circle", "frame-square"]).nullable(),
hideLabel: z.boolean(),
});
const PathElementSchema = BaseFeltElementSchema.extend(Geographic.shape)
.extend(Strokable.shape)
.extend({
type: z.literal("Path"),
coordinates: MultiLineStringGeometrySchema.shape.coordinates,
distanceMarker: z.boolean(),
endCaps: z.boolean(),
routingMode: z.enum(["driving", "cycling", "walking", "flying"]).nullable(),
});
const PolygonElementSchema = BaseFeltElementSchema.extend(Geographic.shape)
.extend(Strokable.shape)
.extend({
type: z.literal("Polygon"),
coordinates: MultiLineStringGeometrySchema.shape.coordinates,
fillOpacity: z.number().min(0).max(1),
areaMarker: z.boolean(),
});
const CircleElementSchema = BaseFeltElementSchema.extend(Geographic.shape)
.extend(Strokable.shape)
.extend({
type: z.literal("Circle"),
coordinates: LngLatTupleSchema,
radius: z.number(),
radiusDisplayAngle: z.number(),
radiusDisplayUnit: DistanceUnit.nullable(),
fillOpacity: z.number().min(0).max(1),
radiusMarker: z.boolean(),
});
const MarkerElementSchema = BaseFeltElementSchema.extend({
type: z.literal("Marker"),
coordinates: MultiLineStringGeometrySchema.shape.coordinates,
opacity: z.number().min(0).max(1),
size: z.number().min(0).max(100),
zoom: z.number().min(0).max(23),
});
const HighlighterElementSchema = BaseFeltElementSchema.extend({
type: z.literal("Highlighter"),
coordinates: MultiLineStringGeometrySchema.shape.coordinates,
opacity: z.number().min(0).max(1),
});
const DerivedCoords = z.object({
position: LngLatTupleSchema,
rotation: z.number(),
scale: z.number(),
zoom: z.number().min(0).max(23),
coordinates: MultiLineStringGeometrySchema.shape.coordinates,
});
const Textual = z.object({
text: z.string(),
align: z.enum(["left", "center", "right"]),
style: z.enum(["italic", "light", "regular", "caps"]),
name: z.string(),
});
const TextElementSchema = BaseFeltElementSchema.extend(DerivedCoords.shape)
.extend(Textual.shape)
.extend({
type: z.literal("Text"),
});
const NoteElementSchema = BaseFeltElementSchema.extend(DerivedCoords.shape)
.extend(Textual.shape)
.extend({
type: z.literal("Note"),
widthScale: z.number().min(0),
});
const ImageElementSchema = BaseFeltElementSchema.extend({
type: z.literal("Image"),
coordinates: MultiLineStringGeometrySchema.shape.coordinates,
imageUrl: z.string(),
opacity: z.number().min(0).max(1),
}).omit({ color: true });
const LinkElementSchema = BaseFeltElementSchema.extend({
type: z.literal("Link"),
coordinates: MultiLineStringGeometrySchema.shape.coordinates,
url: z.string(),
});
const requiredCreateProps = { type: true, coordinates: true } as const;
const omitCreateProps = { id: true } as const;
export const PlaceCreateSchema = PlaceElementSchema.partial()
.required(requiredCreateProps)
.omit(omitCreateProps);
export const PathCreateSchema = PathElementSchema.partial()
.required(requiredCreateProps)
.omit(omitCreateProps);
export const PolygonCreateSchema = PolygonElementSchema.partial()
.required(requiredCreateProps)
.omit(omitCreateProps);
export const CircleCreateSchema = CircleElementSchema.partial()
.required(requiredCreateProps)
.required({ radius: true })
.omit(omitCreateProps);
export const MarkerCreateSchema = MarkerElementSchema.partial()
.required(requiredCreateProps)
.omit(omitCreateProps);
export const HighlighterCreateSchema = HighlighterElementSchema.partial()
.required(requiredCreateProps)
.omit(omitCreateProps);
export const TextCreateSchema = TextElementSchema.partial()
.required(requiredCreateProps)
.required({ text: true })
.omit(omitCreateProps)
.omit({ coordinates: true, name: true });
export const NoteCreateSchema = NoteElementSchema.partial()
.required(requiredCreateProps)
.required({ text: true })
.omit(omitCreateProps)
.omit({ coordinates: true, name: true });
const ImageCreateSchema = ImageElementSchema.partial()
.required(requiredCreateProps)
.required({ imageUrl: true })
.omit(omitCreateProps);
export const ElementCreateSchema = z.discriminatedUnion("type", [
PlaceCreateSchema,
PathCreateSchema,
PolygonCreateSchema,
CircleCreateSchema,
MarkerCreateSchema,
HighlighterCreateSchema,
ImageCreateSchema,
TextCreateSchema,
NoteCreateSchema,
]);
const PlaceReadSchema = PlaceElementSchema;
const PathReadSchema = PathElementSchema.omit({ coordinates: true });
const PolygonReadSchema = PolygonElementSchema.omit({
coordinates: true,
});
const CircleReadSchema = CircleElementSchema;
const MarkerReadSchema = MarkerElementSchema.omit({ coordinates: true });
const HighlighterReadSchema = HighlighterElementSchema.omit({
coordinates: true,
});
const TextReadSchema = TextElementSchema.omit({ coordinates: true });
const NoteReadSchema = NoteElementSchema.omit({ coordinates: true });
const ImageReadSchema = ImageElementSchema.omit({ coordinates: true });
const LinkReadSchema = LinkElementSchema.omit({ coordinates: true });
const requiredUpdateProps = { type: true, id: true } as const;
const PlaceUpdateSchema =
PlaceElementSchema.partial().required(requiredUpdateProps);
const PathUpdateSchema =
PathElementSchema.partial().required(requiredUpdateProps);
const PolygonUpdateSchema =
PolygonElementSchema.partial().required(requiredUpdateProps);
const CircleUpdateSchema =
CircleElementSchema.partial().required(requiredUpdateProps);
const MarkerUpdateSchema =
MarkerElementSchema.partial().required(requiredUpdateProps);
const HighlighterUpdateSchema =
HighlighterElementSchema.partial().required(requiredUpdateProps);
const TextUpdateSchema = TextElementSchema.partial()
.required(requiredUpdateProps)
.omit({
coordinates: true,
name: true,
});
const NoteUpdateSchema = NoteElementSchema.partial()
.required(requiredUpdateProps)
.omit({
coordinates: true,
name: true,
});
const ImageUpdateSchema =
ImageElementSchema.partial().required(requiredUpdateProps);
export const ElementUpdateSchema = z.discriminatedUnion("type", [
PlaceUpdateSchema,
PathUpdateSchema,
PolygonUpdateSchema,
CircleUpdateSchema,
MarkerUpdateSchema,
HighlighterUpdateSchema,
TextUpdateSchema,
NoteUpdateSchema,
ImageUpdateSchema,
]);
export interface PlaceElementCreate extends zInfer<typeof PlaceCreateSchema> {}
export interface PathElementCreate extends zInfer<typeof PathCreateSchema> {}
export interface PolygonElementCreate
extends zInfer<typeof PolygonCreateSchema> {}
export interface CircleElementCreate
extends zInfer<typeof CircleCreateSchema> {}
export interface MarkerElementCreate
extends zInfer<typeof MarkerCreateSchema> {}
export interface HighlighterElementCreate
extends zInfer<typeof HighlighterCreateSchema> {}
export interface TextElementCreate extends zInfer<typeof TextCreateSchema> {}
export interface NoteElementCreate extends zInfer<typeof NoteCreateSchema> {}
export interface ImageElementCreate extends zInfer<typeof ImageCreateSchema> {}
export interface PlaceElementRead extends zInfer<typeof PlaceReadSchema> {}
export interface PathElementRead extends zInfer<typeof PathReadSchema> {}
export interface PolygonElementRead extends zInfer<typeof PolygonReadSchema> {}
export interface CircleElementRead extends zInfer<typeof CircleReadSchema> {}
export interface MarkerElementRead extends zInfer<typeof MarkerReadSchema> {}
export interface HighlighterElementRead
extends zInfer<typeof HighlighterReadSchema> {}
export interface TextElementRead extends zInfer<typeof TextReadSchema> {}
export interface NoteElementRead extends zInfer<typeof NoteReadSchema> {}
export interface ImageElementRead extends zInfer<typeof ImageReadSchema> {}
export interface LinkElementRead extends zInfer<typeof LinkReadSchema> {}
export interface PlaceElementUpdate extends zInfer<typeof PlaceUpdateSchema> {}
export interface PathElementUpdate extends zInfer<typeof PathUpdateSchema> {}
export interface PolygonElementUpdate
extends zInfer<typeof PolygonUpdateSchema> {}
export interface CircleElementUpdate
extends zInfer<typeof CircleUpdateSchema> {}
export interface MarkerElementUpdate
extends zInfer<typeof MarkerUpdateSchema> {}
export interface HighlighterElementUpdate
extends zInfer<typeof HighlighterUpdateSchema> {}
export interface TextElementUpdate extends zInfer<typeof TextUpdateSchema> {}
export interface NoteElementUpdate extends zInfer<typeof NoteUpdateSchema> {}
export interface ImageElementUpdate extends zInfer<typeof ImageUpdateSchema> {}
export type ElementCreate =
| PlaceElementCreate
| PathElementCreate
| PolygonElementCreate
| CircleElementCreate
| MarkerElementCreate
| HighlighterElementCreate
| ImageElementCreate
| TextElementCreate
| NoteElementCreate;
export type ElementUpdate =
| PlaceElementUpdate
| PathElementUpdate
| PolygonElementUpdate
| CircleElementUpdate
| MarkerElementUpdate
| HighlighterElementUpdate
| TextElementUpdate
| NoteElementUpdate
| ImageElementUpdate;
/**
* @group Elements
*/
export type Element =
| PlaceElementRead
| PathElementRead
| PolygonElementRead
| CircleElementRead
| MarkerElementRead
| HighlighterElementRead
| TextElementRead
| NoteElementRead
| ImageElementRead
| LinkElementRead;
/**
* @group Element Groups
*/
export interface ElementGroup {
/**
* A string identifying the element group.
*/
id: string;
/**
* The name of the element group. This is shown in the legend.
*/
name: string;
/**
* The caption of the element group. This is shown in the legend.
*/
caption: string | null;
/**
* The ids of the elements in the element group.
*
* @remarks
* You can use these ids to get the full element objects via the `getElements` method.
*/
elementIds: Array<string>;
/**
* Whether the element group is visible or not.
*/
visible: boolean;
/**
* Whether the element group is shown in the legend or not.
*/
shownInLegend: boolean;
}
/**
* The constraints to apply when getting elements.
*
* @group Elements
*/
export interface GetElementsConstraint
extends zInfer<typeof GetElementsConstraintSchema> {}
export const GetElementsConstraintSchema = z.object({
/**
* The ids of the elements to get.
*/
ids: z.array(z.string()).optional(),
});
/**
* The constraints to apply when getting element groups.
*
* @group Element Groups
*/
export interface GetElementGroupsConstraint
extends zInfer<typeof GetElementGroupsConstraintSchema> {}
/**
* @ignore
*/
export const GetElementGroupsConstraintSchema = z.object({
/**
* The ids of the element groups to get.
*/
ids: z.array(z.string()).optional(),
});
/**
* The parameters for the `onElementChange` listener.
*
* @group Elements
*/
export interface ElementChangeCallbackParams {
/**
* The new data for the element or null if the element was removed.
*/
element: Element | null;
}
/**
* The parameters for the `onElementGroupChange` listener.
*
* @group Element Groups
*/
export interface ElementGroupChangeCallbackParams {
elementGroup: ElementGroup | null;
}