File size: 1,156 Bytes
0c85e62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Shared frontend widget helpers for ComfyUI-Koolook nodes.

// Pysssss's presetText.js wraps every STRING widget's serializeValue with a
// callback chain that does `value.replace(...)`. If widget.value is undefined
// or null (widget-to-input conversion voiding the value, or a workflow saved
// before this widget existed), `.replace` throws and graphToPrompt aborts.
// Trap reads/writes via a property descriptor so widget.value is always a
// string regardless of caller.
export function bulletproofStringWidget(widget, fallback = "") {
    if (!widget) return;
    let stored;
    const initial = widget.value;
    if (typeof initial === "string") {
        stored = initial;
    } else if (initial == null) {
        stored = fallback;
    } else {
        stored = String(initial);
    }
    Object.defineProperty(widget, "value", {
        configurable: true,
        enumerable: true,
        get() { return stored; },
        set(v) {
            if (typeof v === "string") stored = v;
            else if (v == null) stored = fallback;
            else stored = String(v);
        },
    });
}