@html-graph/html-graph 0.0.52 → 0.0.53

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/README.md CHANGED
@@ -10,12 +10,10 @@
10
10
  <img width="100%" src="https://raw.githubusercontent.com/html-graph/html-graph/master/media/full-demo.gif"/>
11
11
  </a>
12
12
 
13
- Instead of connecting nodes dirrectly this library uses concept of ports, which provide greater fexibility at managing edges.
14
- Port is an entity of a node to which edge can be attached to.
15
-
16
- This library fits for tasks where easy nodes customization and interactiveness are required.
13
+ Visit <a target="_blank" href="https://html-graph.github.io">live demo</a>.
17
14
 
18
- Visit <a target="_blank" href="https://html-graph.github.io">live demo</a>
15
+ Instead of connecting nodes directly this library uses concept of ports, which provide greater fexibility at managing edges.
16
+ Port is an entity of a node to which edge can be attached to.
19
17
 
20
18
  ## Features:
21
19
 
@@ -37,8 +35,14 @@ import { AddNodePorts, HtmlGraphBuilder } from "@html-graph/html-graph";
37
35
 
38
36
  const canvas = new HtmlGraphBuilder()
39
37
  .setOptions({
40
- background: { type: "dots" },
41
- edges: { hasTargetArrow: true },
38
+ background: {
39
+ type: "dots",
40
+ },
41
+ edges: {
42
+ shape: {
43
+ hasTargetArrow: true,
44
+ },
45
+ },
42
46
  })
43
47
  .setUserDraggableNodes()
44
48
  .setUserTransformableCanvas()
@@ -64,12 +68,13 @@ function createNode(
64
68
  backPort.classList.add("port");
65
69
  node.appendChild(backPort);
66
70
 
67
- const ports = new Map([
68
- [frontPortId, frontPort],
69
- [backPortId, backPort],
70
- ]);
71
-
72
- return [node, ports];
71
+ return [
72
+ node,
73
+ [
74
+ [frontPortId, frontPort],
75
+ [backPortId, backPort],
76
+ ],
77
+ ];
73
78
  }
74
79
 
75
80
  const [node1, ports1] = createNode("Node 1", "port-1-1", "port-1-2");
package/dist/main.d.ts CHANGED
@@ -2,11 +2,11 @@ export declare interface AddEdgeRequest {
2
2
  id?: unknown;
3
3
  from: string;
4
4
  to: string;
5
- options?: EdgeOptions;
5
+ options?: EdgeShape;
6
6
  priority?: number;
7
7
  }
8
8
 
9
- export declare type AddNodePorts = ReadonlyMap<unknown, MarkNodePortRequest>;
9
+ export declare type AddNodePorts = Iterable<readonly [unknown, MarkNodePortRequest]>;
10
10
 
