Add button to bulk import existing tags

d3ce9ea0d5d7238d0c9c2532d5319fddacf5c4d4

d-ber <davide.bertalero@studenti.unimi.it>

2 files changed, +23 -5Ignore whitespace
public/scripts/BulkEditOverlay.js+16 -1
@@ -200,7 +200,7 @@ class BulkTagPopupHandler {
200200 <div id="bulk_tag_popup">
201201 <div id="bulk_tag_popup_holder">
202202 <h3 class="marginBot5">Modify tags of ${this.characterIds.length} characters</h3>
203203 <small class="bulk_tags_desc m-b-1">Add or remove the mutual tags of all selected characters. Import all or existing tags for all selected characters.</small>
204204 <div id="bulk_tags_avatars_block" class="avatars_inline avatars_inline_small tags tags_inline"></div>
205205 <br>
206206 <div id="bulk_tags_div" class="marginBot5" data-characters='${characterData}'>
@@ -222,6 +222,9 @@ class BulkTagPopupHandler {
222222 <div id="bulk_tag_popup_import_all_tags" class="menu_button" title="Import all tags from selected characters" data-i18n="[title]Import all tags from selected characters">
223223 Import All
224224 </div>
225+ <div id="bulk_tag_popup_import_existing_tags" class="menu_button" title="Import existing tags from selected characters" data-i18n="[title]Import existing tags from selected characters">
226+ Import Existing
227+ </div>
225228 <div id="bulk_tag_popup_cancel" class="menu_button" data-i18n="Cancel">Close</div>
226229 </div>
227230 </div>
@@ -258,6 +261,18 @@ class BulkTagPopupHandler {
258261 document.querySelector('#bulk_tag_popup_remove_mutual').addEventListener('click', this.removeMutual.bind(this));
259262 document.querySelector('#bulk_tag_popup_cancel').addEventListener('click', this.hide.bind(this));
260263 document.querySelector('#bulk_tag_popup_import_all_tags').addEventListener('click', this.importAllTags.bind(this));
264+ document.querySelector('#bulk_tag_popup_import_existing_tags').addEventListener('click', this.importExistingTags.bind(this));
265+ }
266+
267+ /**
268+ * Import existing tags for all selected characters
269+ */
270+ async importExistingTags() {
271+ for (const characterId of this.characterIds) {
272+ await importTags(characters[characterId], { importExisting: true });
273+ }
274+
275+ $('#bulkTagList').empty();
261276 }
262277
263278 /**
public/scripts/tags.js+7 -4
@@ -709,12 +709,13 @@ const ANTI_TROLL_MAX_TAGS = 15;
709709 * @param {Character} character - The character
710710 * @param {object} [options] - Options
711711 * @param {boolean} [options.importAll=false] - Whether to import all tags without dialog
712+ * @param {boolean} [options.importExisting=false] - Whether to import existing tags without dialog
712713 * @param {boolean} [options.forceShow=false] - Whether to force showing the import dialog
713714 * @returns {Promise<boolean>} Boolean indicating whether any tag was imported
714715 */
715716async function importTags(character, { importAll = false, importExisting = false, forceShow = false } = {}) {
716717 // Gather the tags to import based on the selected setting
717718 const tagNamesToImport = await handleTagImport(character, { importAll, importExisting, forceShow });
718719 if (!tagNamesToImport?.length) {
719720 console.debug('No tags to import');
720721 return;
@@ -734,10 +735,11 @@ async function importTags(character, { importAll = false, forceShow = false } =
734735 * @param {Character} character - The character
735736 * @param {object} [options] - Options
736737 * @param {boolean} [options.importAll=false] - Whether to import all tags without dialog
738+ * @param {boolean} [options.importExisting=false] - Whether to import existing tags without dialog
737739 * @param {boolean} [options.forceShow=false] - Whether to force showing the import dialog
738740 * @returns {Promise<string[]>} Array of strings representing the tags to import
739741 */
740742async function handleTagImport(character, { importAll = false, importExisting = false, forceShow = false } = {}) {
741743 /** @type {string[]} */
742744 const importTags = character.tags.map(t => t.trim()).filter(t => t)
743745 .filter(t => !IMPORT_EXLCUDED_TAGS.includes(t))
@@ -750,7 +752,8 @@ async function handleTagImport(character, { importAll = false, forceShow = false
750752 // Choose the setting for this dialog. If from settings, verify the setting really exists, otherwise take "ASK".
751753 const setting = forceShow ? tag_import_setting.ASK
752754 : importAll ? tag_import_setting.ALL
753- : Object.values(tag_import_setting).find(setting => setting === power_user.tag_import_setting) ?? tag_import_setting.ASK;
755+ : importExisting ? tag_import_setting.ONLY_EXISTING
756+ : Object.values(tag_import_setting).find(setting => setting === power_user.tag_import_setting) ?? tag_import_setting.ASK;
754757
755758 switch (setting) {
756759 case tag_import_setting.ALL: