Asked for: "wenn man eingebundene bilder anklickt, maximiert werden (Großansicht)" and "im editor eine Vorschau haben. damit ich sehe wie es dann am ende aussieht". - assets/js/lightbox.js: event delegation for containers with data-lightbox-images - every picture in a wiki page opens the gallery's large view, paging through all pictures of the page with the alt text as caption; a picture that is itself a link keeps following the link. wiki.php loads it for the page content. - wiki_edit.php / assets/js/wiki-editor.js: view switch "Schreiben" / "Nebeneinander" / "Vorschau" (side by side is hidden below 900 px). The preview shows the page as the wiki will: same width next to a note instead of the table of contents, with path, number (new pages get the next free one), title and WIP mark from the form. - 'eye' icon, styles, patch note, README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
5.2 KiB
JavaScript
124 lines
5.2 KiB
JavaScript
/**
|
||
* Einfache Grossansicht fuer Bilder: Links mit data-lightbox="gruppe" oeffnen das Bild in
|
||
* einem Overlay, mit Pfeilen / Wischen durch die Bilder derselben Gruppe blaettern.
|
||
*
|
||
* In Bereichen mit data-lightbox-images (z.B. Wiki-Seiten) oeffnet jedes Bild die Grossansicht, das nicht selbst ein
|
||
* Link ist - geblaettert wird durch die Bilder dieses Bereichs, Bildunterschrift ist der Alternativtext. Die Klicks
|
||
* faengt das Dokument ab, so klappt es auch mit Inhalten, die erst spaeter dazukommen (Vorschau im Wiki-Editor,
|
||
* Live-Aktualisierung, erst nach Zustimmung geladene Bilder).
|
||
*/
|
||
(function () {
|
||
'use strict';
|
||
|
||
var overlay = null;
|
||
var img = null;
|
||
var caption = null;
|
||
var group = [];
|
||
var index = 0;
|
||
var touchX = null;
|
||
|
||
function build() {
|
||
overlay = document.createElement('div');
|
||
overlay.className = 'lightbox';
|
||
overlay.hidden = true;
|
||
overlay.innerHTML =
|
||
'<button type="button" class="lightbox-close" aria-label="Schließen">✕</button>' +
|
||
'<button type="button" class="lightbox-prev" aria-label="Vorheriges Bild">‹</button>' +
|
||
'<figure><img alt=""><figcaption></figcaption></figure>' +
|
||
'<button type="button" class="lightbox-next" aria-label="Nächstes Bild">›</button>';
|
||
document.body.appendChild(overlay);
|
||
img = overlay.querySelector('img');
|
||
caption = overlay.querySelector('figcaption');
|
||
|
||
overlay.addEventListener('click', function (e) {
|
||
if (e.target === overlay || e.target.classList.contains('lightbox-close')) close();
|
||
else if (e.target.classList.contains('lightbox-prev')) show(index - 1);
|
||
else if (e.target.classList.contains('lightbox-next')) show(index + 1);
|
||
});
|
||
overlay.addEventListener('touchstart', function (e) { touchX = e.touches[0].clientX; }, { passive: true });
|
||
overlay.addEventListener('touchend', function (e) {
|
||
if (touchX === null || group.length < 2) return;
|
||
var dx = e.changedTouches[0].clientX - touchX;
|
||
if (Math.abs(dx) > 50) show(index + (dx < 0 ? 1 : -1));
|
||
touchX = null;
|
||
});
|
||
}
|
||
|
||
/** Eintrag der Grossansicht aus einem Link mit data-lightbox */
|
||
function fromLink(link) {
|
||
return {
|
||
src: link.href,
|
||
caption: link.dataset.caption || '',
|
||
commentsUrl: link.dataset.commentsUrl || '',
|
||
commentsCount: parseInt(link.dataset.commentsCount || '0', 10)
|
||
};
|
||
}
|
||
|
||
/** Eintrag aus einem Bild in einem Bereich mit data-lightbox-images */
|
||
function fromImage(image) {
|
||
return { src: image.currentSrc || image.src, caption: image.alt || '', commentsUrl: '', commentsCount: 0 };
|
||
}
|
||
|
||
function show(i) {
|
||
index = (i + group.length) % group.length;
|
||
var item = group[index];
|
||
img.src = item.src;
|
||
img.alt = item.caption;
|
||
caption.textContent = item.caption;
|
||
// Galeriebilder mit Kommentaren: Link zur Bildseite
|
||
if (item.commentsUrl) {
|
||
var comments = document.createElement('a');
|
||
comments.className = 'lightbox-comments';
|
||
comments.href = item.commentsUrl;
|
||
comments.textContent = item.commentsCount > 0
|
||
? '💬 ' + item.commentsCount + (item.commentsCount === 1 ? ' Kommentar' : ' Kommentare')
|
||
: '💬 Kommentieren';
|
||
caption.appendChild(comments);
|
||
}
|
||
overlay.querySelector('.lightbox-prev').hidden = group.length < 2;
|
||
overlay.querySelector('.lightbox-next').hidden = group.length < 2;
|
||
}
|
||
|
||
function open(items, start) {
|
||
if (!overlay) build();
|
||
group = items;
|
||
overlay.hidden = false;
|
||
document.body.style.overflow = 'hidden';
|
||
show(Math.max(0, start));
|
||
}
|
||
|
||
function close() {
|
||
overlay.hidden = true;
|
||
document.body.style.overflow = '';
|
||
img.src = '';
|
||
}
|
||
|
||
document.addEventListener('click', function (e) {
|
||
// Mit gedrueckter Taste (neuer Tab usw.) bleibt es beim normalen Link
|
||
if (e.defaultPrevented || e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
|
||
var link = e.target.closest('a[data-lightbox]');
|
||
if (link) {
|
||
e.preventDefault();
|
||
var links = Array.prototype.filter.call(document.querySelectorAll('a[data-lightbox]'), function (l) {
|
||
return l.dataset.lightbox === link.dataset.lightbox;
|
||
});
|
||
open(links.map(fromLink), links.indexOf(link));
|
||
return;
|
||
}
|
||
var image = e.target.closest('[data-lightbox-images] img');
|
||
if (image && !image.closest('a')) {
|
||
e.preventDefault();
|
||
var images = Array.prototype.filter.call(image.closest('[data-lightbox-images]').querySelectorAll('img'), function (i) {
|
||
return !i.closest('a');
|
||
});
|
||
open(images.map(fromImage), images.indexOf(image));
|
||
}
|
||
});
|
||
|
||
document.addEventListener('keydown', function (e) {
|
||
if (!overlay || overlay.hidden) return;
|
||
if (e.key === 'Escape') close();
|
||
else if (e.key === 'ArrowLeft') show(index - 1);
|
||
else if (e.key === 'ArrowRight') show(index + 1);
|
||
});
|
||
})();
|