@phun-ky/arclace 0.0.0-stage → 0.2.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,411 @@
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 stop element the line arrives at. Defaults to `'auto'`.
56
+ */
57
+ stopSide?: 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 stop. 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
+ stopElement: 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
+ stop: ArcPointInterface;
166
+ startSide: ArcSideType;
167
+ stopSide: ArcSideType;
168
+ labelPoint: ArcPointInterface;
169
+ d: string;
170
+ };
171
+
172
+ /**
173
+ * Connection options that have no default.
174
+ */
175
+ type ArcOptionalKeysType = 'id' | 'label' | 'color' | 'className';
176
+ /**
177
+ * Connection options with every default filled in.
178
+ */
179
+ type ArcResolvedConnectionOptionsType = Required<Omit<ArcConnectionOptionsType, ArcOptionalKeysType>> & Pick<ArcConnectionOptionsType, ArcOptionalKeysType> & {
180
+ id: string;
181
+ };
182
+ /**
183
+ * Options every connection falls back to.
184
+ */
185
+ declare const DEFAULT_CONNECTION_OPTIONS: Required<Omit<ArcConnectionOptionsType, ArcOptionalKeysType>>;
186
+ type ArcConnectionParamsType = {
187
+ id: string;
188
+ startElement: Element;
189
+ stopElement: Element;
190
+ options: ArcConnectionOptionsType;
191
+ defaults?: ArcConnectionOptionsType;
192
+ owner: Arclace;
193
+ };
194
+ /**
195
+ * A line between two elements. Created with {@link Arclace.connect}.
196
+ *
197
+ * Each connection renders as:
198
+ *
199
+ * ```html
200
+ * <g class="arc-connection arc-bezier">
201
+ * <marker class="arc-marker">…</marker> <!-- when `arrow` is set -->
202
+ * <path class="arc-path" />
203
+ * <text class="arc-label">…</text> <!-- when `label` is set -->
204
+ * <circle class="arc-anchor arc-anchor-start" /> <!-- when `anchors` is set -->
205
+ * <circle class="arc-anchor arc-anchor-stop" />
206
+ * </g>
207
+ * ```
208
+ *
209
+ * The line, arrows and anchors use `currentColor`, so setting `color` on
210
+ * `.arc-connection` colors all of them.
211
+ */
212
+ declare class Connection {
213
+ #private;
214
+ readonly id: string;
215
+ readonly startElement: Element;
216
+ readonly stopElement: Element;
217
+ /**
218
+ * The `<g>` element holding everything this connection draws.
219
+ */
220
+ readonly element: SVGGElement;
221
+ /**
222
+ * The `<path>` element of the line.
223
+ */
224
+ readonly path: SVGPathElement;
225
+ /**
226
+ * The geometry from the last update, or `null` when the connection is
227
+ * hidden because one of its elements is not rendered.
228
+ */
229
+ geometry: ArcGeometryType | null;
230
+ constructor(params: ArcConnectionParamsType);
231
+ /**
232
+ * The connection's options merged with the instance defaults.
233
+ */
234
+ get options(): Readonly<ArcResolvedConnectionOptionsType>;
235
+ /**
236
+ * Changes options and redraws.
237
+ *
238
+ * @example
239
+ * ```ts
240
+ * connection.setOptions({ type: 'orthogonal', arrow: 'end' });
241
+ * ```
242
+ */
243
+ setOptions(options: Omit<ArcConnectionOptionsType, 'id'>): this;
244
+ /**
245
+ * Redraws every connection of the owning instance.
246
+ */
247
+ update(): void;
248
+ /**
249
+ * Removes this connection.
250
+ */
251
+ remove(): void;
252
+ }
253
+
254
+ /**
255
+ * Draws and maintains SVG lines between DOM elements.
256
+ *
257
+ * An instance adds one `<svg class="arc-svg">` overlay to its container and
258
+ * draws every connection into it.
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * import { Arclace } from '@phun-ky/arclace';
263
+ *
264
+ * const arclace = new Arclace({ container: '#diagram' });
265
+ *
266
+ * arclace.connect('#a', '#b', { arrow: 'end', label: 'depends on' });
267
+ * ```
268
+ */
269
+ declare class Arclace {
270
+ #private;
271
+ /**
272
+ * The element the overlay was added to.
273
+ */
274
+ readonly container: HTMLElement;
275
+ /**
276
+ * The SVG overlay all lines are drawn in.
277
+ */
278
+ readonly svg: SVGSVGElement;
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 stopElement - The element (or selector) the line stops at.
289
+ * @param options - Options for this connection, merged over the instance defaults.
290
+ * @throws {Error} When an element can't be found or the id is taken.
291
+ * @example
292
+ * ```ts
293
+ * const connection = arclace.connect(a, b, { type: 'orthogonal', arrow: 'end' });
294
+ * ```
295
+ */
296
+ connect(startElement: ArcElementTargetType, stopElement: ArcElementTargetType, options?: ArcConnectionOptionsType): Connection;
297
+ /**
298
+ * Connects many pairs of elements and draws them in one go.
299
+ *
300
+ * @example
301
+ * ```ts
302
+ * arclace.connectAll([
303
+ * { startElement: '#a', stopElement: '#b' },
304
+ * { startElement: '#a', stopElement: '#c', dashed: true }
305
+ * ]);
306
+ * ```
307
+ */
308
+ connectAll(definitions: ArcConnectionDefinitionType[]): Connection[];
309
+ /**
310
+ * Returns the connection with the given id.
311
+ */
312
+ get(id: string): Connection | undefined;
313
+ /**
314
+ * Returns every connection that starts or stops at an element.
315
+ */
316
+ find(target: ArcElementTargetType): Connection[];
317
+ /**
318
+ * Removes connections.
319
+ *
320
+ * @param target - A connection, a connection id, or an element (or
321
+ * selector) whose connections should all be removed. Targets that match
322
+ * nothing are ignored.
323
+ * @example
324
+ * ```ts
325
+ * arclace.disconnect(connection);
326
+ * arclace.disconnect('my-connection-id');
327
+ * arclace.disconnect(document.querySelector('#a'));
328
+ * ```
329
+ */
330
+ disconnect(target: Connection | string | Element): void;
331
+ /**
332
+ * Removes every connection.
333
+ */
334
+ clear(): void;
335
+ /**
336
+ * Remeasures every element and redraws every connection. Called
337
+ * automatically when `autoUpdate` is on; call it yourself after moving
338
+ * elements in ways the page can't observe.
339
+ */
340
+ update(): void;
341
+ /**
342
+ * Removes every connection and the overlay, and stops listening for
343
+ * changes. The instance can't be used afterwards.
344
+ */
345
+ destroy(): void;
346
+ }
347
+ /**
348
+ * Creates a {@link Arclace} instance and connects the given pairs.
349
+ *
350
+ * @example
351
+ * ```ts
352
+ * import { createConnections } from '@phun-ky/arclace';
353
+ *
354
+ * const arclace = createConnections(
355
+ * [
356
+ * { startElement: '#a', stopElement: '#b' },
357
+ * { startElement: '#b', stopElement: '#c', arrow: 'end' }
358
+ * ],
359
+ * { defaults: { type: 'orthogonal' } }
360
+ * );
361
+ * ```
362
+ */
363
+ declare const createConnections: (definitions: ArcConnectionDefinitionType[], options?: ArcOptionsType) => Arclace;
364
+
365
+ type ArcPathResultType = {
366
+ /**
367
+ * SVG path data.
368
+ */
369
+ d: string;
370
+ /**
371
+ * The middle of the path, used to place labels.
372
+ */
373
+ labelPoint: ArcPointInterface;
374
+ };
375
+ /**
376
+ * A straight line between two points.
377
+ *
378
+ * @example
379
+ * ```ts
380
+ * straightPath({ x: 0, y: 0 }, { x: 100, y: 0 }).d; // 'M 0 0 L 100 0'
381
+ * ```
382
+ */
383
+ declare const straightPath: (start: ArcPointInterface, stop: ArcPointInterface) => ArcPathResultType;
384
+ /**
385
+ * A cubic bezier curve that leaves `start` and arrives at `stop`
386
+ * perpendicular to their sides.
387
+ *
388
+ * @example
389
+ * ```ts
390
+ * bezierPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left').d;
391
+ * ```
392
+ */
393
+ declare const bezierPath: (start: ArcPointInterface, startSide: ArcSideType, stop: ArcPointInterface, stopSide: ArcSideType, curvature?: number) => ArcPathResultType;
394
+ /**
395
+ * An orthogonal line with rounded corners.
396
+ *
397
+ * @example
398
+ * ```ts
399
+ * orthogonalPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left', {
400
+ * radius: 8,
401
+ * offset: 20
402
+ * }).d;
403
+ * ```
404
+ */
405
+ declare const orthogonalPath: (start: ArcPointInterface, startSide: ArcSideType, stop: ArcPointInterface, stopSide: ArcSideType, { radius, offset }?: {
406
+ radius?: number;
407
+ offset?: number;
408
+ }) => ArcPathResultType;
409
+
410
+ export { Arclace, Connection, DEFAULT_CONNECTION_OPTIONS, bezierPath, createConnections, orthogonalPath, straightPath };
411
+ export type { ArcArrowType, ArcConnectionDefinitionType, ArcConnectionOptionsType, ArcConnectionParamsType, ArcElementTargetType, ArcGeometryType, ArcOptionalKeysType, ArcOptionsType, ArcPathResultType, ArcPathType, ArcPointInterface, ArcRectInterface, ArcResolvedConnectionOptionsType, ArcSideOptionType, ArcSideType };