All files / src/contexts GraphConfigContext.ts

100% Statements 26/26
66.67% Branches 4/6
100% Functions 10/10
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x                                                                                                                                                 1x 11x 11x 11x   1x 14x     1x 95572x   95259x     1x 10x     1x 10583x   10583x     1x 14x     1x 31758x   31739x   1x   1x      
import * as React from "react";
import {
  ICanvasEdge,
  ICanvasNode,
  ICanvasPort,
  IGraphProps,
  TSetData
} from "../components";
 
interface IItemConfigArgs<T> {
  model: T;
  graphProps: IGraphProps;
  setData: TSetData;
}
 
export interface INodeDrawArgs extends IItemConfigArgs<ICanvasNode> {}
 
export interface INodeConfig {
  portShape?: string;
  getPorts?(args: INodeDrawArgs): ICanvasPort[];
  render(args: IItemConfigArgs<ICanvasNode>): React.ReactNode;
  getStyle?(node: ICanvasNode): React.CSSProperties;
  getWidth(node: ICanvasNode): number;
  getHeight(node: ICanvasNode): number;
}
 
export interface IEdgeDrawArgs extends IItemConfigArgs<ICanvasEdge> {
  x1: number;
  y1: number;
  x2: number;
  y2: number;
}
 
export interface IEdgeConfig {
  render(args: IEdgeDrawArgs): React.ReactNode;
  getStyle?(edge: ICanvasEdge): React.CSSProperties;
}
 
export interface IPortDrawArgs extends IItemConfigArgs<ICanvasPort> {
  parentNode: ICanvasNode;
  x: number;
  y: number;
}
 
export interface IPortConfig {
  render(args: IPortDrawArgs): React.ReactNode;
  getStyle?(port: ICanvasPort): React.CSSProperties;
}
 
export interface IGraphConfig {
  registerNode(name: string, config: INodeConfig): void;
  getNodeConfigByName(name: string): INodeConfig | void;
  registerEdge(name: string, config: IEdgeConfig): void;
  getEdgeConfigByName(name: string): IEdgeConfig | void;
  registerPort(name: string, config: IPortConfig): void;
  getPortConfigByName(name: string): IPortConfig | void;
}
 
interface INodeConfigItem {
  name: string;
  config: INodeConfig;
}
 
interface IEdgeConfigItem {
  name: string;
  config: IEdgeConfig;
}
 
interface IPortConfigItem {
  name: string;
  config: IPortConfig;
}
 
export class GraphConfig implements IGraphConfig {
  private readonly nodeConfigList: INodeConfigItem[] = [];
  private readonly edgeConfigList: IEdgeConfigItem[] = [];
  private readonly portConfigList: IPortConfigItem[] = [];
 
  public registerNode(name: string, config: INodeConfig): void {
    this.nodeConfigList.push({ name, config });
  }
 
  public getNodeConfigByName(name: string): INodeConfig | void {
    const item = this.nodeConfigList.find(config => config.name === name);
 
    return item ? item.config : undefined;
  }
 
  public registerEdge(name: string, config: IEdgeConfig): void {
    this.edgeConfigList.push({ name, config });
  }
 
  public getEdgeConfigByName(name: string): IEdgeConfig | void {
    const item = this.edgeConfigList.find(config => config.name === name);
 
    return item ? item.config : undefined;
  }
 
  public registerPort(name: string, config: IPortConfig): void {
    this.portConfigList.push({ name, config });
  }
 
  public getPortConfigByName(name: string): IPortConfig | void {
    const item = this.portConfigList.find(config => config.name === name);
 
    return item ? item.config : undefined;
  }
}
 
export const GraphConfigContext = React.createContext<IGraphConfig>(
  new GraphConfig()
);