@html-graph/html-graph 0.0.46 → 0.0.48

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
@@ -6,8 +6,6 @@
6
6
 
7
7
  ![CI](https://github.com/html-graph/html-graph/actions/workflows/ci.yml/badge.svg?branch=master)
8
8
 
9
- Visit <a target="_blank" href="https://html-graph.github.io">live demo</a>
10
-
11
9
  <a target="_blank" href="https://html-graph.github.io/">
12
10
  <img width="100%" src="https://raw.githubusercontent.com/html-graph/html-graph/master/media/full-demo.gif"/>
13
11
  </a>
@@ -17,6 +15,8 @@ Port is an entity of the node to which connection can be attached to.
17
15
 
18
16
  This library fits for tasks where easy nodes customization and interactiveness are required.
19
17
 
18
+ Visit <a target="_blank" href="https://html-graph.github.io">live demo</a>
19
+
20
20
  ## Features:
21
21
 
22
22
  - easy nodes customization using HTML
@@ -33,7 +33,7 @@ npm i @html-graph/html-graph
33
33
  ```
34
34
 
35
35
  ```typescript
36
- import { MarkPortRequest, HtmlGraphBuilder } from "@html-graph/html-graph";
36
+ import { MarkNodePortRequest, HtmlGraphBuilder } from "@html-graph/html-graph";
37
37
 
38
38
  const canvas = new HtmlGraphBuilder()
39
39
  .setOptions({
@@ -87,7 +87,6 @@ Use node version >= 20
87
87
 
88
88
  ```
89
89
  npm install
90
-
91
90
  npm run start
92
91
  ```
93
92
 
package/dist/main.d.ts CHANGED
@@ -10,35 +10,10 @@ export declare interface AddNodeRequest {
10
10
  element: HTMLElement;
11
11
  x: number;
12
12
  y: number;
13
- ports?: Record<string, MarkPortRequest>;
13
+ ports?: Record<string, MarkNodePortRequest>;
14
14
  centerFn?: CenterFn;
15
15
  }
16
16
 
17
- export declare interface ApiContentMoveTransform {
18
- x?: number;
19
- y?: number;
20
- }
21
-
22
- export declare interface ApiContentScaleTransform {
23
- scale: number;
24
- x?: number;
25
- y?: number;
26
- }
27
-
28
- export declare interface ApiPort {
29
- readonly id?: string;
30
- readonly element: HTMLElement;
31
- readonly nodeId: string;
32
- readonly centerFn?: CenterFn;
33
- readonly direction?: number | null;
34
- }
35
-
36
- export declare interface ApiTransform {
37
- scale?: number;
38
- x?: number;
39
- y?: number;
40
- }
41
-
42
17
  export declare type BackgroundDrawingFn = (ctx: CanvasRenderingContext2D, transformer: PublicViewportTransformer) => void;
43
18
 
44
19
  declare type BackgroundOptions = {
@@ -107,7 +82,7 @@ export declare interface Canvas {
107
82
  /**
108
83
  * marks element as port of node
109
84
  */
110
- markPort(port: ApiPort): Canvas;
85
+ markPort(port: MarkPortRequest): Canvas;
111
86
  /**
112
87
  * updates connections attached to port
113
88
  */
@@ -128,31 +103,19 @@ export declare interface Canvas {
128
103
  /**
129
104
  * applies transformation for viewport
130
105
  */
131
- patchViewportTransform(apiTransform: ApiTransform): Canvas;
132
- /**
133
- * applies move transformation for content
134
- */
135
- moveContent(apiTransform: ApiContentMoveTransform): Canvas;
136
- /**
137
- * applies scale transformation for content
138
- */
139
- scaleContent(apiTransform: ApiContentScaleTransform): Canvas;
106
+ patchViewportState(request: PatchViewportRequest): Canvas;
140
107
  /**
141
- * applies shift transformation for content
108
+ * moves viewport to nodes
142
109
  */
143
110
  moveToNodes(nodeIds: readonly string[]): Canvas;
144
111
  /**
145
112
  * updates node absolute coordinates
146
113
  */
147
- updateNodeCoords(nodeId: string, x: number, y: number): Canvas;
114
+ updateNodeCoordinates(nodeId: string, x: number, y: number): Canvas;
148
115
  /**
149
116
  * updates connection
150
117
  */
151
- updateConnectionController(connectionId: string, controller: ConnectionController): Canvas;
152
- /**
153
- * drags node in viewport
154
- */
155
- dragNode(nodeId: string, dx: number, dy: number): Canvas;
118
+ updateConnection(connectionId: string, request: UpdateConnectionRequest): Canvas;
156
119
  /**
157
120
  * moves specified node on top
158
121
  */
@@ -180,30 +143,27 @@ export declare interface Canvas {
180
143
  }
181
144
 
182
145
  /**
183
- * Provides core API for acting on graph
146
+ * Provides low level API for acting on graph
184
147
  */
185
148
  export declare class CanvasCore implements Canvas {
186
149
  private readonly apiOptions?;
187
150
  readonly transformation: PublicViewportTransformer;
188
151
  readonly model: PublicGraphStore;
189
- private readonly options;
190
152
  private readonly di;
153
+ private readonly connectionControllerFactory;
191
154
  constructor(apiOptions?: CoreOptions | undefined);
192
155
  addNode(node: AddNodeRequest): CanvasCore;
193
156
  moveNodeOnTop(nodeId: string): CanvasCore;
194
157
  removeNode(nodeId: string): CanvasCore;
195
- markPort(port: ApiPort): CanvasCore;
158
+ markPort(port: MarkPortRequest): CanvasCore;
196
159
  updatePortConnections(portId: string): CanvasCore;
197
160
  unmarkPort(portId: string): CanvasCore;
198
161
  addConnection(connection: AddConnectionRequest): CanvasCore;
199
162
  removeConnection(connectionId: string): CanvasCore;
200
- patchViewportTransform(apiTransform: ApiTransform): CanvasCore;
201
- moveContent(apiTransform: ApiContentMoveTransform): CanvasCore;
202
- scaleContent(apiTransform: ApiContentScaleTransform): CanvasCore;
163
+ patchViewportState(request: PatchViewportRequest): CanvasCore;
203
164
  moveToNodes(nodeIds: readonly string[]): CanvasCore;
204
- updateNodeCoords(nodeId: string, x: number, y: number): CanvasCore;
205
- updateConnectionController(connectionId: string, controller: ConnectionController): CanvasCore;
206
- dragNode(nodeId: string, dx: number, dy: number): CanvasCore;
165
+ updateNodeCoordinates(nodeId: string, x: number, y: number): CanvasCore;
166
+ updateConnection(connectionId: string, request: UpdateConnectionRequest): CanvasCore;
207
167
  clear(): CanvasCore;
208
168
  attach(element: HTMLElement): CanvasCore;
209
169
  detach(): CanvasCore;
@@ -311,6 +271,7 @@ export declare class DraggableNodesCanvas implements Canvas {
311
271
  readonly model: PublicGraphStore;
312
272
  private readonly nodes;
313
273
  private grabbedNodeId;
274
+ private onNodeDrag;
314
275
  private readonly nodeIdGenerator;
315
276
  private element;
316
277
  private readonly onCanvasMouseUp;
@@ -319,27 +280,31 @@ export declare class DraggableNodesCanvas implements Canvas {
319
280
  private readonly onCanvasTouchMove;
320
281
  private readonly onCanvasTouchEnd;
321
282
  private previousTouchCoords;
322
- constructor(canvas: Canvas);
283
+ constructor(canvas: Canvas, dragOptions?: DragOptions);
323
284
  addNode(node: AddNodeRequest): DraggableNodesCanvas;
324
285
  removeNode(nodeId: string): DraggableNodesCanvas;
325
- markPort(port: ApiPort): DraggableNodesCanvas;
286
+ markPort(port: MarkPortRequest): DraggableNodesCanvas;
326
287
  updatePortConnections(portId: string): DraggableNodesCanvas;
327
288
  unmarkPort(portId: string): DraggableNodesCanvas;
328
289
  addConnection(connection: AddConnectionRequest): DraggableNodesCanvas;
329
290
  removeConnection(connectionId: string): DraggableNodesCanvas;
330
- patchViewportTransform(apiTransform: ApiTransform): DraggableNodesCanvas;
331
- moveContent(apiTransform: ApiContentMoveTransform): DraggableNodesCanvas;
332
- scaleContent(apiTransform: ApiContentScaleTransform): DraggableNodesCanvas;
291
+ patchViewportState(request: PatchViewportRequest): DraggableNodesCanvas;
333
292
  moveToNodes(nodeIds: readonly string[]): DraggableNodesCanvas;
334
- updateNodeCoords(nodeId: string, x: number, y: number): DraggableNodesCanvas;
335
- updateConnectionController(connectionId: string, controller: ConnectionController): DraggableNodesCanvas;
336
- dragNode(nodeId: string, dx: number, dy: number): DraggableNodesCanvas;
293
+ updateNodeCoordinates(nodeId: string, x: number, y: number): DraggableNodesCanvas;
294
+ updateConnection(connectionId: string, request: UpdateConnectionRequest): DraggableNodesCanvas;
337
295
  moveNodeOnTop(nodeId: string): DraggableNodesCanvas;
338
296
  clear(): DraggableNodesCanvas;
339
297
  attach(element: HTMLElement): DraggableNodesCanvas;
340
298
  detach(): DraggableNodesCanvas;
341
299
  destroy(): void;
342
300
  private setCursor;
301
+ private dragNode;
302
+ }
303
+
304
+ export declare interface DragOptions {
305
+ events?: {
306
+ onNodeDrag?: (nodeId: string) => void;
307
+ };
343
308
  }
344
309
 
345
310
  declare class GraphStore {
@@ -351,9 +316,7 @@ declare class GraphStore {
351
316
  private incommingConnections;
352
317
  private outcommingConnections;
353
318
  private cycleConnections;
354
- getAllNodes(): readonly string[];
355
- getAllPorts(): readonly string[];
356
- getAllConnections(): readonly string[];
319
+ getAllNodes(): Readonly<Record<string, NodePayload>>;
357
320
  addNode(nodeId: string, element: HTMLElement, x: number, y: number, centerFn: CenterFn): void;
358
321
  hasNode(nodeId: string): boolean;
359
322
  getNode(nodeId: string): NodePayload;
@@ -369,36 +332,52 @@ declare class GraphStore {
369
332
  getConnection(connectionId: string): ConnectionPayload;
370
333
  hasConnection(connectionId: string): boolean;
371
334
  removeConnection(connectionId: string): void;
372
- getPortIncomingConnections(portId: string): readonly string[];
373
- getPortOutcomingConnections(portId: string): readonly string[];
374
- getPortCycleConnections(portId: string): readonly string[];
375
335
  getPortAdjacentConnections(portId: string): readonly string[];
376
- getNodeIncomingConnections(nodeId: string): readonly string[];
377
- getNodeOutcomingConnections(nodeId: string): readonly string[];
378
- getNodeCycleConnections(nodeId: string): readonly string[];
379
336
  getNodeAdjacentConnections(nodeId: string): readonly string[];
380
337
  clear(): void;
338
+ private getPortIncomingConnections;
339
+ private getPortOutcomingConnections;
340
+ private getPortCycleConnections;
341
+ private getNodeIncomingConnections;
342
+ private getNodeOutcomingConnections;
343
+ private getNodeCycleConnections;
381
344
  }
382
345
 
383
346
  export declare class HtmlGraphBuilder {
384
347
  private coreOptions;
348
+ private dragOptions;
385
349
  private transformOptions;
386
350
  private isDraggable;
387
351
  private isTransformable;
388
352
  setOptions(options: CoreOptions): HtmlGraphBuilder;
389
- setDraggableNodes(): HtmlGraphBuilder;
353
+ setDraggableNodes(dragOptions?: DragOptions): HtmlGraphBuilder;
390
354
  setTransformableCanvas(options?: TransformOptions): HtmlGraphBuilder;
391
355
  build(): Canvas;
392
356
  }
393
357
 
394
358
  declare type LayersMode = "connections-on-top" | "nodes-on-top" | "connections-follow-node";
395
359
 
396
- export declare type MarkPortRequest = HTMLElement | {
360
+ export declare type MarkNodePortRequest = HTMLElement | {
397
361
  readonly element: HTMLElement;
398
362
  readonly centerFn?: CenterFn;
399
363
  readonly direction?: number | null;
400
364
  };
401
365
 
366
+ export declare interface MarkPortRequest {
367
+ readonly id?: string;
368
+ readonly element: HTMLElement;
369
+ readonly nodeId: string;
370
+ readonly centerFn?: CenterFn;
371
+ readonly direction?: number | null;
372
+ }
373
+
374
+ export declare interface NodeItem {
375
+ readonly id: string;
376
+ readonly x: number;
377
+ readonly y: number;
378
+ readonly element: HTMLElement;
379
+ }
380
+
402
381
  declare interface NodePayload {
403
382
  element: HTMLElement;
404
383
  x: number;
@@ -406,42 +385,31 @@ declare interface NodePayload {
406
385
  centerFn: CenterFn;
407
386
  }
408
387
 
388
+ export declare interface NodeResponse {
389
+ readonly x: number;
390
+ readonly y: number;
391
+ readonly element: HTMLElement;
392
+ }
393
+
394
+ export declare interface PatchViewportRequest {
395
+ scale?: number;
396
+ x?: number;
397
+ y?: number;
398
+ }
399
+
409
400
  declare type Point = [number, number];
410
401
 
411
402
  export declare interface PortPayload {
412
- element: HTMLElement;
413
- centerFn: CenterFn;
414
- direction: number | null;
403
+ readonly element: HTMLElement;
404
+ readonly centerFn: CenterFn;
405
+ readonly direction: number | null;
415
406
  }
416
407
 
417
408
  declare class PublicGraphStore {
418
409
  private readonly graphStore;
419
410
  constructor(graphStore: GraphStore);
420
- getNode(nodeId: string): {
421
- x: number;
422
- y: number;
423
- } | null;
424
- getPort(portId: string): {
425
- nodeId: string;
426
- } | null;
427
- getConnection(connectionId: string): {
428
- from: string;
429
- to: string;
430
- } | null;
431
- getAllNodes(): readonly string[];
432
- getAllPorts(): readonly string[];
433
- getAllConnections(): readonly string[];
434
- hasNode(nodeId: string): boolean;
435
- hasPort(portId: string): boolean;
436
- hasConnection(connectionId: string): boolean;
437
- getPortIncomingConnections(portId: string): readonly string[];
438
- getPortOutcomingConnections(portId: string): readonly string[];
439
- getPortCycleConnections(portId: string): readonly string[];
440
- getPortAdjacentConnections(portId: string): readonly string[];
441
- getNodeIncomingConnections(nodeId: string): readonly string[];
442
- getNodeOutcomingConnections(nodeId: string): readonly string[];
443
- getNodeCycleConnections(nodeId: string): readonly string[];
444
- getNodeAdjacentConnections(nodeId: string): readonly string[];
411
+ getNode(nodeId: string): NodeResponse | null;
412
+ getNodes(ids?: readonly string[]): readonly NodeItem[];
445
413
  }
446
414
 
447
415
  export declare class PublicViewportTransformer {
@@ -505,10 +473,11 @@ export declare class TransformableCanvas implements Canvas {
505
473
  private element;
506
474
  private isMoving;
507
475
  private prevTouches;
476
+ private onTransform;
508
477
  private readonly isScalable;
509
478
  private readonly isShiftable;
510
- private readonly minContentScale;
511
- private readonly maxContentScale;
479
+ private readonly minViewScale;
480
+ private readonly maxViewScale;
512
481
  private readonly wheelSensitivity;
513
482
  private readonly onMouseDown;
514
483
  private readonly onMouseMove;
@@ -520,26 +489,24 @@ export declare class TransformableCanvas implements Canvas {
520
489
  constructor(canvas: Canvas, options?: TransformOptions | undefined);
521
490
  addNode(node: AddNodeRequest): TransformableCanvas;
522
491
  removeNode(nodeId: string): TransformableCanvas;
523
- markPort(port: ApiPort): TransformableCanvas;
492
+ markPort(port: MarkPortRequest): TransformableCanvas;
524
493
  updatePortConnections(portId: string): TransformableCanvas;
525
494
  unmarkPort(portId: string): TransformableCanvas;
526
495
  addConnection(connection: AddConnectionRequest): TransformableCanvas;
527
496
  removeConnection(connectionId: string): TransformableCanvas;
528
- patchViewportTransform(apiTransform: ApiTransform): TransformableCanvas;
529
- moveContent(apiTransform: ApiContentMoveTransform): TransformableCanvas;
530
- scaleContent(apiTransform: ApiContentScaleTransform): TransformableCanvas;
497
+ patchViewportState(request: PatchViewportRequest): TransformableCanvas;
531
498
  moveToNodes(nodeIds: readonly string[]): TransformableCanvas;
532
- updateNodeCoords(nodeId: string, x: number, y: number): TransformableCanvas;
533
- updateConnectionController(connectionId: string, controller: ConnectionController): TransformableCanvas;
534
- dragNode(nodeId: string, dx: number, dy: number): TransformableCanvas;
499
+ updateNodeCoordinates(nodeId: string, x: number, y: number): TransformableCanvas;
500
+ updateConnection(connectionId: string, request: UpdateConnectionRequest): TransformableCanvas;
535
501
  moveNodeOnTop(nodeId: string): TransformableCanvas;
536
502
  clear(): TransformableCanvas;
537
503
  attach(element: HTMLElement): TransformableCanvas;
538
504
  detach(): TransformableCanvas;
539
505
  destroy(): void;
540
506
  private getAverageTouch;
541
- private checkNextScaleValid;
542
507
  private setCursor;
508
+ private moveViewport;
509
+ private scaleViewport;
543
510
  }
544
511
 
545
512
  export declare interface TransformOptions {
@@ -552,34 +519,17 @@ export declare interface TransformOptions {
552
519
  shift?: {
553
520
  enabled?: boolean;
554
521
  };
522
+ events?: {
523
+ onTransform?: () => void;
524
+ };
525
+ }
526
+
527
+ declare interface UpdateConnectionRequest {
528
+ readonly controller?: ConnectionController;
555
529
  }
556
530
 
557
531
  declare class ViewportTransformer {
558
532
  private state;
559
- /**
560
- * dx2 - traslate x
561
- * dy2 - traslate y
562
- *
563
- * direct transform
564
- * s1 0 dx1 1 0 dx2
565
- * 0 s1 dy1 0 1 dy2
566
- * 0 0 1 0 0 1
567
- *
568
- * [s, dx, dy] = [s1, s * dx2 + dx1, s * dy2 + dy1]
569
- */
570
- applyShift(dx: number, dy: number): void;
571
- /**
572
- * s2 - scale
573
- * cx - scale pivot x
574
- * cy - scale pivot y
575
- *
576
- * s1 0 dx1 s2 0 (1 - s2) * cx
577
- * 0 s1 dy1 0 s2 (1 - s2) * cy
578
- * 0 0 1 0 0 1
579
- *
580
- * [s, dx, dy] = [s1 * s2, s1 * (1 - s2) * cx + dx1, s1 * (1 - s2) * cy + dy1]
581
- */
582
- applyScale(s2: number, cx: number, cy: number): void;
583
533
  getViewCoords(xa: number, ya: number): [number, number];
584
534
  getViewScale(): number;
585
535
  getAbsCoords(xv: number, yv: number): [number, number];