Serialize NodeInputSlot.widget (#622)

Serialize linked widget on node input slot properly according to current
save format in
76818b54e6/src/extensions/core/widgetInputs.ts
This commit is contained in:
Chenlei Hu
2025-02-26 14:15:31 -05:00
committed by GitHub
parent c166a34fd8
commit d20aab757c
5 changed files with 54 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ import { LiteGraph } from "./litegraph"
import { LinkDirection, RenderShape } from "./types/globalEnums"
import { ISerialisedNodeOutputSlot } from "./types/serialisation"
import { ISerialisedNodeInputSlot } from "./types/serialisation"
import { omitBy } from "./utils/object"
export interface ConnectionColorContext {
default_connection_color: {
@@ -33,16 +34,13 @@ interface IDrawOptions {
export function serializeSlot(slot: INodeInputSlot): ISerialisedNodeInputSlot
export function serializeSlot(slot: INodeOutputSlot): ISerialisedNodeOutputSlot
export function serializeSlot(slot: INodeInputSlot | INodeOutputSlot): ISerialisedNodeInputSlot | ISerialisedNodeOutputSlot {
const serialized = { ...slot }
delete serialized._layoutElement
if ("_data" in serialized) {
delete serialized._data
}
// Widget input slots' pos is calculated during layout, so we don't need to serialize it.
if (isWidgetInputSlot(slot) && "pos" in serialized) {
delete serialized.pos
}
return serialized
return omitBy({
...slot,
_layoutElement: undefined,
_data: undefined,
pos: isWidgetInputSlot(slot) ? undefined : slot.pos,
widget: isWidgetInputSlot(slot) && slot.widget?.name ? { name: slot.widget.name } : undefined,
}, value => value === undefined) as ISerialisedNodeInputSlot | ISerialisedNodeOutputSlot
}
export function toNodeSlotClass(slot: INodeSlot): NodeSlot {

View File

@@ -41,7 +41,7 @@ export interface SerialisableGraph {
}
export type ISerialisedNodeInputSlot = Omit<INodeInputSlot, "_layoutElement"> & {
widget?: { name?: string }
widget?: { name: string }
}
export type ISerialisedNodeOutputSlot = Omit<INodeOutputSlot, "_layoutElement" | "_data">

5
src/utils/object.ts Normal file
View File

@@ -0,0 +1,5 @@
export function omitBy<T extends object>(obj: T, predicate: (value: any) => boolean): Partial<T> {
return Object.fromEntries(
Object.entries(obj).filter(([_key, value]) => !predicate(value)),
) as Partial<T>
}