From 75657ab66185edbf93083c406f45343c742470b9 Mon Sep 17 00:00:00 2001 From: Felix Hofmann Date: Wed, 14 Aug 2024 15:01:45 +0200 Subject: [PATCH] added generics for nodes and links in typescript --- src/index.d.ts | 165 +++++++++++++++++++++++++++---------------------- src/index.js | 4 +- 2 files changed, 94 insertions(+), 75 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 6a67eec..b4ea342 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,7 +1,8 @@ -export interface GraphData { - nodes: NodeObject[]; - links: LinkObject[]; +export interface TypedGraphData> { + nodes: N[]; + links: L[]; } +export interface GraphData extends TypedGraphData> {} export interface NodeObject { id?: string | number; @@ -13,14 +14,15 @@ export interface NodeObject { fy?: number; } -export interface LinkObject { - source?: string | number | NodeObject; - target?: string | number | NodeObject; +export interface TypedLinkObject { + source?: string | number | N; + target?: string | number | N; } +export interface LinkObject extends TypedLinkObject {} type Accessor = Out | string | ((obj: In) => Out); -type NodeAccessor = Accessor; -type LinkAccessor = Accessor; +type NodeAccessor = Accessor; +type LinkAccessor, T> = Accessor; type CanvasCustomRenderMode = 'replace' | 'before' | 'after'; export type CanvasCustomRenderModeFn = (obj: T) => CanvasCustomRenderMode | any; @@ -35,14 +37,19 @@ interface ForceFn { [key: string]: any; } -export interface ForceGraphGenericInstance { +export interface FullTypedForceGraphGenericInstance< + N extends NodeObject, + L extends TypedLinkObject, + D extends TypedGraphData, + ChainableInstance +> { (element: HTMLElement): ChainableInstance; resetProps(): ChainableInstance; _destructor(): void; // Data input - graphData(): GraphData; - graphData(data: GraphData): ChainableInstance; + graphData(): D; + graphData(data: D): ChainableInstance; nodeId(): string; nodeId(id: string): ChainableInstance; linkSource(): string; @@ -61,59 +68,59 @@ export interface ForceGraphGenericInstance { // Node styling nodeRelSize(): number; nodeRelSize(size: number): ChainableInstance; - nodeVal(): NodeAccessor; - nodeVal(valAccessor: NodeAccessor): ChainableInstance; - nodeLabel(): NodeAccessor; - nodeLabel(labelAccessor: NodeAccessor): ChainableInstance; - nodeVisibility(): NodeAccessor; - nodeVisibility(visibilityAccessor: NodeAccessor): ChainableInstance; - nodeColor(): NodeAccessor; - nodeColor(colorAccessor: NodeAccessor): ChainableInstance; - nodeAutoColorBy(): NodeAccessor; - nodeAutoColorBy(colorByAccessor: NodeAccessor): ChainableInstance; - nodeCanvasObject(): CanvasCustomRenderFn; - nodeCanvasObject(renderFn: CanvasCustomRenderFn): ChainableInstance; - nodeCanvasObjectMode(): string | CanvasCustomRenderModeFn; - nodeCanvasObjectMode(modeAccessor: string | CanvasCustomRenderModeFn): ChainableInstance; - nodePointerAreaPaint(): CanvasPointerAreaPaintFn; - nodePointerAreaPaint(renderFn: CanvasPointerAreaPaintFn): ChainableInstance; + nodeVal(): NodeAccessor; + nodeVal(valAccessor: NodeAccessor): ChainableInstance; + nodeLabel(): NodeAccessor; + nodeLabel(labelAccessor: NodeAccessor): ChainableInstance; + nodeVisibility(): NodeAccessor; + nodeVisibility(visibilityAccessor: NodeAccessor): ChainableInstance; + nodeColor(): NodeAccessor; + nodeColor(colorAccessor: NodeAccessor): ChainableInstance; + nodeAutoColorBy(): NodeAccessor; + nodeAutoColorBy(colorByAccessor: NodeAccessor): ChainableInstance; + nodeCanvasObject(): CanvasCustomRenderFn; + nodeCanvasObject(renderFn: CanvasCustomRenderFn): ChainableInstance; + nodeCanvasObjectMode(): string | CanvasCustomRenderModeFn; + nodeCanvasObjectMode(modeAccessor: string | CanvasCustomRenderModeFn): ChainableInstance; + nodePointerAreaPaint(): CanvasPointerAreaPaintFn; + nodePointerAreaPaint(renderFn: CanvasPointerAreaPaintFn): ChainableInstance; // Link styling - linkLabel(): LinkAccessor; - linkLabel(labelAccessor: LinkAccessor): ChainableInstance; - linkVisibility(): LinkAccessor; - linkVisibility(visibilityAccessor: LinkAccessor): ChainableInstance; - linkColor(): LinkAccessor; - linkColor(colorAccessor: LinkAccessor): ChainableInstance; - linkAutoColorBy(): LinkAccessor; - linkAutoColorBy(colorByAccessor: LinkAccessor): ChainableInstance; - linkLineDash(): LinkAccessor; - linkLineDash(linkLineDashAccessor: LinkAccessor): ChainableInstance; - linkWidth(): LinkAccessor; - linkWidth(widthAccessor: LinkAccessor): ChainableInstance; - linkCurvature(): LinkAccessor; - linkCurvature(curvatureAccessor: LinkAccessor): ChainableInstance; - linkCanvasObject(): CanvasCustomRenderFn; - linkCanvasObject(renderFn: CanvasCustomRenderFn): ChainableInstance; - linkCanvasObjectMode(): string | CanvasCustomRenderModeFn; - linkCanvasObjectMode(modeAccessor: string | CanvasCustomRenderModeFn): ChainableInstance; - linkDirectionalArrowLength(): LinkAccessor; - linkDirectionalArrowLength(lengthAccessor: LinkAccessor): ChainableInstance; - linkDirectionalArrowColor(): LinkAccessor; - linkDirectionalArrowColor(colorAccessor: LinkAccessor): ChainableInstance; - linkDirectionalArrowRelPos(): LinkAccessor; - linkDirectionalArrowRelPos(fractionAccessor: LinkAccessor): ChainableInstance; - linkDirectionalParticles(): LinkAccessor; - linkDirectionalParticles(numParticlesAccessor: LinkAccessor): ChainableInstance; - linkDirectionalParticleSpeed(): LinkAccessor; - linkDirectionalParticleSpeed(relDistancePerFrameAccessor: LinkAccessor): ChainableInstance; - linkDirectionalParticleWidth(): LinkAccessor; - linkDirectionalParticleWidth(widthAccessor: LinkAccessor): ChainableInstance; - linkDirectionalParticleColor(): LinkAccessor; - linkDirectionalParticleColor(colorAccessor: LinkAccessor): ChainableInstance; - emitParticle(link: LinkObject): ChainableInstance; - linkPointerAreaPaint(): CanvasPointerAreaPaintFn; - linkPointerAreaPaint(renderFn: CanvasPointerAreaPaintFn): ChainableInstance; + linkLabel(): LinkAccessor; + linkLabel(labelAccessor: LinkAccessor): ChainableInstance; + linkVisibility(): LinkAccessor; + linkVisibility(visibilityAccessor: LinkAccessor): ChainableInstance; + linkColor(): LinkAccessor; + linkColor(colorAccessor: LinkAccessor): ChainableInstance; + linkAutoColorBy(): LinkAccessor; + linkAutoColorBy(colorByAccessor: LinkAccessor): ChainableInstance; + linkLineDash(): LinkAccessor; + linkLineDash(linkLineDashAccessor: LinkAccessor): ChainableInstance; + linkWidth(): LinkAccessor; + linkWidth(widthAccessor: LinkAccessor): ChainableInstance; + linkCurvature(): LinkAccessor; + linkCurvature(curvatureAccessor: LinkAccessor): ChainableInstance; + linkCanvasObject(): CanvasCustomRenderFn; + linkCanvasObject(renderFn: CanvasCustomRenderFn): ChainableInstance; + linkCanvasObjectMode(): string | CanvasCustomRenderModeFn; + linkCanvasObjectMode(modeAccessor: string | CanvasCustomRenderModeFn): ChainableInstance; + linkDirectionalArrowLength(): LinkAccessor; + linkDirectionalArrowLength(lengthAccessor: LinkAccessor): ChainableInstance; + linkDirectionalArrowColor(): LinkAccessor; + linkDirectionalArrowColor(colorAccessor: LinkAccessor): ChainableInstance; + linkDirectionalArrowRelPos(): LinkAccessor; + linkDirectionalArrowRelPos(fractionAccessor: LinkAccessor): ChainableInstance; + linkDirectionalParticles(): LinkAccessor; + linkDirectionalParticles(numParticlesAccessor: LinkAccessor): ChainableInstance; + linkDirectionalParticleSpeed(): LinkAccessor; + linkDirectionalParticleSpeed(relDistancePerFrameAccessor: LinkAccessor): ChainableInstance; + linkDirectionalParticleWidth(): LinkAccessor; + linkDirectionalParticleWidth(widthAccessor: LinkAccessor): ChainableInstance; + linkDirectionalParticleColor(): LinkAccessor; + linkDirectionalParticleColor(colorAccessor: LinkAccessor): ChainableInstance; + emitParticle(link: L): ChainableInstance; + linkPointerAreaPaint(): CanvasPointerAreaPaintFn; + linkPointerAreaPaint(renderFn: CanvasPointerAreaPaintFn): ChainableInstance; // Render control autoPauseRedraw(): boolean; @@ -124,7 +131,7 @@ export interface ForceGraphGenericInstance { centerAt(x?: number, y?: number, durationMs?: number): ChainableInstance; zoom(): number; zoom(scale: number, durationMs?: number): ChainableInstance; - zoomToFit(durationMs?: number, padding?: number, nodeFilter?: (node: NodeObject) => boolean): ChainableInstance; + zoomToFit(durationMs?: number, padding?: number, nodeFilter?: (node: N) => boolean): ChainableInstance; minZoom(): number; minZoom(scale: number): ChainableInstance; maxZoom(): number; @@ -137,8 +144,8 @@ export interface ForceGraphGenericInstance { dagMode(mode: DagMode | null): ChainableInstance; dagLevelDistance(): number | null; dagLevelDistance(distance: number): ChainableInstance; - dagNodeFilter(): (node: NodeObject) => boolean; - dagNodeFilter(filterFn: (node: NodeObject) => boolean): ChainableInstance; + dagNodeFilter(): (node: N) => boolean; + dagNodeFilter(filterFn: (node: N) => boolean): ChainableInstance; onDagError(): (loopNodeIds: (string | number)[]) => void; onDagError(errorHandleFn: (loopNodeIds: (string | number)[]) => void): ChainableInstance; d3AlphaMin(): number; @@ -160,14 +167,14 @@ export interface ForceGraphGenericInstance { onEngineStop(callback: () => void): ChainableInstance; // Interaction - onNodeClick(callback: (node: NodeObject, event: MouseEvent) => void): ChainableInstance; - onNodeRightClick(callback: (node: NodeObject, event: MouseEvent) => void): ChainableInstance; - onNodeHover(callback: (node: NodeObject | null, previousNode: NodeObject | null) => void): ChainableInstance; - onNodeDrag(callback: (node: NodeObject, translate: { x: number, y: number }) => void): ChainableInstance; - onNodeDragEnd(callback: (node: NodeObject, translate: { x: number, y: number }) => void): ChainableInstance; - onLinkClick(callback: (link: LinkObject, event: MouseEvent) => void): ChainableInstance; - onLinkRightClick(callback: (link: LinkObject, event: MouseEvent) => void): ChainableInstance; - onLinkHover(callback: (link: LinkObject | null, previousLink: LinkObject | null) => void): ChainableInstance; + onNodeClick(callback: (node: N, event: MouseEvent) => void): ChainableInstance; + onNodeRightClick(callback: (node: N, event: MouseEvent) => void): ChainableInstance; + onNodeHover(callback: (node: N | null, previousNode: N | null) => void): ChainableInstance; + onNodeDrag(callback: (node: N, translate: { x: number, y: number }) => void): ChainableInstance; + onNodeDragEnd(callback: (node: N, translate: { x: number, y: number }) => void): ChainableInstance; + onLinkClick(callback: (link: L, event: MouseEvent) => void): ChainableInstance; + onLinkRightClick(callback: (link: L, event: MouseEvent) => void): ChainableInstance; + onLinkHover(callback: (link: L | null, previousLink: L | null) => void): ChainableInstance; linkHoverPrecision(): number; linkHoverPrecision(precision: number): ChainableInstance; onBackgroundClick(callback: (event: MouseEvent) => void): ChainableInstance; @@ -184,13 +191,23 @@ export interface ForceGraphGenericInstance { enablePointerInteraction(enable?: boolean): ChainableInstance; // Utility - getGraphBbox(nodeFilter?: (node: NodeObject) => boolean): { x: [number, number], y: [number, number] }; + getGraphBbox(nodeFilter?: (node: N) => boolean): { x: [number, number], y: [number, number] }; screen2GraphCoords(x: number, y: number): { x: number, y: number }; graph2ScreenCoords(x: number, y: number): { x: number, y: number }; } +export interface TypedForceGraphGenericInstance< + N extends NodeObject, + L extends TypedLinkObject, + ChainableInstance +> extends FullTypedForceGraphGenericInstance, ChainableInstance> {} +export interface ForceGraphGenericInstance extends FullTypedForceGraphGenericInstance {} + +export type TypedForceGraphInstance> = TypedForceGraphGenericInstance>; export type ForceGraphInstance = ForceGraphGenericInstance; +declare function TypedForceGraph>(): TypedForceGraphInstance; declare function ForceGraph(): ForceGraphInstance; +export {TypedForceGraph}; export default ForceGraph; diff --git a/src/index.js b/src/index.js index 3c23e1b..2218036 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ import './force-graph.css'; +import ForceGraph from './force-graph'; -export { default } from "./force-graph"; \ No newline at end of file +export {ForceGraph as TypedForceGraph}; +export default ForceGraph;