Fix some more citation update bugs

This commit is contained in:
Adomas Venčkauskas 2018-02-06 15:09:20 +02:00
parent edc18a4fe4
commit 09ceaa953b

View file

@ -686,7 +686,7 @@ Zotero.Integration.Interface.prototype.setDocPrefs = Zotero.Promise.coroutine(fu
fieldNoteTypes.push(this._session.data.prefs.noteType); fieldNoteTypes.push(this._session.data.prefs.noteType);
} }
} else if(convertBibliographies } else if(convertBibliographies
&& type === INTEGRATION_TYPE_BIBLIOGRAPHY) { && field.type === INTEGRATION_TYPE_BIBLIOGRAPHY) {
fieldsToConvert.push(fields[i]); fieldsToConvert.push(fields[i]);
fieldNoteTypes.push(0); fieldNoteTypes.push(0);
} }
@ -1516,10 +1516,17 @@ Zotero.Integration.Session.prototype.addCitation = Zotero.Promise.coroutine(func
Zotero.Integration.Session.prototype.getCiteprocLists = function() { Zotero.Integration.Session.prototype.getCiteprocLists = function() {
var citations = []; var citations = [];
for(let i in this.citationsByIndex) { var fieldToCitationIdxMapping = [];
citations.push([this.citationsByIndex[i].citationID, this.citationsByIndex[i].properties.noteIndex]); var citationToFieldIdxMapping = {};
var i = 0;
// This relies on the order of citationsByIndex keys being stable and sorted by insertion order
// Which it seems to currently be true for every modern JS engine, so we're probably fine
for(let idx in this.citationsByIndex) {
citations.push([this.citationsByIndex[idx].citationID, this.citationsByIndex[idx].properties.noteIndex]);
fieldToCitationIdxMapping[i] = idx;
citationToFieldIdxMapping[idx] = i++;
} }
return citations; return [citations, fieldToCitationIdxMapping, citationToFieldIdxMapping];
} }
/** /**
@ -1531,7 +1538,7 @@ Zotero.Integration.Session.prototype._updateCitations = async function () {
Zotero.debug("Integration: Indices of updated citations"); Zotero.debug("Integration: Indices of updated citations");
Zotero.debug(Object.keys(this.updateIndices)); Zotero.debug(Object.keys(this.updateIndices));
var citations = this.getCiteprocLists(); var [citations, fieldToCitationIdxMapping, citationToFieldIdxMapping] = this.getCiteprocLists();
for (let indexList of [this.newIndices, this.updateIndices]) { for (let indexList of [this.newIndices, this.updateIndices]) {
for (let index in indexList) { for (let index in indexList) {
@ -1543,12 +1550,12 @@ Zotero.Integration.Session.prototype._updateCitations = async function () {
if (!citation) continue; if (!citation) continue;
citation = citation.toJSON(); citation = citation.toJSON();
let citationsPre = citations.slice(0, index); let citationsPre = citations.slice(0, citationToFieldIdxMapping[index]);
var citationsPost; var citationsPost;
if (index in this.newIndices) { if (index in this.newIndices) {
citationsPost = []; citationsPost = [];
} else { } else {
citationsPost = citations.slice(index+1); citationsPost = citations.slice(citationToFieldIdxMapping[index]+1);
} }
Zotero.debug("Integration: style.processCitationCluster("+citation.toSource()+", "+citationsPre.toSource()+", "+citationsPost.toSource()); Zotero.debug("Integration: style.processCitationCluster("+citation.toSource()+", "+citationsPre.toSource()+", "+citationsPost.toSource());
@ -1557,7 +1564,7 @@ Zotero.Integration.Session.prototype._updateCitations = async function () {
this.bibliographyHasChanged |= info.bibchange; this.bibliographyHasChanged |= info.bibchange;
for (let citationInfo of newCitations) { for (let citationInfo of newCitations) {
let idx = citationInfo[0], text = citationInfo[1]; let idx = fieldToCitationIdxMapping[citationInfo[0]], text = citationInfo[1];
this.updateIndices[idx] = true; this.updateIndices[idx] = true;
this.citationsByIndex[idx].text = text; this.citationsByIndex[idx].text = text;
} }