Skip to content

Emit searchbox triggering canvas events #10

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

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

This is the litegraph version used in [ComfyUI_frontend](https://github.com/Comfy-Org/ComfyUI_frontend).

This repo is litegraph with following modifications:
This repo is litegraph with the following modifications:
- Accumulated comfyUI custom changes (2024-01 ~ 2024-05) (https://github.com/Comfy-Org/litegraph.js/pull/1)
- Type schema change for ComfyUI_frontend TS migration (https://github.com/Comfy-Org/litegraph.js/pull/3)
- Zoom fix (https://github.com/Comfy-Org/litegraph.js/pull/7)
- Emit search box triggering custom events (<https://github.com/Comfy-Org/litegraph.js/pull/10>)

# Install
`npm i @comfyorg/litegraph`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comfyorg/litegraph",
"version": "0.7.21",
"version": "0.7.22",
"description": "A graph node editor similar to PD or UDK Blueprints. It works in an HTML5 Canvas and allows to export graphs to be included in applications.",
"main": "src/litegraph.js",
"types": "src/litegraph.d.ts",
Expand Down
46 changes: 30 additions & 16 deletions src/litegraph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export interface INodeSlot {
type: string | -1;
label?: string;
dir?:
| typeof LiteGraph.UP
| typeof LiteGraph.RIGHT
| typeof LiteGraph.DOWN
| typeof LiteGraph.LEFT;
| typeof LiteGraph.UP
| typeof LiteGraph.RIGHT
| typeof LiteGraph.DOWN
| typeof LiteGraph.LEFT;
color_on?: string;
color_off?: string;
shape?: SlotShape;
Expand Down Expand Up @@ -104,8 +104,8 @@ export interface IComboWidget
string[],
{
values:
| string[]
| ((widget: IComboWidget, node: LGraphNode) => string[]);
| string[]
| ((widget: IComboWidget, node: LGraphNode) => string[]);
}
> {
type: "combo";
Expand Down Expand Up @@ -146,6 +146,20 @@ export type ContextMenuEventListener = (
node: LGraphNode
) => boolean | void;

export type LinkReleaseContext = {
node_from: LGraphNode;
slot_from: INodeSlot;
type_filter_in: string;
};

export type LiteGraphCanvasEventType = "empty-release" | "empty-double-click";

export type LiteGraphCanvasEvent = CustomEvent<{
subType: string;
originalEvent: Event,
linkReleaseContext?: LinkReleaseContext;
}>;

export const LiteGraph: {
DEFAULT_GROUP_FONT_SIZE: any;
overlapBounding(visible_area: any, _bounding: any): unknown;
Expand Down Expand Up @@ -244,7 +258,7 @@ export const LiteGraph: {

createNode<T extends LGraphNode = LGraphNode>(type: string): T;
/** Register a node class so it can be listed when the user wants to create a new one */
registerNodeType(type: string, base: { new (): LGraphNode }): void;
registerNodeType(type: string, base: { new(): LGraphNode }): void;
/** removes a node type from the system */
unregisterNodeType(type: string): void;
/** Removes all previously registered node's types. */
Expand Down Expand Up @@ -426,7 +440,7 @@ export declare class LGraph {
/**
* Positions every node in a more readable manner
*/
arrange(margin?: number,layout?: string): void;
arrange(margin?: number, layout?: string): void;
/**
* Returns the amount of time the graph has been running in milliseconds
* @return number of milliseconds the graph has been running
Expand Down Expand Up @@ -602,10 +616,10 @@ export type SerializedLGraphNode<T extends LGraphNode = LGraphNode> = {

/** https://github.com/jagenjo/litegraph.js/blob/master/guides/README.md#lgraphnode */
export declare class LGraphNode {
onResize?: Function;
onResize?: Function;

// Used in group node
setInnerNodes(nodes: LGraphNode[]);
setInnerNodes(nodes: LGraphNode[]);

static title_color: string;
static title: string;
Expand Down Expand Up @@ -1054,8 +1068,8 @@ export declare class LGraphNode {
}

export type LGraphNodeConstructor<T extends LGraphNode = LGraphNode> = {
nodeData: any; // Used by group node.
new (): T;
nodeData: any; // Used by group node.
new(): T;
};

export type SerializedLGraphGroup = {
Expand Down Expand Up @@ -1257,10 +1271,10 @@ export declare class LGraphCanvas {
/** Called by `LGraphCanvas.showSearchBox` */
onSearchBox:
| ((
helper: Element,
value: string,
graphCanvas: LGraphCanvas
) => string[])
helper: Element,
value: string,
graphCanvas: LGraphCanvas
) => string[])
| null;
onSearchBoxSelection:
| ((name: string, event: MouseEvent, graphCanvas: LGraphCanvas) => void)
Expand Down
62 changes: 42 additions & 20 deletions src/litegraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -6186,10 +6186,22 @@ LGraphNode.prototype.executeAction = function(action)
}
}

if (is_double_click && !this.read_only && this.allow_searchbox) {
this.showSearchBox(e);
e.preventDefault();
e.stopPropagation();
if (is_double_click && !this.read_only) {
if (this.allow_searchbox) {
this.showSearchBox(e);
e.preventDefault();
e.stopPropagation();
}
this.canvas.dispatchEvent(new CustomEvent(
"litegraph:canvas",
{
bubbles: true,
detail: {
type: "empty-double-click",
originalEvent: e,
}
},
));
}

clicking_canvas_bg = true;
Expand Down Expand Up @@ -6756,9 +6768,7 @@ LGraphNode.prototype.executeAction = function(action)
// look for a good slot
this.connecting_node.connectByType(this.connecting_slot,node,connType);
}

}else if (this.connecting_input){

} else if (this.connecting_input) {
var slot = this.isOverNodeOutput(
node,
e.canvasX,
Expand All @@ -6772,22 +6782,34 @@ LGraphNode.prototype.executeAction = function(action)
// look for a good slot
this.connecting_node.connectByTypeOutput(this.connecting_slot,node,connType);
}

}


//}

}else{

} else {
const linkReleaseContext = this.connecting_output ? {
node_from: this.connecting_node,
slot_from: this.connecting_output,
type_filter_in: this.connecting_output.type
} : {
node_to: this.connecting_node,
slot_from: this.connecting_input,
type_filter_out: this.connecting_input.type
};

this.canvas.dispatchEvent(new CustomEvent(
"litegraph:canvas", {
bubbles: true,
detail: {
subType: "empty-release",
originalEvent: e,
linkReleaseContext,
},
}
));
// add menu when releasing link in empty space
if (LiteGraph.release_link_on_empty_shows_menu){
if (e.shiftKey && this.allow_searchbox){
if(this.connecting_output){
this.showSearchBox(e,{node_from: this.connecting_node, slot_from: this.connecting_output, type_filter_in: this.connecting_output.type});
}else if(this.connecting_input){
this.showSearchBox(e,{node_to: this.connecting_node, slot_from: this.connecting_input, type_filter_out: this.connecting_input.type});
}
if (e.shiftKey){
if (this.allow_searchbox) {
this.showSearchBox(e, linkReleaseContext);
}
}else{
if(this.connecting_output){
this.showConnectionMenu({nodeFrom: this.connecting_node, slotFrom: this.connecting_output, e: e});
Expand Down