CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | /**
* Client-side JavaScript — embedded inline in the layout.
*
* Provides interactivity without requiring a build step or framework:
* - Copy to clipboard
* - Markdown preview in comment forms
* - Keyboard shortcuts
* - Toast notifications
* - Async form submission for stars/actions
* - Live search debouncing
* - Tab indentation in textareas
* - Mobile hamburger menu
*/
export const clientJs = `
(function() {
'use strict';
// ─── Toast Notification System ──────────────────────────────────────────
const toastContainer = document.createElement('div');
toastContainer.id = 'toast-container';
document.body.appendChild(toastContainer);
function toast(message, type) {
type = type || 'info';
var el = document.createElement('div');
el.className = 'toast toast-' + type;
el.textContent = message;
toastContainer.appendChild(el);
requestAnimationFrame(function() { el.classList.add('toast-visible'); });
setTimeout(function() {
el.classList.remove('toast-visible');
setTimeout(function() { el.remove(); }, 300);
}, 3000);
}
// ─── Copy to Clipboard ─────────────────────────────────────────────────
document.addEventListener('click', function(e) {
var btn = e.target.closest('[data-clipboard]');
if (!btn) return;
e.preventDefault();
var text = btn.getAttribute('data-clipboard');
if (navigator.clipboard) {
navigator.clipboard.writeText(text).then(function() {
var original = btn.textContent;
btn.textContent = 'Copied!';
btn.classList.add('btn-success');
setTimeout(function() {
btn.textContent = original;
btn.classList.remove('btn-success');
}, 2000);
});
}
});
// ─── Markdown Preview ──────────────────────────────────────────────────
document.addEventListener('click', function(e) {
var tab = e.target.closest('[data-tab]');
if (!tab) return;
var editor = tab.closest('.comment-editor');
if (!editor) return;
var tabName = tab.getAttribute('data-tab');
var tabs = editor.querySelectorAll('[data-tab]');
var textarea = editor.querySelector('textarea');
var preview = editor.querySelector('.editor-preview');
tabs.forEach(function(t) { t.classList.toggle('active', t.getAttribute('data-tab') === tabName); });
if (tabName === 'preview') {
textarea.style.display = 'none';
preview.style.display = 'block';
preview.innerHTML = '<div style="padding:12px;color:var(--text-muted)">Loading preview...</div>';
// Simple markdown rendering (bold, italic, code, links, headers)
var md = textarea.value || 'Nothing to preview';
var html = md
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/^### (.+)$/gm, '<h3>$1</h3>')
.replace(/^## (.+)$/gm, '<h2>$1</h2>')
.replace(/^# (.+)$/gm, '<h1>$1</h1>')
.replace(/\\.([^\\x60]+)\\.\\./g, '<code>$1</code>')
.replace(/\\*\\*(.+?)\\*\\*/g, '<strong>$1</strong>')
.replace(/\\*(.+?)\\*/g, '<em>$1</em>')
.replace(/\\[([^\\]]+)\\]\\(([^)]+)\\)/g, '<a href="$2">$1</a>')
.replace(/\\n/g, '<br>');
preview.innerHTML = '<div class="markdown-body" style="padding:12px">' + html + '</div>';
} else {
textarea.style.display = '';
preview.style.display = 'none';
}
});
// ─── Tab Key in Textareas ──────────────────────────────────────────────
document.addEventListener('keydown', function(e) {
if (e.key !== 'Tab') return;
var textarea = e.target;
if (textarea.tagName !== 'TEXTAREA') return;
e.preventDefault();
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var value = textarea.value;
textarea.value = value.substring(0, start) + ' ' + value.substring(end);
textarea.selectionStart = textarea.selectionEnd = start + 2;
});
// ─── Keyboard Shortcuts ────────────────────────────────────────────────
var shortcutOverlay = null;
function toggleShortcuts() {
if (shortcutOverlay) {
shortcutOverlay.remove();
shortcutOverlay = null;
return;
}
shortcutOverlay = document.createElement('div');
shortcutOverlay.className = 'shortcut-overlay';
shortcutOverlay.innerHTML = [
'<div class="shortcut-modal">',
'<h3>Keyboard Shortcuts</h3>',
'<div class="shortcut-grid">',
'<div><kbd>?</kbd> Show shortcuts</div>',
'<div><kbd>/</kbd> Focus search</div>',
'<div><kbd>g</kbd> <kbd>h</kbd> Go home</div>',
'<div><kbd>g</kbd> <kbd>e</kbd> Go to explore</div>',
'<div><kbd>g</kbd> <kbd>n</kbd> New repository</div>',
'<div><kbd>g</kbd> <kbd>s</kbd> Go to settings</div>',
'<div><kbd>Esc</kbd> Close modal</div>',
'</div>',
'<button class="btn btn-sm" onclick="this.closest(\\'.shortcut-overlay\\').remove()">Close</button>',
'</div>'
].join('');
document.body.appendChild(shortcutOverlay);
shortcutOverlay.addEventListener('click', function(e) {
if (e.target === shortcutOverlay) {
shortcutOverlay.remove();
shortcutOverlay = null;
}
});
}
var gPressed = false;
var gTimeout;
document.addEventListener('keydown', function(e) {
// Don't trigger shortcuts when typing in inputs
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') return;
if (e.key === '?') {
e.preventDefault();
toggleShortcuts();
return;
}
if (e.key === 'Escape') {
if (shortcutOverlay) {
shortcutOverlay.remove();
shortcutOverlay = null;
}
return;
}
if (e.key === '/') {
var searchInput = document.querySelector('.search-input') || document.querySelector('input[name="q"]');
if (searchInput) {
e.preventDefault();
searchInput.focus();
}
return;
}
if (e.key === 'g') {
if (!gPressed) {
gPressed = true;
gTimeout = setTimeout(function() { gPressed = false; }, 500);
return;
}
}
if (gPressed) {
gPressed = false;
clearTimeout(gTimeout);
if (e.key === 'h') { window.location.href = '/'; return; }
if (e.key === 'e') { window.location.href = '/explore'; return; }
if (e.key === 'n') { window.location.href = '/new'; return; }
if (e.key === 's') { window.location.href = '/settings'; return; }
}
});
// ─── Star Button Async ─────────────────────────────────────────────────
document.addEventListener('click', function(e) {
var starBtn = e.target.closest('.star-btn[type="submit"]');
if (!starBtn) return;
var form = starBtn.closest('form');
if (!form) return;
e.preventDefault();
var action = form.getAttribute('action');
fetch(action, {
method: 'POST',
credentials: 'same-origin',
headers: { 'X-Requested-With': 'XMLHttpRequest' }
}).then(function() {
// Toggle visual state
starBtn.classList.toggle('starred');
var currentText = starBtn.textContent.trim();
var match = currentText.match(/(\\d+)/);
if (match) {
var count = parseInt(match[1]);
var newCount = starBtn.classList.contains('starred') ? count + 1 : count - 1;
starBtn.textContent = (starBtn.classList.contains('starred') ? '\\u2605 ' : '\\u2606 ') + Math.max(0, newCount);
}
toast(starBtn.classList.contains('starred') ? 'Starred!' : 'Unstarred', 'success');
}).catch(function() {
// Fall back to normal form submission
form.submit();
});
});
// ─── Mobile Hamburger Menu ─────────────────────────────────────────────
var navRight = document.querySelector('.nav-right');
if (navRight && window.innerWidth < 768) {
var hamburger = document.createElement('button');
hamburger.className = 'hamburger-btn';
hamburger.innerHTML = '\\u2630';
hamburger.setAttribute('aria-label', 'Toggle menu');
navRight.parentElement.insertBefore(hamburger, navRight);
navRight.classList.add('mobile-hidden');
hamburger.addEventListener('click', function() {
navRight.classList.toggle('mobile-hidden');
navRight.classList.toggle('mobile-visible');
});
}
// ─── Relative Time Auto-Update ─────────────────────────────────────────
function updateTimes() {
document.querySelectorAll('[data-time]').forEach(function(el) {
var date = new Date(el.getAttribute('data-time'));
var now = new Date();
var diff = Math.floor((now - date) / 60000);
if (diff < 1) el.textContent = 'just now';
else if (diff < 60) el.textContent = diff + 'm ago';
else if (diff < 1440) el.textContent = Math.floor(diff / 60) + 'h ago';
else if (diff < 43200) el.textContent = Math.floor(diff / 1440) + 'd ago';
});
}
var _timesInterval = setInterval(updateTimes, 60000);
// ─── Confirmation on Dangerous Actions ─────────────────────────────────
document.addEventListener('submit', function(e) {
var form = e.target;
if (!form.classList.contains('confirm-action')) return;
var msg = form.getAttribute('data-confirm') || 'Are you sure?';
if (!confirm(msg)) {
e.preventDefault();
}
});
})();
`;
|