Add media-shared-assets package (from MediaCollection/MediaShared)
Browser theme CSS/JS assets shared across MediaCollection's client apps, moved here per owner request to centralize shared components. The build:theme sync script now lives inside the package itself rather than assuming a monorepo-root scripts/ sibling; its 'test' script is a no-op placeholder since the package has no tests of its own yet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94af6d7601
commit
79d9461769
38 files changed
+10807
No files matched your search
@@ -0,0 +1,481 @@
|
||||
// NCMediaPreview - shared fullscreen image / video preview component.
|
||||
// Served at /shared/js/media/media-preview.js by applySharedBase.
|
||||
//
|
||||
// Two modes:
|
||||
// attach(opts) -> attach to existing overlay + frame elements in the DOM.
|
||||
// Returns a controller with { open, close, prev, next,
|
||||
// setItems, isOpen, currentIndex, preloader, destroy }.
|
||||
//
|
||||
// lightbox(opts) -> dynamically create a minimal single-image lightbox.
|
||||
// Returns { open(url, alt), close, destroy }.
|
||||
//
|
||||
// attach() opts:
|
||||
// overlayEl {Element} required - fixed fullscreen overlay div
|
||||
// frameEl {Element} required - inner div where img/video is mounted
|
||||
// preloadRadius {number} default 2 - neighbors to eagerly preload
|
||||
// loop {boolean} default false - wrap-around navigation
|
||||
// renderItem {function(frameEl, item)} - custom render; used instead of
|
||||
// default img/video creation when provided
|
||||
// onClose {function()}
|
||||
// onNavigate {function(idx, item)}
|
||||
//
|
||||
// Items shape for setItems(): [{ url, thumbnail?, name?, mediaType? }]
|
||||
// mediaType 'video' forces video element; otherwise URL extension is used.
|
||||
//
|
||||
// lightbox() opts:
|
||||
// id {string} default 'ncLightboxOverlay'
|
||||
// zIndex {string} default 'z-50'
|
||||
// imgClass {string} default: centered object-contain image style
|
||||
|
||||
(function (global) {
|
||||
'use strict';
|
||||
|
||||
var VIDEO_EXT = /\.(mp4|webm|mov|avi|mkv)$/i;
|
||||
var AUDIO_EXT = /\.(mp3|flac|wav|ogg|aac|m4a|opus|wma|aiff)$/i;
|
||||
|
||||
/** Returns true when the URL path has a recognised video extension. */
|
||||
function isVideo(url) {
|
||||
return VIDEO_EXT.test(String(url || '').split('?')[0]);
|
||||
}
|
||||
|
||||
/** Returns true when the URL path has a recognised audio extension. */
|
||||
function isAudio(url) {
|
||||
return AUDIO_EXT.test(String(url || '').split('?')[0]);
|
||||
}
|
||||
|
||||
/** Build a simple image-preload cache. */
|
||||
function makePreloader() {
|
||||
var cache = new Map();
|
||||
return {
|
||||
load: function (url) {
|
||||
if (!url) return Promise.reject(new Error('no url'));
|
||||
if (cache.has(url)) return cache.get(url);
|
||||
var p = new Promise(function (res, rej) {
|
||||
var im = new Image();
|
||||
im.onload = function () {
|
||||
res(im);
|
||||
};
|
||||
im.onerror = rej;
|
||||
im.src = url;
|
||||
});
|
||||
cache.set(url, p);
|
||||
return p;
|
||||
},
|
||||
clear: function () {
|
||||
cache.clear();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Shared CSS for the media element inside the frame. */
|
||||
var MEDIA_CLS =
|
||||
'max-h-full max-w-full rounded-md bg-black object-contain shadow-[var(--shadow-2xl,0_10px_40px_rgba(0,0,0,.6))]';
|
||||
|
||||
/**
|
||||
* Keep fullscreen media inside comfortable insets so content is not clipped
|
||||
* by overlay edges or covered by overlay chrome.
|
||||
*/
|
||||
function applySafeMediaLayout(node) {
|
||||
if (!node) return;
|
||||
try {
|
||||
node.style.width = 'auto';
|
||||
node.style.height = 'auto';
|
||||
node.style.maxWidth = 'calc(100% - 1.5rem)';
|
||||
node.style.maxHeight = 'calc(100% - 1.5rem)';
|
||||
node.style.objectFit = 'contain';
|
||||
node.style.objectPosition = 'center';
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle overlay visibility using both class-based and attribute-based
|
||||
* strategies so it works with all existing overlay markup in the codebase.
|
||||
*/
|
||||
function setVisible(overlayEl, visible) {
|
||||
if (!overlayEl) return;
|
||||
if (visible) {
|
||||
overlayEl.classList.remove('hidden');
|
||||
overlayEl.classList.add('flex', 'is-active');
|
||||
overlayEl.style.removeProperty('display');
|
||||
overlayEl.hidden = false;
|
||||
overlayEl.setAttribute('aria-hidden', 'false');
|
||||
try {
|
||||
document.body.classList.add('overflow-hidden');
|
||||
document.documentElement.classList.add('overflow-hidden');
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
} else {
|
||||
overlayEl.classList.remove('flex', 'is-active');
|
||||
overlayEl.classList.add('hidden');
|
||||
overlayEl.style.setProperty('display', 'none', 'important');
|
||||
overlayEl.hidden = true;
|
||||
overlayEl.setAttribute('aria-hidden', 'true');
|
||||
try {
|
||||
document.body.classList.remove('overflow-hidden');
|
||||
document.documentElement.classList.remove('overflow-hidden');
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
// -- attach() ----------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Attach a media preview controller to existing overlay + frame DOM elements.
|
||||
*
|
||||
* The component automatically wires:
|
||||
* • [data-overlay-prev] -> prev()
|
||||
* • [data-overlay-next] -> next()
|
||||
* • [aria-label="Close"] -> close() (searched inside overlayEl)
|
||||
* • Backdrop click (click directly on overlayEl) -> close()
|
||||
* • Keyboard: Escape -> close, ArrowLeft -> prev, ArrowRight -> next
|
||||
*
|
||||
* @param {object} opts
|
||||
* @returns controller
|
||||
*/
|
||||
function attach(opts) {
|
||||
opts = opts || {};
|
||||
var overlayEl = opts.overlayEl;
|
||||
var frameEl = opts.frameEl;
|
||||
if (!overlayEl || !frameEl) {
|
||||
throw new Error('NCMediaPreview.attach: overlayEl and frameEl are required');
|
||||
}
|
||||
|
||||
var radius = Math.max(0, Number(opts.preloadRadius) || 2);
|
||||
var loop = !!opts.loop;
|
||||
var items = []; // [{ url, thumbnail, name, mediaType }]
|
||||
var _idx = -1;
|
||||
var _open = false;
|
||||
var preloader = makePreloader();
|
||||
|
||||
// Auto-discover standard buttons within the overlay
|
||||
var closeBtn = overlayEl.querySelector('[aria-label="Close"]');
|
||||
var prevBtn = overlayEl.querySelector('[data-overlay-prev]');
|
||||
var nextBtn = overlayEl.querySelector('[data-overlay-next]');
|
||||
|
||||
var hasInFrameClose = !!(closeBtn && frameEl.contains(closeBtn));
|
||||
|
||||
// Reserve safe space inside the frame so media is never hidden under
|
||||
// in-frame close controls and never touches overlay boundaries.
|
||||
try {
|
||||
frameEl.style.boxSizing = 'border-box';
|
||||
frameEl.style.padding = hasInFrameClose ? '3.25rem 0.75rem 0.75rem 0.75rem' : '0.75rem';
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
|
||||
function applyFramePaddingForItem(item) {
|
||||
try {
|
||||
if (!hasInFrameClose) {
|
||||
frameEl.style.padding = '0.75rem';
|
||||
return;
|
||||
}
|
||||
|
||||
var kind = String((item && item.mediaType) || '').toLowerCase();
|
||||
var url = String((item && item.url) || '');
|
||||
var isImageLike = kind !== 'video' && kind !== 'audio' && !isVideo(url) && !isAudio(url);
|
||||
|
||||
// For image previews, keep vertical insets symmetric.
|
||||
frameEl.style.padding = isImageLike
|
||||
? '3.25rem 0.75rem 3.25rem 0.75rem'
|
||||
: '3.25rem 0.75rem 0.75rem 0.75rem';
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Ensure overlays always start hidden after attach in case responsive
|
||||
// display utilities override class-based hidden rules on first paint.
|
||||
setVisible(overlayEl, false);
|
||||
|
||||
function updateButtons() {
|
||||
if (prevBtn) prevBtn.disabled = loop ? false : _idx <= 0;
|
||||
if (nextBtn) nextBtn.disabled = loop ? false : _idx >= items.length - 1;
|
||||
}
|
||||
|
||||
/** Pause + remove any img/video/audio children from the frame. */
|
||||
function clearFrame() {
|
||||
Array.from(frameEl.querySelectorAll('video,audio')).forEach(function (v) {
|
||||
try {
|
||||
v.pause();
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
});
|
||||
Array.from(frameEl.querySelectorAll('img,video,audio')).forEach(function (n) {
|
||||
if (n.parentNode === frameEl) frameEl.removeChild(n);
|
||||
});
|
||||
}
|
||||
|
||||
/** Mount the media element for the given item. */
|
||||
function renderFrame(item) {
|
||||
clearFrame();
|
||||
if (!item) return;
|
||||
applyFramePaddingForItem(item);
|
||||
|
||||
// Delegate to caller-supplied renderer if provided
|
||||
if (opts.renderItem) {
|
||||
try {
|
||||
opts.renderItem(frameEl, item);
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
return;
|
||||
}
|
||||
|
||||
if (isVideo(item.url) || item.mediaType === 'video') {
|
||||
var v = document.createElement('video');
|
||||
v.className = MEDIA_CLS;
|
||||
v.src = item.url;
|
||||
v.controls = true;
|
||||
v.autoplay = true;
|
||||
v.loop = true;
|
||||
v.muted = false;
|
||||
v.playsInline = true;
|
||||
applySafeMediaLayout(v);
|
||||
frameEl.appendChild(v);
|
||||
} else if (isAudio(item.url) || item.mediaType === 'audio') {
|
||||
var a = document.createElement('audio');
|
||||
a.src = item.url;
|
||||
a.controls = true;
|
||||
a.autoplay = true;
|
||||
a.style.minWidth = '320px';
|
||||
a.style.maxWidth = '90vw';
|
||||
frameEl.appendChild(a);
|
||||
} else {
|
||||
var img = document.createElement('img');
|
||||
img.className = MEDIA_CLS;
|
||||
img.alt = item.name || 'Media preview';
|
||||
applySafeMediaLayout(img);
|
||||
frameEl.appendChild(img);
|
||||
// Show thumbnail immediately, swap to full-res when ready
|
||||
if (item.thumbnail) img.src = item.thumbnail;
|
||||
var capturedIdx = _idx;
|
||||
preloader
|
||||
.load(item.url)
|
||||
.then(function () {
|
||||
img.src = item.url;
|
||||
})
|
||||
.catch(function () {
|
||||
img.src = item.url;
|
||||
})
|
||||
.finally(function () {
|
||||
// Preload neighbors after current is resolved
|
||||
for (var d = 1; d <= radius; d++) {
|
||||
var li = capturedIdx - d,
|
||||
ri = capturedIdx + d;
|
||||
if (li >= 0 && items[li]) preloader.load(items[li].url).catch(function () {});
|
||||
if (ri < items.length && items[ri])
|
||||
preloader.load(items[ri].url).catch(function () {});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function open(i) {
|
||||
if (!items.length) return;
|
||||
_idx = Math.max(0, Math.min(i, items.length - 1));
|
||||
_open = true;
|
||||
renderFrame(items[_idx]);
|
||||
setVisible(overlayEl, true);
|
||||
updateButtons();
|
||||
if (opts.onNavigate)
|
||||
try {
|
||||
opts.onNavigate(_idx, items[_idx]);
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function close() {
|
||||
_open = false;
|
||||
clearFrame();
|
||||
_idx = -1;
|
||||
setVisible(overlayEl, false);
|
||||
if (opts.onClose)
|
||||
try {
|
||||
opts.onClose();
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function prev() {
|
||||
if (!_open || !items.length) return;
|
||||
var n = loop ? (_idx - 1 + items.length) % items.length : _idx - 1;
|
||||
if (!loop && n < 0) return;
|
||||
_idx = n;
|
||||
renderFrame(items[_idx]);
|
||||
updateButtons();
|
||||
if (opts.onNavigate)
|
||||
try {
|
||||
opts.onNavigate(_idx, items[_idx]);
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function next() {
|
||||
if (!_open || !items.length) return;
|
||||
var n = loop ? (_idx + 1) % items.length : _idx + 1;
|
||||
if (!loop && n >= items.length) return;
|
||||
_idx = n;
|
||||
renderFrame(items[_idx]);
|
||||
updateButtons();
|
||||
if (opts.onNavigate)
|
||||
try {
|
||||
opts.onNavigate(_idx, items[_idx]);
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the item list. Clears the preload cache.
|
||||
* Must be called with the full up-to-date list before open().
|
||||
*/
|
||||
function setItems(newItems) {
|
||||
items = Array.isArray(newItems) ? newItems : [];
|
||||
preloader.clear();
|
||||
}
|
||||
|
||||
// Wire built-in buttons
|
||||
if (closeBtn) closeBtn.addEventListener('click', close);
|
||||
if (prevBtn) prevBtn.addEventListener('click', prev);
|
||||
if (nextBtn) nextBtn.addEventListener('click', next);
|
||||
|
||||
// Backdrop click - close when clicking directly on the overlay backdrop
|
||||
overlayEl.addEventListener('click', function (e) {
|
||||
if (e.target === overlayEl) close();
|
||||
});
|
||||
|
||||
// Keyboard - Escape / arrows
|
||||
function onKey(e) {
|
||||
if (!_open) return;
|
||||
if (e.key === 'Escape') close();
|
||||
else if (e.key === 'ArrowLeft') prev();
|
||||
else if (e.key === 'ArrowRight') next();
|
||||
}
|
||||
window.addEventListener('keydown', onKey);
|
||||
|
||||
var ctrl = {
|
||||
open: open,
|
||||
close: close,
|
||||
prev: prev,
|
||||
next: next,
|
||||
setItems: setItems,
|
||||
isOpen: function () {
|
||||
return _open;
|
||||
},
|
||||
preloader: preloader,
|
||||
destroy: function () {
|
||||
window.removeEventListener('keydown', onKey);
|
||||
},
|
||||
};
|
||||
Object.defineProperty(ctrl, 'currentIndex', {
|
||||
get: function () {
|
||||
return _idx;
|
||||
},
|
||||
});
|
||||
return ctrl;
|
||||
}
|
||||
|
||||
// -- lightbox() --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Build a minimal single-item image lightbox that creates its own DOM.
|
||||
* Useful for cases where no overlay markup exists (e.g. MediaModerator).
|
||||
*
|
||||
* @param {object} [opts] { id, zIndex, imgClass }
|
||||
* @returns {{ open(url, alt?), close, destroy }}
|
||||
*/
|
||||
function lightbox(opts) {
|
||||
opts = opts || {};
|
||||
var id = opts.id || 'ncLightboxOverlay';
|
||||
var zIndex = opts.zIndex || 'z-50';
|
||||
var imgCls =
|
||||
opts.imgClass ||
|
||||
'block max-h-full max-w-full rounded-lg object-contain shadow-[0_8px_24px_rgba(0,0,0,0.5)]';
|
||||
|
||||
var el = null,
|
||||
img = null;
|
||||
|
||||
function ensure() {
|
||||
if (el) return;
|
||||
el = document.createElement('div');
|
||||
el.id = id;
|
||||
el.setAttribute('role', 'dialog');
|
||||
el.setAttribute('aria-modal', 'true');
|
||||
el.className = [
|
||||
'fixed inset-0',
|
||||
zIndex,
|
||||
'hidden items-center justify-center bg-black/85 p-4',
|
||||
].join(' ');
|
||||
|
||||
var inner = document.createElement('div');
|
||||
inner.className = 'relative flex h-full w-full items-center justify-center';
|
||||
|
||||
var closeBtn = document.createElement('button');
|
||||
closeBtn.type = 'button';
|
||||
closeBtn.setAttribute('aria-label', 'Close');
|
||||
closeBtn.className =
|
||||
'absolute right-4 top-4 z-10 flex h-9 w-9 cursor-pointer items-center justify-center rounded-full border border-slate-700 bg-black text-white';
|
||||
closeBtn.innerHTML =
|
||||
'<svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/></svg>';
|
||||
closeBtn.addEventListener('click', close);
|
||||
|
||||
img = document.createElement('img');
|
||||
img.alt = 'Preview';
|
||||
img.className = imgCls;
|
||||
|
||||
inner.appendChild(closeBtn);
|
||||
inner.appendChild(img);
|
||||
el.appendChild(inner);
|
||||
document.body.appendChild(el);
|
||||
|
||||
el.addEventListener('click', function (e) {
|
||||
if (e.target === el) close();
|
||||
});
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Escape' && !el.classList.contains('hidden')) close();
|
||||
});
|
||||
}
|
||||
|
||||
function open(url, alt) {
|
||||
ensure();
|
||||
img.src = url || '';
|
||||
if (alt) img.alt = alt;
|
||||
applySafeMediaLayout(img);
|
||||
try {
|
||||
// Use equal viewport insets so image top and bottom spacing match.
|
||||
img.style.maxWidth = 'calc(100vw - 4rem)';
|
||||
img.style.maxHeight = 'calc(100vh - 4rem)';
|
||||
img.style.margin = '0 auto';
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
el.classList.remove('hidden');
|
||||
el.classList.add('flex');
|
||||
try {
|
||||
document.body.classList.add('overflow-hidden');
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function close() {
|
||||
if (!el) return;
|
||||
el.classList.add('hidden');
|
||||
el.classList.remove('flex');
|
||||
img.src = '';
|
||||
try {
|
||||
document.body.classList.remove('overflow-hidden');
|
||||
// eslint-disable-next-line no-empty
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function destroy() {
|
||||
close();
|
||||
if (el) {
|
||||
el.remove();
|
||||
el = null;
|
||||
img = null;
|
||||
}
|
||||
}
|
||||
|
||||
return { open: open, close: close, destroy: destroy };
|
||||
}
|
||||
|
||||
global.NCMediaPreview = { attach: attach, lightbox: lightbox, isVideo: isVideo };
|
||||
})(window);
|
||||
Reference in new issue
Block a user