@@ -73,48 +73,26 @@ Ext.onReady(function() {
7373 this . _setFieldValue ( key , cachedItem . values [ -- cachedItem . visible ] ) ;
7474 }
7575 } ;
76+ const freePromptCache = {
77+ _cache : { } ,
78+ get ( key ) {
79+ if ( ! this . _cache [ key ] ) {
80+ this . _cache [ key ] = {
81+ visible : - 1 ,
82+ history : [ ]
83+ }
84+ }
7685
77- const createWantEl = ( ) => {
78- const wandEl = document . createElement ( 'span' ) ;
79- wandEl . style . cursor = 'pointer' ;
80- wandEl . style . marginLeft = '5px' ;
81- wandEl . style . verticalAlign = 'middle' ;
82- wandEl . style . fontSize = '24px' ;
83- wandEl . innerText = '🪄'
84-
85- return wandEl ;
86- }
87-
88- const createGenerateButton = ( field , fieldName ) => {
89- const wandEl = createWantEl ( ) ;
90-
91- wandEl . addEventListener ( 'click' , ( ) => {
92- Ext . Msg . wait ( 'Generating ...' , 'Please wait' ) ;
86+ return this . _cache [ key ] ;
87+ }
88+ } ;
9389
94- MODx . Ajax . request ( {
95- url : MODx . config . connector_url ,
96- params : {
97- action : 'modAI\\Processors\\Prompt\\Text' ,
98- id : MODx . request . id ,
99- field : fieldName
100- } ,
101- listeners : {
102- success : {
103- fn : ( r ) => {
104- cache . store ( fieldName , r . object . content ) ;
105- Ext . Msg . hide ( ) ;
106- }
107- } ,
108- failure : {
109- fn : function ( ) {
110- Ext . Msg . alert ( "Failed" , "Failed to generated. Please try again." ) ;
111- Ext . Msg . hide ( ) ;
112- } ,
113- scope : this
114- }
115- }
116- } ) ;
117- } ) ;
90+ const createWandEl = ( ) => {
91+ const wandEl = document . createElement ( 'button' ) ;
92+ wandEl . className = 'modai-generate' ;
93+ wandEl . innerText = '🪄'
94+ wandEl . type = 'button'
95+ wandEl . title = 'Generate using AI'
11896
11997 return wandEl ;
12098 }
@@ -183,7 +161,36 @@ Ext.onReady(function() {
183161
184162 wrapper . historyNav = historyNav ;
185163
186- wrapper . appendChild ( createGenerateButton ( field , fieldName ) ) ;
164+ const wandEl = createWandEl ( ) ;
165+ wandEl . addEventListener ( 'click' , ( ) => {
166+ Ext . Msg . wait ( 'Generating ...' , 'Please wait' ) ;
167+
168+ MODx . Ajax . request ( {
169+ url : MODx . config . connector_url ,
170+ params : {
171+ action : 'modAI\\Processors\\Prompt\\Text' ,
172+ id : MODx . request . id ,
173+ field : fieldName
174+ } ,
175+ listeners : {
176+ success : {
177+ fn : ( r ) => {
178+ cache . store ( fieldName , r . object . content ) ;
179+ Ext . Msg . hide ( ) ;
180+ }
181+ } ,
182+ failure : {
183+ fn : function ( ) {
184+ Ext . Msg . alert ( "Failed" , "Failed to generated. Please try again." ) ;
185+ Ext . Msg . hide ( ) ;
186+ } ,
187+ scope : this
188+ }
189+ }
190+ } ) ;
191+ } ) ;
192+
193+ wrapper . appendChild ( wandEl ) ;
187194 wrapper . appendChild ( historyNav ) ;
188195
189196 cache . init ( fieldName , field , wrapper ) ;
@@ -195,7 +202,7 @@ Ext.onReady(function() {
195202 document . querySelectorAll ( '.imageplus-panel-input' ) . forEach ( ( el ) => {
196203 const imagePlus = Ext . getCmp ( el . firstChild . id ) ;
197204
198- const imageWand = createWantEl ( ) ;
205+ const imageWand = createWandEl ( ) ;
199206 imageWand . addEventListener ( 'click' , ( ) => {
200207 const createColumn = MODx . load ( {
201208 xtype : 'modai-window-image_prompt' ,
@@ -215,7 +222,7 @@ Ext.onReady(function() {
215222 createColumn . show ( ) ;
216223 } ) ;
217224
218- const altTextWand = createWantEl ( ) ;
225+ const altTextWand = createWandEl ( ) ;
219226 altTextWand . addEventListener ( 'click' , ( ) => {
220227 const imgElement = imagePlus . imagePreview . el . dom ;
221228
@@ -240,7 +247,7 @@ Ext.onReady(function() {
240247 listeners : {
241248 success : {
242249 fn : ( r ) => {
243- imagePlus . items . items [ 1 ] . items . items [ 1 ] . items . items [ 0 ] . setValue ( r . object . content ) ;
250+ imagePlus . altTextField . items . items [ 0 ] . setValue ( r . object . content ) ;
244251 imagePlus . image . altTag = r . object . content ;
245252 imagePlus . updateValue ( ) ;
246253 Ext . Msg . hide ( ) ;
@@ -257,11 +264,40 @@ Ext.onReady(function() {
257264 } ) ;
258265 } ) ;
259266
260- imagePlus . items . items [ 0 ] . el . dom . appendChild ( imageWand ) ;
261- imagePlus . items . items [ 1 ] . items . items [ 1 ] . el . dom . appendChild ( altTextWand ) ;
267+ imagePlus . altTextField . el . dom . style . display = 'flex' ;
268+ imagePlus . altTextField . el . dom . style . justifyItems = 'center' ;
269+
270+ imagePlus . el . dom . parentElement . parentElement . parentElement . querySelector ( 'label' ) . appendChild ( imageWand ) ;
271+ imagePlus . altTextField . el . dom . appendChild ( altTextWand ) ;
262272 } )
263273 } ;
264274
275+ const attachContent = ( ) => {
276+ const cmp = Ext . getCmp ( 'modx-resource-content' ) ;
277+ const label = cmp . el . dom . querySelector ( 'label' ) ;
278+
279+ const wandEl = createWandEl ( ) ;
280+ wandEl . addEventListener ( 'click' , ( ) => {
281+ const win = MODx . load ( {
282+ xtype : 'modai-window-text_prompt' ,
283+ title : 'Text' ,
284+ cache : freePromptCache . get ( 'modx-resource-content' ) ,
285+ listeners : {
286+ success : {
287+ fn : function ( res ) {
288+ console . log ( res ) ;
289+ } ,
290+ scope :this
291+ }
292+ }
293+ } ) ;
294+
295+ win . show ( ) ;
296+ } ) ;
297+
298+ label . appendChild ( wandEl ) ;
299+ } ;
300+
265301 Ext . defer ( function ( ) {
266302 attach ( 'modx-resource-pagetitle' , 'pagetitle' ) ;
267303
@@ -273,6 +309,8 @@ Ext.onReady(function() {
273309 attach ( 'modx-resource-description' , 'description' ) ;
274310 attach ( 'seosuite-description' , 'description' ) ;
275311
312+ attachContent ( ) ;
313+
276314 attachImagePlus ( ) ;
277315
278316 } , 500 ) ;
0 commit comments