@phun-ky/arclace 0.0.0-stage → 0.3.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.
@@ -0,0 +1,456 @@
1
+ /**
2
+ * A side of an element's bounding box.
3
+ */
4
+ type ArcSideType = 'top' | 'right' | 'bottom' | 'left';
5
+ /**
6
+ * A side, or `'auto'` to let Arclace pick the side that faces the
7
+ * other element.
8
+ */
9
+ type ArcSideOptionType = ArcSideType | 'auto';
10
+ /**
11
+ * How the line between two elements is drawn.
12
+ *
13
+ * - `bezier`: a smooth cubic curve (default)
14
+ * - `straight`: a straight line
15
+ * - `orthogonal`: horizontal and vertical segments with rounded corners
16
+ */
17
+ type ArcPathType = 'bezier' | 'straight' | 'orthogonal';
18
+ /**
19
+ * Where to draw arrow heads.
20
+ */
21
+ type ArcArrowType = 'none' | 'start' | 'end' | 'both';
22
+ /**
23
+ * An element, or a CSS selector that resolves to one.
24
+ */
25
+ type ArcElementTargetType = Element | string;
26
+ interface ArcPointInterface {
27
+ x: number;
28
+ y: number;
29
+ }
30
+ interface ArcRectInterface {
31
+ left: number;
32
+ top: number;
33
+ right: number;
34
+ bottom: number;
35
+ width: number;
36
+ height: number;
37
+ }
38
+ /**
39
+ * Options for a single connection.
40
+ */
41
+ type ArcConnectionOptionsType = {
42
+ /**
43
+ * A unique id for the connection. Generated when omitted.
44
+ */
45
+ id?: string;
46
+ /**
47
+ * How the line is drawn. Defaults to `'bezier'`.
48
+ */
49
+ type?: ArcPathType;
50
+ /**
51
+ * The side of the start element the line leaves from. Defaults to `'auto'`.
52
+ */
53
+ startSide?: ArcSideOptionType;
54
+ /**
55
+ * The side of the end element the line arrives at. Defaults to `'auto'`.
56
+ */
57
+ endSide?: ArcSideOptionType;
58
+ /**
59
+ * Where to draw arrow heads. Defaults to `'none'`.
60
+ */
61
+ arrow?: ArcArrowType;
62
+ /**
63
+ * Text drawn at the middle of the line.
64
+ */
65
+ label?: string;
66
+ /**
67
+ * Line color. Any CSS color. Defaults to `currentColor` of the container,
68
+ * or `--arc-color` when the stylesheet is loaded.
69
+ */
70
+ color?: string;
71
+ /**
72
+ * Line width in pixels. Defaults to `2`.
73
+ */
74
+ strokeWidth?: number;
75
+ /**
76
+ * Draw a dashed line. Defaults to `false`.
77
+ */
78
+ dashed?: boolean;
79
+ /**
80
+ * Animate the dashes from start to end. Implies `dashed`. Requires the
81
+ * stylesheet. Defaults to `false`.
82
+ */
83
+ animated?: boolean;
84
+ /**
85
+ * Draw a dot where the line touches each element. Defaults to `false`.
86
+ */
87
+ anchors?: boolean;
88
+ /**
89
+ * Extra class names added to the connection's `<g>` element.
90
+ */
91
+ className?: string;
92
+ /**
93
+ * Distance in pixels between the element's edge and the line's end.
94
+ * Defaults to `0`.
95
+ */
96
+ gap?: number;
97
+ /**
98
+ * When several connections share the same side of an element, spread their
99
+ * end points along that side instead of stacking them in the middle.
100
+ * Defaults to `true`.
101
+ */
102
+ spread?: boolean;
103
+ /**
104
+ * Preferred distance in pixels between spread end points. Shrinks to fit
105
+ * the side. Defaults to `16`.
106
+ */
107
+ spacing?: number;
108
+ /**
109
+ * `bezier` only: multiplies how far the curve bulges out from the
110
+ * elements. Defaults to `1`.
111
+ */
112
+ curvature?: number;
113
+ /**
114
+ * `orthogonal` only: corner radius in pixels. Use `0` for sharp corners.
115
+ * Defaults to `8`.
116
+ */
117
+ radius?: number;
118
+ /**
119
+ * `orthogonal` only: how far in pixels the line travels straight out of an
120
+ * element before it turns. Defaults to `20`.
121
+ */
122
+ offset?: number;
123
+ };
124
+ /**
125
+ * A connection between two elements, used with
126
+ * {@link Arclace.connectAll} and {@link createConnections}.
127
+ */
128
+ type ArcConnectionDefinitionType = ArcConnectionOptionsType & {
129
+ startElement: ArcElementTargetType;
130
+ endElement: ArcElementTargetType;
131
+ };
132
+ /**
133
+ * Options for a {@link Arclace} instance.
134
+ */
135
+ type ArcOptionsType = {
136
+ /**
137
+ * The element the SVG overlay is added to. All connected elements should
138
+ * be inside it. Defaults to `document.body`.
139
+ */
140
+ container?: ArcElementTargetType;
141
+ /**
142
+ * Keep lines in sync with the page.
143
+ *
144
+ * - `true`: redraw on window resize, scroll, element resize and DOM or
145
+ * style changes inside the container (default)
146
+ * - `'frame'`: redraw on every animation frame, for CSS animations and
147
+ * transitions
148
+ * - `false`: only redraw when you call `update()`
149
+ */
150
+ autoUpdate?: boolean | 'frame';
151
+ /**
152
+ * Default options for every connection made by this instance.
153
+ */
154
+ defaults?: ArcConnectionOptionsType;
155
+ /**
156
+ * Extra class names added to the SVG overlay.
157
+ */
158
+ className?: string;
159
+ };
160
+ /**
161
+ * The computed geometry of a connection after the last update.
162
+ */
163
+ type ArcGeometryType = {
164
+ start: ArcPointInterface;
165
+ end: ArcPointInterface;
166
+ startSide: ArcSideType;
167
+ endSide: ArcSideType;
168
+ labelPoint: ArcPointInterface;
169
+ d: string;
170
+ };
171
+
172
+ /**
173
+ * Connection options with every default filled in. `label`, `color` and
174
+ * `className` have no default.
175
+ */
176
+ type ArcResolvedConnectionOptionsType = Required<Omit<ArcConnectionOptionsType, 'id' | 'label' | 'color' | 'className'>> & Pick<ArcConnectionOptionsType, 'label' | 'color' | 'className'> & {
177
+ id: string;
178
+ };
179
+ /**
180
+ * Options every connection falls back to. Frozen: set your own defaults with
181
+ * the `defaults` option of {@link Arclace} instead.
182
+ */
183
+ declare const DEFAULT_CONNECTION_OPTIONS: Readonly<Required<Omit<ArcConnectionOptionsType, 'id' | 'label' | 'color' | 'className'>>>;
184
+ /**
185
+ * A line between two elements. Created with {@link Arclace.connect} or
186
+ * {@link Arclace.connectAll}, not with `new`.
187
+ *
188
+ * Each connection renders as:
189
+ *
190
+ * ```html
191
+ * <g class="arc-connection arc-bezier">
192
+ * <marker class="arc-marker">…</marker> <!-- when `arrow` is set -->
193
+ * <path class="arc-path" />
194
+ * <text class="arc-label">…</text> <!-- when `label` is set -->
195
+ * <circle class="arc-anchor arc-anchor-start" /> <!-- when `anchors` is set -->
196
+ * <circle class="arc-anchor arc-anchor-end" />
197
+ * </g>
198
+ * ```
199
+ *
200
+ * The line, arrows and anchors use `currentColor`, so setting `color` on
201
+ * `.arc-connection` colors all of them.
202
+ */
203
+ declare class Connection {
204
+ #private;
205
+ readonly id: string;
206
+ readonly startElement: Element;
207
+ readonly endElement: Element;
208
+ /**
209
+ * The `<g>` element holding everything this connection draws.
210
+ */
211
+ readonly element: SVGGElement;
212
+ /**
213
+ * The `<path>` element of the line.
214
+ */
215
+ readonly path: SVGPathElement;
216
+ private constructor();
217
+ /**
218
+ * The geometry from the last update, or `null` when the connection is
219
+ * hidden because one of its elements is not rendered.
220
+ */
221
+ get geometry(): Readonly<ArcGeometryType> | null;
222
+ /**
223
+ * The connection's options merged with the instance defaults.
224
+ */
225
+ get options(): Readonly<ArcResolvedConnectionOptionsType>;
226
+ /**
227
+ * Changes options and redraws.
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * connection.setOptions({ type: 'orthogonal', arrow: 'end' });
232
+ * ```
233
+ * @throws {TypeError} When an option has the wrong type, or `id` is set.
234
+ * @throws {RangeError} When an option is out of range.
235
+ * @throws {ArclaceError} When the connection has been removed.
236
+ */
237
+ setOptions(options: Omit<ArcConnectionOptionsType, 'id'>): this;
238
+ /**
239
+ * Redraws every connection of the owning instance.
240
+ */
241
+ update(): void;
242
+ /**
243
+ * Removes this connection.
244
+ */
245
+ remove(): void;
246
+ }
247
+
248
+ /**
249
+ * Draws and maintains SVG lines between DOM elements.
250
+ *
251
+ * An instance adds one `<svg class="arc-svg">` overlay to its container and
252
+ * draws every connection into it.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * import { Arclace } from '@phun-ky/arclace';
257
+ *
258
+ * const arclace = new Arclace({ container: '#diagram' });
259
+ *
260
+ * arclace.connect('#a', '#b', { arrow: 'end', label: 'depends on' });
261
+ * ```
262
+ */
263
+ declare class Arclace {
264
+ #private;
265
+ /**
266
+ * The element the overlay was added to.
267
+ */
268
+ readonly container: HTMLElement;
269
+ /**
270
+ * The SVG overlay all lines are drawn in.
271
+ */
272
+ readonly svg: SVGSVGElement;
273
+ /**
274
+ * @throws {TypeError} When an option has the wrong type.
275
+ * @throws {RangeError} When an option is out of range.
276
+ * @throws {ArclaceError} When there is no DOM, or the container can't be
277
+ * found.
278
+ */
279
+ constructor(options?: ArcOptionsType);
280
+ /**
281
+ * All current connections, in the order they were made.
282
+ */
283
+ get connections(): Connection[];
284
+ /**
285
+ * Connects two elements and draws the line right away.
286
+ *
287
+ * @param startElement - The element (or selector) the line starts at.
288
+ * @param endElement - The element (or selector) the line ends at.
289
+ * @param options - Options for this connection, merged over the instance defaults.
290
+ * @throws {TypeError} When an element or an option has the wrong type.
291
+ * @throws {RangeError} When an option is out of range.
292
+ * @throws {ArclaceError} When an element can't be found, the id is taken
293
+ * or the instance has been destroyed. Nothing is added.
294
+ * @example
295
+ * ```ts
296
+ * const connection = arclace.connect(a, b, { type: 'orthogonal', arrow: 'end' });
297
+ * ```
298
+ */
299
+ connect(startElement: ArcElementTargetType, endElement: ArcElementTargetType, options?: ArcConnectionOptionsType): Connection;
300
+ /**
301
+ * Connects many pairs of elements and draws them in one go. Every
302
+ * definition is checked first, so either all of them are added or none.
303
+ *
304
+ * @throws {TypeError} When `definitions` isn't an array, or an element or
305
+ * an option has the wrong type.
306
+ * @throws {RangeError} When an option is out of range.
307
+ * @throws {ArclaceError} When an element can't be found, an id is taken or
308
+ * repeated, or the instance has been destroyed.
309
+ * @example
310
+ * ```ts
311
+ * arclace.connectAll([
312
+ * { startElement: '#a', endElement: '#b' },
313
+ * { startElement: '#a', endElement: '#c', dashed: true }
314
+ * ]);
315
+ * ```
316
+ */
317
+ connectAll(definitions: ArcConnectionDefinitionType[]): Connection[];
318
+ /**
319
+ * Returns the connection with the given id.
320
+ */
321
+ get(id: string): Connection | undefined;
322
+ /**
323
+ * Returns every connection that starts or ends at an element. A selector
324
+ * that matches nothing returns an empty array.
325
+ *
326
+ * @throws {TypeError} When the target is neither an element nor a string.
327
+ * @throws {ArclaceError} When the selector is invalid.
328
+ */
329
+ find(target: ArcElementTargetType): Connection[];
330
+ /**
331
+ * Removes connections.
332
+ *
333
+ * @param target - A connection, a connection id, or an element (or
334
+ * selector) whose connections should all be removed. Targets that match
335
+ * nothing, and `null` or `undefined`, are ignored.
336
+ * @throws {TypeError} When the target is another kind of value.
337
+ * @throws {ArclaceError} When a string is neither an id nor a valid
338
+ * selector.
339
+ * @example
340
+ * ```ts
341
+ * arclace.disconnect(connection);
342
+ * arclace.disconnect('my-connection-id');
343
+ * arclace.disconnect(document.querySelector('#a'));
344
+ * ```
345
+ */
346
+ disconnect(target: Connection | string | Element): void;
347
+ /**
348
+ * Removes every connection.
349
+ */
350
+ clear(): void;
351
+ /**
352
+ * Remeasures every element and redraws every connection. Called
353
+ * automatically when `autoUpdate` is on; call it yourself after moving
354
+ * elements in ways the page can't observe.
355
+ */
356
+ update(): void;
357
+ /**
358
+ * Removes every connection and the overlay, and stops listening for
359
+ * changes. The instance can't be used afterwards.
360
+ */
361
+ destroy(): void;
362
+ }
363
+ /**
364
+ * Creates a {@link Arclace} instance and connects the given pairs.
365
+ *
366
+ * @example
367
+ * ```ts
368
+ * import { createConnections } from '@phun-ky/arclace';
369
+ *
370
+ * const arclace = createConnections(
371
+ * [
372
+ * { startElement: '#a', endElement: '#b' },
373
+ * { startElement: '#b', endElement: '#c', arrow: 'end' }
374
+ * ],
375
+ * { defaults: { type: 'orthogonal' } }
376
+ * );
377
+ * ```
378
+ */
379
+ declare const createConnections: (definitions: ArcConnectionDefinitionType[], options?: ArcOptionsType) => Arclace;
380
+
381
+ /**
382
+ * Thrown when Arclace can't do what was asked, for example when a selector
383
+ * matches nothing, an id is taken, or the instance has been destroyed.
384
+ *
385
+ * Arguments of the wrong type throw a `TypeError`, and values out of range a
386
+ * `RangeError`, instead. When another error is behind the failure, such as
387
+ * an invalid selector, it is the `cause`.
388
+ *
389
+ * @example
390
+ * ```ts
391
+ * try {
392
+ * arclace.connect('#a', '#b', { id: 'a-to-b' });
393
+ * arclace.connect('#b', '#c', { id: 'a-to-b' }); // the id is taken
394
+ * } catch (error) {
395
+ * if (error instanceof ArclaceError) console.warn(error.message);
396
+ * }
397
+ * ```
398
+ */
399
+ declare class ArclaceError extends Error {
400
+ name: string;
401
+ constructor(message: string, options?: ErrorOptions);
402
+ }
403
+
404
+ type ArcPathResultType = {
405
+ /**
406
+ * SVG path data.
407
+ */
408
+ d: string;
409
+ /**
410
+ * The middle of the path, used to place labels.
411
+ */
412
+ labelPoint: ArcPointInterface;
413
+ };
414
+ /**
415
+ * A straight line between two points.
416
+ *
417
+ * @throws {TypeError} When a point doesn't have finite `x` and `y`.
418
+ * @example
419
+ * ```ts
420
+ * straightPath({ x: 0, y: 0 }, { x: 100, y: 0 }).d; // 'M 0 0 L 100 0'
421
+ * ```
422
+ */
423
+ declare const straightPath: (start: ArcPointInterface, end: ArcPointInterface) => ArcPathResultType;
424
+ /**
425
+ * A cubic bezier curve that leaves `start` and arrives at `end`
426
+ * perpendicular to their sides.
427
+ *
428
+ * @throws {TypeError} When a point or `curvature` isn't finite.
429
+ * @throws {RangeError} When a side isn't `top`, `right`, `bottom` or `left`.
430
+ * @example
431
+ * ```ts
432
+ * bezierPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left').d;
433
+ * ```
434
+ */
435
+ declare const bezierPath: (start: ArcPointInterface, startSide: ArcSideType, end: ArcPointInterface, endSide: ArcSideType, curvature?: number) => ArcPathResultType;
436
+ /**
437
+ * An orthogonal line with rounded corners.
438
+ *
439
+ * @throws {TypeError} When a point, `radius` or `offset` isn't finite.
440
+ * @throws {RangeError} When a side isn't `top`, `right`, `bottom` or `left`,
441
+ * or `radius` or `offset` is negative.
442
+ * @example
443
+ * ```ts
444
+ * orthogonalPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left', {
445
+ * radius: 8,
446
+ * offset: 20
447
+ * }).d;
448
+ * ```
449
+ */
450
+ declare const orthogonalPath: (start: ArcPointInterface, startSide: ArcSideType, end: ArcPointInterface, endSide: ArcSideType, { radius, offset }?: {
451
+ radius?: number;
452
+ offset?: number;
453
+ }) => ArcPathResultType;
454
+
455
+ export { Arclace, ArclaceError, Connection, DEFAULT_CONNECTION_OPTIONS, bezierPath, createConnections, orthogonalPath, straightPath };
456
+ export type { ArcArrowType, ArcConnectionDefinitionType, ArcConnectionOptionsType, ArcElementTargetType, ArcGeometryType, ArcOptionsType, ArcPathResultType, ArcPathType, ArcPointInterface, ArcRectInterface, ArcResolvedConnectionOptionsType, ArcSideOptionType, ArcSideType };