diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index 7c591a276..92005c79d 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -116,6 +116,7 @@ var Zotero_File_Interface = new function() { this.bibliographyFromCollection = bibliographyFromCollection; this.bibliographyFromItems = bibliographyFromItems; this.copyItemsToClipboard = copyItemsToClipboard; + this.copyCitationToClipboard = copyCitationToClipboard; /* * Creates Zotero.Translate instance and shows file picker for file export @@ -340,6 +341,41 @@ var Zotero_File_Interface = new function() { clipboardService.setData(transferable, null, Components.interfaces.nsIClipboard.kGlobalClipboard); } + + /* + * Copies HTML and text citations for passed items in given style + * + * Does not check that items are actual references (and not notes or attachments) + */ + function copyCitationToClipboard(items, style) { + // copy to clipboard + var transferable = Components.classes["@mozilla.org/widget/transferable;1"]. + createInstance(Components.interfaces.nsITransferable); + var clipboardService = Components.classes["@mozilla.org/widget/clipboard;1"]. + getService(Components.interfaces.nsIClipboard); + + var csl = Zotero.Cite.getStyle(style); + csl.preprocessItems(items); + + // add HTML + var bibliography = csl.createCitation({citationType:1, items:items}, "HTML"); + var str = Components.classes["@mozilla.org/supports-string;1"]. + createInstance(Components.interfaces.nsISupportsString); + str.data = bibliography; + transferable.addDataFlavor("text/html"); + transferable.setTransferData("text/html", str, bibliography.length*2); + + // add text + var bibliography = csl.createCitation({citationType:1, items:items}, "Text"); + var str = Components.classes["@mozilla.org/supports-string;1"]. + createInstance(Components.interfaces.nsISupportsString); + str.data = bibliography; + transferable.addDataFlavor("text/unicode"); + transferable.setTransferData("text/unicode", str, bibliography.length*2); + + clipboardService.setData(transferable, null, Components.interfaces.nsIClipboard.kGlobalClipboard); + } + /* * Shows bibliography options and creates a bibliography */ @@ -528,4 +564,4 @@ Zotero_File_Interface.Progress = new function() { window.setTimeout(_callback, 1500); } } -} +} \ No newline at end of file diff --git a/chrome/content/zotero/xpcom/cite.js b/chrome/content/zotero/xpcom/cite.js index 35ea8ee40..75351072d 100644 --- a/chrome/content/zotero/xpcom/cite.js +++ b/chrome/content/zotero/xpcom/cite.js @@ -444,6 +444,22 @@ Zotero.CSL._locatorTerms = { /* * create a citation (in-text or footnote) + * accepts a citation object containing: + * + * citation.citationType + * 1 = first + * 2 = subsequent + * 3 = ibid + * 4 = ibid + pages + * + * citation.locators + * array of locators + * + * citation.locatorTypes + * array of types for given locators (see above) + * + * citation.items/citation.itemIDs + * array of items/itemIDs to be cited */ Zotero.CSL.prototype.createCitation = function(citation, format) { var string = new Zotero.CSL.FormattedString(this, format); @@ -475,8 +491,10 @@ Zotero.CSL.prototype.createCitation = function(citation, format) { } } } else { // indicates primary or subsequent - var lasti = citation.itemIDs.length-1; - for(var i in citation.itemIDs) { + if(citation.itemIDs) citation.items = citation.itemIDs; + + var lasti = citation.items.length-1; + for(var i in citation.items) { var locatorType = false; var locator = false; if(citation.locators) { @@ -485,7 +503,15 @@ Zotero.CSL.prototype.createCitation = function(citation, format) { } var position = (citation.citationType[i] == 1 ? "first" : "subsequent"); - var citationString = this._getCitation(Zotero.Items.get(citation.itemIDs[i]), + + // allow either item objects or ids + if(typeof citation.items[i] == "object") { + item = citation.items[i]; + } else { + item = Zotero.Items.get(citation.items[i]); + } + + var citationString = this._getCitation(item, position, locatorType, locator, format, this._cit); string.concat(citationString);