mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-19 03:44:23 +01:00
Pause spoilered videos on image pages, unpause on unspoiler (#259)
* Pause spoilered videos on image pages, unpause on unspoiler * Add test * Fix comment
This commit is contained in:
parent
7592b0f4d9
commit
65da36369d
3 changed files with 22 additions and 2 deletions
|
@ -87,11 +87,12 @@ function pickAndResize(elem) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const muted = store.get('unmute_videos') ? '' : 'muted';
|
const muted = store.get('unmute_videos') ? '' : 'muted';
|
||||||
|
const autoplay = elem.classList.contains('hidden') ? '' : 'autoplay'; // Fix for spoilered image pages
|
||||||
|
|
||||||
if (imageFormat === 'mp4') {
|
if (imageFormat === 'mp4') {
|
||||||
elem.classList.add('full-height');
|
elem.classList.add('full-height');
|
||||||
elem.insertAdjacentHTML('afterbegin',
|
elem.insertAdjacentHTML('afterbegin',
|
||||||
`<video controls autoplay loop ${muted} playsinline preload="auto" id="image-display"
|
`<video controls ${autoplay} loop ${muted} playsinline preload="auto" id="image-display"
|
||||||
width="${imageWidth}" height="${imageHeight}">
|
width="${imageWidth}" height="${imageHeight}">
|
||||||
<source src="${uris.webm}" type="video/webm">
|
<source src="${uris.webm}" type="video/webm">
|
||||||
<source src="${uris.mp4}" type="video/mp4">
|
<source src="${uris.mp4}" type="video/mp4">
|
||||||
|
@ -104,7 +105,7 @@ function pickAndResize(elem) {
|
||||||
}
|
}
|
||||||
else if (imageFormat === 'webm') {
|
else if (imageFormat === 'webm') {
|
||||||
elem.insertAdjacentHTML('afterbegin',
|
elem.insertAdjacentHTML('afterbegin',
|
||||||
`<video controls autoplay loop ${muted} playsinline id="image-display">
|
`<video controls ${autoplay} loop ${muted} playsinline id="image-display">
|
||||||
<source src="${uri}" type="video/webm">
|
<source src="${uri}" type="video/webm">
|
||||||
<source src="${uri.replace(/webm$/, 'mp4')}" type="video/mp4">
|
<source src="${uri.replace(/webm$/, 'mp4')}" type="video/mp4">
|
||||||
<p class="block block--fixed block--warning">
|
<p class="block block--fixed block--warning">
|
||||||
|
|
|
@ -369,6 +369,19 @@ describe('Image utils', () => {
|
||||||
expect(mockShowElement).toHaveClass(spoilerPendingClass);
|
expect(mockShowElement).toHaveClass(spoilerPendingClass);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should play the video if it is present', () => {
|
||||||
|
const mockElement = document.createElement('div');
|
||||||
|
const { mockShowElement } = createImageShowElement(mockElement);
|
||||||
|
const mockVideo = document.createElement('video');
|
||||||
|
mockShowElement.appendChild(mockVideo);
|
||||||
|
|
||||||
|
const playSpy = vi.spyOn(mockVideo, 'play').mockReturnValue(Promise.resolve());
|
||||||
|
|
||||||
|
showBlock(mockElement);
|
||||||
|
|
||||||
|
expect(playSpy).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
it('should not throw if image-filtered element is missing', () => {
|
it('should not throw if image-filtered element is missing', () => {
|
||||||
const mockElement = document.createElement('div');
|
const mockElement = document.createElement('div');
|
||||||
createImageShowElement(mockElement);
|
createImageShowElement(mockElement);
|
||||||
|
|
|
@ -64,9 +64,15 @@ export function showThumb(img: HTMLDivElement) {
|
||||||
export function showBlock(img: HTMLDivElement) {
|
export function showBlock(img: HTMLDivElement) {
|
||||||
img.querySelector('.image-filtered')?.classList.add('hidden');
|
img.querySelector('.image-filtered')?.classList.add('hidden');
|
||||||
const imageShowClasses = img.querySelector('.image-show')?.classList;
|
const imageShowClasses = img.querySelector('.image-show')?.classList;
|
||||||
|
|
||||||
if (imageShowClasses) {
|
if (imageShowClasses) {
|
||||||
imageShowClasses.remove('hidden');
|
imageShowClasses.remove('hidden');
|
||||||
imageShowClasses.add('spoiler-pending');
|
imageShowClasses.add('spoiler-pending');
|
||||||
|
|
||||||
|
const vidEl = img.querySelector('video');
|
||||||
|
if (vidEl) {
|
||||||
|
vidEl.play();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue