Changes to colored tag toggling via keyboard

- Update Date Modified when toggling tags via keyboard
  (https://forums.zotero.org/discussion/90371/bug-toggling-a-colored-tag-with-number-keys-doesnt-update-date-modified)
- If any selected items don't have the tag, add it to all items, instead
  of deciding based on the first item
  (https://forums.zotero.org/discussion/79900/minor-issue-related-to-colored-tags)
This commit is contained in:
Dan Stillman 2021-06-22 00:58:47 -04:00
parent 42b4b173f3
commit 91ebbc1bc9
2 changed files with 18 additions and 20 deletions

View file

@ -3955,6 +3955,7 @@ Zotero.Item.prototype.setTags = function (tags) {
*
* @param {String} name
* @param {Number} [type=0]
* @return {Boolean} - True if the tag was added; false if the item already had the tag
*/
Zotero.Item.prototype.addTag = function (name, type) {
type = type ? parseInt(type) : 0;
@ -4023,6 +4024,9 @@ Zotero.Item.prototype.replaceTag = function (oldTag, newTag) {
* Remove a tag from the item
*
* A separate save() is required to update the database.
*
* @param {String} tagName
* @return {Boolean} - True if the tag was removed; false if the item didn't have the tag
*/
Zotero.Item.prototype.removeTag = function(tagName) {
this._requireData('tags');
@ -4030,9 +4034,10 @@ Zotero.Item.prototype.removeTag = function(tagName) {
var newTags = oldTags.filter(tagData => tagData.tag !== tagName);
if (newTags.length == oldTags.length) {
Zotero.debug('Cannot remove missing tag ' + tagName + ' from item ' + this.libraryKey);
return;
return false;
}
this.setTags(newTags);
return true;
}

View file

@ -736,29 +736,22 @@ Zotero.Tags = new function() {
// Color setting can exist without tag. If missing, we have to add the tag.
var tagID = this.getID(tagName);
return Zotero.DB.executeTransaction(function* () {
// Base our action on the first item. If it has the tag,
// remove the tag from all items. If it doesn't, add it to all.
var firstItem = items[0];
// Remove from all items
if (tagID && firstItem.hasTag(tagName)) {
for (let i=0; i<items.length; i++) {
let item = items[i];
item.removeTag(tagName);
yield item.save({
skipDateModifiedUpdate: true
});
return Zotero.DB.executeTransaction(async function () {
// If all items already have the tag, remove it from all items
if (tagID && items.every(x => x.hasTag(tagName))) {
for (let item of items) {
if (item.removeTag(tagName)) {
await item.save();
}
}
Zotero.Prefs.set('purge.tags', true);
}
// Add to all items
// Otherwise add to all items
else {
for (let i=0; i<items.length; i++) {
let item = items[i];
item.addTag(tagName);
yield item.save({
skipDateModifiedUpdate: true
});
for (let item of items) {
if (item.addTag(tagName)) {
await item.save();
}
}
}
}.bind(this));