Files
hub/public/image-viewer.js
T
2026-10-28 12:53:26 -04:00

30 lines
1.9 KiB
JavaScript

'use strict';
// Photos in Signal and Chat open in a full-screen viewer inside the app instead of a new tab.
// Close with the X, a tap outside the photo, Escape, or the phone's Back gesture; the chat is right where it was.
(function () {
let box = null, pushed = false;
function close(fromPop) {
if (!box) return; box.remove(); box = null; document.removeEventListener('keydown', onKey, true);
if (pushed && !fromPop) { pushed = false; history.back(); } else pushed = false;
}
const onKey = e => { if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); close(); } };
function open(src, name) {
close();
box = document.createElement('div'); box.id = 'imgview'; box.setAttribute('role', 'dialog'); box.setAttribute('aria-modal', 'true'); box.setAttribute('aria-label', name || 'Photo');
box.innerHTML = '<button type="button" class="ibtn ivx" aria-label="Close photo"></button><a class="ivsave" download aria-label="Save photo">Save</a><img alt="">';
const x = box.querySelector('.ivx'); x.innerHTML = typeof icon === 'function' ? icon('x') : '✕';
const im = box.querySelector('img'); im.alt = name || 'Photo'; im.src = src;
const sv = box.querySelector('.ivsave'); sv.href = src; if (name) sv.setAttribute('download', name);
x.onclick = () => close();
box.onclick = e => { if (e.target === box) close(); };
document.body.appendChild(box); document.addEventListener('keydown', onKey, true); x.focus({ preventScroll: true });
try { history.pushState({ imgview: 1 }, ''); pushed = true; } catch { /* no history: the X still closes it */ }
}
addEventListener('popstate', () => { if (box) { pushed = false; close(true); } });
document.addEventListener('click', e => {
const im = e.target.closest && e.target.closest('img.sgimg'); if (!im) return;
const a = im.closest('a[href]'); if (!a) return;
e.preventDefault(); open(a.getAttribute('href'), im.alt);
});
})();