diff --git a/chrome/content/zotero/xpcom/cite.js b/chrome/content/zotero/xpcom/cite.js index 0745c4375..12b016bba 100644 --- a/chrome/content/zotero/xpcom/cite.js +++ b/chrome/content/zotero/xpcom/cite.js @@ -291,9 +291,10 @@ Zotero.Cite = { * Get an item by ID, either by retrieving it from the library or looking for the document it * belongs to. * @param {String|Number|Array} id + * @param {Boolean} [getZoteroItems=false] - whether to get CSL or Zotero items for embedded items * @return {Zotero.Item} item */ - "getItem":function getItem(id) { + "getItem":function getItem(id, getZoteroItems=false) { var slashIndex; if(id instanceof Array) { @@ -303,7 +304,11 @@ Zotero.Cite = { session = Zotero.Integration.sessions[sessionID], item; if(session) { - item = session.embeddedItems[id.substr(slashIndex+1)]; + if (getZoteroItems) { + item = session.embeddedZoteroItems[id.substr(slashIndex+1)]; + } else { + item = session.embeddedItems[id.substr(slashIndex+1)]; + } } if(!item) { @@ -498,7 +503,7 @@ Zotero.Cite.System.prototype = { /** * citeproc-js system function for getting items * See http://gsl-nagoya-u.net/http/pub/citeproc-doc.html#retrieveitem - * @param {String|Integer} Item ID, or string item for embedded citations + * @param {String|Integer} item - Item ID, or string item for embedded citations * @return {Object} citeproc-js item */ "retrieveItem":function retrieveItem(item) { @@ -509,10 +514,10 @@ Zotero.Cite.System.prototype = { } else if(typeof item === "string" && (slashIndex = item.indexOf("/")) !== -1) { // is an embedded item var sessionID = item.substr(0, slashIndex); - var session = Zotero.Integration.sessions[sessionID] + var session = Zotero.Integration.sessions[sessionID]; if(session) { var embeddedCitation = session.embeddedItems[item.substr(slashIndex+1)]; - if(embeddedCitation) { + if (embeddedCitation) { embeddedCitation.id = item; return embeddedCitation; } diff --git a/chrome/content/zotero/xpcom/integration.js b/chrome/content/zotero/xpcom/integration.js index 1b11322ff..85dc07062 100644 --- a/chrome/content/zotero/xpcom/integration.js +++ b/chrome/content/zotero/xpcom/integration.js @@ -888,7 +888,7 @@ Zotero.Integration.Fields.prototype._processFields = Zotero.Promise.coroutine(fu let field = Zotero.Integration.Field.loadExisting(this._fields[i]); if (field.type === INTEGRATION_TYPE_ITEM) { var noteIndex = field.getNoteIndex(), - citation = new Zotero.Integration.Citation(field); + citation = new Zotero.Integration.Citation(field, noteIndex); let action = yield citation.loadItemData(); @@ -1130,35 +1130,31 @@ Zotero.Integration.Fields.prototype.addEditCitation = Zotero.Promise.coroutine(f // ------------------- // Preparing stuff to pass into CitationEditInterface - var fieldIndexPromise = this.get().then(function(fields) { - for (var i=0, n=fields.length; i= fieldIndex){ - if(indexB < fieldIndex) return 1; + if(indexA >= this._fieldIndex){ + if(indexB < this._fieldIndex) return 1; return indexA - indexB; } - if(indexB > fieldIndex) return -1; + if(indexB > this._fieldIndex) return -1; return indexB - indexA; }); @@ -1286,6 +1279,7 @@ Zotero.Integration.CitationEditInterface.prototype = { */ Zotero.Integration.Session = function(doc, app) { this.embeddedItems = {}; + this.embeddedZoteroItems = {}; this.embeddedItemsByURI = {}; this.resetRequest(doc); this.primaryFieldType = app.primaryFieldType; @@ -1646,7 +1640,7 @@ Zotero.Integration.Session.prototype.writeDelayedCitation = Zotero.Promise.corou Zotero.Integration.Session.prototype.getItems = function() { - return Zotero.Cite.getItem(Object.keys(this.citationsByItemID)); + return Zotero.Cite.getItem(Object.keys(this.citationsByItemID), true); } @@ -1979,7 +1973,7 @@ Zotero.Integration.URIMap.prototype.getZoteroItemForURIs = Zotero.Promise.corout }); Zotero.Integration.Field = class { - constructor(field) { + constructor(field, rawCode) { if (field instanceof Zotero.Integration.Field) { throw new Error("Trying to instantiate Integration.Field with Integration.Field, not doc field"); } @@ -1990,6 +1984,7 @@ Zotero.Integration.Field = class { } } this._field = field; + this._code = rawCode; this.type = INTEGRATION_TYPE_TEMP; } @@ -2002,15 +1997,18 @@ Zotero.Integration.Field = class { } else { this._field.setCode(`TEMP`); } + this._code = code; } getCode() { - let code = this._field.getCode(); - let start = code.indexOf('{'); + if (!this._code) { + ths._code = this._field.getCode(); + } + let start = this._code.indexOf('{'); if (start == -1) { return '{}'; } - return code.substring(start, code.lastIndexOf('}')+1); + return this._code.substring(start, this._code.lastIndexOf('}')+1); } clearCode() { @@ -2042,37 +2040,29 @@ Zotero.Integration.Field.loadExisting = function(docField) { var field; // Already loaded if (docField instanceof Zotero.Integration.Field) return docField; - let rawCode = docField.getCode(); + var rawCode = docField.getCode(); // ITEM/CITATION CSL_ITEM {json: 'data'} for (let type of ["ITEM", "CITATION"]) { if (rawCode.substr(0, type.length) === type) { - field = new Zotero.Integration.CitationField(docField); + field = new Zotero.Integration.CitationField(docField, rawCode); } } // BIBL {json: 'data'} CSL_BIBLIOGRAPHY if (rawCode.substr(0, 4) === "BIBL") { - field = new Zotero.Integration.BibliographyField(docField); + field = new Zotero.Integration.BibliographyField(docField, rawCode); } if (!field) { - field = new Zotero.Integration.Field(docField); + field = new Zotero.Integration.Field(docField, rawCode); } - if (field) { - let start = rawCode.indexOf('{'); - if (start != -1) { - field._code = rawCode.substring(start, rawCode.lastIndexOf('}')+1); - } else { - field._code = rawCode.substr(rawCode.indexOf(' ')+1); - } - }; return field; }; Zotero.Integration.CitationField = class extends Zotero.Integration.Field { - constructor(field) { - super(field); + constructor(field, rawCode) { + super(field, rawCode); this.type = INTEGRATION_TYPE_ITEM; } @@ -2230,8 +2220,8 @@ Zotero.Integration.CitationField = class extends Zotero.Integration.Field { Zotero.Integration.BibliographyField = class extends Zotero.Integration.Field { - constructor(field) { - super(field); + constructor(field, rawCode) { + super(field, rawCode); this.type = INTEGRATION_TYPE_BIBLIOGRAPHY; }; @@ -2261,12 +2251,12 @@ Zotero.Integration.BibliographyField = class extends Zotero.Integration.Field { }; Zotero.Integration.Citation = class { - constructor(citationField) { + constructor(citationField, noteIndex) { let data = citationField.unserialize(); this.citationID = data.citationID; this.citationItems = data.citationItems; this.properties = data.properties; - this.properties.noteIndex = citationField.getNoteIndex(); + this.properties.noteIndex = noteIndex; this._field = citationField; } @@ -2331,9 +2321,10 @@ Zotero.Integration.Citation = class { // assign a random string as an item ID var anonymousID = Zotero.randomString(); var globalID = itemData.id = citationItem.id = Zotero.Integration.currentSession.data.sessionID+"/"+anonymousID; + Zotero.Integration.currentSession.embeddedItems[anonymousID] = itemData; // assign a Zotero item - var surrogateItem = Zotero.Integration.currentSession.embeddedItems[anonymousID] = new Zotero.Item(); + var surrogateItem = Zotero.Integration.currentSession.embeddedZoteroItems[anonymousID] = new Zotero.Item(); Zotero.Utilities.itemFromCSLJSON(surrogateItem, itemData); surrogateItem.cslItemID = globalID; surrogateItem.cslURIs = citationItem.uris;