Fix tag hide/show toggle in Tag Management not persisting (#5226)
* Initial plan * Fix tag hide/show in Tag Management: pass original tag refs instead of copies, fix inverted tooltip Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert unrelated package-lock.json changes Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert tooltip change: tooltip shows current state, not action after click Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Pre-calculate tag counts into a Map and pass to sortTags for sort-by-usage Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert unintended package-lock.json changes from npm install Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Use instanceof Map for counts checks, add missing JSDoc param for sortTags Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert unintended package-lock.json changes Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Remove legacy `count` field * Improve readability (marginally) --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com>
This commit is contained in:
+12
-11
@@ -326,7 +326,6 @@ const TAG_FOLDER_DEFAULT_TYPE = 'NONE';
|
||||
* @property {string} [folder_type] - The bogus folder type of this tag (based on `TAG_FOLDER_TYPES`)
|
||||
* @property {string} [filter_state] - The saved state of the filter chosen of this tag (based on `FILTER_STATES`)
|
||||
* @property {number} [sort_order] - A custom integer representing the sort order if tags are sorted
|
||||
* @property {number} [count] - The number of entities that have this tag assigned
|
||||
* @property {string} [color] - The background color of the tag
|
||||
* @property {string} [color2] - The foreground color of the tag
|
||||
* @property {number} [create_date] - A number representing the date when this tag was created
|
||||
@@ -1764,10 +1763,11 @@ function makeTagListDraggable(tagContainer) {
|
||||
* Sorts the given tags, returning a shallow copy of it
|
||||
*
|
||||
* @param {Tag[]} tags - The tags
|
||||
* @param {Map<string, number>} [counts=null] - Optional map of tag ID to usage count
|
||||
* @returns {Tag[]} The sorted tags
|
||||
*/
|
||||
function sortTags(tags) {
|
||||
return tags.slice().sort(compareTagsForSort);
|
||||
function sortTags(tags, counts = null) {
|
||||
return tags.slice().sort((a, b) => compareTagsForSort(a, b, counts));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1775,15 +1775,18 @@ function sortTags(tags) {
|
||||
*
|
||||
* @param {Tag} a - First tag
|
||||
* @param {Tag} b - Second tag
|
||||
* @param {Map<string, number>} [counts=null] - Optional map of tag ID to usage count
|
||||
* @returns {number} The compare result
|
||||
*/
|
||||
function compareTagsForSort(a, b) {
|
||||
function compareTagsForSort(a, b, counts = null) {
|
||||
// default sort: alphabetical, case insensitive
|
||||
const defaultSort = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
|
||||
|
||||
// sort on number of entries
|
||||
if (power_user.tag_sort_mode === tag_sort_mode.BY_ENTRIES) {
|
||||
return ((b.count || 0) - (a.count || 0)) || defaultSort;
|
||||
const aCount = counts instanceof Map ? (counts.get(a.id) || 0) : 0;
|
||||
const bCount = counts instanceof Map ? (counts.get(b.id) || 0) : 0;
|
||||
return (bCount - aCount) || defaultSort;
|
||||
}
|
||||
|
||||
// alphabetical sort
|
||||
@@ -2273,13 +2276,11 @@ function copyTags(data) {
|
||||
function printViewTagList(tagContainer, empty = true) {
|
||||
if (empty) tagContainer.empty();
|
||||
const everything = Object.values(tag_map).flat();
|
||||
const tagsWithCounts = tags.map(tag => {
|
||||
const count = everything.filter(x => x === tag.id).length;
|
||||
return { ...tag, count: count };
|
||||
});
|
||||
const sortedTags = sortTags(tagsWithCounts);
|
||||
const counts = new Map(tags.map(tag => [tag.id, everything.filter(x => x === tag.id).length]));
|
||||
const sortedTags = sortTags(tags, counts);
|
||||
for (const tag of sortedTags) {
|
||||
appendViewTagToList(tagContainer, tag, tag.count);
|
||||
const count = counts.get(tag.id) || 0;
|
||||
appendViewTagToList(tagContainer, tag, count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user