@@ -2,27 +2,36 @@ import { app } from "../../../scripts/app.js";
22import { ComfyWidgets } from "../../../scripts/widgets.js" ;
33
44// Displays input text on a node
5+
6+ // TODO: This should need to be so complicated. Refactor at some point.
7+
58app . registerExtension ( {
69 name : "pysssss.ShowText" ,
710 async beforeRegisterNodeDef ( nodeType , nodeData , app ) {
811 if ( nodeData . name === "ShowText|pysssss" ) {
912 function populate ( text ) {
1013 if ( this . widgets ) {
11- for ( let i = 1 ; i < this . widgets . length ; i ++ ) {
14+ // On older frontend versions there is a hidden converted-widget
15+ const isConvertedWidget = + ! ! this . inputs ?. [ 0 ] . widget ;
16+ for ( let i = isConvertedWidget ; i < this . widgets . length ; i ++ ) {
1217 this . widgets [ i ] . onRemove ?. ( ) ;
1318 }
14- this . widgets . length = 1 ;
19+ this . widgets . length = isConvertedWidget ;
1520 }
1621
1722 const v = [ ...text ] ;
1823 if ( ! v [ 0 ] ) {
1924 v . shift ( ) ;
2025 }
21- for ( const list of v ) {
22- const w = ComfyWidgets [ "STRING" ] ( this , "text2" , [ "STRING" , { multiline : true } ] , app ) . widget ;
23- w . inputEl . readOnly = true ;
24- w . inputEl . style . opacity = 0.6 ;
25- w . value = list ;
26+ for ( let list of v ) {
27+ // Force list to be an array, not sure why sometimes it is/isn't
28+ if ( ! ( list instanceof Array ) ) list = [ list ] ;
29+ for ( const l of list ) {
30+ const w = ComfyWidgets [ "STRING" ] ( this , "text_" + this . widgets ?. length ?? 0 , [ "STRING" , { multiline : true } ] , app ) . widget ;
31+ w . inputEl . readOnly = true ;
32+ w . inputEl . style . opacity = 0.6 ;
33+ w . value = l ;
34+ }
2635 }
2736
2837 requestAnimationFrame ( ( ) => {
@@ -45,11 +54,23 @@ app.registerExtension({
4554 populate . call ( this , message . text ) ;
4655 } ;
4756
57+ const VALUES = Symbol ( ) ;
58+ const configure = nodeType . prototype . configure ;
59+ nodeType . prototype . configure = function ( ) {
60+ // Store unmodified widget values as they get removed on configure by new frontend
61+ this [ VALUES ] = arguments [ 0 ] ?. widgets_values ;
62+ return configure ?. apply ( this , arguments ) ;
63+ } ;
64+
4865 const onConfigure = nodeType . prototype . onConfigure ;
4966 nodeType . prototype . onConfigure = function ( ) {
5067 onConfigure ?. apply ( this , arguments ) ;
51- if ( this . widgets_values ?. length ) {
52- populate . call ( this , this . widgets_values . slice ( + this . widgets_values . length > 1 ) ) ;
68+ const widgets_values = this [ VALUES ] ;
69+ if ( widgets_values ?. length ) {
70+ // In newer frontend there seems to be a delay in creating the initial widget
71+ requestAnimationFrame ( ( ) => {
72+ populate . call ( this , widgets_values . slice ( + ( widgets_values . length > 1 && this . inputs ?. [ 0 ] . widget ) ) ) ;
73+ } ) ;
5374 }
5475 } ;
5576 }
0 commit comments