From 24d89330234368fb4bdf488e393436a5942e5089 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 3 Aug 2020 12:11:46 +0200 Subject: [PATCH] Use a `DocumentFragment` when building the attachmentsView in `PDFAttachmentViewer.render` This approach is already used in other parts of the code-base, see e.g. `PDFOutlineViewer`, and has the advantage of only invalidating the DOM once rather than for every attachment item. --- web/pdf_attachment_viewer.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index 529c77121..2210dfde4 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -129,23 +129,22 @@ class PDFAttachmentViewer { * @param {PDFAttachmentViewerRenderParameters} params */ render({ attachments, keepRenderedCapability = false }) { - let attachmentsCount = 0; - if (this.attachments) { this.reset(keepRenderedCapability === true); } this.attachments = attachments || null; if (!attachments) { - this._dispatchEvent(attachmentsCount); + this._dispatchEvent(/* attachmentsCount = */ 0); return; } const names = Object.keys(attachments).sort(function (a, b) { return a.toLowerCase().localeCompare(b.toLowerCase()); }); - attachmentsCount = names.length; + const attachmentsCount = names.length; + const fragment = document.createDocumentFragment(); for (let i = 0; i < attachmentsCount; i++) { const item = attachments[names[i]]; const filename = removeNullCharacters(getFilenameFromUrl(item.filename)); @@ -164,8 +163,9 @@ class PDFAttachmentViewer { } div.appendChild(button); - this.container.appendChild(div); + fragment.appendChild(div); } + this.container.appendChild(fragment); this._dispatchEvent(attachmentsCount); }