From 8b11737b82a9dc13dc0068b038ac4b201f8dedaf Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Wed, 13 Oct 2021 17:43:27 +0300 Subject: [PATCH] Import existing images when opening a note #2186 --- chrome/content/zotero/xpcom/data/notes.js | 9 +++--- chrome/content/zotero/xpcom/editorInstance.js | 31 ++++++------------- note-editor | 2 +- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/notes.js b/chrome/content/zotero/xpcom/data/notes.js index 7e74a03f13..6679da0524 100644 --- a/chrome/content/zotero/xpcom/data/notes.js +++ b/chrome/content/zotero/xpcom/data/notes.js @@ -49,10 +49,7 @@ Zotero.Notes = new function() { // 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); - } + this._editorInstances = this._editorInstances.filter(x => x !== instance); }); }; @@ -282,6 +279,10 @@ Zotero.Notes = new function() { throw new Error('Item is not a note'); } + if (this._editorInstances.some(x => x._item && x._item.id === item.id)) { + return; + } + let note = item.getNote(); let parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] .createInstance(Components.interfaces.nsIDOMParser); diff --git a/chrome/content/zotero/xpcom/editorInstance.js b/chrome/content/zotero/xpcom/editorInstance.js index 80e16d0db8..8956de6f11 100644 --- a/chrome/content/zotero/xpcom/editorInstance.js +++ b/chrome/content/zotero/xpcom/editorInstance.js @@ -67,7 +67,6 @@ class EditorInstance { this._disableSaving = false; this._subscriptions = []; this._quickFormatWindow = null; - this._isAttachment = this._item.isAttachment(); this._citationItemsList = []; this._initPromise = new Promise((resolve, reject) => { this._resolveInitPromise = resolve; @@ -98,12 +97,13 @@ class EditorInstance { this._postMessage({ action: 'init', value: this._state || this._item.note, + reloaded: this._reloaded, viewMode: this._viewMode, readOnly: this._readOnly, unsaved: !this._item.id, disableUI: this._disableUI, enableReturnButton: !!this._onReturn, - isAttachmentNote: this._isAttachment, + isAttachmentNote: this._item.isAttachment(), placeholder: options.placeholder, dir: Zotero.dir, font: this._getFont(), @@ -122,24 +122,15 @@ class EditorInstance { } } - uninit() { + async uninit() { this._prefObserverIDs.forEach(id => Zotero.Prefs.unregisterObserver(id)); - if (this._quickFormatWindow) { this._quickFormatWindow.close(); this._quickFormatWindow = null; } - // TODO: Allow editor instance to finish its work before - // the uninitialization. I.e. to finish image importing - - // As long as the message listeners are attached on - // both sides, editor instance can continue its work - // in the backstage. Although the danger here is that - // multiple editor instances of the same note can start - // competing this._iframeWindow.removeEventListener('message', this._messageHandler); - Zotero.Notes.unregisterEditorInstance(this); this.saveSync(); + await Zotero.Notes.unregisterEditorInstance(this); if (!this._item.isAttachment()) { Zotero.Notes.deleteUnusedEmbeddedImages(this._item); } @@ -171,7 +162,7 @@ class EditorInstance { let uris = items.map(x => Zotero.URI.getItemURI(x)).filter(x => x); let citationItemsList = this._citationItemsList .filter(ci => ci.uris && uris.some(uri => ci.uris.includes(uri))); - await this._updateCitationItems(citationItemsList, true); + await this._updateCitationItems(citationItemsList); } saveSync() { @@ -683,11 +674,7 @@ class EditorInstance { newList.push(item); } } - // Force note saving only when note was opened by user and not by - // reload, otherwise it can result to sync carousel, if different - // clients have different citation item data and the same note is - // opened on both clients - await this._updateCitationItems(newList, !this._reloaded); + await this._updateCitationItems(newList); this._citationItemsList = list; return; } @@ -730,7 +717,7 @@ class EditorInstance { if (this._readOnly) { return; } - if (this._isAttachment) { + if (this._item.isAttachment()) { return; } for (let image of images) { @@ -762,7 +749,7 @@ class EditorInstance { } } - async _updateCitationItems(citationItemsList, forceSaving) { + async _updateCitationItems(citationItemsList) { let citationItems = []; for (let { uris } of citationItemsList) { let item = await Zotero.EditorInstance.getItemFromURIs(uris); @@ -772,7 +759,7 @@ class EditorInstance { } } if (citationItems.length) { - this._postMessage({ action: 'updateCitationItems', citationItems, forceSaving }); + this._postMessage({ action: 'updateCitationItems', citationItems }); } } diff --git a/note-editor b/note-editor index 0dc6d65a28..71c11bdf5f 160000 --- a/note-editor +++ b/note-editor @@ -1 +1 @@ -Subproject commit 0dc6d65a28d1a2ec755e9f426fb98eb732109d92 +Subproject commit 71c11bdf5f655be917595c8b0c85acca310895b9