Browse Source

Fix autodownloading

master
Eduard Kuzmenko 3 years ago
parent
commit
f4c6dd6fa8
  1. 31
      src/components/appMediaViewer.ts
  2. 2
      src/components/preloader.ts
  3. 51
      src/components/wrappers.ts
  4. 2
      src/scss/partials/_chatBubble.scss

31
src/components/appMediaViewer.ts

@ -527,6 +527,13 @@ class AppMediaViewerBase<ContentAdditionType extends string, ButtonsAdditionType
let mediaElement: HTMLImageElement | HTMLVideoElement; let mediaElement: HTMLImageElement | HTMLVideoElement;
let src: string; let src: string;
if(target instanceof HTMLVideoElement) {
const elements = Array.from(target.parentElement.querySelectorAll('img')) as HTMLImageElement[];
if(elements.length) {
target = elements.pop();
}
}
if(target.tagName === 'DIV' || target.tagName === 'AVATAR-ELEMENT') { // useContainerAsTarget if(target.tagName === 'DIV' || target.tagName === 'AVATAR-ELEMENT') { // useContainerAsTarget
const images = Array.from(target.querySelectorAll('img')) as HTMLImageElement[]; const images = Array.from(target.querySelectorAll('img')) as HTMLImageElement[];
const image = images.pop(); const image = images.pop();
@ -856,7 +863,27 @@ class AppMediaViewerBase<ContentAdditionType extends string, ButtonsAdditionType
//if(target instanceof SVGSVGElement) { //if(target instanceof SVGSVGElement) {
const el = target.tagName.toLowerCase() === tagName ? target : target.querySelector(tagName) as HTMLElement; const el = target.tagName.toLowerCase() === tagName ? target : target.querySelector(tagName) as HTMLElement;
if(el) { if(el) {
// two parentElements because element can be contained in aspecter
const preloader = target.parentElement.parentElement.querySelector('.preloader-container') as HTMLElement;
if(preloader) {
if(tagName === 'video') {
if(preloader.classList.contains('manual')) {
preloader.click();
// return;
}
return;
}
preloader.remove();
}
renderImageFromUrl(el, url); renderImageFromUrl(el, url);
// ! костыль, но он тут даже и не нужен
if(el.classList.contains('thumbnail') && el.parentElement.classList.contains('media-container-aspecter')) {
el.classList.remove('thumbnail');
}
} }
/* } else { /* } else {
@ -1196,12 +1223,12 @@ class AppMediaViewerBase<ContentAdditionType extends string, ButtonsAdditionType
//if(!video.parentElement) { //if(!video.parentElement) {
div.firstElementChild.lastElementChild.append(video); div.firstElementChild.lastElementChild.append(video);
//} //}
this.updateMediaSource(mover, url, 'video');
} else { } else {
renderImageFromUrl(video, url); renderImageFromUrl(video, url);
} }
this.updateMediaSource(target, url, 'video');
createPlayer(); createPlayer();
}); });

2
src/components/preloader.ts

@ -21,7 +21,7 @@ export default class ProgressivePreloader {
private downloadSvg: HTMLElement; private downloadSvg: HTMLElement;
private tempId = 0; private tempId = 0;
private detached = true; public detached = true;
public promise: CancellablePromise<any> = null; public promise: CancellablePromise<any> = null;

51
src/components/wrappers.ts

@ -80,7 +80,7 @@ export function wrapVideo({doc, container, message, boxWidth, boxHeight, withTai
spanTime.innerText = (doc.duration + '').toHHMMSS(false); spanTime.innerText = (doc.duration + '').toHHMMSS(false);
if(!noPlayButton && doc.type !== 'round') { if(!noPlayButton && doc.type !== 'round') {
if(canAutoplay) { if(canAutoplay && !noAutoDownload) {
spanTime.classList.add('tgico', 'can-autoplay'); spanTime.classList.add('tgico', 'can-autoplay');
} else { } else {
needPlayButton = true; needPlayButton = true;
@ -232,6 +232,15 @@ export function wrapVideo({doc, container, message, boxWidth, boxHeight, withTai
attachClickEvent(canvas, (e) => { attachClickEvent(canvas, (e) => {
cancelEvent(e); cancelEvent(e);
// ! костыль
if(preloader && !preloader.detached) {
preloader.onClick();
}
if(globalVideo.readyState < 2) {
return;
}
if(globalVideo.paused) { if(globalVideo.paused) {
globalVideo.play(); globalVideo.play();
} else { } else {
@ -316,7 +325,7 @@ export function wrapVideo({doc, container, message, boxWidth, boxHeight, withTai
}); });
} }
let f = noAutoDownload && photoRes?.preloader?.loadFunc; let loadPhotoThumbFunc = noAutoDownload && photoRes?.preloader?.loadFunc;
const load = () => { const load = () => {
if(preloader && noAutoDownload && !withoutPreloader) { if(preloader && noAutoDownload && !withoutPreloader) {
preloader.construct(); preloader.construct();
@ -348,11 +357,15 @@ export function wrapVideo({doc, container, message, boxWidth, boxHeight, withTai
if(preloader) { if(preloader) {
preloader.detach(); preloader.detach();
} }
if(!deferred.isFulfilled) {
deferred.resolve();
}
}, {once: true}); }, {once: true});
if(!noAutoDownload && f) { if(!noAutoDownload && loadPhotoThumbFunc) {
f(); loadPhotoThumbFunc();
f = null; loadPhotoThumbFunc = null;
} }
noAutoDownload = undefined; noAutoDownload = undefined;
@ -373,6 +386,10 @@ export function wrapVideo({doc, container, message, boxWidth, boxHeight, withTai
animationIntersector.addAnimation(video, group); animationIntersector.addAnimation(video, group);
} }
if(preloader) {
preloader.detach();
}
deferred.resolve(); deferred.resolve();
}); });
@ -382,10 +399,6 @@ export function wrapVideo({doc, container, message, boxWidth, boxHeight, withTai
}); });
} }
video.addEventListener('error', (e) => {
deferred.resolve();
});
video.muted = true; video.muted = true;
video.loop = true; video.loop = true;
//video.play(); //video.play();
@ -823,7 +836,7 @@ export function wrapPhoto({photo, message, container, boxWidth, boxHeight, withT
preloader = message.media.preloader; preloader = message.media.preloader;
preloader.attach(container); preloader.attach(container);
noAutoDownload = undefined; noAutoDownload = undefined;
} else { } else if(!cacheContext.downloaded) {
preloader = new ProgressivePreloader({ preloader = new ProgressivePreloader({
attachMethod: 'prepend' attachMethod: 'prepend'
}); });
@ -834,8 +847,6 @@ export function wrapPhoto({photo, message, container, boxWidth, boxHeight, withT
appDocsManager.downloadDoc(photo, /* undefined, */lazyLoadQueue?.queueId) : appDocsManager.downloadDoc(photo, /* undefined, */lazyLoadQueue?.queueId) :
appPhotosManager.preloadPhoto(photo, size, lazyLoadQueue?.queueId, noAutoDownload); appPhotosManager.preloadPhoto(photo, size, lazyLoadQueue?.queueId, noAutoDownload);
noAutoDownload = undefined;
return promise; return promise;
}; };
@ -846,24 +857,36 @@ export function wrapPhoto({photo, message, container, boxWidth, boxHeight, withT
}; };
let loadPromise: Promise<any>; let loadPromise: Promise<any>;
const canAttachPreloader = (
(size as PhotoSize.photoSize).w >= 150 &&
(size as PhotoSize.photoSize).h >= 150
) || noAutoDownload;
const load = () => { const load = () => {
if(noAutoDownload && !withoutPreloader) { if(noAutoDownload && !withoutPreloader && preloader) {
preloader.construct(); preloader.construct();
preloader.setManual(); preloader.setManual();
} }
const promise = getDownloadPromise(); const promise = getDownloadPromise();
if(!cacheContext.downloaded && !withoutPreloader && (size as PhotoSize.photoSize).w >= 150 && (size as PhotoSize.photoSize).h >= 150) { if(preloader &&
!cacheContext.downloaded &&
!withoutPreloader &&
canAttachPreloader
) {
preloader.attach(container, false, promise); preloader.attach(container, false, promise);
} }
noAutoDownload = undefined;
const renderPromise = promise.then(onLoad); const renderPromise = promise.then(onLoad);
renderPromise.catch(() => {}); renderPromise.catch(() => {});
return {download: promise, render: renderPromise}; return {download: promise, render: renderPromise};
}; };
if(preloader) {
preloader.setDownloadFunction(load); preloader.setDownloadFunction(load);
}
if(cacheContext.downloaded) { if(cacheContext.downloaded) {
loadThumbPromise = loadPromise = load().render; loadThumbPromise = loadPromise = load().render;

2
src/scss/partials/_chatBubble.scss

@ -611,7 +611,7 @@ $bubble-margin: .25rem;
&-fitted { &-fitted {
background-color: transparent !important; background-color: transparent !important;
.thumbnail { > .thumbnail {
opacity: .8; opacity: .8;
} }
} }

Loading…
Cancel
Save