Backspace delete selected (Nodes + Groups) (#265)

* Backspace delete selected (Nodes + Groups)

* nit

* nit
This commit is contained in:
Chenlei Hu
2024-11-03 17:22:08 -05:00
committed by GitHub
parent 62bc1ecd9f
commit 6180f5ef55
2 changed files with 56 additions and 20 deletions

View File

@@ -2900,7 +2900,7 @@ export class LGraphCanvas {
else if (e.keyCode == 46 || e.keyCode == 8) {
// @ts-expect-error
if (e.target.localName != "input" && e.target.localName != "textarea") {
this.deleteSelectedNodes()
this.deleteSelectedItems()
block_default = true
}
}
@@ -3334,35 +3334,43 @@ export class LGraphCanvas {
}
/**
* deletes all nodes in the current selection from the graph
**/
deleteSelectedNodes(): void {
* Deletes all selected items from the graph.
*/
deleteSelectedItems(): void {
this.graph.beforeChange()
for (const i in this.selected_nodes) {
const node = this.selected_nodes[i]
if (node.block_delete) continue
//autoconnect when possible (very basic, only takes into account first input-output)
if (node.inputs?.length && node.outputs && node.outputs.length && LiteGraph.isValidConnection(node.inputs[0].type, node.outputs[0].type) && node.inputs[0].link && node.outputs[0].links && node.outputs[0].links.length) {
const input_link = node.graph._links.get(node.inputs[0].link)
const output_link = node.graph._links.get(node.outputs[0].links[0])
const input_node = node.getInputNode(0)
const output_node = node.getOutputNodes(0)[0]
if (input_node && output_node)
input_node.connect(input_link.origin_slot, output_node, output_link.target_slot)
for (const item of this.selectedItems) {
if (item instanceof LGraphNode) {
const node = item
if (node.block_delete) continue
node.connectInputToOutput()
}
if (item instanceof LGraphGroup || item instanceof LGraphNode) {
this.graph.remove(item)
}
if (item instanceof LGraphNode) {
this.onNodeDeselected?.(item)
}
this.graph.remove(node)
this.onNodeDeselected?.(node)
}
this.selected_nodes = {}
this.selectedItems.clear()
this.current_node = null
this.highlighted_links = {}
this.setDirty(true)
this.graph.afterChange()
}
/**
* deletes all nodes in the current selection from the graph
* @deprecated See {@link LGraphCanvas.deleteSelectedItems}
**/
deleteSelectedNodes(): void {
this.deleteSelectedItems()
}
/**
* centers the camera on a given node
**/

View File

@@ -2373,4 +2373,32 @@ export class LGraphNode implements Positionable {
currentX += badge.getWidth(ctx) + gap
}
}
/**
* Try auto-connect input to output without this node when possible
* (very basic, only takes into account first input-output)
*
* @returns true if connected, false otherwise
*/
connectInputToOutput(): boolean {
if (
this.inputs?.length &&
this.outputs &&
this.outputs.length &&
LiteGraph.isValidConnection(this.inputs[0].type, this.outputs[0].type) &&
this.inputs[0].link &&
this.outputs[0].links &&
this.outputs[0].links.length
) {
const input_link = this.graph._links.get(this.inputs[0].link)
const output_link = this.graph._links.get(this.outputs[0].links[0])
const input_node = this.getInputNode(0)
const output_node = this.getOutputNodes(0)[0]
if (input_node && output_node) {
input_node.connect(input_link.origin_slot, output_node, output_link.target_slot)
return true
}
}
return false
}
}