fix(core): add missing Type generic to node types

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
braks
2023-04-11 20:28:02 +02:00
committed by Braks
parent aaeb6dc4b8
commit ab482bcf5d
+30 -9
View File
@@ -1,6 +1,6 @@
import type { Component, VNode } from 'vue' import type { Component, VNode } from 'vue'
import type { ClassFunc, Dimensions, ElementData, Position, StyleFunc, Styles, XYPosition, XYZPosition } from './flow' import type { ClassFunc, Dimensions, ElementData, Position, StyleFunc, Styles, XYPosition, XYZPosition } from './flow'
import type { DefaultNodeTypes, NodeComponent } from './components' import type { NodeComponent } from './components'
import type { HandleConnectable, HandleElement, ValidConnectionFunc } from './handle' import type { HandleConnectable, HandleElement, ValidConnectionFunc } from './handle'
import type { CustomEvent, NodeEventsHandler, NodeEventsOn } from './hooks' import type { CustomEvent, NodeEventsHandler, NodeEventsOn } from './hooks'
@@ -29,7 +29,7 @@ export type WidthFunc = (node: GraphNode) => number | string | void
/** @deprecated will be removed in next major release */ /** @deprecated will be removed in next major release */
export type HeightFunc = (node: GraphNode) => number | string | void export type HeightFunc = (node: GraphNode) => number | string | void
export interface Node<Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any> { export interface Node<Data = ElementData, CustomEvents extends Record<string, CustomEvent> = any, Type extends string = string> {
/** Unique node id */ /** Unique node id */
id: string id: string
/** A node label */ /** A node label */
@@ -37,7 +37,7 @@ export interface Node<Data = ElementData, CustomEvents extends Record<string, Cu
/** initial node position x, y */ /** initial node position x, y */
position: XYPosition position: XYPosition
/** node type, can be a default type or a custom type */ /** node type, can be a default type or a custom type */
type?: keyof DefaultNodeTypes | string type?: Type
/** handle position */ /** handle position */
targetPosition?: Position targetPosition?: Position
/** handle position */ /** handle position */
@@ -46,11 +46,13 @@ export interface Node<Data = ElementData, CustomEvents extends Record<string, Cu
draggable?: boolean draggable?: boolean
/** Disable/enable selecting node */ /** Disable/enable selecting node */
selectable?: boolean selectable?: boolean
/** Disable/enable connecting node */
connectable?: HandleConnectable connectable?: HandleConnectable
/** Disable/enable focusing node (a11y) */ /** Disable/enable focusing node (a11y) */
focusable?: boolean focusable?: boolean
/** Disable/enable deleting node */ /** Disable/enable deleting node */
deletable?: boolean deletable?: boolean
/** element selector as drag handle for node (can only be dragged from the dragHandle el) */
dragHandle?: string dragHandle?: string
/** @deprecated will be removed in next major release */ /** @deprecated will be removed in next major release */
/** called when used as target for new connection */ /** called when used as target for new connection */
@@ -97,7 +99,7 @@ export interface GraphNode<
Data = ElementData, Data = ElementData,
CustomEvents extends Record<string, CustomEvent> = any, CustomEvents extends Record<string, CustomEvent> = any,
Type extends string = string, Type extends string = string,
> extends Node<Data, CustomEvents> { > extends Node<Data, CustomEvents, Type> {
/** absolute position in relation to parent elements + z-index */ /** absolute position in relation to parent elements + z-index */
computedPosition: XYZPosition computedPosition: XYZPosition
handleBounds: NodeHandleBounds handleBounds: NodeHandleBounds
@@ -114,11 +116,11 @@ export interface GraphNode<
} }
/** these props are passed to node components */ /** these props are passed to node components */
export interface NodeProps<Data = ElementData, CustomEvents = {}, Type extends string = keyof DefaultNodeTypes> { export interface NodeProps<Data = ElementData, CustomEvents = {}, Type extends string = string> {
/** unique node id */ /** unique node id */
id: string id: string
/** node type */ /** node type */
type?: Type type: Type
/** is node selected */ /** is node selected */
selected: boolean selected: boolean
/** can node handles be connected */ /** can node handles be connected */
@@ -133,14 +135,24 @@ export interface NodeProps<Data = ElementData, CustomEvents = {}, Type extends s
* Object is just a type-hack for Vue, ignore that * Object is just a type-hack for Vue, ignore that
*/ */
label?: string | VNode | Component | Object label?: string | VNode | Component | Object
/** called when used as target for new connection */ /**
* @deprecated will be removed in next major release and replaced by just `isValidConnection` prop
* called when used as target for new connection
*/
isValidTargetPos?: ValidConnectionFunc isValidTargetPos?: ValidConnectionFunc
/** called when used as source for new connection */ /**
* @deprecated will be removed in next major release and replaced by just `isValidConnection` prop
* called when used as source for new connection
*/
isValidSourcePos?: ValidConnectionFunc isValidSourcePos?: ValidConnectionFunc
/** parent node id */ /**
* todo: rename to `parentNodeId`
* parent node id
*/
parent?: string parent?: string
/** is node currently dragging */ /** is node currently dragging */
dragging: boolean dragging: boolean
/** is node currently resizing */
resizing: boolean resizing: boolean
/** node z-index */ /** node z-index */
zIndex: number zIndex: number
@@ -156,3 +168,12 @@ export interface NodeProps<Data = ElementData, CustomEvents = {}, Type extends s
/** contextual and custom events of node */ /** contextual and custom events of node */
events: NodeEventsOn<CustomEvents> events: NodeEventsOn<CustomEvents>
} }
/**
* Transform a Node type to a GraphNode type
*/
export type ToGraphNode<T extends Node> = GraphNode<
T extends Node<infer Data> ? Data : never,
T extends Node<any, infer Events> ? Events : never,
T extends Node<any, any, infer Type> ? Type : never
>