Skip to content

Commit

Permalink
Merge changes from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
fbennett committed Mar 31, 2018
2 parents e97f938 + 37e850c commit 14e067d
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 52 deletions.
5 changes: 4 additions & 1 deletion chrome/content/zotero/xpcom/data/item.js
Expand Up @@ -147,6 +147,9 @@ Zotero.defineProperty(Zotero.Item.prototype, 'parentItemKey', {
get: function() { return this.parentKey; },
set: function(val) { return this.parentKey = val; }
});
Zotero.defineProperty(Zotero.Item.prototype, 'parentItem', {
get: function() { return Zotero.Items.get(this.parentID) || undefined; },
});


Zotero.defineProperty(Zotero.Item.prototype, 'firstCreator', {
Expand Down Expand Up @@ -3313,7 +3316,7 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentFilename', {
if (!path) {
return '';
}
var prefixedPath = path.match(/^(?:attachments|storage):(.+)$/);
var prefixedPath = path.match(/^(?:attachments|storage):(.*)$/);
if (prefixedPath) {
return prefixedPath[1];
}
Expand Down
7 changes: 4 additions & 3 deletions chrome/content/zotero/xpcom/data/tags.js
Expand Up @@ -758,12 +758,13 @@ Zotero.Tags = new function() {
* @return {Q Promise} A Q promise for a data: URL for a PNG
*/
this.generateItemsListImage = function (colors, extraImage) {
var multiplier = (extraImage && extraImage.indexOf('2x') != -1) ? 2 : 1;
var multiplier = Zotero.hiDPI ? 2 : 1;

var swatchWidth = 8 * multiplier;
var separator = 3 * multiplier;
var extraImageSeparator = 1 * multiplier;
var extraImageWidth = 16 * multiplier;
var extraImageHeight = 16 * multiplier;
var canvasHeight = 16 * multiplier;
var swatchHeight = 8 * multiplier;
var prependExtraImage = true;
Expand Down Expand Up @@ -831,7 +832,7 @@ Zotero.Tags = new function() {

// When extra image has loaded, draw it
img.onload = function () {
ctx.drawImage(img, x, 0);
ctx.drawImage(img, x, 0, extraImageWidth, extraImageHeight);

var dataURI = canvas.toDataURL("image/png");
var dataURIPromise = Zotero.Promise.resolve(dataURI);
Expand All @@ -852,7 +853,7 @@ Zotero.Tags = new function() {
// for the composite image once it's ready
return _itemsListExtraImagePromises[extraImage]
.then(function (img) {
ctx.drawImage(img, x, 0);
ctx.drawImage(img, x, 0, extraImageWidth, extraImageHeight);

var dataURI = canvas.toDataURL("image/png");
var dataURIPromise = Zotero.Promise.resolve(dataURI);
Expand Down
26 changes: 22 additions & 4 deletions chrome/content/zotero/xpcom/integration.js
Expand Up @@ -398,7 +398,6 @@ Zotero.Integration = new function() {
}
if (!session) {
session = new Zotero.Integration.Session(doc, app);
session.reload = true;
}
try {
yield session.setData(data);
Expand Down Expand Up @@ -1315,6 +1314,7 @@ Zotero.Integration.Session = function(doc, app) {
this.embeddedItemsByURI = {};
this.extractedItems = {};
//this.reselectedItems = {};
this.citationsByIndex = {};
this.resetRequest(doc);
this.primaryFieldType = app.primaryFieldType;
this.secondaryFieldType = app.secondaryFieldType;
Expand All @@ -1331,9 +1331,22 @@ Zotero.Integration.Session.prototype.resetRequest = function(doc) {

this.bibliographyHasChanged = false;
this.bibliographyDataHasChanged = false;
this.updateIndices = {};
// After adding fields to the session
// citations that are new to the document will be marked
// as new, so that they are correctly loaded into and processed with citeproc
this.newIndices = {};

// After the processing of new indices with citeproc, some
// citations require additional work (because of disambiguation, numbering changes, etc)
// and will be marked for an additional reprocessing with citeproc to retrieve updated text
this.updateIndices = {};

// When processing citations this list will be checked for citations that are new to the document
// (i.e. copied from somewhere else) and marked as newIndices to be processed with citeproc if
// not present
this.oldCitations = new Set();
for (let i in this.citationsByIndex) {
this.oldCitations.add(this.citationsByIndex[i].citationID);
}
this.citationsByItemID = {};
this.citationsByIndex = {};
this.documentCitationIDs = {};
Expand Down Expand Up @@ -1600,10 +1613,15 @@ Zotero.Integration.Session.prototype.addCitation = Zotero.Promise.coroutine(func
}
if(needNewID) {
Zotero.debug("Integration: "+citation.citationID+" ("+index+") needs new citationID");
citation.citationID = Zotero.randomString();
citation.citationID = Zotero.Utilities.randomString();
}
this.newIndices[index] = true;
}
// Deal with citations that are copied into the document from somewhere else
// and have not been added to the processor yet
if (! this.oldCitations.has(citation.citationID)) {
this.newIndices[index] = true;
}
Zotero.debug("Integration: Adding citationID "+citation.citationID);
this.documentCitationIDs[citation.citationID] = index;
});
Expand Down
2 changes: 1 addition & 1 deletion chrome/content/zotero/xpcom/schema.js
Expand Up @@ -2625,7 +2625,7 @@ Zotero.Schema = new function(){
if (matches) {
// Wrong libraryID
if (matches[1] != userID) {
yield Zotero.DB.queryAsync(`UPDATE itemRelations SET object='http://zotero.org/users/${userID}/items/${matches[2]}' WHERE itemID=? AND predicateID=?`, [row.itemID, predicateID]);
yield Zotero.DB.queryAsync(`UPDATE OR REPLACE itemRelations SET object='http://zotero.org/users/${userID}/items/${matches[2]}' WHERE itemID=? AND predicateID=?`, [row.itemID, predicateID]);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions chrome/content/zotero/xpcom/server.js
Expand Up @@ -32,6 +32,7 @@ Zotero.Server = new function() {
300:"Multiple Choices",
400:"Bad Request",
404:"Not Found",
409:"Conflict",
412:"Precondition Failed",
500:"Internal Server Error",
501:"Not Implemented",
Expand Down
89 changes: 58 additions & 31 deletions chrome/content/zotero/xpcom/server_connector.js
Expand Up @@ -81,7 +81,7 @@ Zotero.Server.Connector.SessionManager = {
},

create: function (id) {
// Legacy client
// Legacy connector
if (!id) {
Zotero.debug("No session id provided by client", 2);
id = Zotero.Utilities.randomString();
Expand Down Expand Up @@ -124,33 +124,43 @@ Zotero.Server.Connector.SaveSession.prototype.addItems = async function (items)
return this._addObjects('item', items);
};

/**
* Change the target data for this session and update any items that have already been saved
*/
Zotero.Server.Connector.SaveSession.prototype.update = async function (libraryID, collectionID, tags) {
this._currentLibraryID = libraryID;
this._currentCollectionID = collectionID;
this._currentTags = tags || "";

// Select new destination in collections pane
var win = Zotero.getActiveZoteroPane();
if (collectionID) {
var targetID = "C" + collectionID;
}
else {
var targetID = "L" + libraryID;
}
if (win && win.collectionsView) {
if (collectionID) {
var targetID = "C" + collectionID;
}
else {
var targetID = "L" + libraryID;
}
await win.collectionsView.selectByID(targetID);
}
else {
Zotero.Prefs.set('lastViewedFolder', targetID);
}

await this._updateObjects(this._objects);

// TODO: Update active item saver

// If a single item was saved, select it
// If a single item was saved, select it (or its parent, if it now has one)
if (win && win.collectionsView) {
if (this._objects && this._objects.item) {
let items = Array.from(this._objects.item).filter(item => item.isTopLevelItem());
if (items.length == 1) {
await win.selectItem(items[0].id);
if (this._objects.item.size == 1) {
let item = Array.from(this._objects.item)[0];
item = item.isTopLevelItem() ? item : item.parentItem;
// Don't select if in trash
if (!item.deleted) {
await win.selectItem(item.id);
}
}
}
}
Expand All @@ -160,17 +170,19 @@ Zotero.Server.Connector.SaveSession.prototype._addObjects = async function (obje
if (!this._objects[objectType]) {
this._objects[objectType] = new Set();
}
for (let object of objects) {
this._objects[objectType].add(object);
}

// If target has changed since the save began, update the objects
// Update the objects with the current target data, in case it changed since the save began
await this._updateObjects({
[objectType]: objects
});

for (let object of objects) {
this._objects[objectType].add(object);
}
};

/**
* Update the passed objects with the current target and tags
*/
Zotero.Server.Connector.SaveSession.prototype._updateObjects = async function (objects) {
if (Object.keys(objects).every(type => objects[type].length == 0)) {
return;
Expand All @@ -186,20 +198,23 @@ Zotero.Server.Connector.SaveSession.prototype._updateObjects = async function (o
return Zotero.DB.executeTransaction(async function () {
for (let objectType in objects) {
for (let object of objects[objectType]) {
Zotero.debug(object.libraryID);
Zotero.debug(libraryID);
if (object.libraryID != libraryID) {
throw new Error("Can't move objects between libraries");
}

// Keep automatic tags
let originalTags = object.getTags().filter(tag => tag.type == 1);

// Assign manual tags and collections to top-level items
if (objectType == 'item' && object.isTopLevelItem()) {
object.setTags(originalTags.concat(tags));
object.setCollections(collectionID ? [collectionID] : []);
await object.save();
// Assign manual tags and collections to the item, or the parent item if it's now
// a child item (e.g., from Retrieve Metadata for PDF)
if (objectType == 'item') {
let item = object.isTopLevelItem() ? object : object.parentItem;
if (!Zotero.Items.exists(item.id)) {
Zotero.debug(`Item ${item.id} in save session no longer exists`);
continue;
}
// Keep automatic tags
let originalTags = item.getTags().filter(tag => tag.type == 1);
item.setTags(originalTags.concat(tags));
item.setCollections(collectionID ? [collectionID] : []);
await item.save();
}
}
}
Expand Down Expand Up @@ -850,29 +865,41 @@ Zotero.Server.Connector.Import.prototype = {
supportedDataTypes: '*',
permitBookmarklet: false,

init: Zotero.Promise.coroutine(function* (options) {
init: async function (options) {
let translate = new Zotero.Translate.Import();
translate.setString(options.data);
let translators = yield translate.getTranslators();
let translators = await translate.getTranslators();
if (!translators || !translators.length) {
return 400;
}
translate.setTranslator(translators[0]);
var { library, collection, editable } = Zotero.Server.Connector.getSaveTarget();
var libraryID = library.libraryID;
if (!library.editable) {
Zotero.logError("Can't import into read-only library " + library.name);
return [500, "application/json", JSON.stringify({ libraryEditable: false })];
}
let items = yield translate.translate({
libraryID: library.libraryID,

try {
var session = Zotero.Server.Connector.SessionManager.create(options.query.session);
}
catch (e) {
return [409, "application/json", JSON.stringify({ error: "SESSION_EXISTS" })];
}
await session.update(libraryID, collection ? collection.id : false);

let items = await translate.translate({
libraryID,
collections: collection ? [collection.id] : null,
// Import translation skips selection by default, so force it to occur
saveOptions: {
skipSelect: false
}
});
session.addItems(items);

return [201, "application/json", JSON.stringify(items)];
})
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion chrome/content/zotero/xpcom/storage/storageLocal.js
Expand Up @@ -687,7 +687,7 @@ Zotero.Sync.Storage.Local = {

var filename = item.attachmentFilename;
if (!filename) {
throw new Error("Empty path for item " + item.key);
Zotero.debug("Empty filename for item " + item.key, 2);
}
// Don't save Windows aliases
if (filename.endsWith('.lnk')) {
Expand Down
21 changes: 12 additions & 9 deletions chrome/content/zotero/xpcom/zotero.js
Expand Up @@ -2778,14 +2778,17 @@ Zotero.DragDrop = {
Zotero.Browser = new function() {
var nBrowsers = 0;

this.createHiddenBrowser = createHiddenBrowser;
this.deleteHiddenBrowser = deleteHiddenBrowser;

function createHiddenBrowser(win) {
if (!win) {
var win = Services.wm.getMostRecentWindow("navigator:browser");
if(!win) {
var win = Services.ww.activeWindow;
this.createHiddenBrowser = function (win) {
if (!win) {
win = Services.wm.getMostRecentWindow("navigator:browser");
if (!win) {
win = Services.ww.activeWindow;
}
// Use the hidden DOM window on macOS with the main window closed
if (!win) {
let appShellService = Components.classes["@mozilla.org/appshell/appShellService;1"]
.getService(Components.interfaces.nsIAppShellService);
win = appShellService.hiddenDOMWindow;
}
if (!win) {
throw new Error("Parent window not available for hidden browser");
Expand All @@ -2808,7 +2811,7 @@ Zotero.Browser = new function() {
return hiddenBrowser;
}

function deleteHiddenBrowser(myBrowsers) {
this.deleteHiddenBrowser = function (myBrowsers) {
if(!(myBrowsers instanceof Array)) myBrowsers = [myBrowsers];
for(var i=0; i<myBrowsers.length; i++) {
var myBrowser = myBrowsers[i];
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Expand Up @@ -6,7 +6,7 @@

<em:id>juris-m@juris-m.github.io</em:id>
<em:name>Juris-M</em:name>
<em:version>5.0.39m17.SOURCE</em:version>
<em:version>5.0.42m18.SOURCE</em:version>
<em:creator>Center for History and New Media<br/>George Mason University</em:creator>
<em:contributor>Dan Cohen</em:contributor>
<em:contributor>Sean Takats</em:contributor>
Expand Down

0 comments on commit 14e067d

Please sign in to comment.