@html-graph/html-graph 0.0.54 → 0.0.56

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
@@ -31,7 +31,7 @@ npm i @html-graph/html-graph
31
31
  ```
32
32
 
33
33
  ```typescript
34
- import { AddNodePorts, HtmlGraphBuilder } from "@html-graph/html-graph";
34
+ import { HtmlGraphBuilder, AddNodeRequest } from "@html-graph/html-graph";
35
35
 
36
36
  const canvas = new HtmlGraphBuilder()
37
37
  .setOptions({
@@ -50,9 +50,11 @@ const canvas = new HtmlGraphBuilder()
50
50
 
51
51
  function createNode(
52
52
  name: string,
53
+ x: number,
54
+ y: number,
53
55
  frontPortId: string,
54
56
  backPortId: string,
55
- ): [HTMLElement, AddNodePorts] {
57
+ ): AddNodeRequest {
56
58
  const node = document.createElement("div");
57
59
  node.classList.add("node");
58
60
 
@@ -68,33 +70,33 @@ function createNode(
68
70
  backPort.classList.add("port");
69
71
  node.appendChild(backPort);
70
72
 
71
- return [
72
- node,
73
- [
74
- [frontPortId, frontPort],
75
- [backPortId, backPort],
73
+ return {
74
+ element: node,
75
+ x: x,
76
+ y: y,
77
+ ports: [
78
+ { id: frontPortId, element: frontPort },
79
+ { id: backPortId, element: backPort },
76
80
  ],
77
- ];
81
+ };
78
82
  }
79
83
 
80
- const [node1, ports1] = createNode("Node 1", "port-1-1", "port-1-2");
81
- const [node2, ports2] = createNode("Node 2", "port-2-1", "port-2-2");
84
+ const node1 = createNode("Node 1", 200, 400, "port-1-1", "port-1-2");
85
+ const node2 = createNode("Node 2", 600, 500, "port-2-1", "port-2-2");
82
86
 
83
87
  const canvasElement = document.getElementById("canvas")!;
84
88
 
85
89
  canvas
86
90
  .attach(canvasElement)
87
- .addNode({ element: node1, x: 200, y: 400, ports: ports1 })
88
- .addNode({ element: node2, x: 600, y: 500, ports: ports2 })
91
+ .addNode(node1)
92
+ .addNode(node2)
89
93
  .addEdge({ from: "port-1-2", to: "port-2-1" });
90
94
  ```
91
95
 
92
- Refer to [Examples](examples) for more.
96
+ Refer to [Use cases](use-cases) for more.
93
97
 
94
98
  ## Run examples
95
99
 
96
- Use node version >= 20
97
-
98
100
  ```
99
101
  npm install
100
102
  npm run start
package/dist/main.d.ts CHANGED
@@ -1,21 +1,21 @@
1
1
  export declare interface AddEdgeRequest {
2
- id?: unknown;
3
- from: string;
4
- to: string;
5
- options?: EdgeShape_2;
6
- priority?: number;
2
+ readonly id?: unknown;
3
+ readonly from: string;
4
+ readonly to: string;
5
+ readonly shape?: EdgeShape_2;
6
+ readonly priority?: number;
7
7
  }
8
8
 
9
- export declare type AddNodePorts = Iterable<readonly [unknown, MarkNodePortRequest]>;
9
+ export declare type AddNodePorts = Iterable<MarkNodePortRequest>;
10
10
 
11
11
  export declare interface AddNodeRequest {
12
- id?: unknown;
13
- element: HTMLElement;
14
- x: number;
15
- y: number;
16
- ports?: AddNodePorts;
17
- centerFn?: CenterFn;
18
- priority?: number;
12
+ readonly id?: unknown;
13
+ readonly element: HTMLElement;
14
+ readonly x: number;
15
+ readonly y: number;
16
+ readonly ports?: AddNodePorts;
17
+ readonly centerFn?: CenterFn;
18
+ readonly priority?: number;
19
19
  }
20
20
 
21
21
  export declare type BackgroundDrawingFn = (ctx: CanvasRenderingContext2D, transformer: PublicViewportTransformer) => void;
@@ -114,11 +114,11 @@ export declare interface Canvas {
114
114
  /**
115
115
  * applies transformation for viewport
116
116
  */
117
- patchViewportState(request: PatchTransformRequest): Canvas;
117
+ patchViewportMatrix(request: PatchMatrixRequest): Canvas;
118
118
  /**
119
119
  * applies transformation for content
120
120
  */
121
- patchContentState(request: PatchTransformRequest): Canvas;
121
+ patchContentMatrix(request: PatchMatrixRequest): Canvas;
122
122
  /**
123
123
  * attaches canvas to given element
124
124
  */
@@ -160,15 +160,15 @@ export declare class CanvasCore implements Canvas {
160
160
  addEdge(edge: AddEdgeRequest): CanvasCore;
161
161
  updateEdge(edgeId: unknown, request: UpdateEdgeRequest): CanvasCore;
162
162
  removeEdge(edgeId: unknown): CanvasCore;
163
- patchViewportState(request: PatchTransformRequest): CanvasCore;
164
- patchContentState(request: PatchTransformRequest): CanvasCore;
163
+ patchViewportMatrix(request: PatchMatrixRequest): CanvasCore;
164
+ patchContentMatrix(request: PatchMatrixRequest): CanvasCore;
165
165
  clear(): CanvasCore;
166
166
  attach(element: HTMLElement): CanvasCore;
167
167
  detach(): CanvasCore;
168
168
  destroy(): void;
169
169
  }
170
170
 
171
- export declare type CenterFn = (width: number, height: number) => [number, number];
171
+ export declare type CenterFn = (width: number, height: number) => Point;
172
172
 
173
173
  declare type ConstantPriority = number;
174
174
 
@@ -278,7 +278,7 @@ export declare const createVerticalEdgeShapeFactory: (options: {
278
278
 
279
279
  declare interface CustomEdgeShape {
280
280
  readonly type: "custom";
281
- readonly controllerFactory: EdgeShapeFactory;
281
+ readonly factory: EdgeShapeFactory;
282
282
  }
283
283
 
284
284
  declare type CustomPriority = PriorityFn;
@@ -393,13 +393,17 @@ declare class GraphStore {
393
393
  private readonly outcommingEdges;
394
394
  private readonly cycleEdges;
395
395
  addNode(nodeId: unknown, element: HTMLElement, x: number, y: number, centerFn: CenterFn, priority: number): void;
396
+ getAllNodes(): readonly unknown[];
396
397
  getNode(nodeId: unknown): NodePayload | undefined;
398
+ getNodePorts(nodeId: unknown): readonly unknown[] | undefined;
397
399
  removeNode(nodeId: unknown): void;
398
400
  addPort(portId: unknown, element: HTMLElement, nodeId: unknown, centerFn: CenterFn, dir: number): void;
401
+ getAllPorts(): readonly unknown[];
399
402
  getPort(portId: unknown): PortPayload | undefined;
400
- getPortNode(portId: string): unknown | undefined;
401
- removePort(portId: string): void;
403
+ getPortNode(portId: unknown): unknown | undefined;
404
+ removePort(portId: unknown): void;
402
405
  addEdge(edgeId: unknown, fromPortId: string, toPortId: string, shape: EdgeShape, priority: number): void;
406
+ getAllEdges(): readonly unknown[];
403
407
  getEdge(edgeId: unknown): EdgePayload | undefined;
404
408
  removeEdge(edgeId: unknown): void;
405
409
  getPortAdjacentEdges(portId: string): readonly unknown[];
@@ -458,7 +462,8 @@ export declare class HtmlGraphBuilder {
458
462
 
459
463
  declare type IncrementalPriority = "incremental";
460
464
 
461
- export declare type MarkNodePortRequest = HTMLElement | {
465
+ export declare type MarkNodePortRequest = {
466
+ readonly id?: unknown;
462
467
  readonly element: HTMLElement;
463
468
  readonly centerFn?: CenterFn;
464
469
  readonly direction?: number;
@@ -487,10 +492,15 @@ declare interface NodePayload {
487
492
  priority: number;
488
493
  }
489
494
 
490
- export declare interface PatchTransformRequest {
491
- scale?: number;
492
- x?: number;
493
- y?: number;
495
+ export declare interface PatchMatrixRequest {
496
+ readonly scale?: number;
497
+ readonly dx?: number;
498
+ readonly dy?: number;
499
+ }
500
+
501
+ export declare interface Point {
502
+ readonly x: number;
503
+ readonly y: number;
494
504
  }
495
505
 
496
506
  export declare interface PortPayload {
@@ -506,9 +516,13 @@ export declare type PriorityFn = () => number;
506
516
  declare class PublicGraphStore {
507
517
  private readonly graphStore;
508
518
  constructor(graphStore: GraphStore);
519
+ getAllNodes(): readonly unknown[];
520
+ getAllPorts(): readonly unknown[];
509
521
  getNode(nodeId: unknown): GraphNode | null;
522
+ getNodePorts(nodeId: unknown): readonly unknown[] | undefined;
510
523
  getPort(portId: unknown): GraphPort | null;
511
524
  getPortNode(portId: string): unknown | null;
525
+ getAllEdges(): readonly unknown[];
512
526
  getEdge(edgeId: unknown): GraphEdge | null;
513
527
  getPortAdjacentEdges(portId: string): readonly unknown[];
514
528
  getNodeAdjacentEdges(nodeId: unknown): readonly unknown[];
@@ -560,36 +574,53 @@ declare interface StraightEdgeShape_2 {
560
574
  readonly detourDirection?: number;
561
575
  }
562
576
 
577
+ export declare type TransformFinishedFn = (transform: TransformPayload) => void;
578
+
563
579
  export declare interface TransformOptions {
564
- scale?: {
565
- enabled?: boolean;
566
- min?: number;
567
- max?: number;
568
- wheelSensitivity?: number;
580
+ readonly scale?: {
581
+ readonly enabled?: boolean;
582
+ readonly wheelSensitivity?: number;
569
583
  };
570
- shift?: {
571
- enabled?: boolean;
584
+ readonly shift?: {
585
+ readonly enabled?: boolean;
572
586
  };
573
- events?: {
574
- onTransform?: (payload: TransformPayload) => void;
575
- onBeforeTransform?: (payload: TransformPayload) => boolean;
587
+ readonly transformPreprocessor?: TransformPreprocessorOption | TransformPreprocessorOption[];
588
+ readonly events?: {
589
+ readonly onTransformFinished?: TransformFinishedFn;
576
590
  };
577
591
  }
578
592
 
579
593
  export declare interface TransformPayload {
580
594
  readonly scale: number;
581
- readonly x: number;
582
- readonly y: number;
595
+ readonly dx: number;
596
+ readonly dy: number;
583
597
  }
584
598
 
599
+ export declare type TransformPreprocessorFn = (prevTransform: TransformPayload, nextTransform: TransformPayload, canvasWidth: number, canvasHeight: number) => TransformPayload;
600
+
601
+ declare type TransformPreprocessorOption = {
602
+ readonly type: "scale-limit";
603
+ readonly minContentScale?: number;
604
+ readonly maxContentScale?: number;
605
+ } | {
606
+ readonly type: "shift-limit";
607
+ readonly minX?: number;
608
+ readonly maxX?: number;
609
+ readonly minY?: number;
610
+ readonly maxY?: number;
611
+ } | {
612
+ readonly type: "custom";
613
+ readonly preprocessorFn: TransformPreprocessorFn;
614
+ };
615
+
585
616
  declare interface TransformState {
586
- scale: number;
587
- x: number;
588
- y: number;
617
+ readonly scale: number;
618
+ readonly dx: number;
619
+ readonly dy: number;
589
620
  }
590
621
 
591
622
  export declare interface UpdateEdgeRequest {
592
- readonly controller?: EdgeShape;
623
+ readonly shape?: EdgeShape_2;
593
624
  readonly priority?: number;
594
625
  }
595
626
 
@@ -622,7 +653,8 @@ export declare class UserDraggableNodesCanvas implements Canvas {
622
653
  private readonly onCanvasTouchMove;
623
654
  private readonly onCanvasTouchEnd;
624
655
  private previousTouchCoords;
625
- private freezePriority;
656
+ private readonly freezePriority;
657
+ private readonly window;
626
658
  constructor(canvas: Canvas, dragOptions?: DragOptions);
627
659
  addNode(request: AddNodeRequest): UserDraggableNodesCanvas;
628
660
  updateNode(nodeId: unknown, request: UpdateNodeRequest): UserDraggableNodesCanvas;
@@ -633,16 +665,19 @@ export declare class UserDraggableNodesCanvas implements Canvas {
633
665
  addEdge(edge: AddEdgeRequest): UserDraggableNodesCanvas;
634
666
  updateEdge(edgeId: unknown, request: UpdateEdgeRequest): UserDraggableNodesCanvas;
635
667
  removeEdge(edgeId: unknown): UserDraggableNodesCanvas;
636
- patchViewportState(request: PatchTransformRequest): UserDraggableNodesCanvas;
637
- patchContentState(request: PatchTransformRequest): UserDraggableNodesCanvas;
668
+ patchViewportMatrix(request: PatchMatrixRequest): UserDraggableNodesCanvas;
669
+ patchContentMatrix(request: PatchMatrixRequest): UserDraggableNodesCanvas;
638
670
  clear(): UserDraggableNodesCanvas;
639
671
  attach(element: HTMLElement): UserDraggableNodesCanvas;
640
672
  detach(): UserDraggableNodesCanvas;
641
673
  destroy(): void;
642
- private setCursor;
643
674
  private dragNode;
644
675
  private updateMaxNodePriority;
645
676
  private moveNodeOnTop;
677
+ private cancelMouseDrag;
678
+ private removeMouseDragListeners;
679
+ private cancelTouchDrag;
680
+ private removeTouchDragListeners;
646
681
  }
647
682
 
648
683
  export declare class UserTransformableCanvas implements Canvas {
@@ -651,15 +686,13 @@ export declare class UserTransformableCanvas implements Canvas {
651
686
  readonly model: PublicGraphStore;
652
687
  readonly transformation: PublicViewportTransformer;
653
688
  private element;
654
- private isMoving;
655
689
  private prevTouches;
656
- private onTransform;
657
- private onBeforeTransform;
690
+ private readonly onTransformFinished;
691
+ private readonly transformPreprocessor;
658
692
  private readonly isScalable;
659
693
  private readonly isShiftable;
660
- private readonly minViewScale;
661
- private readonly maxViewScale;
662
694
  private readonly wheelSensitivity;
695
+ private window;
663
696
  private readonly onMouseDown;
664
697
  private readonly onMouseMove;
665
698
  private readonly onMouseUp;
@@ -667,6 +700,7 @@ export declare class UserTransformableCanvas implements Canvas {
667
700
  private readonly onTouchStart;
668
701
  private readonly onTouchMove;
669
702
  private readonly onTouchEnd;
703
+ private readonly observer;
670
704
  constructor(canvas: Canvas, options?: TransformOptions | undefined);
671
705
  addNode(node: AddNodeRequest): UserTransformableCanvas;
672
706
  updateNode(nodeId: unknown, request: UpdateNodeRequest): UserTransformableCanvas;
@@ -677,16 +711,19 @@ export declare class UserTransformableCanvas implements Canvas {
677
711
  addEdge(edge: AddEdgeRequest): UserTransformableCanvas;
678
712
  updateEdge(edgeId: unknown, request: UpdateEdgeRequest): UserTransformableCanvas;
679
713
  removeEdge(edgeId: unknown): UserTransformableCanvas;
680
- patchViewportState(request: PatchTransformRequest): UserTransformableCanvas;
681
- patchContentState(request: PatchTransformRequest): UserTransformableCanvas;
714
+ patchViewportMatrix(request: PatchMatrixRequest): UserTransformableCanvas;
715
+ patchContentMatrix(request: PatchMatrixRequest): UserTransformableCanvas;
682
716
  clear(): UserTransformableCanvas;
683
717
  attach(element: HTMLElement): UserTransformableCanvas;
684
718
  detach(): UserTransformableCanvas;
685
719
  destroy(): void;
686
720
  private getAverageTouch;
687
- private setCursor;
688
721
  private moveViewport;
689
722
  private scaleViewport;
723
+ private stopMouseDrag;
724
+ private removeMouseDragListeners;
725
+ private stopTouchDrag;
726
+ private removeTouchDragListeners;
690
727
  }
691
728
 
692
729
  export declare class VerticalEdgeShape implements EdgeShape {
@@ -725,8 +762,8 @@ declare class ViewportTransformer {
725
762
  private contentMatrix;
726
763
  getViewportMatrix(): TransformState;
727
764
  getContentMatrix(): TransformState;
728
- patchViewportState(scale: number | null, x: number | null, y: number | null): void;
729
- patchContentState(scale: number | null, x: number | null, y: number | null): void;
765
+ patchViewportMatrix(scale: number | null, x: number | null, y: number | null): void;
766
+ patchContentMatrix(scale: number | null, dx: number | null, dy: number | null): void;
730
767
  private calculateReverseMatrix;
731
768
  }
732
769