Tools site: Markdown editor, JSON editor, 24-hour pastebin
Single FastAPI container with a registry-driven single-page front end, so new tools are one module plus one registry entry. Runs as one container behind a reverse proxy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
9d910eb147
26 changed files with 2591 additions and 0 deletions
210
app/static/js/app.js
Normal file
210
app/static/js/app.js
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
// App shell: navigation, routing, theme. Tools themselves live in js/tools/*.js.
|
||||
//
|
||||
// A tool module exports mount(el, ctx) and may return a cleanup function.
|
||||
// ctx = {
|
||||
// rest, // path after the tool's own path, e.g. "abc123" for /paste/abc123
|
||||
// query, // URLSearchParams of the current URL
|
||||
// navigate, // navigate('/paste/abc') — client-side navigation
|
||||
// setTitle, // setTitle('abc123') — extra text in the top bar and tab title
|
||||
// }
|
||||
import { TOOLS, CATEGORY_ORDER } from './registry.js';
|
||||
import { el, store } from './ui.js';
|
||||
|
||||
const view = document.getElementById('view');
|
||||
const navList = document.getElementById('nav-list');
|
||||
const navFilter = document.getElementById('nav-filter');
|
||||
const titleEl = document.getElementById('topbar-title');
|
||||
let cleanup = null;
|
||||
let routeSeq = 0;
|
||||
|
||||
// ---------- theme ----------
|
||||
|
||||
const THEMES = ['auto', 'light', 'dark'];
|
||||
const svg = (d) => `<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${d}</svg>`;
|
||||
const THEME_ICON = {
|
||||
auto: svg('<circle cx="12" cy="12" r="9"/><path d="M12 3v18" /><path d="M12 3a9 9 0 0 1 0 18z" fill="currentColor"/>'),
|
||||
light: svg('<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/>'),
|
||||
dark: svg('<path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"/>'),
|
||||
};
|
||||
const themeBtn = document.getElementById('theme-btn');
|
||||
function applyTheme(t) {
|
||||
if (t === 'auto') delete document.documentElement.dataset.theme;
|
||||
else document.documentElement.dataset.theme = t;
|
||||
themeBtn.innerHTML = THEME_ICON[t];
|
||||
themeBtn.title = `Theme: ${t}`;
|
||||
}
|
||||
let theme = store.get('theme', 'auto');
|
||||
applyTheme(theme);
|
||||
themeBtn.addEventListener('click', () => {
|
||||
theme = THEMES[(THEMES.indexOf(theme) + 1) % THEMES.length];
|
||||
store.set('theme', theme);
|
||||
applyTheme(theme);
|
||||
});
|
||||
|
||||
// ---------- navigation ----------
|
||||
|
||||
function grouped(tools) {
|
||||
const groups = new Map();
|
||||
for (const t of tools) {
|
||||
if (!groups.has(t.category)) groups.set(t.category, []);
|
||||
groups.get(t.category).push(t);
|
||||
}
|
||||
const rank = (c) => (CATEGORY_ORDER.includes(c) ? CATEGORY_ORDER.indexOf(c) : 999);
|
||||
return [...groups].sort(([a], [b]) => rank(a) - rank(b) || a.localeCompare(b));
|
||||
}
|
||||
|
||||
function matches(t, q) {
|
||||
q = q.trim().toLowerCase();
|
||||
return !q || `${t.name} ${t.description} ${t.category}`.toLowerCase().includes(q);
|
||||
}
|
||||
|
||||
function renderNav() {
|
||||
const q = navFilter.value;
|
||||
navList.replaceChildren(
|
||||
el('a.nav-item.nav-home', { href: '/', dataset: { path: '/' } }, el('span.nav-icon', {}, '⌂'), 'All tools'),
|
||||
...grouped(TOOLS.filter((t) => matches(t, q))).map(([cat, tools]) =>
|
||||
el('div.nav-group', {},
|
||||
el('div.nav-heading', {}, cat),
|
||||
tools.map((t) => el('a.nav-item', { href: t.path, dataset: { path: t.path } }, el('span.nav-icon', {}, t.icon), t.name)),
|
||||
),
|
||||
),
|
||||
);
|
||||
markActive();
|
||||
}
|
||||
|
||||
function markActive() {
|
||||
const tool = findTool(location.pathname);
|
||||
for (const a of navList.querySelectorAll('.nav-item')) {
|
||||
const on = tool ? a.dataset.path === tool.path : a.dataset.path === '/';
|
||||
a.classList.toggle('active', on);
|
||||
if (on) a.setAttribute('aria-current', 'page');
|
||||
else a.removeAttribute('aria-current');
|
||||
}
|
||||
}
|
||||
|
||||
navFilter.addEventListener('input', renderNav);
|
||||
navFilter.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
const first = TOOLS.find((t) => matches(t, navFilter.value));
|
||||
if (first) {
|
||||
navFilter.value = '';
|
||||
renderNav();
|
||||
navigate(first.path);
|
||||
}
|
||||
} else if (e.key === 'Escape') {
|
||||
navFilter.value = '';
|
||||
renderNav();
|
||||
navFilter.blur();
|
||||
}
|
||||
});
|
||||
document.addEventListener('keydown', (e) => {
|
||||
const typing = /^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement?.tagName) || document.activeElement?.isContentEditable;
|
||||
if (e.key === '/' && !typing) {
|
||||
e.preventDefault();
|
||||
setDrawer(true);
|
||||
navFilter.focus();
|
||||
}
|
||||
});
|
||||
|
||||
// Mobile drawer
|
||||
const menuBtn = document.getElementById('menu-btn');
|
||||
function setDrawer(open) {
|
||||
document.body.classList.toggle('drawer-open', open);
|
||||
menuBtn.setAttribute('aria-expanded', String(open));
|
||||
}
|
||||
menuBtn.addEventListener('click', () => setDrawer(!document.body.classList.contains('drawer-open')));
|
||||
document.getElementById('scrim').addEventListener('click', () => setDrawer(false));
|
||||
|
||||
// ---------- routing ----------
|
||||
|
||||
function findTool(pathname) {
|
||||
return TOOLS.find((t) => pathname === t.path || pathname.startsWith(`${t.path}/`));
|
||||
}
|
||||
|
||||
export function navigate(path, { replace = false } = {}) {
|
||||
if (path === location.pathname + location.search) return;
|
||||
history[replace ? 'replaceState' : 'pushState']({}, '', path);
|
||||
route();
|
||||
}
|
||||
|
||||
document.addEventListener('click', (e) => {
|
||||
const a = e.target.closest('a[href]');
|
||||
if (!a || a.target || a.hasAttribute('download') || e.defaultPrevented) return;
|
||||
if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
||||
const url = new URL(a.href, location.href);
|
||||
if (url.origin !== location.origin || url.pathname.startsWith('/raw/') || url.pathname.startsWith('/api/')) return;
|
||||
e.preventDefault();
|
||||
setDrawer(false);
|
||||
navigate(url.pathname + url.search);
|
||||
});
|
||||
window.addEventListener('popstate', route);
|
||||
|
||||
function setTitle(tool, extra) {
|
||||
titleEl.textContent = tool ? [tool.name, extra].filter(Boolean).join(' · ') : '';
|
||||
document.title = tool ? `${[extra, tool.name].filter(Boolean).join(' · ')} — tools` : 'tools';
|
||||
}
|
||||
|
||||
async function route() {
|
||||
const seq = ++routeSeq;
|
||||
if (cleanup) {
|
||||
try { cleanup(); } catch (err) { console.error(err); }
|
||||
cleanup = null;
|
||||
}
|
||||
markActive();
|
||||
const tool = findTool(location.pathname);
|
||||
document.body.dataset.tool = tool ? tool.id : 'home';
|
||||
if (!tool) {
|
||||
if (location.pathname !== '/') history.replaceState({}, '', '/');
|
||||
setTitle(null);
|
||||
renderHome();
|
||||
return;
|
||||
}
|
||||
setTitle(tool);
|
||||
view.replaceChildren(el('div.loading', {}, 'Loading…'));
|
||||
try {
|
||||
const mod = await tool.load();
|
||||
if (seq !== routeSeq) return; // navigated away while loading
|
||||
view.replaceChildren();
|
||||
const ctx = {
|
||||
rest: location.pathname.slice(tool.path.length).replace(/^\//, ''),
|
||||
query: new URLSearchParams(location.search),
|
||||
navigate,
|
||||
setTitle: (extra) => setTitle(tool, extra),
|
||||
};
|
||||
cleanup = (await mod.mount(view, ctx)) || null;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
if (seq === routeSeq) view.replaceChildren(el('div.empty', {}, `Couldn't load ${tool.name}: ${err.message}`));
|
||||
}
|
||||
}
|
||||
|
||||
function renderHome() {
|
||||
const filter = el('input.home-filter', { type: 'search', placeholder: 'Find a tool…', 'aria-label': 'Find a tool' });
|
||||
const grid = el('div.card-grid');
|
||||
const draw = () =>
|
||||
grid.replaceChildren(
|
||||
...TOOLS.filter((t) => matches(t, filter.value)).map((t) =>
|
||||
el('a.tool-card', { href: t.path },
|
||||
el('div.tool-card-icon', {}, t.icon),
|
||||
el('div', {}, el('div.tool-card-name', {}, t.name), el('div.tool-card-desc', {}, t.description)),
|
||||
),
|
||||
),
|
||||
);
|
||||
filter.addEventListener('input', draw);
|
||||
filter.addEventListener('keydown', (e) => {
|
||||
const first = e.key === 'Enter' && grid.querySelector('a');
|
||||
if (first) navigate(new URL(first.href).pathname);
|
||||
});
|
||||
draw();
|
||||
view.replaceChildren(
|
||||
el('div.home', {},
|
||||
el('h1', {}, 'Tools'),
|
||||
el('p.muted', {}, 'Small utilities that run in your browser. Only pastes are stored on the server, and only for 24 hours.'),
|
||||
filter,
|
||||
grid,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
renderNav();
|
||||
route();
|
||||
Loading…
Add table
Add a link
Reference in a new issue