Skip to content

Commit

Permalink
Fix feed item read shortcut, and delay 1 second before marking as read
Browse files Browse the repository at this point in the history
This allows keyboard navigation without marking all items in between as
read.
  • Loading branch information
dstillman committed Nov 3, 2017
1 parent c40e321 commit a3e711b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
22 changes: 5 additions & 17 deletions chrome/content/zotero/itemPane.js
Expand Up @@ -321,30 +321,18 @@ var ZoteroItemPane = new function() {
ZoteroItemPane.setTranslateButton();
};


this.setToggleReadLabel = function() {
var markRead = false;
var items = ZoteroPane_Local.itemsView.getSelectedItems();
for (let item of items) {
if (!item.isRead) {
markRead = true;
break;
}
}

this.setReadLabel = function (isRead) {
var elem = document.getElementById('zotero-feed-item-toggleRead-button');
if (markRead) {
var label = Zotero.getString('pane.item.markAsRead');
} else {
label = Zotero.getString('pane.item.markAsUnread');
}
var label = Zotero.getString('pane.item.' + (isRead ? 'markAsUnread' : 'markAsRead'));
elem.setAttribute('label', label);

var key = Zotero.Keys.getKeyForCommand('toggleRead');
var tooltip = label + (Zotero.rtl ? ' \u202B' : ' ') + '(' + key + ')'
elem.setAttribute('tooltiptext', tooltip);
};


function _updateNoteCount() {
var c = _notesList.childNodes.length;

Expand Down
3 changes: 2 additions & 1 deletion chrome/content/zotero/xpcom/itemTreeView.js
Expand Up @@ -185,6 +185,7 @@ Zotero.ItemTreeView.prototype.setTree = async function (treebox) {
}

event.preventDefault();
event.stopPropagation();

Zotero.spawn(function* () {
if (coloredTagsRE.test(key)) {
Expand Down Expand Up @@ -711,7 +712,7 @@ Zotero.ItemTreeView.prototype.notify = Zotero.Promise.coroutine(function* (actio
}

else if (collectionTreeRow.isFeed()) {
this._ownerDocument.defaultView.ZoteroItemPane.setToggleReadLabel();
this._ownerDocument.defaultView.ZoteroPane.updateReadLabel();
}

// If no quicksearch, process modifications manually
Expand Down
81 changes: 50 additions & 31 deletions chrome/content/zotero/zoteroPane.js
Expand Up @@ -619,6 +619,11 @@ var ZoteroPane = new function()
let row = this.collectionsView.getRow(this.collectionsView.selection.currentIndex);
if (!row || !row.isFeed()) return;
this.toggleSelectedItemsRead();
if (itemReadPromise) {
itemReadPromise.cancel();
itemReadPromise = null;
}
return;
}
}

Expand Down Expand Up @@ -1503,17 +1508,15 @@ var ZoteroPane = new function()
// if (!item.isTranslated) {
// item.translate();
// }
item.isRead = true;
ZoteroItemPane.setToggleReadLabel();
yield item.saveTx();
// this.startItemReadTimeout(item.id);
this.updateReadLabel();
this.startItemReadTimeout(item.id);
}
}
}
// Zero or multiple items selected
else {
if (collectionTreeRow.isFeed()) {
ZoteroItemPane.setToggleReadLabel();
this.updateReadLabel();
}

let count = selectedItems.length;
Expand Down Expand Up @@ -4610,41 +4613,57 @@ var ZoteroPane = new function()
});


let itemReadTimeout;
this.startItemReadTimeout = function(feedItemID) {
if (itemReadTimeout) {
itemReadTimeout.cancel();
itemReadTimeout = null;
this.updateReadLabel = function () {
var items = this.getSelectedItems();
var isUnread = false;
for (let item of items) {
if (!item.isRead) {
isUnread = true;
break;
}
}
ZoteroItemPane.setReadLabel(!isUnread);
};


var itemReadPromise;
this.startItemReadTimeout = function (feedItemID) {
if (itemReadPromise) {
itemReadPromise.cancel();
}

let feedItem;
itemReadTimeout = Zotero.FeedItems.getAsync(feedItemID)
.then(function(newFeedItem) {
if (!newFeedItem) {
const FEED_READ_TIMEOUT = 1000;

itemReadPromise = Zotero.Promise.delay(FEED_READ_TIMEOUT)
.then(async function () {
itemReadPromise = null;

// Check to make sure we're still on the same item
var items = this.getSelectedItems();
if (items.length != 1 || items[0].id != feedItemID) {
Zotero.debug(items.length);
Zotero.debug(items[0].id);
Zotero.debug(feedItemID);

return;
}
var feedItem = items[0];
if (!(feedItem instanceof Zotero.FeedItem)) {
throw new Zotero.Promise.CancellationError('Not a FeedItem');
} else if(newFeedItem.isRead) {
throw new Zotero.Promise.CancellationError('FeedItem already read.');
}
feedItem = newFeedItem;
})
.delay(3000)
.then(() => {
itemReadTimeout = null;
// Check to make sure we're still on the same item
if (this.itemsView.selection.count !== 1) return;

let row = this.itemsView.getRow(this.itemsView.selection.currentIndex);
if (!row || !row.ref || !row.ref.id == feedItemID) return;
if (feedItem.isRead) {
return;
}

return feedItem.toggleRead(true);
})
.catch(function(e) {
await feedItem.toggleRead(true);
ZoteroItemPane.setReadLabel(true);
}.bind(this))
.catch(function (e) {
if (e instanceof Zotero.Promise.CancellationError) {
Zotero.debug(e.message);
return;
}

Zotero.debug(e, 1);
Zotero.logError(e);
});
}

Expand Down

0 comments on commit a3e711b

Please sign in to comment.