From 77a98bfd14bdc8d64f751af22e4b29706bfac489 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Mon, 19 Mar 2018 12:47:36 +0200 Subject: [PATCH] Render video and audio previews only after canplay event fires --- client/js/renderPreview.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/js/renderPreview.js b/client/js/renderPreview.js index c7474d52..99fb0f53 100644 --- a/client/js/renderPreview.js +++ b/client/js/renderPreview.js @@ -17,15 +17,17 @@ function renderPreview(preview, msg) { preview.shown = preview.shown && options.shouldOpenMessagePreview(preview.type); const template = $(templates.msg_preview({preview})); - const image = template.find("img:first"); + const image = template.find("img, video, audio").first(); if (image.length === 0) { return appendPreview(preview, msg, template); } + const loadEvent = image.prop("tagName") === "IMG" ? "load" : "canplay"; + // If there is an image in preview, wait for it to load before appending it to DOM // This is done to prevent problems keeping scroll to the bottom while images load - image.on("load.preview", () => { + image.on(`${loadEvent}.preview`, () => { image.off(".preview"); appendPreview(preview, msg, template);