Remove all colored tags on selected items if 0 is pressed

Like Thunderbird
This commit is contained in:
Dan Stillman 2019-10-26 16:44:16 -04:00
parent e64273fb93
commit c25fbe7c1c
3 changed files with 56 additions and 1 deletions

View file

@ -757,6 +757,32 @@ Zotero.Tags = new function() {
};
/**
* @param {Zotero.Item[]}
* @return {Promise}
*/
this.removeColoredTagsFromItems = async function (items) {
return Zotero.DB.executeTransaction(async function () {
for (let item of items) {
let colors = this.getColors(item.libraryID);
let tags = item.getTags();
let changed = false;
for (let tag of tags) {
if (colors.has(tag.tag)) {
item.removeTag(tag.tag);
changed = true;
}
}
if (changed) {
await item.save({
skipDateModifiedUpdate: true
});
}
}
}.bind(this));
};
/**
* A tree cell can show only one image, and (as of Fx19) it can't be an SVG,
* so we need to generate a composite image containing the existing item type

View file

@ -148,7 +148,7 @@ Zotero.ItemTreeView.prototype.setTree = async function (treebox) {
// Add a keypress listener for expand/collapse
var tree = this._getTreeElement();
var self = this;
var coloredTagsRE = new RegExp("^[1-" + Zotero.Tags.MAX_COLORED_TAGS + "]{1}$");
var coloredTagsRE = new RegExp("^[0-" + Zotero.Tags.MAX_COLORED_TAGS + "]{1}$");
var listener = function(event) {
if (self._skipKeyPress) {
self._skipKeyPress = false;
@ -199,6 +199,11 @@ Zotero.ItemTreeView.prototype.setTree = async function (treebox) {
if (coloredTagsRE.test(key)) {
let libraryID = self.collectionTreeRow.ref.libraryID;
let position = parseInt(key) - 1;
// When 0 is pressed, remove all colored tags
if (position == -1) {
let items = self.getSelectedItems();
return Zotero.Tags.removeColoredTagsFromItems(items);
}
let colorData = Zotero.Tags.getColorByPosition(libraryID, position);
// If a color isn't assigned to this number or any
// other numbers, allow key navigation

View file

@ -198,4 +198,28 @@ describe("Zotero.Tags", function () {
assert.isNull(o);
});
});
describe("#removeColoredTagsFromItems()", function () {
it("shouldn't remove regular tags", async function () {
var libraryID = Zotero.Libraries.userLibraryID;
var item = await createDataObject('item', {
tags: [
{ tag: 'A' },
{ tag: 'B', type: 1 },
{ tag: 'C' },
{ tag: 'D', type: 1 }
]
});
await Zotero.Tags.setColor(libraryID, 'C', '#111111', 0);
await Zotero.Tags.setColor(libraryID, 'D', '#222222', 1);
await Zotero.Tags.removeColoredTagsFromItems([item]);
assert.sameDeepMembers(item.getTags(), [
{ tag: 'A' },
{ tag: 'B', type: 1 }
]);
});
});
})