philomena/assets/js/utils/image.ts

143 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

2019-10-05 02:09:52 +02:00
import { clearEl } from './dom';
import store from './store';
2022-04-17 14:42:13 +02:00
function showVideoThumb(img: HTMLDivElement, size: string, uris: Record<string, string>) {
2019-10-05 02:09:52 +02:00
const thumbUri = uris[size];
const vidEl = img.querySelector('video');
if (!vidEl) return false;
const imgEl = img.querySelector('img');
if (!imgEl || imgEl.classList.contains('hidden')) return false;
imgEl.classList.add('hidden');
vidEl.innerHTML = `
<source src="${thumbUri}" type="video/webm"/>
<source src="${thumbUri.replace(/webm$/, 'mp4')}" type="video/mp4"/>
`;
vidEl.classList.remove('hidden');
vidEl.play();
2022-03-26 00:44:28 +01:00
const overlay = img.querySelector('.js-spoiler-info-overlay');
if (overlay) overlay.classList.add('hidden');
2019-10-05 02:09:52 +02:00
return true;
}
2022-03-26 00:44:28 +01:00
export function showThumb(img: HTMLDivElement) {
2019-10-05 02:09:52 +02:00
const size = img.dataset.size;
2022-03-26 00:44:28 +01:00
const urisString = img.dataset.uris;
2022-04-03 03:01:15 +02:00
if (!size || !urisString) return false;
2022-03-26 00:44:28 +01:00
2022-04-17 14:42:13 +02:00
const uris: Record<string, string> = JSON.parse(urisString);
2019-10-05 02:09:52 +02:00
const thumbUri = uris[size].replace(/webm$/, 'gif');
const picEl = img.querySelector('picture');
2022-04-17 14:42:13 +02:00
if (!picEl) return showVideoThumb(img, size, uris);
2019-10-05 02:09:52 +02:00
const imgEl = picEl.querySelector('img');
if (!imgEl || imgEl.src.indexOf(thumbUri) !== -1) return false;
if (store.get('serve_hidpi') && !thumbUri.endsWith('.gif')) {
// Check whether the HiDPI option is enabled, and make an exception for GIFs due to their size
const x2Size = size === 'medium' ? uris.large : uris.medium;
// use even larger thumb if normal size is medium already
imgEl.srcset = `${thumbUri} 1x, ${x2Size} 2x`;
2019-10-05 02:09:52 +02:00
}
imgEl.src = thumbUri;
2022-03-26 00:44:28 +01:00
const overlay = img.querySelector('.js-spoiler-info-overlay');
2022-04-03 03:01:15 +02:00
if (!overlay) return false;
2022-03-26 00:44:28 +01:00
2019-10-05 02:09:52 +02:00
if (uris[size].indexOf('.webm') !== -1) {
overlay.classList.remove('hidden');
overlay.innerHTML = 'WebM';
}
else {
2022-03-26 00:44:28 +01:00
overlay.classList.add('hidden');
2019-10-05 02:09:52 +02:00
}
return true;
}
2022-03-26 00:44:28 +01:00
export function showBlock(img: HTMLDivElement) {
img.querySelector('.image-filtered')?.classList.add('hidden');
const imageShowClasses = img.querySelector('.image-show')?.classList;
if (imageShowClasses) {
imageShowClasses.remove('hidden');
imageShowClasses.add('spoiler-pending');
}
2019-10-05 02:09:52 +02:00
}
2022-03-26 00:44:28 +01:00
function hideVideoThumb(img: HTMLDivElement, spoilerUri: string, reason: string) {
2019-10-05 02:09:52 +02:00
const vidEl = img.querySelector('video');
if (!vidEl) return;
const imgEl = img.querySelector('img');
const imgOverlay = img.querySelector('.js-spoiler-info-overlay');
if (!imgEl) return;
imgEl.classList.remove('hidden');
imgEl.src = spoilerUri;
2022-03-26 00:44:28 +01:00
if (imgOverlay) {
imgOverlay.innerHTML = reason;
imgOverlay.classList.remove('hidden');
}
2019-10-05 02:09:52 +02:00
clearEl(vidEl);
vidEl.classList.add('hidden');
vidEl.pause();
}
2022-03-26 00:44:28 +01:00
export function hideThumb(img: HTMLDivElement, spoilerUri: string, reason: string) {
2019-10-05 02:09:52 +02:00
const picEl = img.querySelector('picture');
if (!picEl) return hideVideoThumb(img, spoilerUri, reason);
const imgEl = picEl.querySelector('img');
const imgOverlay = img.querySelector('.js-spoiler-info-overlay');
if (!imgEl || imgEl.src.indexOf(spoilerUri) !== -1) return;
imgEl.srcset = '';
imgEl.src = spoilerUri;
2022-03-26 00:44:28 +01:00
if (imgOverlay) {
imgOverlay.innerHTML = reason;
imgOverlay.classList.remove('hidden');
}
2019-10-05 02:09:52 +02:00
}
2022-03-26 00:44:28 +01:00
export function spoilerThumb(img: HTMLDivElement, spoilerUri: string, reason: string) {
2019-10-05 02:09:52 +02:00
hideThumb(img, spoilerUri, reason);
switch (window.booru.spoilerType) {
case 'click':
img.addEventListener('click', event => { if (showThumb(img)) event.preventDefault(); });
img.addEventListener('mouseleave', () => hideThumb(img, spoilerUri, reason));
break;
case 'hover':
img.addEventListener('mouseenter', () => showThumb(img));
img.addEventListener('mouseleave', () => hideThumb(img, spoilerUri, reason));
break;
default:
break;
}
}
2022-03-26 00:44:28 +01:00
export function spoilerBlock(img: HTMLDivElement, spoilerUri: string, reason: string) {
2022-04-03 03:01:15 +02:00
const imgFiltered = img.querySelector('.image-filtered');
const imgEl = imgFiltered?.querySelector<HTMLImageElement>('img');
2019-10-05 02:09:52 +02:00
if (!imgEl) return;
2022-04-17 14:42:13 +02:00
const imgReason = img.querySelector<HTMLElement>('.filter-explanation');
const imageShow = img.querySelector('.image-show');
2019-10-05 02:09:52 +02:00
imgEl.src = spoilerUri;
2022-03-26 00:44:28 +01:00
if (imgReason) {
imgReason.innerHTML = reason;
}
2019-10-05 02:09:52 +02:00
2022-04-17 14:42:13 +02:00
imageShow?.classList.add('hidden');
2022-04-03 03:01:15 +02:00
if (imgFiltered) imgFiltered.classList.remove('hidden');
2019-10-05 02:09:52 +02:00
}