mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Copy tags on duplicating
This commit is contained in:
@ -172,6 +172,7 @@ import {
|
|||||||
importTags,
|
importTags,
|
||||||
tag_filter_types,
|
tag_filter_types,
|
||||||
compareTagsForSort,
|
compareTagsForSort,
|
||||||
|
initTags,
|
||||||
} from './scripts/tags.js';
|
} from './scripts/tags.js';
|
||||||
import {
|
import {
|
||||||
SECRET_KEYS,
|
SECRET_KEYS,
|
||||||
@ -412,6 +413,7 @@ export const event_types = {
|
|||||||
CHARACTER_FIRST_MESSAGE_SELECTED: 'character_first_message_selected',
|
CHARACTER_FIRST_MESSAGE_SELECTED: 'character_first_message_selected',
|
||||||
// TODO: Naming convention is inconsistent with other events
|
// TODO: Naming convention is inconsistent with other events
|
||||||
CHARACTER_DELETED: 'characterDeleted',
|
CHARACTER_DELETED: 'characterDeleted',
|
||||||
|
CHARACTER_DUPLICATED: 'character_duplicated',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const eventSource = new EventEmitter();
|
export const eventSource = new EventEmitter();
|
||||||
@ -864,6 +866,7 @@ async function firstLoadInit() {
|
|||||||
getSystemMessages();
|
getSystemMessages();
|
||||||
sendSystemMessage(system_message_types.WELCOME);
|
sendSystemMessage(system_message_types.WELCOME);
|
||||||
initLocales();
|
initLocales();
|
||||||
|
initTags();
|
||||||
await getUserAvatars(true, user_avatar);
|
await getUserAvatars(true, user_avatar);
|
||||||
await getCharacters();
|
await getCharacters();
|
||||||
await getBackgrounds();
|
await getBackgrounds();
|
||||||
@ -4323,6 +4326,8 @@ async function DupeChar() {
|
|||||||
});
|
});
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
toastr.success('Character Duplicated');
|
toastr.success('Character Duplicated');
|
||||||
|
const data = await response.json();
|
||||||
|
await eventSource.emit(event_types.CHARACTER_DUPLICATED, { oldAvatar: body.avatar_url, newAvatar: data.path });
|
||||||
getCharacters();
|
getCharacters();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,16 +48,24 @@ class CharacterContextMenu {
|
|||||||
* Duplicate one or more characters
|
* Duplicate one or more characters
|
||||||
*
|
*
|
||||||
* @param characterId
|
* @param characterId
|
||||||
* @returns {Promise<Response>}
|
* @returns {Promise<any>}
|
||||||
*/
|
*/
|
||||||
static duplicate = async (characterId) => {
|
static duplicate = async (characterId) => {
|
||||||
const character = CharacterContextMenu.#getCharacter(characterId);
|
const character = CharacterContextMenu.#getCharacter(characterId);
|
||||||
|
const body = { avatar_url: character.avatar };
|
||||||
|
|
||||||
return fetch('/api/characters/duplicate', {
|
const result = await fetch('/api/characters/duplicate', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: getRequestHeaders(),
|
headers: getRequestHeaders(),
|
||||||
body: JSON.stringify({ avatar_url: character.avatar }),
|
body: JSON.stringify(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
throw new Error('Character not duplicated');
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await result.json();
|
||||||
|
await eventSource.emit(event_types.CHARACTER_DUPLICATED, { oldAvatar: body.avatar_url, newAvatar: data.path });
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,6 +8,8 @@ import {
|
|||||||
entitiesFilter,
|
entitiesFilter,
|
||||||
printCharacters,
|
printCharacters,
|
||||||
buildAvatarList,
|
buildAvatarList,
|
||||||
|
eventSource,
|
||||||
|
event_types,
|
||||||
} from '../script.js';
|
} from '../script.js';
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
import { FILTER_TYPES, FILTER_STATES, isFilterState, FilterHelper } from './filters.js';
|
import { FILTER_TYPES, FILTER_STATES, isFilterState, FilterHelper } from './filters.js';
|
||||||
@ -132,7 +134,7 @@ function filterByTagState(entities, { globalDisplayFilters = false, subForEntity
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (subForEntity !== undefined && subForEntity.type === 'tag') {
|
if (subForEntity !== undefined && subForEntity.type === 'tag') {
|
||||||
entities = filterTagSubEntities(subForEntity.item, entities, { filterHidden : filterHidden });
|
entities = filterTagSubEntities(subForEntity.item, entities, { filterHidden: filterHidden });
|
||||||
}
|
}
|
||||||
|
|
||||||
return entities;
|
return entities;
|
||||||
@ -1140,7 +1142,7 @@ function onClearAllFiltersClick() {
|
|||||||
// We have to manually go through the elements and unfilter by clicking...
|
// We have to manually go through the elements and unfilter by clicking...
|
||||||
// Thankfully nearly all filter controls are three-state-toggles
|
// Thankfully nearly all filter controls are three-state-toggles
|
||||||
const filterTags = $('.rm_tag_controls .rm_tag_filter').find('.tag');
|
const filterTags = $('.rm_tag_controls .rm_tag_filter').find('.tag');
|
||||||
for(const tag of filterTags) {
|
for (const tag of filterTags) {
|
||||||
const toggleState = $(tag).attr('data-toggle-state');
|
const toggleState = $(tag).attr('data-toggle-state');
|
||||||
if (toggleState !== undefined && !isFilterState(toggleState ?? FILTER_STATES.UNDEFINED, FILTER_STATES.UNDEFINED)) {
|
if (toggleState !== undefined && !isFilterState(toggleState ?? FILTER_STATES.UNDEFINED, FILTER_STATES.UNDEFINED)) {
|
||||||
toggleTagThreeState($(tag), { stateOverride: FILTER_STATES.UNDEFINED, simulateClick: true });
|
toggleTagThreeState($(tag), { stateOverride: FILTER_STATES.UNDEFINED, simulateClick: true });
|
||||||
@ -1151,7 +1153,17 @@ function onClearAllFiltersClick() {
|
|||||||
$('#character_search_bar').val('').trigger('input');
|
$('#character_search_bar').val('').trigger('input');
|
||||||
}
|
}
|
||||||
|
|
||||||
jQuery(() => {
|
/**
|
||||||
|
* Copy tags from one character to another.
|
||||||
|
* @param {{oldAvatar: string, newAvatar: string}} data Event data
|
||||||
|
*/
|
||||||
|
function copyTags(data) {
|
||||||
|
const prevTagMap = tag_map[data.oldAvatar] || [];
|
||||||
|
const newTagMap = tag_map[data.newAvatar] || [];
|
||||||
|
tag_map[data.newAvatar] = Array.from(new Set([...prevTagMap, ...newTagMap]));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function initTags() {
|
||||||
createTagInput('#tagInput', '#tagList');
|
createTagInput('#tagInput', '#tagList');
|
||||||
createTagInput('#groupTagInput', '#groupTagList');
|
createTagInput('#groupTagInput', '#groupTagList');
|
||||||
|
|
||||||
@ -1168,5 +1180,5 @@ jQuery(() => {
|
|||||||
$(document).on('click', '.tag_view_create', onTagCreateClick);
|
$(document).on('click', '.tag_view_create', onTagCreateClick);
|
||||||
$(document).on('click', '.tag_view_backup', onTagsBackupClick);
|
$(document).on('click', '.tag_view_backup', onTagsBackupClick);
|
||||||
$(document).on('click', '.tag_view_restore', onBackupRestoreClick);
|
$(document).on('click', '.tag_view_restore', onBackupRestoreClick);
|
||||||
});
|
eventSource.on(event_types.CHARACTER_DUPLICATED, copyTags);
|
||||||
|
}
|
||||||
|
@ -1008,7 +1008,7 @@ router.post('/duplicate', jsonParser, async function (request, response) {
|
|||||||
|
|
||||||
fs.copyFileSync(filename, newFilename);
|
fs.copyFileSync(filename, newFilename);
|
||||||
console.log(`${filename} was copied to ${newFilename}`);
|
console.log(`${filename} was copied to ${newFilename}`);
|
||||||
response.sendStatus(200);
|
response.send({ path: path.parse(newFilename).base });
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
Reference in New Issue
Block a user