From 96fb0c0370cb31af271f49c72e905535f86019e3 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 14 Aug 2017 12:34:24 +0200 Subject: [PATCH 1/2] Call the `reset()` methods in the `PDFAttachmentViewer` and `PDFOutlineViewer` constructors Rather than duplicating initialization code, we can just call `this.reset()` instead (which is also similar to other existing code, e.g. `PDFViewer`). This will also help ensure that the DOM is completely reset, before any outline items or attachments are displayed. --- web/pdf_attachment_viewer.js | 5 ++--- web/pdf_outline_viewer.js | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index eb4a3034be0d5..8223cbd4ac891 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -35,13 +35,12 @@ class PDFAttachmentViewer { * @param {PDFAttachmentViewerOptions} options */ constructor({ container, eventBus, downloadManager, }) { - this.attachments = null; - this.container = container; this.eventBus = eventBus; this.downloadManager = downloadManager; - this._renderedCapability = createPromiseCapability(); + this.reset(); + this.eventBus.on('fileattachmentannotation', this._appendAttachment.bind(this)); } diff --git a/web/pdf_outline_viewer.js b/web/pdf_outline_viewer.js index 18d21471a928e..7aee5b626ddfe 100644 --- a/web/pdf_outline_viewer.js +++ b/web/pdf_outline_viewer.js @@ -36,12 +36,11 @@ class PDFOutlineViewer { * @param {PDFOutlineViewerOptions} options */ constructor({ container, linkService, eventBus, }) { - this.outline = null; - this.lastToggleIsShow = true; - this.container = container; this.linkService = linkService; this.eventBus = eventBus; + + this.reset(); } reset() { From 9273350c6b7c7bff4b6c748eb84f226240285dda Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 17 Aug 2017 14:12:42 +0200 Subject: [PATCH 2/2] Attempt to delay disabling of the attachment view until FileAttachment annotations of the *initial* page has been parsed As discussed in PR 8673, we cannot solve the general issue (since that would require parsing every single page). However, we can mitigate the effect somewhat, by waiting for the FileAttachment annotations of the initially rendered page. --- web/pdf_attachment_viewer.js | 4 ++-- web/pdf_sidebar.js | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index 8223cbd4ac891..27d1b4eabb83f 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -62,12 +62,12 @@ class PDFAttachmentViewer { * @private */ _dispatchEvent(attachmentsCount) { + this._renderedCapability.resolve(); + this.eventBus.dispatch('attachmentsloaded', { source: this, attachmentsCount, }); - - this._renderedCapability.resolve(); } /** diff --git a/web/pdf_sidebar.js b/web/pdf_sidebar.js index f3a7a26f6d694..caabedd5738d7 100644 --- a/web/pdf_sidebar.js +++ b/web/pdf_sidebar.js @@ -420,17 +420,31 @@ class PDFSidebar { }); this.eventBus.on('attachmentsloaded', (evt) => { - let attachmentsCount = evt.attachmentsCount; + if (evt.attachmentsCount) { + this.attachmentsButton.disabled = false; - this.attachmentsButton.disabled = !attachmentsCount; - - if (attachmentsCount) { this._showUINotification(SidebarView.ATTACHMENTS); - } else if (this.active === SidebarView.ATTACHMENTS) { - // If the attachment view was opened during document load, switch away - // from it if it turns out that the document has no attachments. - this.switchView(SidebarView.THUMBS); + return; } + + // Attempt to avoid temporarily disabling, and switching away from, the + // attachment view for documents that do not contain proper attachments + // but *only* FileAttachment annotations. Hence we defer those operations + // slightly to allow time for parsing any FileAttachment annotations that + // may be present on the *initially* rendered page of the document. + Promise.resolve().then(() => { + if (this.attachmentsView.hasChildNodes()) { + // FileAttachment annotations were appended to the attachment view. + return; + } + this.attachmentsButton.disabled = true; + + if (this.active === SidebarView.ATTACHMENTS) { + // If the attachment view was opened during document load, switch away + // from it if it turns out that the document has no attachments. + this.switchView(SidebarView.THUMBS); + } + }); }); // Update the thumbnailViewer, if visible, when exiting presentation mode.