@@ -1227,11 +1227,17 @@ function setOptions() {
1227
1227
}
1228
1228
let prompts_options = '' ;
1229
1229
let prompt_id = 0 ;
1230
+ let by_user = '' ;
1230
1231
if ( typeof ( all_prompts ) !== "undefined" ) {
1231
1232
prompts_options += '<select name="prompt"><option selected="selected" disabled="disabled">Awesome Prompts</option>' ;
1232
1233
all_prompts . forEach ( the_prompt => {
1234
+ if ( the_prompt . by_user ) {
1235
+ by_user = `class="by_user" data-created_time="${ the_prompt . created_time } "` ;
1236
+ } else {
1237
+ by_user = '' ;
1238
+ }
1233
1239
let prompt_text = the_prompt . prompt . replace ( / " / g, '"' ) ;
1234
- prompts_options += `<option id="prompt_id${ prompt_id } " value="${ prompt_text } ">${ the_prompt . act } </option>` ;
1240
+ prompts_options += `<option ${ by_user } id="prompt_id${ prompt_id } " value="${ prompt_text } ">${ the_prompt . act } </option>` ;
1235
1241
prompt_id ++ ;
1236
1242
} ) ;
1237
1243
prompts_options += '</select>' ;
@@ -1270,8 +1276,9 @@ function setOptions() {
1270
1276
<button onclick="saveModel()" class="save_model">Save Model</button></div><hr>
1271
1277
<div><strong>System Prompt</strong>
1272
1278
${ prompts_options }
1279
+ <input id="prompt_name" type="text" name="prompt_name" placeholder="Name your prompt">
1273
1280
<textarea class="system_prompt" placeholder="(Optional) How the AI should respond?">${ system_prompt } </textarea>
1274
- <button onclick="savePrompt()" class="save_prompt">Save Prompt</button><br>
1281
+ <button onclick="savePrompt()" class="save_prompt">Save Prompt</button> <button id="delete_prompt">Delete Prompt</button> <br>
1275
1282
${ platform_info }
1276
1283
<span>${ add_new_models } </span>
1277
1284
<span>${ plugin_option } </span>
@@ -1283,6 +1290,28 @@ function setOptions() {
1283
1290
1284
1291
setTimeout ( ( ) => {
1285
1292
1293
+ let sys_prompt = document . querySelector ( "textarea.system_prompt" ) ;
1294
+ let last_prompt = sys_prompt . value . trim ( ) ;
1295
+ let prompt_name = document . querySelector ( "#prompt_name" ) ;
1296
+ if ( sys_prompt ) {
1297
+ sys_prompt . onkeyup = ( ) => {
1298
+ let current_prompt = sys_prompt . value . trim ( ) ;
1299
+ if ( current_prompt !== last_prompt ) {
1300
+ prompt_name . style . display = 'inline-block' ;
1301
+ prompt_name . setAttribute ( "required" , "true" ) ;
1302
+ let del_prompt = document . querySelector ( "#delete_prompt" ) ;
1303
+ if ( del_prompt ) {
1304
+ del_prompt . style . display = 'none' ;
1305
+ }
1306
+ } else {
1307
+ prompt_name . style . display = 'none' ;
1308
+ prompt_name . value = '' ;
1309
+ prompt_name . setAttribute ( "required" , "false" ) ;
1310
+
1311
+ }
1312
+ }
1313
+ }
1314
+
1286
1315
let sl_platform = document . querySelector ( "select[name=platform]" ) ;
1287
1316
if ( sl_platform ) {
1288
1317
sl_platform . onchange = ( ) => {
@@ -1296,14 +1325,31 @@ function setOptions() {
1296
1325
let sl_prompt = document . querySelector ( "select[name=prompt]" ) ;
1297
1326
if ( sl_prompt ) {
1298
1327
sl_prompt . onchange = ( item => {
1328
+ let selectedOption = sl_prompt . options [ sl_prompt . selectedIndex ] ;
1329
+ let delete_prompt_bnt = document . querySelector ( "#delete_prompt" ) ;
1330
+ if ( selectedOption . getAttribute ( 'data-created_time' ) ) {
1331
+ if ( delete_prompt_bnt ) {
1332
+ delete_prompt_bnt . style . display = 'inline-block' ;
1333
+ delete_prompt_bnt . onclick = ( ) => {
1334
+ deletePrompt ( ) ;
1335
+ }
1336
+ }
1337
+ } else {
1338
+ delete_prompt_bnt . style . display = 'none' ;
1339
+ }
1340
+ prompt_name . style . display = 'none' ;
1341
+ prompt_name . value = '' ;
1342
+ prompt_name . setAttribute ( "required" , "false" ) ;
1299
1343
let btn_sp = document . querySelector ( '.save_prompt' ) ;
1300
1344
if ( btn_sp ) {
1301
1345
btn_sp . classList . add ( 'animate' ) ;
1302
1346
}
1303
1347
let txt_area = document . querySelector ( "textarea.system_prompt" ) ;
1304
1348
if ( txt_area ) {
1305
1349
txt_area . value = item . target . value ;
1350
+ last_prompt = item . target . value ;
1306
1351
}
1352
+
1307
1353
} )
1308
1354
}
1309
1355
} , 500 )
@@ -1583,19 +1629,37 @@ function orderTopics() {
1583
1629
1584
1630
}
1585
1631
1586
- function savePrompt ( ) {
1632
+ function savePrompt ( close_dialog = true ) {
1587
1633
let btn_sp = document . querySelector ( '.save_prompt' ) ;
1588
1634
if ( btn_sp ) {
1589
1635
btn_sp . classList . remove ( 'animate' ) ;
1590
1636
}
1591
1637
let sys_prompt = document . querySelector ( "textarea.system_prompt" ) . value . trim ( ) ;
1592
1638
if ( sys_prompt . length ) {
1639
+ let prompt_name = document . querySelector ( "#prompt_name" ) ;
1640
+ if ( prompt_name && prompt_name . value . trim ( ) . length > 0 ) {
1641
+ // new prompt add by the user
1642
+ let user_prompts = localStorage . getItem ( 'user_new_prompts' ) ;
1643
+ if ( user_prompts ) {
1644
+ user_prompts = JSON . parse ( user_prompts ) ;
1645
+ } else {
1646
+ user_prompts = [ ] ;
1647
+ }
1648
+ let current_time = Date . now ( ) ;
1649
+ let u_new_prompt = { act : prompt_name . value , prompt : sys_prompt , by_user : true , created_time : current_time } ;
1650
+ user_prompts . unshift ( u_new_prompt ) ;
1651
+ all_prompts . unshift ( u_new_prompt ) ;
1652
+ localStorage . setItem ( 'user_new_prompts' , JSON . stringify ( user_prompts ) ) ;
1653
+ }
1593
1654
localStorage . setItem ( 'system_prompt' , sys_prompt ) ;
1594
1655
} else {
1595
1656
localStorage . removeItem ( 'system_prompt' )
1596
1657
}
1658
+
1597
1659
saveModel ( ) ;
1598
- closeDialogs ( ) ;
1660
+ if ( close_dialog ) {
1661
+ closeDialogs ( ) ;
1662
+ }
1599
1663
}
1600
1664
1601
1665
function saveModel ( ) {
@@ -2941,15 +3005,15 @@ function unlockScroll() {
2941
3005
}
2942
3006
if ( event . key === "ArrowDown" ) {
2943
3007
if ( chat_msg . scrollTop <= last_position ) {
2944
- chat_msg . scrollTop += 35 ;
3008
+ chat_msg . scrollTop += 30 ;
2945
3009
//console.log('forcing scroll down')
2946
3010
} else {
2947
3011
// console.log('all fine: down')
2948
3012
}
2949
3013
last_position = chat_msg . scrollTop ;
2950
3014
} else if ( event . key === "ArrowUp" ) {
2951
3015
if ( chat_msg . scrollTop >= last_position ) {
2952
- chat_msg . scrollTop -= 35 ;
3016
+ chat_msg . scrollTop -= 30 ;
2953
3017
//console.log('forcing scroll up')
2954
3018
} else {
2955
3019
//console.log('all fine: up')
@@ -3069,6 +3133,61 @@ document.addEventListener('keydown', function (e) {
3069
3133
3070
3134
} ) ;
3071
3135
3136
+ function loadUserAddedPrompts ( ) {
3137
+ let u_prompt = localStorage . getItem ( 'user_new_prompts' ) ;
3138
+ if ( u_prompt ) {
3139
+ try {
3140
+ u_prompt = JSON . parse ( u_prompt ) ;
3141
+ u_prompt . forEach ( new_prompt => {
3142
+ all_prompts . unshift ( new_prompt )
3143
+
3144
+ } )
3145
+ } catch ( e ) {
3146
+ console . error ( e )
3147
+ }
3148
+ }
3149
+ }
3150
+ loadUserAddedPrompts ( )
3151
+
3152
+
3153
+ function deletePrompt ( ) {
3154
+ let sl_prompt = document . querySelector ( "select[name=prompt]" ) ;
3155
+ let selectedOption = sl_prompt . options [ sl_prompt . selectedIndex ] ;
3156
+ if ( selectedOption ) {
3157
+ let created_time = selectedOption . getAttribute ( 'data-created_time' ) ;
3158
+ if ( created_time ) {
3159
+ created_time = parseInt ( created_time ) ;
3160
+ let all_user_prompt = localStorage . getItem ( 'user_new_prompts' ) ;
3161
+ if ( all_user_prompt ) {
3162
+ all_user_prompt = JSON . parse ( all_user_prompt ) ;
3163
+ for ( let i = 0 ; i < all_user_prompt . length ; i ++ ) {
3164
+ if ( all_user_prompt [ i ] . created_time === created_time ) {
3165
+ all_user_prompt . splice ( i , 1 ) ;
3166
+ break ;
3167
+ }
3168
+ }
3169
+ localStorage . setItem ( 'user_new_prompts' , JSON . stringify ( all_user_prompt ) ) ;
3170
+
3171
+ // update all_prompts removing the prompt deleted
3172
+ for ( let i = 0 ; i < all_prompts . length ; i ++ ) {
3173
+ if ( all_prompts [ i ] . created_time === created_time ) {
3174
+ all_prompts . splice ( i , 1 ) ;
3175
+ break ;
3176
+ }
3177
+ }
3178
+
3179
+
3180
+
3181
+ selectedOption . remove ( ) ;
3182
+ console . log ( selectedOption )
3183
+ document . querySelector ( "textarea.system_prompt" ) . value = '' ;
3184
+ savePrompt ( ) ;
3185
+ }
3186
+
3187
+ }
3188
+ }
3189
+
3190
+ }
3072
3191
3073
3192
let new_url = document . URL ;
3074
3193
new_url = new_url . split ( '?' ) [ 0 ] ;
0 commit comments