Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: classic preset docs #642

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading