From d3ce9ea0d5d7238d0c9c2532d5319fddacf5c4d4 Mon Sep 17 00:00:00 2001 From: d-ber Date: Thu, 15 Aug 2024 15:47:04 +0200 Subject: [PATCH] Add button to bulk import existing tags --- public/scripts/BulkEditOverlay.js | 17 ++++++++++++++++- public/scripts/tags.js | 11 +++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/public/scripts/BulkEditOverlay.js b/public/scripts/BulkEditOverlay.js index a70cfe782..b10a59f26 100644 --- a/public/scripts/BulkEditOverlay.js +++ b/public/scripts/BulkEditOverlay.js @@ -200,7 +200,7 @@ class BulkTagPopupHandler {

Modify tags of ${this.characterIds.length} characters

- Add or remove the mutual tags of all selected characters. + Add or remove the mutual tags of all selected characters. Import all or existing tags for all selected characters.

@@ -222,6 +222,9 @@ class BulkTagPopupHandler { +
@@ -258,6 +261,18 @@ class BulkTagPopupHandler { document.querySelector('#bulk_tag_popup_remove_mutual').addEventListener('click', this.removeMutual.bind(this)); document.querySelector('#bulk_tag_popup_cancel').addEventListener('click', this.hide.bind(this)); document.querySelector('#bulk_tag_popup_import_all_tags').addEventListener('click', this.importAllTags.bind(this)); + document.querySelector('#bulk_tag_popup_import_existing_tags').addEventListener('click', this.importExistingTags.bind(this)); + } + + /** + * Import existing tags for all selected characters + */ + async importExistingTags() { + for (const characterId of this.characterIds) { + await importTags(characters[characterId], { importExisting: true }); + } + + $('#bulkTagList').empty(); } /** diff --git a/public/scripts/tags.js b/public/scripts/tags.js index d563b00bc..b97d19fdd 100644 --- a/public/scripts/tags.js +++ b/public/scripts/tags.js @@ -709,12 +709,13 @@ const ANTI_TROLL_MAX_TAGS = 15; * @param {Character} character - The character * @param {object} [options] - Options * @param {boolean} [options.importAll=false] - Whether to import all tags without dialog + * @param {boolean} [options.importExisting=false] - Whether to import existing tags without dialog * @param {boolean} [options.forceShow=false] - Whether to force showing the import dialog * @returns {Promise} Boolean indicating whether any tag was imported */ -async function importTags(character, { importAll = false, forceShow = false } = {}) { +async function importTags(character, { importAll = false, importExisting = false, forceShow = false } = {}) { // Gather the tags to import based on the selected setting - const tagNamesToImport = await handleTagImport(character, { importAll, forceShow }); + const tagNamesToImport = await handleTagImport(character, { importAll, importExisting, forceShow }); if (!tagNamesToImport?.length) { console.debug('No tags to import'); return; @@ -734,10 +735,11 @@ async function importTags(character, { importAll = false, forceShow = false } = * @param {Character} character - The character * @param {object} [options] - Options * @param {boolean} [options.importAll=false] - Whether to import all tags without dialog + * @param {boolean} [options.importExisting=false] - Whether to import existing tags without dialog * @param {boolean} [options.forceShow=false] - Whether to force showing the import dialog * @returns {Promise} Array of strings representing the tags to import */ -async function handleTagImport(character, { importAll = false, forceShow = false } = {}) { +async function handleTagImport(character, { importAll = false, importExisting = false, forceShow = false } = {}) { /** @type {string[]} */ const importTags = character.tags.map(t => t.trim()).filter(t => t) .filter(t => !IMPORT_EXLCUDED_TAGS.includes(t)) @@ -750,7 +752,8 @@ async function handleTagImport(character, { importAll = false, forceShow = false // Choose the setting for this dialog. If from settings, verify the setting really exists, otherwise take "ASK". const setting = forceShow ? tag_import_setting.ASK : importAll ? tag_import_setting.ALL - : Object.values(tag_import_setting).find(setting => setting === power_user.tag_import_setting) ?? tag_import_setting.ASK; + : importExisting ? tag_import_setting.ONLY_EXISTING + : Object.values(tag_import_setting).find(setting => setting === power_user.tag_import_setting) ?? tag_import_setting.ASK; switch (setting) { case tag_import_setting.ALL: