@@ -1082,94 +1082,38 @@ function addPreviewOptions(nodeType) {
10821082 } ) ;
10831083}
10841084function addFormatWidgets ( nodeType , nodeData ) {
1085- function parseFormats ( options ) {
1086- options . fullvalues = options . _values ;
1087- options . _values = [ ] ;
1088- for ( let format of options . fullvalues ) {
1089- if ( Array . isArray ( format ) ) {
1090- options . _values . push ( format [ 0 ] ) ;
1091- } else {
1092- options . _values . push ( format ) ;
1093- }
1094- }
1095- }
10961085 chainCallback ( nodeType . prototype , "onNodeCreated" , function ( ) {
10971086 var formatWidget = null ;
10981087 var formatWidgetIndex = - 1 ;
10991088 for ( let i = 0 ; i < this . widgets . length ; i ++ ) {
11001089 if ( this . widgets [ i ] . name === "format" ) {
11011090 formatWidget = this . widgets [ i ] ;
11021091 formatWidgetIndex = i + 1 ;
1092+ break
11031093 }
11041094 }
11051095 let formatWidgetsCount = 0 ;
11061096 //Pre-process options to just names
1107- formatWidget . options . _values = formatWidget . options . values ;
1108- parseFormats ( formatWidget . options ) ;
1109- Object . defineProperty ( formatWidget . options , "values" , {
1110- set : ( value ) => {
1111- formatWidget . options . _values = value ;
1112- parseFormats ( formatWidget . options ) ;
1113- } ,
1114- get : ( ) => {
1115- return formatWidget . options . _values ;
1116- }
1117- } )
1118-
1119- formatWidget . _value = formatWidget . value ;
1120- Object . defineProperty ( formatWidget , "value" , {
1121- set : ( value ) => {
1122- const formats = ( LiteGraph . registered_node_types [ this . type ]
1123- ?. nodeData ?. input ?. required ?. format ?. [ 1 ] ?. formats )
1124- formatWidget . _value = value ;
1125- let newWidgets = [ ] ;
1126- const fullDef = formatWidget . options . fullvalues . find ( ( w ) => Array . isArray ( w ) ? w [ 0 ] === value : w === value ) ;
1127-
1128- if ( ! Array . isArray ( fullDef ) && ! formats ?. [ value ] ) {
1129- formatWidget . _value = value ;
1130- } else {
1131- formatWidget . _value = value ;
1132- let formatWidgets = formats ?. [ value ] ?? fullDef [ 1 ]
1133- for ( let wDef of formatWidgets ) {
1134- //create widgets. Heavy borrowed from web/scripts/app.js
1135- //default implementation doesn't work since it automatically adds
1136- //the widget in the wrong spot.
1137- //TODO: consider letting this happen and just removing from list?
1138- let w = { } ;
1139- w . name = wDef [ 0 ] ;
1140- w . config = wDef . slice ( 1 ) ;
1141- let inputData = wDef . slice ( 1 ) ;
1142- w . type = inputData [ 0 ] ;
1143- w . options = inputData [ 1 ] ? inputData [ 1 ] : { } ;
1144- if ( Array . isArray ( w . type ) ) {
1145- w . value = w . type [ 0 ] ;
1146- w . options . values = w . type ;
1147- w . type = "combo" ;
1148- }
1149- if ( inputData [ 1 ] ?. default != undefined ) {
1150- w . value = inputData [ 1 ] . default ;
1151- }
1152- if ( w . type == "INT" ) {
1153- Object . assign ( w . options , { "precision" : 0 , "step" : 10 } )
1154- w . callback = function ( v ) {
1155- const s = this . options . step / 10 ;
1156- this . value = Math . round ( v / s ) * s ;
1157- }
1158- }
1159- const typeTable = { BOOLEAN : "toggle" , STRING : "text" , INT : "number" , FLOAT : "number" } ;
1160- if ( w . type in typeTable ) {
1161- w . type = typeTable [ w . type ] ;
1162- }
1163- newWidgets . push ( w ) ;
1097+ chainCallback ( formatWidget , "callback" , ( value ) => {
1098+ const formats = ( LiteGraph . registered_node_types [ this . type ]
1099+ ?. nodeData ?. input ?. required ?. format ?. [ 1 ] ?. formats )
1100+ let newWidgets = [ ] ;
1101+ if ( formats ?. [ value ] ) {
1102+ let formatWidgets = formats [ value ]
1103+ for ( let wDef of formatWidgets ) {
1104+ let type = wDef [ 1 ]
1105+ if ( Array . isArray ( type ) ) {
1106+ type = "COMBO"
11641107 }
1108+ app . widgets [ type ] ( this , wDef [ 0 ] , wDef . slice ( 1 ) , app )
1109+ let w = this . widgets . pop ( )
1110+ w . config = wDef . slice ( 1 )
1111+ newWidgets . push ( w )
11651112 }
1166- this . widgets . splice ( formatWidgetIndex , formatWidgetsCount , ...newWidgets ) ;
1167- fitHeight ( this ) ;
1168- formatWidgetsCount = newWidgets . length ;
1169- } ,
1170- get : ( ) => {
1171- return formatWidget . _value ;
11721113 }
1114+ this . widgets . splice ( formatWidgetIndex , formatWidgetsCount , ...newWidgets ) ;
1115+ fitHeight ( this ) ;
1116+ formatWidgetsCount = newWidgets . length ;
11731117 } ) ;
11741118 } ) ;
11751119}
0 commit comments