Merge pull request #3266 from d-ber/character_import_speedup Character import speedup by delaying tag import
Signed| @@ -7305,7 +7305,7 @@ export function select_rm_info(type, charId, previousCharId = null) { | ||
| 7305 | 7305 | // Set a timeout so multiple flashes don't overlap |
| 7306 | 7306 | clearTimeout(importFlashTimeout); |
| 7307 | 7307 | importFlashTimeout = setTimeout(function () { |
| 7308 | 7308 | if (type === 'char_import' || type === 'char_create' || type === 'char_import_no_toast') { |
| 7309 | 7309 | // Find the page at which the character is located |
| 7310 | 7310 | const avatarFileName = charId; |
| 7311 | 7311 | const charData = getEntitiesList({ doFilter: true }); |
| @@ -8857,24 +8857,61 @@ export async function processDroppedFiles(files, data = new Map()) { | ||
| 8857 | 8857 | 'charx', |
| 8858 | 8858 | ]; |
| 8859 | 8859 | |
| 8860 | + const avatarFileNames = []; | |
| 8860 | 8861 | for (const file of files) { |
| 8861 | 8862 | const extension = file.name.split('.').pop().toLowerCase(); |
| 8862 | 8863 | if (allowedMimeTypes.some(x => file.type.startsWith(x)) || allowedExtensions.includes(extension)) { |
| 8863 | 8864 | const preservedName = data instanceof Map && data.get(file); |
| 8864 | 8865 | const avatarFileName = await importCharacter(file, { preserveFileName: preservedName }); |
| 8866 | + if (avatarFileName !== undefined) { | |
| 8867 | + avatarFileNames.push(avatarFileName); | |
| 8868 | + } | |
| 8865 | 8869 | } else { |
| 8866 | 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 | + */ | |
| 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); | |
| 8869 | 8904 | } |
| 8870 | 8905 | |
| 8871 | 8906 | /** |
| 8872 | 8907 | * Imports a character from a file. |
| 8873 | 8908 | * @param {File} file File to import |
| 8874 | 8909 | * @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>} | |
| 8876 | 8913 | */ |
| 8877 | 8914 | async function importCharacter(file, { preserveFileName = '', importTags = false } = {}) { |
| 8878 | 8915 | if (is_group_generating || is_send_press) { |
| 8879 | 8916 | toastr.error(t`Cannot import characters while generating. Stop the request and try again.`, t`Import aborted`); |
| 8880 | 8917 | throw new Error('Cannot import character while generating'); |
| @@ -8910,19 +8947,14 @@ async function importCharacter(file, preserveFileName = '') { | ||
| 8910 | 8947 | if (data.file_name !== undefined) { |
| 8911 | 8948 | $('#character_search_bar').val('').trigger('input'); |
| 8912 | 8949 | |
| 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]); | |
| 8917 | 8954 | |
| 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 | } |
| 8928 | 8960 | |
| @@ -10801,8 +10833,17 @@ jQuery(async function () { | ||
| 10801 | 10833 | return; |
| 10802 | 10834 | } |
| 10803 | 10835 | |
| 10836 | + const avatarFileNames = []; | |
| 10804 | 10837 | for (const file of e.target.files) { |
| 10805 | 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 | }); |
| 10808 | 10849 | |