(function () {
function applyTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
try { localStorage.setItem('arks-theme', theme); } catch (_) {}
}
function initTheme() {
var saved = null;
try { saved = localStorage.getItem('arks-theme'); } catch (_) {}
if (saved === 'light' || saved === 'dark') {
applyTheme(saved);
}
}
function hydrateDiffs() {
var containers = document.querySelectorAll('[data-diff2html-raw]');
if (!containers.length || typeof window.Diff2Html === 'undefined') return;
var mode = 'line-by-line';
try {
var saved = localStorage.getItem('arks-diff-mode');
if (saved === 'side-by-side' || saved === 'line-by-line') mode = saved;
} catch (_) {}
containers.forEach(function (el) {
var rawId = el.getAttribute('data-diff2html-raw');
var src = rawId ? document.getElementById(rawId) : null;
if (!src) return;
var diffText = (typeof src.value === 'string' ? src.value : src.textContent) || '';
if (!diffText.trim()) return;
try {
el.innerHTML = window.Diff2Html.html(diffText, {
drawFileList: true,
matching: 'lines',
outputFormat: mode,
colorScheme: document.documentElement.getAttribute('data-theme') === 'light' ? 'light' : 'dark'
});
var uni = document.getElementById('ssr-diff-unified');
var spl = document.getElementById('ssr-diff-split');
if (uni) uni.hidden = true;
if (spl) spl.hidden = true;
} catch (_) {}
});
}
function announceToast(msg) {
var region = document.getElementById('arks-toast');
if (!region) return;
region.textContent = msg;
}
function filterCmdItems(query) {
var q = (query || '').trim().toLowerCase();
document.querySelectorAll('#cmd-modal .cmd-item').forEach(function (el) {
var text = (el.textContent || '').toLowerCase();
el.hidden = Boolean(q) && text.indexOf(q) === -1;
});
}
function toggleCmdModal(show) {
var modal = document.getElementById('cmd-modal');
if (!modal) return;
var isNative = typeof modal.showModal === 'function';
if (show) {
if (isNative) {
if (!modal.open) modal.showModal();
} else {
modal.setAttribute('open', '');
}
var inp = document.getElementById('cmd-input');
if (inp) {
inp.value = '';
filterCmdItems('');
inp.focus();
}
} else if (isNative) {
if (modal.open) modal.close();
} else {
modal.removeAttribute('open');
}
}
function shortcutsDisabled() {
try { return localStorage.getItem('arks-shortcuts-disabled') === '1'; } catch (_) { return false; }
}
document.addEventListener('input', function (e) {
if (e.target && e.target.id === 'cmd-input') {
filterCmdItems(e.target.value);
}
});
document.addEventListener('click', function (e) {
var themeBtn = e.target.closest('[data-action="toggle-theme"]');
if (themeBtn) {
var cur = document.documentElement.getAttribute('data-theme') === 'light' ? 'light' : 'dark';
var next = cur === 'dark' ? 'light' : 'dark';
applyTheme(next);
hydrateDiffs();
announceToast('Theme switched to ' + next);
return;
}
var copyBtn = e.target.closest('[data-copy-target]');
if (copyBtn) {
var target = document.getElementById(copyBtn.getAttribute('data-copy-target'));
var txt = target ? target.textContent.trim() : '';
if (txt && navigator.clipboard) {
navigator.clipboard.writeText(txt).catch(function () {});
}
announceToast('Copied to clipboard');
return;
}
var protoBtn = e.target.closest('[data-clone-cmd]');
if (protoBtn) {
var cmdEl = document.getElementById('clone-cmd-text');
if (cmdEl) cmdEl.textContent = protoBtn.getAttribute('data-clone-cmd');
document.querySelectorAll('[data-clone-cmd]').forEach(function (b) {
b.classList.toggle('active', b === protoBtn);
});
return;
}
var diffModeBtn = e.target.closest('[data-diff-mode]');
if (diffModeBtn) {
var mode = diffModeBtn.getAttribute('data-diff-mode');
try { localStorage.setItem('arks-diff-mode', mode); } catch (_) {}
document.querySelectorAll('[data-diff-mode]').forEach(function (b) {
b.classList.toggle('active', b === diffModeBtn);
});
var uni = document.getElementById('ssr-diff-unified');
var spl = document.getElementById('ssr-diff-split');
if (uni && spl && typeof window.Diff2Html === 'undefined') {
uni.hidden = mode !== 'line-by-line';
spl.hidden = mode !== 'side-by-side';
}
hydrateDiffs();
return;
}
if (e.target.closest('[data-action="open-cmd"]')) {
toggleCmdModal(true);
return;
}
if (e.target.id === 'cmd-modal' || e.target.closest('[data-action="close-cmd"]')) {
toggleCmdModal(false);
return;
}
var ln = e.target.closest('.ln-anchor');
if (ln) {
document.querySelectorAll('tr.line-hl').forEach(function (r) { r.classList.remove('line-hl'); });
var tr = ln.closest('tr');
if (tr) tr.classList.add('line-hl');
}
});
function readMeta(name) {
var el = document.querySelector('meta[name="' + name + '"]');
return el ? (el.getAttribute('content') || '') : '';
}
function mountPath(rel) {
var prefix = readMeta('arks-mount').replace(/\/+$/, '');
return prefix + rel;
}
var gPressed = false;
document.addEventListener('keydown', function (e) {
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
e.preventDefault();
toggleCmdModal(true);
return;
}
if (e.key === 'Escape') {
toggleCmdModal(false);
return;
}
if (shortcutsDisabled()) return;
var tag = (e.target && e.target.tagName) ? e.target.tagName.toLowerCase() : '';
if (tag === 'input' || tag === 'textarea' || tag === 'select' || e.metaKey || e.ctrlKey || e.altKey) {
return;
}
if (e.key === '/') {
var search = document.querySelector('input[type="search"]');
if (search && search.id !== 'cmd-input') {
e.preventDefault();
search.focus();
} else {
e.preventDefault();
toggleCmdModal(true);
}
return;
}
if (e.key === '?') {
var modal = document.getElementById('cmd-modal');
if (modal) {
e.preventDefault();
toggleCmdModal(!modal.open);
}
return;
}
if (e.key === 'g') {
gPressed = true;
setTimeout(function () { gPressed = false; }, 1000);
return;
}
if (gPressed && e.key === 'i') {
gPressed = false;
window.location.href = mountPath('/issues');
} else if (gPressed && e.key === 'p') {
gPressed = false;
window.location.href = mountPath('/pulls');
}
});
document.addEventListener('htmx:configRequest', function (e) {
var csrf = readMeta('arks-csrf');
if (csrf && e.detail && e.detail.headers) {
e.detail.headers['X-CSRF-Token'] = csrf;
}
});
document.addEventListener('DOMContentLoaded', function () {
initTheme();
hydrateDiffs();
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js', { scope: '/' }).catch(function () {});
}
});
document.addEventListener('htmx:afterSwap', function () {
hydrateDiffs();
});
})();