Skip to content

Commit

Permalink
Merge pull request #642 from retejs/docs/impove
Browse files Browse the repository at this point in the history
docs: classic preset docs
  • Loading branch information
Ni55aN authored Jul 19, 2023
2 parents 60f2f1e + a9539ff commit f77e567
Showing 1 changed file with 106 additions and 3 deletions.
109 changes: 106 additions & 3 deletions src/presets/classic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ type PortId = string

/**
* The socket class
* @priority 7
*/
export class Socket {
/**
* @constructor
* @param name Name of the socket
*/
constructor(public name: string) {

}
Expand All @@ -22,55 +27,111 @@ export class Socket {
* General port class
*/
export class Port<S extends Socket> {
/**
* Port id, unique string generated by `getUID` function
*/
id: PortId
/**
* Port index, used for sorting ports. Default is `0`
*/
index?: number

/**
* @constructor
* @param socket Socket instance
* @param label Label of the port
* @param multipleConnections Whether the output port can have multiple connections
*/
constructor(public socket: S, public label?: string, public multipleConnections?: boolean) {
this.id = getUID()
}
}

/**
* The input port class
* @priority 6
*/
export class Input<S extends Socket> extends Port<S> {
/**
* Control instance
*/
control: Control | null = null
/**
* Whether the control is visible. Can be managed dynamically by extensions. Default is `true`
*/
showControl = true

/**
* @constructor
* @param socket Socket instance
* @param label Label of the input port
* @param multipleConnections Whether the output port can have multiple connections. Default is `false`
*/
constructor(public socket: S, public label?: string, public multipleConnections?: boolean) {
super(socket, label, multipleConnections)
}

/**
* Add control to the input port
* @param control Control instance
*/
addControl(control: Control) {
if (this.control) throw new Error('control already added for this input')
this.control = control
}

/**
* Remove control from the input port
*/
removeControl() {
this.control = null
}
}

/**
* The output port class
* @priority 5
*/
export class Output<S extends Socket> extends Port<S> {
/**
* @constructor
* @param socket Socket instance
* @param label Label of the output port
* @param multipleConnections Whether the output port can have multiple connections. Default is `true`
*/
constructor(socket: S, label?: string, multipleConnections?: boolean) {
super(socket, label, multipleConnections !== false)
}
}

/**
* General control class
* @priority 5
*/
export class Control {
/**
* Control id, unique string generated by `getUID` function
*/
id: string
/**
* Control index, used for sorting controls. Default is `0`
*/
index?: number

constructor() {
this.id = getUID()
}
}

/**
* Input control options
*/
type InputControlOptions<N> = {
/** Whether the control is readonly. Default is `false` */
readonly?: boolean,
/** Initial value of the control */
initial?: N,
/** Callback function that is called when the control value changes */
change?: (value: N) => void
}
/**
Expand All @@ -81,6 +142,11 @@ export class InputControl<T extends 'text' | 'number', N = T extends 'text' ? st
value?: N
readonly: boolean

/**
* @constructor
* @param type Type of the control: `text` or `number`
* @param options Control options
*/
constructor(public type: T, public options?: InputControlOptions<N>) {
super()
this.id = getUID()
Expand All @@ -89,6 +155,10 @@ export class InputControl<T extends 'text' | 'number', N = T extends 'text' ? st
if (typeof options?.initial !== 'undefined') this.value = options.initial
}

/**
* Set control value
* @param value Value to set
*/
setValue(value?: N) {
this.value = value
if (this.options?.change) this.options.change(value)
Expand All @@ -97,20 +167,33 @@ export class InputControl<T extends 'text' | 'number', N = T extends 'text' ? st

/**
* The node class
* @typeParam Inputs - The inputs type
* @typeParam Outputs - The outputs type
* @typeParam Controls - The controls type
* @priority 10
* @example new Node('math')
*/
export class Node<
Inputs extends { [key in string]?: Socket } = { [key in string]?: Socket },
Outputs extends { [key in string]?: Socket } = { [key in string]?: Socket },
Controls extends { [key in string]?: Control } = { [key in string]?: Control }
> implements NodeBase {
/**
* Node id, unique string generated by `getUID` function
*/
id: NodeBase['id']
/**
* Node inputs
*/
inputs: { [key in keyof Inputs]?: Input<Exclude<Inputs[key], undefined>> } = {}
/**
* Node outputs
*/
outputs: { [key in keyof Outputs]?: Output<Exclude<Outputs[key], undefined>> } = {}
/**
* Node controls
*/
controls: Controls = {} as Controls
/**
* Whether the node is selected. Default is `false`
*/
selected?: boolean

constructor(public label: string) {
Expand Down Expand Up @@ -160,14 +243,34 @@ export class Node<
}
}

/**
* The connection class
* @priority 9
*/
export class Connection<
Source extends Node,
Target extends Node
> implements ConnectionBase {
/**
* Connection id, unique string generated by `getUID` function
*/
id: ConnectionBase['id']
/**
* Source node id
*/
source: NodeBase['id']
/**
* Target node id
*/
target: NodeBase['id']

/**
* @constructor
* @param source Source node instance
* @param sourceOutput Source node output key
* @param target Target node instance
* @param targetInput Target node input key
*/
constructor(
source: Source,
public sourceOutput: keyof Source['outputs'],
Expand Down

0 comments on commit f77e567

Please sign in to comment.