11
11
  export declare interface AddNodeRequest {
12
12
  id?: unknown;
@@ -22,9 +22,6 @@ export declare type BackgroundDrawingFn = (ctx: CanvasRenderingContext2D, transf
22
22
 
23
23
  declare type BackgroundOptions = {
24
24
  readonly type: "none";
25
- } | {
26
- readonly type: "color";
27
- readonly color?: string;
28
25
  } | {
29
26
  readonly type: "dots";
30
27
  readonly dotColor?: string;
@@ -36,7 +33,7 @@ declare type BackgroundOptions = {
36
33
  readonly drawingFn: BackgroundDrawingFn;
37
34
  };
38
35
 
39
- export declare class BezierEdgeController implements EdgeController {
36
+ export declare class BezierEdgeShape implements EdgeController {
40
37
  private readonly color;
41
38
  private readonly width;
42
39
  private readonly curvature;
@@ -51,7 +48,7 @@ export declare class BezierEdgeController implements EdgeController {
51
48
  update(x: number, y: number, width: number, height: number, from: PortPayload, to: PortPayload): void;
52
49
  }
53
50
 
54
- declare interface BezierEdgeOptions {
51
+ declare interface BezierEdgeShape_2 {
55
52
  readonly type?: "bezier";
56
53
  readonly color?: string;
57
54
  readonly width?: number;
@@ -67,6 +64,10 @@ declare interface BezierEdgeOptions {
67
64
  }
68
65
 
69
66
  export declare interface Canvas {
67
+ /**
68
+ * provides api for accessing graph model
69
+ */
70
+ readonly model: PublicGraphStore;
70
71
  /**
71
72
  * provides api for canvas transformation
72
73
  */
@@ -118,10 +119,6 @@ export declare interface Canvas {
118
119
  * moves viewport to nodes
119
120
  */
120
121
  moveToNodes(nodeIds: readonly string[]): Canvas;
121
- /**
122
- * moves specified node on top
123
- */
124
- moveNodeOnTop(nodeId: unknown): Canvas;
125
122
  /**
126
123
  * attaches canvas to given element
127
124
  */
@@ -150,13 +147,13 @@ export declare interface Canvas {
150
147
  export declare class CanvasCore implements Canvas {
151
148
  private readonly apiOptions?;
152
149
  readonly transformation: PublicViewportTransformer;
150
+ readonly model: PublicGraphStore;
153
151
  private readonly di;
154
- private readonly edgeControllerFactory;
152
+ private readonly edgeShapeFactory;
155
153
  constructor(apiOptions?: CoreOptions | undefined);
156
154
  addNode(node: AddNodeRequest): CanvasCore;
157
155
  updateNode(nodeId: unknown, request: UpdateNodeRequest): CanvasCore;
158
156
  removeNode(nodeId: unknown): CanvasCore;
159
- moveNodeOnTop(nodeId: unknown): CanvasCore;
160
157
  markPort(port: MarkPortRequest): CanvasCore;
161
158
  updatePort(portId: string, request: UpdatePortRequest): CanvasCore;
162
159
  unmarkPort(portId: string): CanvasCore;
@@ -173,6 +170,8 @@ export declare class CanvasCore implements Canvas {
173
170
 
174
171
  export declare type CenterFn = (width: number, height: number) => [number, number];
175
172
 
173
+ declare type ConstantPriority = number;
174
+
176
175
  export declare interface CoreOptions {
177
176
  /**
178
177
  * canvas background settings
@@ -187,6 +186,10 @@ export declare interface CoreOptions {
187
186
  * center of nodes specified in addNode method by x and y
188
187
  */
189
188
  readonly centerFn?: CenterFn;
189
+ /**
190
+ * specifies default z-index value
191
+ */
192
+ readonly priority?: Priority;
190
193
  };
191
194
  /**
192
195
  * ports related behavior
@@ -205,19 +208,19 @@ export declare interface CoreOptions {
205
208
  /**
206
209
  *edges related behavior
207
210
  */
208
- readonly edges?: EdgeOptions;
209
- /**
210
- * layers related behavior
211
- */
212
- readonly layers?: {
211
+ readonly edges?: {
213
212
  /**
214
- * sets layers order
213
+ * specifies default controller
215
214
  */
216
- readonly mode?: LayersMode;
215
+ readonly shape?: EdgeShape;
216
+ /**
217
+ * specifies default z-index value
218
+ */
219
+ readonly priority?: Priority;
217
220
  };
218
221
  }
219
222
 
220
- export declare const createBezierEdgeControllerFactory: (options: {
223
+ export declare const createBezierEdgeShapeFactory: (options: {
221
224
  color: string;
222
225
  width: number;
223
226
  arrowLength: number;
@@ -229,9 +232,9 @@ export declare const createBezierEdgeControllerFactory: (options: {
229
232
  smallCycleRadius: number;
230
233
  detourDistance: number;
231
234
  detourDirection: number;
232
- }) => EdgeControllerFactory;
235
+ }) => EdgeShapeFactory;
233
236
 
234
- export declare const createHorizontalEdgeControllerFactory: (options: {
237
+ export declare const createHorizontalEdgeShapeFactory: (options: {
235
238
  color: string;
236
239
  width: number;
237
240
  arrowLength: number;
@@ -243,9 +246,9 @@ export declare const createHorizontalEdgeControllerFactory: (options: {
243
246
  roundness: number;
244
247
  detourDistance: number;
245
248
  detourDirection: number;
246
- }) => EdgeControllerFactory;
249
+ }) => EdgeShapeFactory;
247
250
 
248
- export declare const createStraightEdgeControllerFactory: (options: {
251
+ export declare const createStraightEdgeShareFactory: (options: {
249
252
  color: string;
250
253
  width: number;
251
254
  arrowLength: number;
@@ -257,9 +260,9 @@ export declare const createStraightEdgeControllerFactory: (options: {
257
260
  roundness: number;
258
261
  detourDistance: number;
259
262
  detourDirection: number;
260
- }) => EdgeControllerFactory;
263
+ }) => EdgeShapeFactory;
261
264
 
262
- export declare const createVerticalEdgeControllerFactory: (options: {
265
+ export declare const createVerticalEdgeShapeFactory: (options: {
263
266
  color: string;
264
267
  width: number;
265
268
  arrowLength: number;
@@ -271,14 +274,16 @@ export declare const createVerticalEdgeControllerFactory: (options: {
271
274
  roundness: number;
272
275
  detourDistance: number;
273
276
  detourDirection: number;
274
- }) => EdgeControllerFactory;
277
+ }) => EdgeShapeFactory;
275
278
 
276
- declare interface CustomEdgeOptions {
279
+ declare interface CustomEdgeShape {
277
280
  readonly type: "custom";
278
- readonly controllerFactory: EdgeControllerFactory;
281
+ readonly controllerFactory: EdgeShapeFactory;
279
282
  }
280
283
 
281
- export declare class CycleCircleEdgeController implements EdgeController {
284
+ declare type CustomPriority = PriorityFn;
285
+
286
+ export declare class CycleCircleEdgeShape implements EdgeController {
282
287
  private readonly color;
283
288
  private readonly width;
284
289
  private readonly arrowLength;
@@ -293,7 +298,7 @@ export declare class CycleCircleEdgeController implements EdgeController {
293
298
  update(x: number, y: number, _width: number, _height: number, from: PortPayload): void;
294
299
  }
295
300
 
296
- export declare class CycleSquareEdgeController implements EdgeController {
301
+ export declare class CycleSquareEdgeShape implements EdgeController {
297
302
  private readonly color;
298
303
  private readonly width;
299
304
  private readonly arrowLength;
@@ -310,7 +315,7 @@ export declare class CycleSquareEdgeController implements EdgeController {
310
315
  update(x: number, y: number, _width: number, _height: number, from: PortPayload): void;
311
316
  }
312
317
 
313
- export declare class DetourStraightEdgeController implements EdgeController {
318
+ export declare class DetourStraightEdgeShape implements EdgeController {
314
319
  private readonly color;
315
320
  private readonly width;
316
321
  private readonly arrowLength;
@@ -329,6 +334,7 @@ export declare class DetourStraightEdgeController implements EdgeController {
329
334
  }
330
335
 
331
336
  export declare interface DragOptions {
337
+ grabPriorityStrategy?: "freeze" | "move-on-top";
332
338
  events?: {
333
339
  onNodeDrag?: (payload: NodeDragPayload) => void;
334
340
  onBeforeNodeDrag?: (payload: NodeDragPayload) => boolean;
@@ -340,9 +346,16 @@ export declare interface EdgeController {
340
346
  update(x: number, y: number, width: number, height: number, from: PortPayload, to: PortPayload): void;
341
347
  }
342
348
 
343
- declare type EdgeControllerFactory = (type: EdgeType) => EdgeController;
349
+ declare interface EdgePayload {
350
+ from: string;
351
+ to: string;
352
+ shape: EdgeController;
353
+ priority: number;
354
+ }
355
+
356
+ declare type EdgeShape = BezierEdgeShape_2 | StraightEdgeShape_2 | CustomEdgeShape | HorizontalEdgeShape_2 | VerticalEdgeShape_2;
344
357
 
345
- declare type EdgeOptions = BezierEdgeOptions | StraightEdgeOptions | CustomEdgeOptions | HorizontalEdgeOptions | VerticalEdgeOptions;
358
+ declare type EdgeShapeFactory = (type: EdgeType) => EdgeController;
346
359
 
347
360
  export declare enum EdgeType {
348
361
  Regular = "regular",
@@ -350,7 +363,57 @@ export declare enum EdgeType {
350
363
  NodeCycle = "node-cycle"
351
364
  }
352
365
 
353
- export declare class HorizontalEdgeController implements EdgeController {
366
+ export declare interface GraphEdge {
367
+ readonly from: unknown;
368
+ readonly to: unknown;
369
+ readonly priority: number;
370
+ }
371
+
372
+ export declare interface GraphNode {
373
+ readonly element: HTMLElement;
374
+ readonly x: number;
375
+ readonly y: number;
376
+ readonly centerFn: CenterFn;
377
+ readonly priority: number;
378
+ }
379
+
380
+ export declare interface GraphPort {
381
+ readonly element: HTMLElement;
382
+ readonly centerFn: CenterFn;
383
+ readonly direction: number;
384
+ }
385
+
386
+ declare class GraphStore {
387
+ private readonly nodes;
388
+ private readonly ports;
389
+ private readonly nodePorts;
390
+ private readonly portNodeId;
391
+ private readonly edges;
392
+ private readonly incommingEdges;
393
+ private readonly outcommingEdges;
394
+ private readonly cycleEdges;
395
+ addNode(nodeId: unknown, element: HTMLElement, x: number, y: number, centerFn: CenterFn, priority: number): void;
396
+ getNode(nodeId: unknown): NodePayload | undefined;
397
+ removeNode(nodeId: unknown): void;
398
+ addPort(portId: unknown, element: HTMLElement, nodeId: unknown, centerFn: CenterFn, dir: number): void;
399
+ getPort(portId: unknown): PortPayload | undefined;
400
+ getPortNode(portId: string): unknown | undefined;
401
+ removePort(portId: string): void;
402
+ addEdge(edgeId: unknown, fromPortId: string, toPortId: string, shape: EdgeController, priority: number): void;
403
+ getEdge(edgeId: unknown): EdgePayload | undefined;
404
+ removeEdge(edgeId: unknown): void;
405
+ getPortAdjacentEdges(portId: string): readonly unknown[];
406
+ getNodeAdjacentEdges(nodeId: unknown): readonly unknown[];
407
+ clear(): void;
408
+ getPortIncomingEdges(portId: unknown): readonly unknown[];
409
+ getPortOutcomingEdges(portId: unknown): readonly unknown[];
410
+ getPortCycleEdges(portId: unknown): readonly unknown[];
411
+ getNodeIncomingEdges(nodeId: unknown): readonly unknown[];
412
+ getNodeOutcomingEdges(nodeId: unknown): readonly unknown[];
413
+ getNodeCycleEdges(nodeId: unknown): readonly unknown[];
414
+ }
415
+
416
+ export declare class HorizontalEdgeShape implements EdgeController {
354
417
  private readonly color;
355
418
  private readonly width;
356
419
  private readonly arrowLength;
@@ -366,7 +429,7 @@ export declare class HorizontalEdgeController implements EdgeController {
366
429
  update(x: number, y: number, width: number, height: number, from: PortPayload, to: PortPayload): void;
367
430
  }
368
431
 
369
- declare interface HorizontalEdgeOptions {
432
+ declare interface HorizontalEdgeShape_2 {
370
433
  readonly type: "horizontal";
371
434
  readonly color?: string;
372
435
  readonly width?: number;
@@ -393,7 +456,7 @@ export declare class HtmlGraphBuilder {
393
456
  build(): Canvas;
394
457
  }
395
458
 
396
- declare type LayersMode = "edges-on-top" | "nodes-on-top" | "edges-follow-node";
459
+ declare type IncrementalPriority = "incremental";
397
460
 
398
461
  export declare type MarkNodePortRequest = HTMLElement | {
399
462
  readonly element: HTMLElement;
@@ -416,17 +479,12 @@ export declare interface NodeDragPayload {
416
479
  readonly y: number;
417
480
  }
418
481
 
419
- export declare interface NodeItem {
420
- readonly id: string;
421
- readonly x: number;
422
- readonly y: number;
423
- readonly element: HTMLElement;
424
- }
425
-
426
- export declare interface NodeResponse {
427
- readonly x: number;
428
- readonly y: number;
429
- readonly element: HTMLElement;
482
+ declare interface NodePayload {
483
+ element: HTMLElement;
484
+ x: number;
485
+ y: number;
486
+ centerFn: CenterFn;
487
+ priority: number;
430
488
  }
431
489
 
432
490
  export declare interface PatchViewportRequest {
@@ -441,6 +499,27 @@ export declare interface PortPayload {
441
499
  direction: number;
442
500
  }
443
501
 
502
+ declare type Priority = ConstantPriority | IncrementalPriority | SharedIncrementalPriority | CustomPriority;
503
+
504
+ export declare type PriorityFn = () => number;
505
+
506
+ declare class PublicGraphStore {
507
+ private readonly graphStore;
508
+ constructor(graphStore: GraphStore);
509
+ getNode(nodeId: unknown): GraphNode | undefined;
510
+ getPort(portId: unknown): GraphPort | undefined;
511
+ getPortNode(portId: string): unknown | undefined;
512
+ getEdge(edgeId: unknown): GraphEdge | undefined;
513
+ getPortAdjacentEdges(portId: string): readonly unknown[];
514
+ getNodeAdjacentEdges(nodeId: unknown): readonly unknown[];
515
+ getPortIncomingEdges(portId: unknown): readonly unknown[];
516
+ getPortOutcomingEdges(portId: unknown): readonly unknown[];
517
+ getPortCycleEdges(portId: unknown): readonly unknown[];
518
+ getNodeIncomingEdges(nodeId: unknown): readonly unknown[];
519
+ getNodeOutcomingEdges(nodeId: unknown): readonly unknown[];
520
+ getNodeCycleEdges(nodeId: unknown): readonly unknown[];
521
+ }
522
+
444
523
  export declare class PublicViewportTransformer {
445
524
  private readonly transformer;
446
525
  constructor(transformer: ViewportTransformer);
@@ -466,7 +545,9 @@ export declare class PublicViewportTransformer {
466
545
  getAbsScale(): number;
467
546
  }
468
547
 
469
- export declare class StraightEdgeController implements EdgeController {
548
+ declare type SharedIncrementalPriority = "shared-incremental";
549
+
550
+ export declare class StraightEdgeShape implements EdgeController {
470
551
  private readonly color;
471
552
  private readonly width;
472
553
  private readonly arrowLength;
@@ -482,7 +563,7 @@ export declare class StraightEdgeController implements EdgeController {
482
563
  update(x: number, y: number, width: number, height: number, from: PortPayload, to: PortPayload): void;
483
564
  }
484
565
 
485
- declare interface StraightEdgeOptions {
566
+ declare interface StraightEdgeShape_2 {
486
567
  readonly type: "straight";
487
568
  readonly color?: string;
488
569
  readonly width?: number;
@@ -538,7 +619,9 @@ declare interface UpdatePortRequest {
538
619
 
539
620
  export declare class UserDraggableNodesCanvas implements Canvas {
540
621
  private readonly canvas;
622
+ readonly model: PublicGraphStore;
541
623
  readonly transformation: PublicViewportTransformer;
624
+ private maxNodePriority;
542
625
  private readonly nodes;
543
626
  private grabbedNodeId;
544
627
  private onNodeDrag;
@@ -551,8 +634,9 @@ export declare class UserDraggableNodesCanvas implements Canvas {
551
634
  private readonly onCanvasTouchMove;
552
635
  private readonly onCanvasTouchEnd;
553
636
  private previousTouchCoords;
637
+ private freezePriority;
554
638
  constructor(canvas: Canvas, dragOptions?: DragOptions);
555
- addNode(node: AddNodeRequest): UserDraggableNodesCanvas;
639
+ addNode(request: AddNodeRequest): UserDraggableNodesCanvas;
556
640
  updateNode(nodeId: unknown, request: UpdateNodeRequest): UserDraggableNodesCanvas;
557
641
  removeNode(nodeId: unknown): UserDraggableNodesCanvas;
558
642
  markPort(port: MarkPortRequest): UserDraggableNodesCanvas;
@@ -563,18 +647,20 @@ export declare class UserDraggableNodesCanvas implements Canvas {
563
647
  removeEdge(edgeId: unknown): UserDraggableNodesCanvas;
564
648
  patchViewportState(request: PatchViewportRequest): UserDraggableNodesCanvas;
565
649
  moveToNodes(nodeIds: readonly string[]): UserDraggableNodesCanvas;
566
- moveNodeOnTop(nodeId: string): UserDraggableNodesCanvas;
567
650
  clear(): UserDraggableNodesCanvas;
568
651
  attach(element: HTMLElement): UserDraggableNodesCanvas;
569
652
  detach(): UserDraggableNodesCanvas;
570
653
  destroy(): void;
571
654
  private setCursor;
572
655
  private dragNode;
656
+ private updateMaxNodePriority;
657
+ private moveNodeOnTop;
573
658
  }
574
659
 
575
660
  export declare class UserTransformableCanvas implements Canvas {
576
661
  private readonly canvas;
577
662
  private readonly options?;
663
+ readonly model: PublicGraphStore;
578
664
  readonly transformation: PublicViewportTransformer;
579
665
  private element;
580
666
  private isMoving;
@@ -605,7 +691,6 @@ export declare class UserTransformableCanvas implements Canvas {
605
691
  removeEdge(edgeId: unknown): UserTransformableCanvas;
606
692
  patchViewportState(request: PatchViewportRequest): UserTransformableCanvas;
607
693
  moveToNodes(nodeIds: readonly string[]): UserTransformableCanvas;
608
- moveNodeOnTop(nodeId: string): UserTransformableCanvas;
609
694
  clear(): UserTransformableCanvas;
610
695
  attach(element: HTMLElement): UserTransformableCanvas;
611
696
  detach(): UserTransformableCanvas;
@@ -616,7 +701,7 @@ export declare class UserTransformableCanvas implements Canvas {
616
701
  private scaleViewport;
617
702
  }
618
703
 
619
- export declare class VerticalEdgeController implements EdgeController {
704
+ export declare class VerticalEdgeShape implements EdgeController {
620
705
  private readonly color;
621
706
  private readonly width;
622
707
  private readonly arrowLength;
@@ -632,7 +717,7 @@ export declare class VerticalEdgeController implements EdgeController {
632
717
  update(x: number, y: number, width: number, height: number, from: PortPayload, to: PortPayload): void;
633
718
  }
634
719
 
635
- declare interface VerticalEdgeOptions {
720
+ declare interface VerticalEdgeShape_2 {
636
721
  readonly type: "vertical";
637
722
  readonly color?: string;
638
723
  readonly width?: number;