first commit
This commit is contained in:
commit
8c4dcae475
89 changed files with 24623 additions and 0 deletions
415
static/main.js
Normal file
415
static/main.js
Normal file
|
|
@ -0,0 +1,415 @@
|
|||
// saved custom CSS; ?nocss=1 clears it
|
||||
(function () {
|
||||
try {
|
||||
if (/[?&]nocss=1(?:&|$)/.test(location.search)) {
|
||||
localStorage.removeItem('snowchive-custom-css');
|
||||
return;
|
||||
}
|
||||
var css = localStorage.getItem('snowchive-custom-css') || '';
|
||||
if (!css) return;
|
||||
var el = document.createElement('style');
|
||||
el.id = 'snowchive-custom-css';
|
||||
el.textContent = css;
|
||||
document.head.appendChild(el);
|
||||
} catch (e) {}
|
||||
})();
|
||||
|
||||
function snowchiveStorage() {
|
||||
try { return window.localStorage || null; } catch (e) { return null; }
|
||||
}
|
||||
|
||||
function isTyping(el) {
|
||||
return !!el && (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName) || el.isContentEditable);
|
||||
}
|
||||
|
||||
// animated thumb rollover
|
||||
(function () {
|
||||
if (window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches) return;
|
||||
const gifThumbs = Array.from(document.querySelectorAll('.thumb img[data-gif], .result-thumb img[data-gif]'));
|
||||
gifThumbs.forEach(img => {
|
||||
img.dataset.still = img.getAttribute('src');
|
||||
const gif = img.getAttribute('data-gif');
|
||||
|
||||
img.addEventListener('mouseenter', () => img.setAttribute('src', gif));
|
||||
img.addEventListener('mouseleave', () => img.setAttribute('src', img.dataset.still));
|
||||
|
||||
img.addEventListener('touchstart', () => {
|
||||
gifThumbs.forEach(o => {
|
||||
if (o !== img) o.setAttribute('src', o.dataset.still);
|
||||
});
|
||||
img.setAttribute('src', gif);
|
||||
}, { passive: true });
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
// card page-preview panel (opt-in, desktop)
|
||||
(function () {
|
||||
if (!window.matchMedia || !matchMedia('(hover: hover) and (pointer: fine)').matches) return;
|
||||
if (!document.querySelector('[data-pp-id]')) return;
|
||||
|
||||
var panel = null, activeEl = null, pages = [], idx = 0, docId = '', timer = null, dragging = false, pinned = false;
|
||||
var openTimer = null;
|
||||
var WKEY = 'snowchive.pp.width', WMIN = 300, WDEFAULT = 460;
|
||||
|
||||
function imgURL(name) { return '/files/' + docId + '/images/' + name; }
|
||||
function cancelClose() { if (timer) { clearTimeout(timer); timer = null; } }
|
||||
function scheduleClose() { if (dragging || pinned) return; cancelClose(); timer = setTimeout(close_, 220); }
|
||||
|
||||
function maxW() { return Math.round(window.innerWidth * 0.9); }
|
||||
function clampW(w) { return Math.max(WMIN, Math.min(w, maxW())); }
|
||||
function loadW() { try { var v = parseInt(localStorage.getItem(WKEY), 10); if (v) return v; } catch (e) {} return WDEFAULT; }
|
||||
function saveW(w) { try { localStorage.setItem(WKEY, String(w)); } catch (e) {} }
|
||||
function applyW(w) { if (panel) panel.style.width = clampW(w) + 'px'; }
|
||||
|
||||
function build() {
|
||||
panel = document.createElement('div');
|
||||
panel.className = 'pp-panel';
|
||||
panel.innerHTML =
|
||||
'<div class="pp-resizer" title="Drag to resize (press s to pin first)"></div>' +
|
||||
'<div class="pp-stage"><img class="pp-img" alt=""></div>' +
|
||||
'<div class="pp-strip"></div>' +
|
||||
'<div class="pp-foot">' +
|
||||
'<span class="pp-counter"></span>' +
|
||||
'<span class="pp-hint pp-hint-free">h / ← prev · l / → next · <b>s</b> pin to resize</span>' +
|
||||
'<span class="pp-hint pp-hint-pinned">📌 pinned · h / ← prev · l / → next · <b>esc</b> close</span>' +
|
||||
'</div>';
|
||||
document.body.appendChild(panel);
|
||||
applyW(loadW());
|
||||
initResize();
|
||||
panel.addEventListener('mouseenter', cancelClose);
|
||||
panel.addEventListener('mouseleave', scheduleClose);
|
||||
}
|
||||
|
||||
function initResize() {
|
||||
var resizer = panel.querySelector('.pp-resizer');
|
||||
function onMove(e) { applyW(window.innerWidth - e.clientX); }
|
||||
function onUp() {
|
||||
dragging = false;
|
||||
resizer.classList.remove('pp-dragging');
|
||||
document.body.style.userSelect = '';
|
||||
document.removeEventListener('mousemove', onMove);
|
||||
document.removeEventListener('mouseup', onUp);
|
||||
saveW(parseInt(panel.style.width, 10));
|
||||
}
|
||||
resizer.addEventListener('mousedown', function (e) {
|
||||
e.preventDefault();
|
||||
dragging = true;
|
||||
cancelClose();
|
||||
resizer.classList.add('pp-dragging');
|
||||
document.body.style.userSelect = 'none';
|
||||
document.addEventListener('mousemove', onMove);
|
||||
document.addEventListener('mouseup', onUp);
|
||||
});
|
||||
}
|
||||
|
||||
function go(n) {
|
||||
if (!pages.length) return;
|
||||
idx = n < 0 ? 0 : (n >= pages.length ? pages.length - 1 : n);
|
||||
panel.querySelector('.pp-img').src = imgURL(pages[idx]);
|
||||
panel.querySelector('.pp-counter').textContent = (idx + 1) + ' / ' + pages.length;
|
||||
var strip = panel.querySelector('.pp-strip'), i;
|
||||
for (i = 0; i < strip.children.length; i++) {
|
||||
strip.children[i].classList.toggle('pp-cur', i === idx);
|
||||
}
|
||||
var cur = strip.children[idx];
|
||||
if (cur) {
|
||||
var cr = cur.getBoundingClientRect(), sr = strip.getBoundingClientRect();
|
||||
strip.scrollLeft += (cr.left + cr.width / 2) - (sr.left + sr.width / 2);
|
||||
}
|
||||
}
|
||||
|
||||
function buildStrip() {
|
||||
var strip = panel.querySelector('.pp-strip');
|
||||
strip.textContent = '';
|
||||
pages.forEach(function (name, i) {
|
||||
var t = new Image();
|
||||
t.className = 'pp-th';
|
||||
t.loading = 'lazy';
|
||||
t.src = imgURL(name);
|
||||
t.alt = 'Page ' + (i + 1);
|
||||
t.addEventListener('click', function () { go(i); });
|
||||
strip.appendChild(t);
|
||||
});
|
||||
}
|
||||
|
||||
function open_(el) {
|
||||
cancelClose();
|
||||
if (el === activeEl) return;
|
||||
var imgs = (el.getAttribute('data-pp-imgs') || '').split(' ').filter(Boolean);
|
||||
if (!imgs.length) { close_(); return; }
|
||||
activeEl = el;
|
||||
docId = el.getAttribute('data-pp-id');
|
||||
pages = imgs;
|
||||
if (!panel) build();
|
||||
buildStrip();
|
||||
go(0);
|
||||
panel.classList.add('pp-open');
|
||||
}
|
||||
|
||||
function close_() {
|
||||
cancelClose();
|
||||
pinned = false;
|
||||
if (panel) { panel.classList.remove('pp-open'); panel.classList.remove('pp-pinned'); }
|
||||
activeEl = null;
|
||||
}
|
||||
|
||||
document.addEventListener('mouseover', function (e) {
|
||||
if (dragging || pinned) return;
|
||||
var el = e.target.closest('[data-pp-id]');
|
||||
if (!el) return;
|
||||
if (el === activeEl) { cancelClose(); return; }
|
||||
clearTimeout(openTimer);
|
||||
openTimer = setTimeout(function () { open_(el); }, 150);
|
||||
});
|
||||
document.addEventListener('mouseout', function (e) {
|
||||
var el = e.target.closest('[data-pp-id]');
|
||||
if (!el) return;
|
||||
var to = e.relatedTarget;
|
||||
if (to && (el.contains(to) || (panel && panel.contains(to)))) return;
|
||||
clearTimeout(openTimer);
|
||||
scheduleClose();
|
||||
});
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (!activeEl) return;
|
||||
if (e.altKey || e.ctrlKey || e.metaKey) return;
|
||||
if (isTyping(document.activeElement)) return;
|
||||
switch (e.key) {
|
||||
case 'h': case 'ArrowLeft': go(idx - 1); e.preventDefault(); break;
|
||||
case 'l': case 'ArrowRight': go(idx + 1); e.preventDefault(); break;
|
||||
case 'Home': if (pinned) { go(0); e.preventDefault(); } break;
|
||||
case 'End': if (pinned) { go(pages.length - 1); e.preventDefault(); } break;
|
||||
case 's': case 'S':
|
||||
pinned = !pinned;
|
||||
panel.classList.toggle('pp-pinned', pinned);
|
||||
if (!pinned && !(panel.matches(':hover') || (activeEl && activeEl.matches(':hover')))) close_();
|
||||
e.preventDefault();
|
||||
break;
|
||||
case 'Escape': close_(); break;
|
||||
}
|
||||
});
|
||||
window.addEventListener('resize', function () { if (panel) applyW(parseInt(panel.style.width, 10) || WDEFAULT); });
|
||||
})();
|
||||
|
||||
// lightbox keyboard and swipe navigation
|
||||
(function () {
|
||||
var total = document.querySelectorAll('.lightbox:not(.lb-alt)').length;
|
||||
if (!total) return;
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (e.altKey || e.ctrlKey || e.metaKey) return;
|
||||
if (isTyping(e.target)) return;
|
||||
if (e.key === 'Escape' && /^#lb-a\d+-\d+$/.test(location.hash)) {
|
||||
location.hash = '#lb-closed';
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
var m = /^#lb-(\d+)$/.exec(location.hash);
|
||||
if (!m) return;
|
||||
var n = parseInt(m[1], 10);
|
||||
switch (e.key) {
|
||||
case 'ArrowLeft': case 'h':
|
||||
if (n > 1) location.hash = '#lb-' + (n - 1);
|
||||
e.preventDefault();
|
||||
break;
|
||||
case 'ArrowRight': case 'l':
|
||||
if (n < total) location.hash = '#lb-' + (n + 1);
|
||||
e.preventDefault();
|
||||
break;
|
||||
case 'Escape':
|
||||
location.hash = '#lb-closed';
|
||||
e.preventDefault();
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('hashchange', function (e) {
|
||||
var was = /#lb-(\d+)$/.exec(e.oldURL), is = /#lb-(\d+)$/.exec(e.newURL);
|
||||
if (is) {
|
||||
var close = document.querySelector('#lb-' + is[1] + ' .lb-close');
|
||||
if (close) close.focus({ preventScroll: true });
|
||||
} else if (was) {
|
||||
var thumbs = document.querySelectorAll('a[href="#lb-' + was[1] + '"]');
|
||||
for (var i = 0; i < thumbs.length; i++) {
|
||||
if (!thumbs[i].closest('.lightbox')) { thumbs[i].focus(); break; }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var tx = 0, ty = 0;
|
||||
document.addEventListener('touchstart', function (e) {
|
||||
if (e.touches.length > 1) { tx = NaN; return; }
|
||||
tx = e.changedTouches[0].clientX;
|
||||
ty = e.changedTouches[0].clientY;
|
||||
}, { passive: true });
|
||||
document.addEventListener('touchend', function (e) {
|
||||
var m = /^#lb-(\d+)$/.exec(location.hash);
|
||||
if (!m) return;
|
||||
var dx = e.changedTouches[0].clientX - tx;
|
||||
var dy = e.changedTouches[0].clientY - ty;
|
||||
if (!(Math.abs(dx) >= 50) || Math.abs(dy) > Math.abs(dx)) return;
|
||||
var n = parseInt(m[1], 10);
|
||||
if (dx < 0 && n < total) location.hash = '#lb-' + (n + 1);
|
||||
else if (dx > 0 && n > 1) location.hash = '#lb-' + (n - 1);
|
||||
}, { passive: true });
|
||||
})();
|
||||
|
||||
// private per-document notes
|
||||
(function () {
|
||||
var box = document.getElementById('note-box');
|
||||
var localStorage = snowchiveStorage();
|
||||
if (!box || !localStorage) return;
|
||||
box.hidden = false;
|
||||
|
||||
var key = 'snowchive-note:' + box.dataset.docId;
|
||||
var ta = document.getElementById('note-text');
|
||||
var status = document.getElementById('note-status');
|
||||
|
||||
try {
|
||||
var saved = localStorage.getItem(key);
|
||||
if (saved) {
|
||||
var obj = JSON.parse(saved);
|
||||
ta.value = obj.text || '';
|
||||
if (ta.value) { box.open = true; status.textContent = '(saved)'; }
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
var t = null;
|
||||
function save() {
|
||||
clearTimeout(t);
|
||||
t = null;
|
||||
try {
|
||||
if (ta.value.trim() === '') {
|
||||
localStorage.removeItem(key);
|
||||
status.textContent = '';
|
||||
} else {
|
||||
localStorage.setItem(key, JSON.stringify({
|
||||
title: box.dataset.docTitle,
|
||||
text: ta.value,
|
||||
updated: new Date().toISOString()
|
||||
}));
|
||||
status.textContent = '(saved)';
|
||||
}
|
||||
} catch (e) { status.textContent = '(could not save)'; }
|
||||
}
|
||||
ta.addEventListener('input', function () {
|
||||
status.textContent = '...';
|
||||
clearTimeout(t);
|
||||
t = setTimeout(save, 400);
|
||||
});
|
||||
function flush() { if (t !== null) save(); }
|
||||
ta.addEventListener('change', flush);
|
||||
window.addEventListener('pagehide', flush);
|
||||
})();
|
||||
|
||||
// the Pinned page's note listing
|
||||
(function () {
|
||||
var box = document.getElementById('note-list-box');
|
||||
var list = document.getElementById('note-list');
|
||||
if (!box || !list) return;
|
||||
var localStorage = snowchiveStorage();
|
||||
if (!localStorage) return;
|
||||
|
||||
var PREFIX = 'snowchive-note:';
|
||||
var items = [];
|
||||
try {
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var k = localStorage.key(i);
|
||||
if (!k || k.indexOf(PREFIX) !== 0) continue;
|
||||
try {
|
||||
var obj = JSON.parse(localStorage.getItem(k));
|
||||
items.push({ id: k.slice(PREFIX.length), title: obj.title || k.slice(PREFIX.length),
|
||||
text: obj.text || '', updated: obj.updated || '' });
|
||||
} catch (e) {}
|
||||
}
|
||||
} catch (e) {}
|
||||
if (!items.length) return;
|
||||
items.sort(function (a, b) { return a.updated < b.updated ? 1 : -1; });
|
||||
|
||||
box.hidden = false;
|
||||
|
||||
items.forEach(function (it) {
|
||||
var li = document.createElement('li');
|
||||
li.className = 'note-item';
|
||||
|
||||
var a = document.createElement('a');
|
||||
a.href = '/doc/' + encodeURIComponent(it.id);
|
||||
a.textContent = it.title;
|
||||
li.appendChild(a);
|
||||
|
||||
if (it.updated) {
|
||||
var d = document.createElement('span');
|
||||
d.className = 'faint note-date';
|
||||
d.textContent = ' ' + it.updated.slice(0, 10);
|
||||
li.appendChild(d);
|
||||
}
|
||||
|
||||
var del = document.createElement('button');
|
||||
del.type = 'button';
|
||||
del.className = 'link-btn unpin-btn';
|
||||
del.textContent = 'delete';
|
||||
del.addEventListener('click', function () {
|
||||
if (!confirm('Delete this note?')) return;
|
||||
try { localStorage.removeItem(PREFIX + it.id); } catch (e) { return; }
|
||||
items.splice(items.indexOf(it), 1);
|
||||
li.remove();
|
||||
if (!list.children.length) box.hidden = true;
|
||||
});
|
||||
li.appendChild(del);
|
||||
|
||||
var p = document.createElement('div');
|
||||
p.className = 'note-text';
|
||||
p.textContent = it.text;
|
||||
li.appendChild(p);
|
||||
|
||||
list.appendChild(li);
|
||||
});
|
||||
|
||||
document.getElementById('note-export').addEventListener('click', function () {
|
||||
var blob = new Blob([JSON.stringify(items, null, 2)], { type: 'application/json' });
|
||||
var a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = 'snowchive-notes.json';
|
||||
a.click();
|
||||
// revoked later: Safari starts the download after click() returns
|
||||
setTimeout(function () { URL.revokeObjectURL(a.href); }, 40000);
|
||||
});
|
||||
})();
|
||||
|
||||
// the Preferences page's custom-CSS editor
|
||||
(function () {
|
||||
var localStorage = snowchiveStorage();
|
||||
if (!localStorage) return;
|
||||
var fs = document.getElementById('custom-css-details');
|
||||
var ta = document.getElementById('custom-css');
|
||||
var status = document.getElementById('custom-css-status');
|
||||
if (!fs || !ta || !status) return;
|
||||
fs.hidden = false;
|
||||
|
||||
var KEY = 'snowchive-custom-css';
|
||||
try {
|
||||
ta.value = localStorage.getItem(KEY) || '';
|
||||
} catch (e) {}
|
||||
|
||||
function apply() {
|
||||
var existing = document.getElementById('snowchive-custom-css');
|
||||
if (existing) existing.remove();
|
||||
if (ta.value) {
|
||||
var el = document.createElement('style');
|
||||
el.id = 'snowchive-custom-css';
|
||||
el.textContent = ta.value;
|
||||
document.head.appendChild(el);
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('custom-css-save').addEventListener('click', function () {
|
||||
try {
|
||||
if (ta.value) {
|
||||
localStorage.setItem(KEY, ta.value);
|
||||
} else {
|
||||
localStorage.removeItem(KEY);
|
||||
}
|
||||
apply();
|
||||
status.textContent = ta.value ? 'Saved and applied.' : 'Cleared - custom CSS is off.';
|
||||
} catch (e) { status.textContent = 'Could not save.'; }
|
||||
});
|
||||
})();
|
||||
2353
static/style.css
Normal file
2353
static/style.css
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue