1
1
Ext . onReady ( function ( ) {
2
2
3
- const cache = {
4
- _cache : { } ,
5
- _setFieldValue ( key , value ) {
6
- const cachedItem = this . _cache [ key ] ;
7
-
8
- cachedItem . els . forEach ( ( { wrapper, field} ) => {
9
- const prevValue = field . getValue ( ) ;
10
- field . setValue ( value ) ;
11
- field . fireEvent ( 'change' , field , value , prevValue ) ;
12
-
13
- wrapper . historyNav . info . update ( cachedItem . visible + 1 , cachedItem . values . length ) ;
14
-
15
- if ( cachedItem . visible <= 0 ) {
16
- wrapper . historyNav . prevButton . disable ( ) ;
17
- } else {
18
- wrapper . historyNav . prevButton . enable ( ) ;
19
- }
20
-
21
- if ( cachedItem . visible === cachedItem . values . length - 1 ) {
22
- wrapper . historyNav . nextButton . disable ( ) ;
23
- } else {
24
- wrapper . historyNav . nextButton . enable ( ) ;
25
- }
26
- } ) ;
27
- } ,
28
- init ( key , field , wrapper ) {
29
- if ( ! this . _cache [ key ] ) {
30
- this . _cache [ key ] = {
31
- els : [ { field, wrapper } ] ,
32
- visible : - 1 ,
33
- values : [ ]
34
- } ;
35
- }
36
-
37
- this . _cache [ key ] . els . push ( { field, wrapper } ) ;
38
- const currentValue = field . getValue ( ) ;
39
- if ( currentValue ) {
40
- this . _cache [ key ] . values = [ currentValue ] ;
41
- this . _cache [ key ] . visible = 0 ;
3
+ const historyNavSync = ( data ) => {
4
+ data . context . els . forEach ( ( { wrapper, field} ) => {
5
+ const prevValue = field . getValue ( ) ;
6
+ field . setValue ( data . value ) ;
7
+ field . fireEvent ( 'change' , field , data . value , prevValue ) ;
8
+
9
+ if ( data . total > 0 ) {
10
+ wrapper . historyNav . show ( ) ;
42
11
}
43
- } ,
44
- store ( key , value ) {
45
- const cachedItem = this . _cache [ key ] ;
46
12
47
- cachedItem . visible = cachedItem . values . push ( value ) - 1 ;
13
+ wrapper . historyNav . info . update ( data . current , data . total ) ;
48
14
49
- if ( cachedItem . values . length > 1 ) {
50
- cachedItem . els . forEach ( ( { wrapper } ) => {
51
- wrapper . historyNav . show ( ) ;
52
- } ) ;
15
+ if ( data . prevStatus ) {
16
+ wrapper . historyNav . prevButton . enable ( ) ;
17
+ } else {
18
+ wrapper . historyNav . prevButton . disable ( ) ;
53
19
}
54
20
55
- this . _setFieldValue ( key , cachedItem . values [ cachedItem . visible ] ) ;
56
- } ,
57
- next ( key ) {
58
- const cachedItem = this . _cache [ key ] ;
59
-
60
- if ( cachedItem . visible === cachedItem . values . length - 1 ) {
61
- return ;
62
- }
63
-
64
- this . _setFieldValue ( key , cachedItem . values [ ++ cachedItem . visible ] ) ;
65
- } ,
66
- prev ( key ) {
67
- const cachedItem = this . _cache [ key ] ;
68
-
69
- if ( cachedItem . visible <= 0 ) {
70
- return ;
21
+ if ( data . nextStatus ) {
22
+ wrapper . historyNav . nextButton . enable ( ) ;
23
+ } else {
24
+ wrapper . historyNav . nextButton . disable ( ) ;
71
25
}
72
-
73
- this . _setFieldValue ( key , cachedItem . values [ -- cachedItem . visible ] ) ;
74
- }
75
- } ;
76
- const freePromptCache = {
77
- _cache : { } ,
78
- get ( key ) {
79
- if ( ! this . _cache [ key ] ) {
80
- this . _cache [ key ] = {
81
- visible : - 1 ,
82
- history : [ ]
83
- }
84
- }
85
-
86
- return this . _cache [ key ] ;
87
- }
26
+ } ) ;
88
27
} ;
89
28
90
29
const createWandEl = ( ) => {
@@ -97,7 +36,7 @@ Ext.onReady(function() {
97
36
return wandEl ;
98
37
}
99
38
100
- const createHistoryNav = ( field , fieldName ) => {
39
+ const createHistoryNav = ( cache ) => {
101
40
const prevButton = document . createElement ( 'button' ) ;
102
41
prevButton . type = 'button' ;
103
42
prevButton . title = 'Previous Version' ;
@@ -110,7 +49,7 @@ Ext.onReady(function() {
110
49
}
111
50
prevButton . innerHTML = 'prev' ;
112
51
prevButton . addEventListener ( 'click' , ( ) => {
113
- cache . prev ( fieldName ) ;
52
+ cache . prev ( ) ;
114
53
} ) ;
115
54
116
55
const nextButton = document . createElement ( 'button' ) ;
@@ -125,7 +64,7 @@ Ext.onReady(function() {
125
64
}
126
65
nextButton . innerHTML = 'next' ;
127
66
nextButton . addEventListener ( 'click' , ( ) => {
128
- cache . next ( fieldName ) ;
67
+ cache . next ( ) ;
129
68
} ) ;
130
69
131
70
const info = document . createElement ( 'span' ) ;
@@ -158,14 +97,14 @@ Ext.onReady(function() {
158
97
return wrapper ;
159
98
}
160
99
161
- const createFreeTextPrompt = ( cacheKey , fieldName ) => {
100
+ const createFreeTextPrompt = ( fieldName ) => {
162
101
const wandEl = createWandEl ( ) ;
163
102
wandEl . addEventListener ( 'click' , ( ) => {
164
103
const win = MODx . load ( {
165
104
xtype : 'modai-window-text_prompt' ,
166
105
title : 'Text' ,
167
106
field : fieldName ,
168
- cache : freePromptCache . get ( cacheKey )
107
+ cacheKey : fieldName
169
108
} ) ;
170
109
171
110
win . show ( ) ;
@@ -176,9 +115,6 @@ Ext.onReady(function() {
176
115
177
116
const createForcedTextPrompt = ( field , fieldName ) => {
178
117
const aiWrapper = document . createElement ( 'span' ) ;
179
- const historyNav = createHistoryNav ( field , fieldName ) ;
180
-
181
- aiWrapper . historyNav = historyNav ;
182
118
183
119
const wandEl = createWandEl ( ) ;
184
120
wandEl . addEventListener ( 'click' , ( ) => {
@@ -196,7 +132,8 @@ Ext.onReady(function() {
196
132
success : {
197
133
fn : ( r ) => {
198
134
modAI . serviceExecutor ( r . object ) . then ( ( result ) => {
199
- cache . store ( fieldName , result . content ) ;
135
+ // cache.store(fieldName, result.content);
136
+ cache . insert ( result . content ) ;
200
137
Ext . Msg . hide ( ) ;
201
138
} ) . catch ( ( err ) => {
202
139
Ext . Msg . hide ( ) ;
@@ -216,9 +153,22 @@ Ext.onReady(function() {
216
153
} ) ;
217
154
218
155
aiWrapper . appendChild ( wandEl ) ;
219
- aiWrapper . appendChild ( historyNav ) ;
220
156
221
- cache . init ( fieldName , field , aiWrapper ) ;
157
+ const cache = modAI . history . init (
158
+ fieldName ,
159
+ historyNavSync ,
160
+ field . getValue ( )
161
+ ) ;
162
+
163
+ if ( ! cache . cachedItem . context . els ) {
164
+ cache . cachedItem . context . els = [ ] ;
165
+ }
166
+ cache . cachedItem . context . els . push ( { field, wrapper : aiWrapper } ) ;
167
+
168
+ const historyNav = createHistoryNav ( cache ) ;
169
+
170
+ aiWrapper . appendChild ( historyNav ) ;
171
+ aiWrapper . historyNav = historyNav ;
222
172
223
173
return aiWrapper ;
224
174
}
@@ -229,6 +179,7 @@ Ext.onReady(function() {
229
179
const createColumn = MODx . load ( {
230
180
xtype : 'modai-window-image_prompt' ,
231
181
title : 'Image' ,
182
+ cacheKey : fieldName ,
232
183
record : {
233
184
resource : MODx . request . id ,
234
185
prompt : defaultPrompt ,
@@ -254,9 +205,7 @@ Ext.onReady(function() {
254
205
if ( ! field ) return ;
255
206
256
207
const wrapper = document . createElement ( 'span' ) ;
257
- const historyNav = createHistoryNav ( field , fieldName ) ;
258
208
259
- wrapper . historyNav = historyNav ;
260
209
261
210
const wandEl = createWandEl ( ) ;
262
211
wandEl . addEventListener ( 'click' , ( ) => {
@@ -274,7 +223,7 @@ Ext.onReady(function() {
274
223
success : {
275
224
fn : ( r ) => {
276
225
modAI . serviceExecutor ( r . object ) . then ( ( result ) => {
277
- cache . store ( fieldName , result . content ) ;
226
+ cache . insert ( result . content ) ;
278
227
Ext . Msg . hide ( ) ;
279
228
} ) . catch ( ( err ) => {
280
229
Ext . Msg . hide ( ) ;
@@ -294,9 +243,22 @@ Ext.onReady(function() {
294
243
} ) ;
295
244
296
245
wrapper . appendChild ( wandEl ) ;
297
- wrapper . appendChild ( historyNav ) ;
298
246
299
- cache . init ( fieldName , field , wrapper ) ;
247
+ const cache = modAI . history . init (
248
+ fieldName ,
249
+ historyNavSync ,
250
+ field . getValue ( )
251
+ ) ;
252
+
253
+ if ( ! cache . cachedItem . context . els ) {
254
+ cache . cachedItem . context . els = [ ] ;
255
+ }
256
+ cache . cachedItem . context . els . push ( { field, wrapper} ) ;
257
+
258
+ const historyNav = createHistoryNav ( cache ) ;
259
+
260
+ wrapper . appendChild ( historyNav ) ;
261
+ wrapper . historyNav = historyNav ;
300
262
301
263
field . label . appendChild ( wrapper ) ;
302
264
}
@@ -375,7 +337,7 @@ Ext.onReady(function() {
375
337
const attachContent = ( ) => {
376
338
const cmp = Ext . getCmp ( 'modx-resource-content' ) ;
377
339
const label = cmp . el . dom . querySelector ( 'label' ) ;
378
- label . appendChild ( createFreeTextPrompt ( 'modx-resource-content' , ' res.content') ) ;
340
+ label . appendChild ( createFreeTextPrompt ( 'res.content' ) ) ;
379
341
} ;
380
342
381
343
const attachTVs = ( ) => {
@@ -405,7 +367,7 @@ Ext.onReady(function() {
405
367
if ( prompt ) {
406
368
label . appendChild ( createForcedTextPrompt ( field , fieldName ) ) ;
407
369
} else {
408
- label . appendChild ( createFreeTextPrompt ( `tv ${ tvId } ` , fieldName ) ) ;
370
+ label . appendChild ( createFreeTextPrompt ( fieldName ) ) ;
409
371
}
410
372
}
411
373
0 commit comments