Skip to content

Commit

Permalink
api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Jul 11, 2023
1 parent ac8edb2 commit fa8c028
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules
coverage
npm-debug.log
/dist
docs
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

108 changes: 104 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"build": "rete build -c rete.config.ts",
"postinstall": "node postinstall.js",
"doc": "rete doc",
"lint": "rete lint",
"test": "rete test"
},
Expand All @@ -27,7 +28,7 @@
},
"devDependencies": {
"jest-environment-jsdom": "^29.1.2",
"rete-cli": "^1.0.0-beta.29",
"rete-cli": "^1.0.0-beta.31",
"typescript": "4.8.4"
},
"dependencies": {
Expand Down
69 changes: 69 additions & 0 deletions src/editor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Scope } from './scope'
import { BaseSchemes } from './types'

/**
* Signal types produced by NodeEditor instance
* @typeParam Scheme - The scheme type
* @priority 10
* @group Primary
*/
export type Root<Scheme extends BaseSchemes> =
| { type: 'nodecreate', data: Scheme['Node'] }
| { type: 'nodecreated', data: Scheme['Node'] }
Expand All @@ -14,6 +20,12 @@ export type Root<Scheme extends BaseSchemes> =
| { type: 'clearcancelled' }
| { type: 'cleared' }

/**
* The NodeEditor class is the entry class. It is used to create and manage nodes and connections.
* @typeParam Scheme - The scheme type
* @priority 7
* @group Primary
*/
export class NodeEditor<Scheme extends BaseSchemes> extends Scope<Root<Scheme>> {
private nodes: Scheme['Node'][] = []
private connections: Scheme['Connection'][] = []
Expand All @@ -22,22 +34,48 @@ export class NodeEditor<Scheme extends BaseSchemes> extends Scope<Root<Scheme>>
super('NodeEditor')
}

/**
* Get a node by id
* @param id - The node id
* @returns The node or undefined
*/
public getNode(id: Scheme['Node']['id']) {
return this.nodes.find(node => node.id === id)
}

/**
* Get all nodes
* @returns All nodes
*/
public getNodes() {
return this.nodes
}

/**
* Get all connections
* @returns All connections
*/
public getConnections() {
return this.connections
}

/**
* Get a connection by id
* @param id - The connection id
* @returns The connection or undefined
*/
public getConnection(id: Scheme['Connection']['id']) {
return this.connections.find(connection => connection.id === id)
}

/**
* Add a node
* @param data - The node data
* @returns Whether the node was added
* @throws If the node has already been added
* @emits nodecreate
* @emits nodecreated
*/
async addNode(data: Scheme['Node']) {
if (this.getNode(data.id)) throw new Error('node has already been added')

Expand All @@ -49,6 +87,14 @@ export class NodeEditor<Scheme extends BaseSchemes> extends Scope<Root<Scheme>>
return true
}

/**
* Add a connection
* @param data - The connection data
* @returns Whether the connection was added
* @throws If the connection has already been added
* @emits connectioncreate
* @emits connectioncreated
*/
async addConnection(data: Scheme['Connection']) {
if (this.getConnection(data.id)) throw new Error('connection has already been added')

Expand All @@ -60,6 +106,14 @@ export class NodeEditor<Scheme extends BaseSchemes> extends Scope<Root<Scheme>>
return true
}

/**
* Remove a node
* @param id - The node id
* @returns Whether the node was removed
* @throws If the node cannot be found
* @emits noderemove
* @emits noderemoved
*/
async removeNode(id: Scheme['Node']['id']) {
const index = this.nodes.findIndex(n => n.id === id)
const node = this.nodes[index]
Expand All @@ -74,6 +128,14 @@ export class NodeEditor<Scheme extends BaseSchemes> extends Scope<Root<Scheme>>
return true
}

/**
* Remove a connection
* @param id - The connection id
* @returns Whether the connection was removed
* @throws If the connection cannot be found
* @emits connectionremove
* @emits connectionremoved
*/
async removeConnection(id: Scheme['Connection']['id']) {
const index = this.connections.findIndex(n => n.id === id)
const connection = this.connections[index]
Expand All @@ -88,6 +150,13 @@ export class NodeEditor<Scheme extends BaseSchemes> extends Scope<Root<Scheme>>
return true
}

/**
* Clear all nodes and connections
* @returns Whether the editor was cleared
* @emits clear
* @emits clearcancelled
* @emits cleared
*/
async clear() {
if (!await this.emit({ type: 'clear' })) {
await this.emit({ type: 'clearcancelled' })
Expand Down
Loading

0 comments on commit fa8c028

Please sign in to comment.