Update merged/replaced tag map links

- On both skip and replace, tag map could run into issues with old IDs being referenced. Fixed that by building a mapping for the actual ID that will be the result.
This commit is contained in:
Wolfsblvt 2024-07-29 00:05:35 +02:00
parent 105f54ac72
commit 1738e8a48d
1 changed files with 15 additions and 3 deletions

View File

@ -1456,6 +1456,8 @@ async function onTagRestoreFileSelect(e) {
} }
const warnings = []; const warnings = [];
/** @type {Map<string, string>} Map import tag ids with existing ids on overwrite */
const idToActualTagIdMap = new Map();
// Import tags // Import tags
for (const tag of data.tags) { for (const tag of data.tags) {
@ -1473,12 +1475,18 @@ async function onTagRestoreFileSelect(e) {
existingTag = getTag(tag.name); existingTag = getTag(tag.name);
if (existingTag && !overwrite) { if (existingTag && !overwrite) {
warnings.push(`Tag with name '${tag.name}' already exists.`); warnings.push(`Tag with name '${tag.name}' already exists.`);
// Remember the tag id, so we can still import the tag map entries for this
idToActualTagIdMap.set(tag.id, existingTag.id);
continue; continue;
} }
if (existingTag) { if (existingTag) {
// On overwrite, we remove and re-add the tag // On overwrite, we remove and re-add the tag
removeFromArray(tags, existingTag); removeFromArray(tags, existingTag);
// And remember the ID if it was different, so we can update the tag map accordingly
if (existingTag.id !== tag.id) {
idToActualTagIdMap.set(existingTag.id, tag.id);
}
} }
tags.push(tag); tags.push(tag);
@ -1504,10 +1512,14 @@ async function onTagRestoreFileSelect(e) {
// Get existing tag ids for this key or empty array. // Get existing tag ids for this key or empty array.
const existingTagIds = tag_map[key] || []; const existingTagIds = tag_map[key] || [];
// Merge existing and new tag ids. Remove duplicates.
tag_map[key] = existingTagIds.concat(tagIds).filter(onlyUnique); // Merge existing and new tag ids. Replace the ones mapped to a new id. Remove duplicates.
const combinedTags = existingTagIds.concat(tagIds)
.map(tagId => (idToActualTagIdMap.has(tagId)) ? idToActualTagIdMap.get(tagId) : tagId)
.filter(onlyUnique);
// Verify that all tags exist. Remove tags that don't exist. // Verify that all tags exist. Remove tags that don't exist.
tag_map[key] = tag_map[key].filter(x => tags.some(y => String(y.id) === String(x))); tag_map[key] = combinedTags.filter(tagId => tags.some(y => String(y.id) === String(tagId)));
} }
if (warnings.length) { if (warnings.length) {