Refactor source, add confirm

This commit is contained in:
Cohee 2024-03-03 20:29:34 +02:00
parent 2e002032b3
commit c4cae9d00f

View File

@ -1380,6 +1380,22 @@ export async function getOneCharacter(avatarUrl) {
} }
} }
function getCharacterSource(chId = this_chid) {
const character = characters[chId];
if (!character) {
return '';
}
const chubId = characters[this_chid]?.data?.extensions?.chub?.full_path;
if (chubId) {
return `https://chub.ai/characters/${chubId}`;
}
return '';
}
async function getCharacters() { async function getCharacters() {
var response = await fetch('/api/characters/all', { var response = await fetch('/api/characters/all', {
method: 'POST', method: 'POST',
@ -6481,13 +6497,6 @@ export function select_selected_character(chid) {
setWorldInfoButtonClass(chid); setWorldInfoButtonClass(chid);
checkEmbeddedWorld(chid); checkEmbeddedWorld(chid);
// Disable or enable Link to Source dropdown menu item depending on the presence of 'full_path' property
$('#character_source').prop( "disabled", true );
const chidSourceData = Object.values(characters[this_chid].data.extensions)
Object.entries(chidSourceData).forEach(function([key, value]) {
if (Object.keys(value).includes('full_path')) $('#character_source').prop( "disabled", false );
});
$('#form_create').attr('actiontype', 'editcharacter'); $('#form_create').attr('actiontype', 'editcharacter');
$('.form_create_bottom_buttons_block .chat_lorebook_button').show(); $('.form_create_bottom_buttons_block .chat_lorebook_button').show();
saveSettingsDebounced(); saveSettingsDebounced();
@ -9816,22 +9825,19 @@ jQuery(async function () {
await importEmbeddedWorldInfo(); await importEmbeddedWorldInfo();
saveCharacterDebounced(); saveCharacterDebounced();
break; break;
case 'character_source': case 'character_source': {
const chidSourceData = characters[this_chid].data.extensions const source = getCharacterSource(this_chid);
switch(true) { if (source && isValidUrl(source)) {
case Object.hasOwn(chidSourceData, 'chub') : const url = new URL(source);
window.open('https://chub.ai/characters/' + chidSourceData.chub.full_path, '_blank'); const confirm = await callPopup(`Open ${url.hostname} in a new tab?`, 'confirm');
break; if (confirm) {
case Object.hasOwn(chidSourceData, 'janitorai') : // not implemented yet window.open(source, '_blank');
window.open('https://janitorai.com/characters/' + chidSourceData.janitorai.full_path, '_blank'); }
break; } else {
case Object.hasOwn(chidSourceData, 'pygmalion') : // not implemented yet toastr.info('This character doesn\'t seem to have a source.');
window.open('https://pygmalion.chat/character/' + chidSourceData.pygmalion.full_path, '_blank');
break;
default: toastr.info("This character doesn't seem to have a source.");
} }
break; } break;
/*case 'delete_button': /*case 'delete_button':
popup_type = "del_ch"; popup_type = "del_ch";
callPopup(` callPopup(`