From a07609540051af79406839739f02a479ab76131b Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 17 Feb 2007 23:13:48 +0000 Subject: [PATCH] Closes #405, Pressing copy keystroke with item selected should copy citation to clipboard Shortcut key defaults to "C" Abstracted clipboard logic in Zotero_File_Interface._doBibliographyOptions() to separate function, Z_F_I.copyItemsToClipboard(items, style), which ZoteroPane.copySelectedItemsToClipboard() calls Currently limited to citation styles, but there's no reason it couldn't support export formats, etc. --- chrome/content/zotero/fileInterface.js | 84 ++++++++++++-------- chrome/content/zotero/overlay.js | 50 +++++++----- chrome/content/zotero/preferences.js | 44 ++++++++++ chrome/content/zotero/preferences.xul | 26 ++++++ chrome/content/zotero/xpcom/zotero.js | 6 +- chrome/locale/en-US/zotero/preferences.dtd | 6 ++ chrome/locale/en-US/zotero/zotero.properties | 1 + chrome/skin/default/zotero/preferences.css | 52 +++++++++++- defaults/preferences/zotero.js | 4 + 9 files changed, 217 insertions(+), 56 deletions(-) diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index 7aa1f0ddc..fd2c0d888 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -113,6 +113,7 @@ var Zotero_File_Interface = new function() { this.importFile = importFile; this.bibliographyFromCollection = bibliographyFromCollection; this.bibliographyFromItems = bibliographyFromItems; + this.copyItemsToClipboard = copyItemsToClipboard; /* * Creates Zotero.Translate instance and shows file picker for file export @@ -278,20 +279,55 @@ var Zotero_File_Interface = new function() { _doBibliographyOptions(Zotero.getString("fileInterface.untitledBibliography"), items); } + + /* + * Copies HTML and text bibliography entries for passed items in given style + * + * Does not check that items are actual references (and not notes or attachments) + */ + function copyItemsToClipboard(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.createBibliography(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.createBibliography(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 */ function _doBibliographyOptions(name, items) { // make sure at least one item is not a standalone note or attachment - var haveNonNote = false; - for(var i in items) { - var type = Zotero.ItemTypes.getName(items[i].getType()); - if(type != "note" && type != "attachment") { - haveNonNote = true; + var haveRegularItem = false; + for each(var item in items) { + if (item.isRegularItem()) { + haveRegularItem = true; break; } } - if(!haveNonNote) { + if (!haveRegularItem) { window.alert(Zotero.getString("fileInterface.noReferencesError")); return; } @@ -310,13 +346,18 @@ var Zotero_File_Interface = new function() { // generate bibliography try { - var csl = Zotero.Cite.getStyle(io.style); - csl.preprocessItems(items); - var bibliography = csl.createBibliography(items, format); + if (io.output == 'clipboard') { + this.copyItemsToClipboard(items, io.style); + return; + } + else { + var csl = Zotero.Cite.getStyle(io.style); + csl.preprocessItems(items); + var bibliography = csl.createBibliography(items, format); + } } catch(e) { window.alert(Zotero.getString("fileInterface.bibliographyGenerationError")); throw(e); - return; } if(io.output == "print") { @@ -380,31 +421,10 @@ var Zotero_File_Interface = new function() { fStream.write(bibliography, bibliography.length); fStream.close(); } - } else if(io.output == "copy-to-clipboard") { - // 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); - - // add 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.createBibliography(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); } } + function _saveBibliography(name, format) { // savable bibliography, using a file stream const nsIFilePicker = Components.interfaces.nsIFilePicker; diff --git a/chrome/content/zotero/overlay.js b/chrome/content/zotero/overlay.js index 550c9ce46..837d0d537 100644 --- a/chrome/content/zotero/overlay.js +++ b/chrome/content/zotero/overlay.js @@ -49,6 +49,7 @@ var ZoteroPane = new function() this.deleteSelectedItem = deleteSelectedItem; this.deleteSelectedCollection = deleteSelectedCollection; this.editSelectedCollection = editSelectedCollection; + this.copySelectedItemsToClipboard = copySelectedItemsToClipboard; this.clearQuicksearch = clearQuicksearch; this.handleSearchKeypress = handleSearchKeypress; this.handleSearchInput = handleSearchInput; @@ -286,6 +287,9 @@ var ZoteroPane = new function() case 'toggleFullscreen': ZoteroPane.fullScreen(); break; + case 'copySelectedItemsToClipboard': + ZoteroPane.copySelectedItemsToClipboard(); + break; default: throw ('Command "' + command + '" not found in ZoteroPane.handleKeyDown()'); } @@ -789,6 +793,34 @@ var ZoteroPane = new function() } + function copySelectedItemsToClipboard() { + var items = this.getSelectedItems(); + if (!items.length) { + return; + } + + // Make sure at least one item is not a standalone note or attachment + var haveRegularItem = false; + for each(var item in items) { + if (item.isRegularItem()) { + haveRegularItem = true; + break; + } + } + if (!haveRegularItem) { + window.alert(Zotero.getString("fileInterface.noReferencesError")); + return; + } + + // TODO: we only support bibliography output at the moment + var mode = Zotero.Prefs.get("export.quickCopy.mode"); + if (mode == 'bibliography') { + var style = Zotero.Prefs.get("export.quickCopy.setting"); + Zotero_File_Interface.copyItemsToClipboard(items, style); + } + } + + function clearQuicksearch() { document.getElementById('zotero-tb-search').value = ""; document.getElementById('zotero-tb-search').doCommand('cmd_zotero_search'); @@ -904,24 +936,6 @@ var ZoteroPane = new function() return this.itemsView.getSelectedItems(asIDs); } return []; - - if (this.itemsView) { - var items = new Array(); - var start = new Object(); - var end = new Object(); - for (var i=0, len=this.itemsView.selection.getRangeCount(); i + + + + + + + + + + + + @@ -157,6 +175,7 @@ To add a new preference: + @@ -209,6 +228,13 @@ To add a new preference: