Merge pull request #3266 from d-ber/character_import_speedup Character import speedup by delaying tag import

d78577c46da72b100cd758a04a53a1d331eecd99

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +58 -17Ignore whitespace
public/script.js+58 -17
@@ -7305,7 +7305,7 @@ export function select_rm_info(type, charId, previousCharId = null) {
7305 // Set a timeout so multiple flashes don't overlap7305 // Set a timeout so multiple flashes don't overlap
7306 clearTimeout(importFlashTimeout);7306 clearTimeout(importFlashTimeout);
7307 importFlashTimeout = setTimeout(function () {7307 importFlashTimeout = setTimeout(function () {
7308 if (type === 'char_import' || type === 'char_create') {7308 if (type === 'char_import' || type === 'char_create' || type === 'char_import_no_toast') {
7309 // Find the page at which the character is located7309 // Find the page at which the character is located
7310 const avatarFileName = charId;7310 const avatarFileName = charId;
7311 const charData = getEntitiesList({ doFilter: true });7311 const charData = getEntitiesList({ doFilter: true });
@@ -8857,24 +8857,61 @@ export async function processDroppedFiles(files, data = new Map()) {
8857 'charx',8857 'charx',
8858 ];8858 ];
88598859
8860 const avatarFileNames = [];
8860 for (const file of files) {8861 for (const file of files) {
8861 const extension = file.name.split('.').pop().toLowerCase();8862 const extension = file.name.split('.').pop().toLowerCase();
8862 if (allowedMimeTypes.some(x => file.type.startsWith(x)) || allowedExtensions.includes(extension)) {8863 if (allowedMimeTypes.some(x => file.type.startsWith(x)) || allowedExtensions.includes(extension)) {
8863 const preservedName = data instanceof Map && data.get(file);8864 const preservedName = data instanceof Map && data.get(file);
8864 await importCharacter(file, preservedName);8865 const avatarFileName = await importCharacter(file, { preserveFileName: preservedName });
8866 if (avatarFileName !== undefined) {
8867 avatarFileNames.push(avatarFileName);
8868 }
8865 } else {8869 } else {
8866 toastr.warning(t`Unsupported file type: ` + file.name);8870 toastr.warning(t`Unsupported file type: ` + file.name);
8867 }8871 }
8868 }8872 }
8873
8874 if (avatarFileNames.length > 0) {
8875 await importCharactersTags(avatarFileNames);
8876 selectImportedChar(avatarFileNames[avatarFileNames.length - 1]);
8877 }
8878}
8879
8880/**
8881 * Imports tags for the given characters
8882 * @param {string[]} avatarFileNames character avatar filenames whose tags are to import
8883 */
8884async function importCharactersTags(avatarFileNames) {
8885 await getCharacters();
8886 for (let i = 0; i < avatarFileNames.length; i++) {
8887 if (power_user.tag_import_setting !== tag_import_setting.NONE) {
8888 const importedCharacter = characters.find(character => character.avatar === avatarFileNames[i]);
8889 await importTags(importedCharacter);
8890 }
8891 }
8892}
8893
8894/**
8895 * Selects the given imported char
8896 * @param {string} charId char to select
8897 */
8898function selectImportedChar(charId) {
8899 let oldSelectedChar = null;
8900 if (this_chid !== undefined) {
8901 oldSelectedChar = characters[this_chid].avatar;
8902 }
8903 select_rm_info('char_import_no_toast', charId, oldSelectedChar);
8869}8904}
88708905
8871/**8906/**
8872 * Imports a character from a file.8907 * Imports a character from a file.
8873 * @param {File} file File to import8908 * @param {File} file File to import
8874 * @param {string?} preserveFileName Whether to preserve original file name8909 * @param {object} [options] - Options
8875 * @returns {Promise<void>}8910 * @param {string} [options.preserveFileName] Whether to preserve original file name
8911 * @param {Boolean} [options.importTags=false] Whether to import tags
8912 * @returns {Promise<string>}
8876 */8913 */
8877async function importCharacter(file, preserveFileName = '') {8914async function importCharacter(file, { preserveFileName = '', importTags = false } = {}) {
8878 if (is_group_generating || is_send_press) {8915 if (is_group_generating || is_send_press) {
8879 toastr.error(t`Cannot import characters while generating. Stop the request and try again.`, t`Import aborted`);8916 toastr.error(t`Cannot import characters while generating. Stop the request and try again.`, t`Import aborted`);
8880 throw new Error('Cannot import character while generating');8917 throw new Error('Cannot import character while generating');
@@ -8910,19 +8947,14 @@ async function importCharacter(file, preserveFileName = '') {
8910 if (data.file_name !== undefined) {8947 if (data.file_name !== undefined) {
8911 $('#character_search_bar').val('').trigger('input');8948 $('#character_search_bar').val('').trigger('input');
89128949
8913 let oldSelectedChar = null;8950 toastr.success(t`Character Created: ${String(data.file_name).replace('.png', '')}`);
8914 if (this_chid !== undefined) {8951 let avatarFileName = `${data.file_name}.png`;
8915 oldSelectedChar = characters[this_chid].avatar;8952 if (importTags) {
8916 }8953 await importCharactersTags([avatarFileName]);
89178954
8918 await getCharacters();8955 selectImportedChar(data.file_name);
8919 select_rm_info('char_import', data.file_name, oldSelectedChar);
8920 if (power_user.tag_import_setting !== tag_import_setting.NONE) {
8921 let currentContext = getContext();
8922 let avatarFileName = `${data.file_name}.png`;
8923 let importedCharacter = currentContext.characters.find(character => character.avatar === avatarFileName);
8924 await importTags(importedCharacter);
8925 }8956 }
8957 return avatarFileName;
8926 }8958 }
8927}8959}
89288960
@@ -10801,8 +10833,17 @@ jQuery(async function () {
10801 return;10833 return;
10802 }10834 }
1080310835
10836 const avatarFileNames = [];
10804 for (const file of e.target.files) {10837 for (const file of e.target.files) {
10805 await importCharacter(file);10838 const avatarFileName = await importCharacter(file);
10839 if (avatarFileName !== undefined) {
10840 avatarFileNames.push(avatarFileName);
10841 }
10842 }
10843
10844 if (avatarFileNames.length > 0) {
10845 await importCharactersTags(avatarFileNames);
10846 selectImportedChar(avatarFileNames[avatarFileNames.length - 1]);
10806 }10847 }
10807 });10848 });
1080810849