diff --git a/chrome/content/zotero/xpcom/data/notes.js b/chrome/content/zotero/xpcom/data/notes.js index 993ad60a3e..2b67959054 100644 --- a/chrome/content/zotero/xpcom/data/notes.js +++ b/chrome/content/zotero/xpcom/data/notes.js @@ -25,10 +25,7 @@ Zotero.Notes = new function() { - this.noteToTitle = noteToTitle; - // Currently active editor instances - this.editorInstances = []; - + this._editorInstances = []; this.__defineGetter__("MAX_TITLE_LENGTH", function() { return 120; }); this.__defineGetter__("defaultNote", function () { return '
'; }); @@ -43,7 +40,7 @@ Zotero.Notes = new function() { /** * Return first line (or first MAX_LENGTH characters) of note content **/ - function noteToTitle(text) { + this.noteToTitle = function(text) { var origText = text; text = text.trim(); text = Zotero.Utilities.unescapeHTML(text); @@ -67,49 +64,69 @@ Zotero.Notes = new function() { t = t.substring(0, ln); } return t; - } + }; - /** - * Replaces local URIs for citation and highlight nodes - * - * Must be called just before the initial sync, - * if called later the item version will be increased, - * which might be incovenient for the future (better) notes sync - * - * @param item Note item - * @returns {Promise} - */ - this.updateURIs = async (item) => { - let html = item.note; - let num = 0; - // "uri":"http://zotero.org/users/local/(.+?)/items/(.+?)" - let regex = new RegExp(/%22uri%22%3A%22http%3A%2F%2Fzotero.org%2Fusers%2Flocal%2F(.+?)%2Fitems%2F(.+?)%22/g); - html = html.replace(regex, function (m, g1, g2) { - num++; - let libraryID = Zotero.URI.getURILibrary('http://zotero.org/users/local/' + g1); - let libraryURI = Zotero.URI.getLibraryURI(libraryID); - return encodeURIComponent('"uri":"' + libraryURI + '/items/' + g2 + '"'); + this.registerEditorInstance = function(instance) { + this._editorInstances.push(instance); + }; + + this.unregisterEditorInstance = async function(instance) { + // Make sure the editor instance is not unregistered while + // Zotero.Notes.updateUser is in progress, otherwise the + // instance might not get the`disableSaving` flag set + await Zotero.DB.executeTransaction(async () => { + let index = this._editorInstances.indexOf(instance); + if (index >= 0) { + this._editorInstances.splice(index, 1); + } }); - if (num) { - item.setNote(html); - // Cut off saving for each editor instance for this item, - // to make sure none of the editor instances will concurrently - // overwrite our changes - this.editorInstances.forEach(editorInstance => { - if (editorInstance.item.id === item.id) { - editorInstance.disableSaving = true; - } - }); - // Although, theoretically, a new editor instance with the old data can still - // be created while asynchronous `item.saveTx` is in progress, but really unlikely + }; - // Observer notification will automatically recreate the affected editor instances - await item.saveTx(); - Zotero.debug(`Updated URIs for item ${item.id}: ${num}`); + /** + * Replace local URIs for citations and highlights + * in all notes. Cut-off note saving for the opened + * notes and then trigger notification to refresh + * + * @param {Number} fromUserID + * @param {Number} toUserID + * @returns {Promise