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 -17Showing whitespace changes
public/script.js+58 -17
@@ -7305,7 +7305,7 @@ export function select_rm_info(type, charId, previousCharId = null) {
73057305 // Set a timeout so multiple flashes don't overlap
73067306 clearTimeout(importFlashTimeout);
73077307 importFlashTimeout = setTimeout(function () {
73087308 if (type === 'char_import' || type === 'char_create' || type === 'char_import_no_toast') {
73097309 // Find the page at which the character is located
73107310 const avatarFileName = charId;
73117311 const charData = getEntitiesList({ doFilter: true });
@@ -8857,24 +8857,61 @@ export async function processDroppedFiles(files, data = new Map()) {
88578857 'charx',
88588858 ];
88598859
8860+ const avatarFileNames = [];
88608861 for (const file of files) {
88618862 const extension = file.name.split('.').pop().toLowerCase();
88628863 if (allowedMimeTypes.some(x => file.type.startsWith(x)) || allowedExtensions.includes(extension)) {
88638864 const preservedName = data instanceof Map && data.get(file);
88648865 const avatarFileName = await importCharacter(file, { preserveFileName: preservedName });
8866+ if (avatarFileName !== undefined) {
8867+ avatarFileNames.push(avatarFileName);
8868+ }
88658869 } else {
88668870 toastr.warning(t`Unsupported file type: ` + file.name);
88678871 }
88688872 }
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+ */
8884+async 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+ */
8898+function 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);
88698904}
88708905
88718906/**
88728907 * Imports a character from a file.
88738908 * @param {File} file File to import
88748909 * @param {string?object} preserveFileName Whether to preserve original[options] file- nameOptions
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>}
88768913 */
88778914async function importCharacter(file, { preserveFileName = '', importTags = false } = {}) {
88788915 if (is_group_generating || is_send_press) {
88798916 toastr.error(t`Cannot import characters while generating. Stop the request and try again.`, t`Import aborted`);
88808917 throw new Error('Cannot import character while generating');
@@ -8910,19 +8947,14 @@ async function importCharacter(file, preserveFileName = '') {
89108947 if (data.file_name !== undefined) {
89118948 $('#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) {
8915- oldSelectedChar = characters[this_chid].avatar;
8916- }
8917-
8918- await getCharacters();
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();
89228951 let avatarFileName = `${data.file_name}.png`;
8923- let importedCharacter = currentContext.characters.find(character => character.avatar === avatarFileName);
8952+ if (importTags) {
89248953 await importTagsimportCharactersTags(importedCharacter[avatarFileName]);
8954+
8955+ selectImportedChar(data.file_name);
89258956 }
8957+ return avatarFileName;
89268958 }
89278959}
89288960
@@ -10801,8 +10833,17 @@ jQuery(async function () {
1080110833 return;
1080210834 }
1080310835
10836+ const avatarFileNames = [];
1080410837 for (const file of e.target.files) {
1080510838 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]);
1080610847 }
1080710848 });
1080810849