Skip to content

Commit

Permalink
Merge pull request #36 from retejs/expose-classic-actions
Browse files Browse the repository at this point in the history
feat: expose classic action classes
  • Loading branch information
Ni55aN authored Jan 18, 2025
2 parents 606ba0a + 2939916 commit 3b92e6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/presets/classic/actions/node.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { BaseSchemes, NodeEditor, NodeId } from 'rete'
import { BaseArea, BaseAreaPlugin } from 'rete-area-plugin'
import { BaseAreaPlugin } from 'rete-area-plugin'

import { Action } from '../../../types'

export type Position = { x: number, y: number }

export class AddNodeAction<S extends BaseSchemes> implements Action {
export class AddNodeAction<S extends BaseSchemes, AreaExtra> implements Action {
node?: S['Node']
position?: Position

constructor(private editor: NodeEditor<S>, private area: BaseAreaPlugin<S, BaseArea<S>>, private nodeId: NodeId) { }
constructor(private editor: NodeEditor<S>, private area: BaseAreaPlugin<S, AreaExtra>, private nodeId: NodeId) { }

async undo() {
this.node = this.editor.getNode(this.nodeId)
Expand All @@ -23,10 +23,10 @@ export class AddNodeAction<S extends BaseSchemes> implements Action {
}
}

export class RemoveNodeAction<S extends BaseSchemes> implements Action {
export class RemoveNodeAction<S extends BaseSchemes, AreaExtra> implements Action {
constructor(
private editor: NodeEditor<S>,
private area: BaseAreaPlugin<S, BaseArea<S>>,
private area: BaseAreaPlugin<S, AreaExtra>,
private node: S['Node'],
private position: Position
) { }
Expand All @@ -41,11 +41,11 @@ export class RemoveNodeAction<S extends BaseSchemes> implements Action {
}
}

export class DragNodeAction<S extends BaseSchemes> implements Action {
export class DragNodeAction<S extends BaseSchemes, AreaExtra> implements Action {
prev!: Position
new!: Position

constructor(private area: BaseAreaPlugin<S, BaseArea<S>>, public nodeId: NodeId, prev: Position) {
constructor(private area: BaseAreaPlugin<S, AreaExtra>, public nodeId: NodeId, prev: Position) {
const view = area.nodeViews.get(nodeId)

if (!view) return
Expand Down
22 changes: 15 additions & 7 deletions src/presets/classic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import { Preset } from '../types'
import { AddConnectionAction, RemoveConnectionAction } from './actions/connection'
import { AddNodeAction, DragNodeAction, Position, RemoveNodeAction } from './actions/node'

export {
AddConnectionAction,
AddNodeAction,
DragNodeAction,
RemoveConnectionAction,
RemoveNodeAction
}

type NodeActions<S extends BaseSchemes> =
| AddNodeAction<S>
| RemoveNodeAction<S>
| DragNodeAction<S>
| AddNodeAction<S, BaseArea<S>>
| RemoveNodeAction<S, BaseArea<S>>
| DragNodeAction<S, BaseArea<S>>

type ConnectionActions<S extends BaseSchemes> =
| AddConnectionAction<S>
Expand All @@ -30,7 +38,7 @@ function trackNodes<S extends BaseSchemes>(history: HistoryPlugin<S, NodeActions
if (context.type === 'nodecreated') {
const { id } = context.data

history.add(new AddNodeAction<S>(editor, area, id))
history.add(new AddNodeAction(editor, area, id))
nodes.set(id, editor.getNode(context.data.id))
}
if (context.type === 'noderemoved') {
Expand All @@ -40,7 +48,7 @@ function trackNodes<S extends BaseSchemes>(history: HistoryPlugin<S, NodeActions

if (!node) throw new Error('node')
if (!position) throw new Error('position' + id)
history.add(new RemoveNodeAction<S>(editor, area, node, position))
history.add(new RemoveNodeAction(editor, area, node, position))

positions.delete(id)
nodes.delete(id)
Expand Down Expand Up @@ -75,7 +83,7 @@ function trackNodes<S extends BaseSchemes>(history: HistoryPlugin<S, NodeActions
if (context.type === 'nodetranslated') {
const { id, position, previous } = context.data
const recent = history.getRecent(timing)
.filter((n): n is ({ time: number, action: DragNodeAction<S> }) => n.action instanceof DragNodeAction)
.filter((n): n is ({ time: number, action: DragNodeAction<S, BaseArea<S>> }) => n.action instanceof DragNodeAction)
.filter(n => n.action.nodeId === id)

if (recent.length > 1) throw new Error('> 1')
Expand All @@ -84,7 +92,7 @@ function trackNodes<S extends BaseSchemes>(history: HistoryPlugin<S, NodeActions
recent[0].action.new = position
recent[0].time = Date.now()
} else {
history.add(new DragNodeAction<S>(area, id, previous))
history.add(new DragNodeAction(area, id, previous))
}
}

Expand Down

0 comments on commit 3b92e6e

Please sign in to comment.