From 04fa89ffc8d53acc372ad2e77882ca4e953a5994 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Mon, 21 Feb 2022 17:13:59 +0200 Subject: [PATCH] Don't copy/import/delete note images if library files aren't editable #2364 --- chrome/content/zotero/collectionTree.jsx | 10 +++-- chrome/content/zotero/xpcom/editorInstance.js | 43 ++++++++++--------- chrome/content/zotero/zoteroPane.js | 2 +- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/chrome/content/zotero/collectionTree.jsx b/chrome/content/zotero/collectionTree.jsx index 16d2cd1122..5955e8c811 100644 --- a/chrome/content/zotero/collectionTree.jsx +++ b/chrome/content/zotero/collectionTree.jsx @@ -1632,7 +1632,9 @@ var CollectionTree = class CollectionTree extends LibraryTree { await newItem.addLinkedItem(item); if (item.isNote()) { - await Zotero.Notes.copyEmbeddedImages(item, newItem); + if (Zotero.Libraries.get(newItem.libraryID).filesEditable) { + await Zotero.Notes.copyEmbeddedImages(item, newItem); + } return newItemID; } @@ -1648,8 +1650,10 @@ var CollectionTree = class CollectionTree extends LibraryTree { await newNote.save({ skipSelect: true }) - - await Zotero.Notes.copyEmbeddedImages(note, newNote); + + if (Zotero.Libraries.get(newNote.libraryID).filesEditable) { + await Zotero.Notes.copyEmbeddedImages(note, newNote); + } await newNote.addLinkedItem(note); } } diff --git a/chrome/content/zotero/xpcom/editorInstance.js b/chrome/content/zotero/xpcom/editorInstance.js index fbed160690..e9965017fd 100644 --- a/chrome/content/zotero/xpcom/editorInstance.js +++ b/chrome/content/zotero/xpcom/editorInstance.js @@ -59,6 +59,7 @@ class EditorInstance { this._reloaded = options.reloaded; this._viewMode = options.viewMode; this._readOnly = options.readOnly || this._isReadOnly(); + this._filesReadOnly = !Zotero.Libraries.get(this._item.libraryID).filesEditable; this._disableUI = options.disableUI; this._onReturn = options.onReturn; this._iframeWindow = options.iframeWindow; @@ -131,7 +132,7 @@ class EditorInstance { this._iframeWindow.removeEventListener('message', this._messageHandler); this.saveSync(); await Zotero.Notes.unregisterEditorInstance(this); - if (!this._item.isAttachment()) { + if (!this._item.isAttachment() && !this._filesReadOnly) { await Zotero.Notes.deleteUnusedEmbeddedImages(this._item); } } @@ -370,7 +371,7 @@ class EditorInstance { } // Image - if (annotation.image) { + if (annotation.image && !this._filesReadOnly) { // We assume that annotation.image is always PNG let imageAttachmentKey = await this._importImage(annotation.image); delete annotation.image; @@ -517,26 +518,28 @@ class EditorInstance { } // Clone all note image attachments and replace keys in the new note - let attachments = Zotero.Items.get(item.getAttachments()); - for (let attachment of attachments) { - if (!await attachment.fileExists()) { - continue; - } - await Zotero.DB.executeTransaction(async () => { - let copiedAttachment = await Zotero.Attachments.copyEmbeddedImage({ - attachment, - note: this._item, - saveOptions: { - notifierData: { - noteEditorID: this.instanceID + if (!this._filesReadOnly) { + let attachments = Zotero.Items.get(item.getAttachments()); + for (let attachment of attachments) { + if (!await attachment.fileExists()) { + continue; + } + await Zotero.DB.executeTransaction(async () => { + let copiedAttachment = await Zotero.Attachments.copyEmbeddedImage({ + attachment, + note: this._item, + saveOptions: { + notifierData: { + noteEditorID: this.instanceID + } } + }); + let node = doc.querySelector(`img[data-attachment-key="${attachment.key}"]`); + if (node) { + node.setAttribute('data-attachment-key', copiedAttachment.key); } }); - let node = doc.querySelector(`img[data-attachment-key="${attachment.key}"]`); - if (node) { - node.setAttribute('data-attachment-key', copiedAttachment.key); - } - }); + } } html += `

${doc.body.innerHTML}

`; @@ -736,7 +739,7 @@ class EditorInstance { } case 'importImages': { let { images } = message; - if (this._readOnly) { + if (this._readOnly || this._filesReadOnly) { return; } if (this._item.isAttachment()) { diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 8f0efe6ddb..0cbdece8b6 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -1665,7 +1665,7 @@ var ZoteroPane = new function() newItem.setCollections([self.getCollectionTreeRow().ref.id]); } yield newItem.save(); - if (item.isNote()) { + if (item.isNote() && Zotero.Libraries.get(newItem.libraryID).filesEditable) { yield Zotero.Notes.copyEmbeddedImages(item, newItem); } for (let relItemKey of item.relatedItems) {