Some more locator input and locking improvements when citations are resorted

See #1776
This commit is contained in:
Adomas Venčkauskas 2020-01-16 11:40:18 +02:00
parent c70c07bb2e
commit 75465e9096

View file

@ -39,6 +39,7 @@ var Zotero_QuickFormat = new function () {
panel, panelPrefix, panelSuffix, panelSuppressAuthor, panelLocatorLabel, panelLocator,
panelLibraryLink, panelInfo, panelRefersToBubble, panelFrameHeight = 0, accepted = false;
var locatorLocked = false;
var locatorNode = null;
var _searchPromise;
const SEARCH_TIMEOUT = 250;
@ -240,15 +241,14 @@ var Zotero_QuickFormat = new function () {
if(!locatorLocked && numRe.test(str)) {
// add to previous cite
var node = _getCurrentEditorTextNode();
var prevNode = node.previousSibling;
let citationItem = JSON.parse(prevNode && prevNode.dataset.citationItem || "null");
let citationItem = JSON.parse(locatorNode && locatorNode.dataset.citationItem || "null");
if (citationItem) {
if (!("locator" in citationItem)) {
citationItem.locator = "";
}
citationItem.locator += str;
prevNode.dataset.citationItem = JSON.stringify(citationItem);
prevNode.textContent = _buildBubbleString(citationItem);
locatorNode.dataset.citationItem = JSON.stringify(citationItem);
locatorNode.textContent = _buildBubbleString(citationItem);
node.nodeValue = "";
_clearEntryList();
return;
@ -271,6 +271,7 @@ var Zotero_QuickFormat = new function () {
node.nodeValue = "";
_clearEntryList();
locatorLocked = false;
locatorNode = prevNode;
return;
}
}
@ -787,7 +788,11 @@ var Zotero_QuickFormat = new function () {
// get next node and clear this one
var node = _getCurrentEditorTextNode();
node.nodeValue = "";
var bubble = _insertBubble(citationItem, node);
// We are setting a locator node here, but below 2 calls reset
// the bubble list for sorting, so we do some additional
// handling to maintain the correct locator node in
// _showCitation()
var bubble = locatorNode = _insertBubble(citationItem, node);
_clearEntryList();
yield _previewAndSort();
_refocusQfe();
@ -923,11 +928,17 @@ var Zotero_QuickFormat = new function () {
&& io.citation.sortedItems
&& io.citation.sortedItems.length) {
for(var i=0, n=io.citation.sortedItems.length; i<n; i++) {
_insertBubble(io.citation.sortedItems[i][1], insertBefore);
const bubble = _insertBubble(io.citation.sortedItems[i][1], insertBefore);
if (locatorNode && bubble.textContent == locatorNode.textContent) {
locatorNode = bubble;
}
}
} else {
for(var i=0, n=io.citation.citationItems.length; i<n; i++) {
_insertBubble(io.citation.citationItems[i], insertBefore);
const bubble = _insertBubble(io.citation.citationItems[i], insertBefore);
if (locatorNode && bubble.textContent == locatorNode.textContent) {
locatorNode = bubble;
}
}
}
}
@ -1184,6 +1195,7 @@ var Zotero_QuickFormat = new function () {
_resize();
_resetSearchTimer();
} else if(keyCode === event.DOM_VK_LEFT || keyCode === event.DOM_VK_RIGHT) {
locatorLocked = true;
var right = keyCode === event.DOM_VK_RIGHT,
bubble = _getSelectedBubble(right);
if(bubble) {
@ -1198,6 +1210,7 @@ var Zotero_QuickFormat = new function () {
selection.addRange(nodeRange);
}
} else if (["Home", "End"].includes(event.key)) {
locatorLocked = true;
setTimeout(() => {
right = event.key == "End";
bubble = _getSelectedBubble(right);
@ -1214,6 +1227,7 @@ var Zotero_QuickFormat = new function () {
}
})
} else if(keyCode === event.DOM_VK_UP && referencePanel.state === "open") {
locatorLocked = true;
var selectedItem = referenceBox.selectedItem;
var previousSibling;
@ -1236,6 +1250,7 @@ var Zotero_QuickFormat = new function () {
};
event.preventDefault();
} else if(keyCode === event.DOM_VK_DOWN) {
locatorLocked = true;
if((Zotero.isMac ? event.metaKey : event.ctrlKey)) {
// If meta key is held down, show the citation properties panel
var bubble = _getSelectedBubble();
@ -1369,6 +1384,7 @@ var Zotero_QuickFormat = new function () {
delete citationItem["suppress-author"];
}
locatorLocked = "locator" in citationItem;
locatorNode = panelRefersToBubble;
panelRefersToBubble.dataset.citationItem = JSON.stringify(citationItem);
panelRefersToBubble.textContent = _buildBubbleString(citationItem);
};