Skip to content

Commit

Permalink
Merge pull request #35 from retejs/fix/drop
Browse files Browse the repository at this point in the history
Fix unexpenced creation of a new instance of last dropped node on drop event
  • Loading branch information
Ni55aN authored May 26, 2024
2 parents 58b1cbf + 07c0677 commit c33dbdb
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/drop-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseSchemes, NodeEditor } from 'rete'
import { AreaPlugin } from 'rete-area-plugin'
import { Position } from 'rete-area-plugin/_types/types'

import { Strategy } from './strategy'

Expand All @@ -11,18 +12,12 @@ export class DropStrategy<K> implements Strategy {
area.container.addEventListener('drop', async event => {
if (!this.current) return

const node = this.current()

await this.editor.addNode(node)

this.area.area.setPointerFrom(event)

const position = this.area.area.pointer
const view = this.area.nodeViews.get(node.id)

if (!view) throw new Error('view')

await view.translate(position.x, position.y)
try {
this.area.area.setPointerFrom(event)
this.drop(this.current(), this.area.area.pointer)
} finally {
delete this.current
}
})
}

Expand All @@ -33,5 +28,15 @@ export class DropStrategy<K> implements Strategy {
this.current = create
})
}

private async drop(node: BaseSchemes['Node'], position: Position) {
await this.editor.addNode(node)

const view = this.area.nodeViews.get(node.id)

if (!view) throw new Error('view')

await view.translate(position.x, position.y)
}
}

0 comments on commit c33dbdb

Please sign in to comment.