Files
2026-11-14 17:05:01 +00:00

448 lines
22 KiB
HTML

{% extends "admin/base.html" %}
{% block admin_content %}
<div class="admin-panel">
<h1>Appearance</h1>
<!-- ==========================================================================
ONE FORM. Every control lives in a single <form>, so a single "Save all"
submits site_title / site_tagline / comments_enabled AND custom_css (the
merged theme + raw CSS) together. Because they are all submitted at once,
saving one section can no longer clobber another — the handler only writes
keys that are present in request.form, and here every key is always present.
Theme styling (accent / background / text / sidebar / font / columns /
header layout / radius / dark) is rendered into the raw-CSS textarea live
as you tweak the controls (see buildTheme() below), so that textarea is the
single source of truth for custom_css: it holds the generated :root block
plus any hand-written CSS you add. The "Reset theme to defaults" button
regenerates the :root block from the built-in defaults.
========================================================================== -->
<form id="appearance-form" method="post" action="{{ url_for('admin.appearance') }}" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<!-- --- Site identity ---------------------------------------------------- -->
<div class="admin-panel identity">
<h2>Site identity</h2>
<div class="form-row">
<div class="form-group">
<label for="site-title">Site title</label>
<input type="text" id="site-title" name="site_title" value="{{ site_title }}" autocomplete="off">
</div>
<div class="form-group">
<label for="site-tagline">Tagline / statement</label>
<input type="text" id="site-tagline" name="site_tagline" value="{{ site_tagline }}" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="meta-description">Default meta description</label>
<input type="text" id="meta-description" name="meta_description" value="{{ meta_description }}" autocomplete="off"
placeholder="Used by search engines and social previews when a page has no excerpt">
<span class="field-hint">Posts use their excerpt first; this is the fallback for home, archive, tag and other pages.</span>
</div>
<!-- Use radio buttons (not checkbox+hidden): a checkbox's unchecked state
omits the key, so the old hidden "0" trick relied on "0" being truthy,
which it isn't in Python — meaning admins could never switch comments
OFF via this page. Radios submit an explicit 1/0 that the route reads
verbatim. -->
<div class="form-group">
<label class="checkbox-label">
<input type="radio" name="comments_enabled" value="1" {% if comments_enabled %}checked{% endif %}>
Enable comments on posts and pages
</label>
<label class="checkbox-label">
<input type="radio" name="comments_enabled" value="0" {% if not comments_enabled %}checked{% endif %}>
Disable comments on posts and pages
</label>
</div>
</div>
<!-- --- Theme editor (GUI) ------------------------------------------------ -->
<div class="theme-editor">
<h2>Theme</h2>
<p class="field-hint">These controls render into the Custom CSS box below as you change them. Edit the box directly for anything more advanced.</p>
<!-- Colour controls -->
<div class="form-row">
<div class="form-group">
<label for="t-accent">Accent / link colour</label>
<div class="swatch-row">
<input type="color" id="t-accent" name="t-accent" value="{{ theme.accent }}">
<input type="text" id="t-accent-hex" class="hex-input" value="{{ theme.accent }}" spellcheck="false" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="t-bg">Page background</label>
<div class="swatch-row">
<input type="color" id="t-bg" name="t-bg" value="{{ theme.background }}">
<input type="text" id="t-bg-hex" class="hex-input" value="{{ theme.background }}" spellcheck="false" autocomplete="off">
</div>
</div>
</div>
<div class="form-row">
<div class="form-group">
<label for="t-text">Text colour</label>
<div class="swatch-row">
<input type="color" id="t-text" name="t-text" value="{{ theme.text }}">
<input type="text" id="t-text-hex" class="hex-input" value="{{ theme.text }}" spellcheck="false" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="t-sidebar">Sidebar / panel background</label>
<div class="swatch-row">
<input type="color" id="t-sidebar" name="t-sidebar" value="{{ theme.sidebar }}">
<input type="text" id="t-sidebar-hex" class="hex-input" value="{{ theme.sidebar }}" spellcheck="false" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="t-font">Font family</label>
<select id="t-font" name="t-font">
{% for opt in theme.fonts %}
<option value="{{ opt.value }}" {% if opt.value == theme.font %}selected{% endif %}>{{ opt.label }}</option>
{% endfor %}
</select>
</div>
</div>
<!-- Layout controls -->
<div class="form-row">
<div class="form-group">
<label for="t-posts">Post grid columns</label>
<select id="t-posts" name="t-posts">
{% for opt in theme.post_columns %}
<option value="{{ opt.value }}" {% if opt.value == theme.posts %}selected{% endif %}>{{ opt.label }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="t-header">Site header</label>
<select id="t-header" name="t-header">
{% for opt in theme.header_layouts %}
<option value="{{ opt.value }}" {% if opt.value == theme.header %}selected{% endif %}>{{ opt.label }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<label for="t-radius">Corner radius: <span id="radius-val">{{ theme.radius }}px</span></label>
<input type="range" id="t-radius" name="t-radius" min="0" max="24" step="1" value="{{ theme.radius }}">
</div>
<label class="checkbox-label" for="t-dark">
<input type="checkbox" id="t-dark" name="t-dark" {% if theme.dark %}checked{% endif %}>
Enable dark mode
</label>
</div>
<!-- --- Raw custom CSS (single source of truth for custom_css) ------------- -->
<div class="admin-panel">
<h2>Custom CSS</h2>
<p class="field-hint">The theme variables above are rendered here live; add any extra rules below them.</p>
<div class="form-group">
<label for="custom-css-editor">Custom CSS</label>
<textarea id="custom-css-editor" rows="16" placeholder="body { background: #fff; }"></textarea>
<input type="hidden" id="custom-css-hidden" name="custom_css" value="{{ custom_css }}">
</div>
</div>
<!-- --- Header image ------------------------------------------------------ -->
<div class="admin-panel">
<h2>Header image</h2>
<p class="field-hint">Upload a banner/header image for the top of every public page. Leave blank to remove it.</p>
{% if header_image %}
<div class="form-group">
<label>Current header image</label>
<img src="{{ header_image }}" alt="{{ site_title }}" style="max-height:90px;width:auto;border:1px solid var(--color-border);border-radius:6px;">
<button type="submit" class="btn-danger" name="remove_header" value="1">Remove</button>
</div>
{% endif %}
<div class="form-group">
<label for="header-image">Header image</label>
<input type="file" id="header-image" name="header_image" accept="image/png,image/jpeg,image/gif,image/webp">
</div>
</div>
<!-- --- Actions ------------------------------------------------------------ -->
<div class="admin-panel">
<button type="submit" class="btn-primary">Save all</button>
<button type="button" id="reset-theme" class="btn-secondary">Reset theme to defaults</button>
</div>
</form>
<!-- Live preview of the themed site -->
<div class="theme-preview-wrap">
<h2>Live preview</h2>
<p class="field-hint">This mini-site uses the exact variables you just set. Changes update as you tweak them.</p>
<div id="theme-preview" class="theme-preview">
<style id="theme-vars"></style>
<header class="site-header" id="preview-header">
<div class="header-inner">
<div class="site-logo"><span id="preview-logo">{{ site_title }}</span></div>
<nav class="main-nav" aria-label="preview nav">
<button type="button" id="preview-menu">Menu</button>
<ul class="nav-menu" id="preview-nav">
<li><a href="#!">Dashboard</a></li>
<li><a href="#!">Posts</a></li>
<li><a href="#!">Categories</a></li>
</ul>
</nav>
</div>
</header>
<main class="site-main" id="preview-main">
<p class="page-heading"><a href="#!">{{ site_title }}</a></p>
<section class="posts-grid" id="preview-posts">
{% for _i in range(0, 6) %}
<article class="post-card">
<h2><a href="#!">I Spent weeks Fighting My Local AI Coding Agent — Here's Everything I Learned</a></h2>
<p class="post-meta"><span>October 27, 2026 by brian</span></p>
<div class="post-excerpt">
<p>How I built a coding agent that runs entirely on my own hardware — no API, no cloud, no monthly bill — and the build steps that actually work. A while back I decided I wanted a coding agent I could leave running without paying for it.</p>
<p>Something that …</p>
</div>
<div class="post-tags"><span class="tag-icon">📁</span><a class="tag" href="#!">ai</a><a class="tag" href="#!">emerging_technology</a><a class="tag" href="#!">thoughts</a></div>
<div class="post-footer"><a href="#!" class="read-more">Read more →</a><a href="#!" class="comment-link">💬 Leave a comment</a></div>
</article>
{% endfor %}
</section>
</main>
</div>
</div>
<style>
/* --- Theme editor controls ------------------------------------------------- */
.theme-editor { border: 1px solid var(--color-border); border-radius: 8px; padding: 20px 24px; }
.swatch-row { display: flex; align-items: center; gap: 12px; margin-top: 6px; }
.swatch-row input[type="color"] { width: 56px; height: 40px; padding: 3px; border: 1px solid var(--color-border); border-radius: 6px; background: #fff; cursor: pointer; }
.swatch-row .hex-input { flex: 1; padding: 11px 12px; border: 1px solid var(--color-border); border-radius: 5px; font-family: var(--font-stack); }
.checkbox-label { display: flex; align-items: center; gap: 8px; margin: 16px 0 14px; font-size: 0.95rem; color: var(--color-text); }
.checkbox-label input { width: auto; }
/* --- Live preview (mini site) ---------------------------------------------- */
.theme-preview-wrap > h2 { margin-top: 0; }
.theme-preview { border: 1px solid var(--color-border); border-radius: 8px; overflow: hidden; background: #fff; }
.theme-preview .site-header { border-bottom-width: 2px; }
@media (max-width: 620px) {
.theme-preview .nav-menu { display: none !important; }
}
/* --- Action buttons -------------------------------------------------------- */
.admin-panel > .form-group:not(:last-of-type) button[type="submit"],
.admin-panel > .form-group:not(:last-of-type) input[type="file"] { margin-bottom: 12px; }
</style>
<script>
(function () {
// Defaults match the built-in theme.css :root so a fresh site looks unchanged.
var DEFAULTS = {
accent: '#00a0d1', background: '#ffffff', text: '#1e1e1e',
font: '"-apple-system", BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif',
posts: 1, header: 'side', radius: 6, dark: false
};
var state = {
accent: document.getElementById('t-accent').value,
background: document.getElementById('t-bg').value,
text: document.getElementById('t-text').value,
sidebar: document.getElementById('t-sidebar').value,
font: document.getElementById('t-font').value,
posts: document.getElementById('t-posts').value,
header: document.getElementById('t-header').value,
radius: document.getElementById('t-radius').value,
dark: document.getElementById('t-dark').checked
};
function toHex(col) {
try { return getComputedStyle(document.documentElement || { style: function () {} }).getPropertyValue('color').trim(); } catch (e) {}
var c = document.createElement('canvas').getContext('2d');
c.fillStyle = col; return '#' + c.fillStyle.toString().slice(4);
}
// Build the :root variable block + grid/header rules as a CSS string. This is
// exactly what gets spliced into the raw-CSS textarea on submit (and reset), so
// the textarea stays the single source of truth for custom_css.
function buildTheme() {
var light = [
['--color-accent', state.accent],
['--color-link', state.accent],
['--color-link-hover', shade(state.accent, -18)],
['--color-text', state.text],
['--color-muted', lerpAlpha(state.text, 0.55)],
['--color-border', lerpAlpha(state.text, 0.92)],
['--color-bg', state.background],
['--color-sidebar-bg', state.sidebar],
['--color-card-bg', '#f7f8fa'],
['--font-stack', state.font],
['--border-radius', Math.round(Number(state.radius)) + 'px']
];
var dark = [
['--color-accent', state.accent],
['--color-link', state.accent],
['--color-link-hover', shade(state.accent, -22)],
['--color-text', state.text],
['--color-muted', lerpAlpha(state.text, 0.55)],
['--color-border', lerpAlpha(state.text, 0.92)],
['--color-bg', state.background],
['--color-sidebar-bg', state.sidebar],
['--color-card-bg', 'rgba(255, 255, 255, 0.03)'],
['--font-stack', state.font],
['--border-radius', Math.round(Number(state.radius)) + 'px']
];
var vars = (state.dark ? dark : light).map(function (pair) { return pair.join(':') + ';'; }).join('\n ');
var rules = [];
var cols = Number(state.posts);
rules.push('.posts-grid { grid-template-columns: repeat(' + cols + ', 1fr); }');
rules.push('#preview-header { flex-direction: ' + (state.header === 'stacked' ? 'column' : 'row') + '; }');
var out = ':root {\n' + vars + '\n}\n';
if (rules.length) out += '\n' + rules.join('\n') + '\n';
// Emit an explicit marker when dark mode is on. The old detection looked for
// a literal #121212/#1c1c1c background, but buildTheme() writes --color-bg
// from the chosen colour — so a custom dark background would silently lose
// dark mode on reload. This marker is unambiguous and always present in dark.
if (state.dark) out += '/* theme-dark */\n';
return out;
}
function renderVars() { document.getElementById('theme-vars').textContent = buildTheme(); }
// The hidden field is the single source of truth for custom_css. It always holds
// the generated theme block (from buildTheme()) followed by whatever manual CSS the
// user has typed in the editor. This avoids parsing/stripping the textarea on every
// change — no risk of duplicating the grid rules, dark marker, or manual CSS.
function setHidden() {
var ta = document.getElementById('custom-css-editor');
var hidden = document.getElementById('custom-css-hidden');
hidden.value = buildTheme() + '\n\n' + (ta.value || '');
}
// On load: the hidden field already holds the stored custom_css (from {{ custom_css }}).
// Split it into the generated block + manual CSS and show only the manual CSS in the
// editor. Any later change to a control or a keystroke rebuilds the hidden field.
function loadEditor() {
var ta = document.getElementById('custom-css-editor');
var stored = document.getElementById('custom-css-hidden').value;
var gen = buildTheme();
var match = new RegExp('^' + gen.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).exec(stored);
ta.value = match ? stored.slice(match[0].length) : stored;
}
document.getElementById('custom-css-editor').addEventListener('input', function () {
setHidden();
});
// Apply the current state to the live preview DOM.
function applyPreview() {
var root = document.documentElement;
root.style.setProperty('--color-accent', state.accent);
root.style.setProperty('--color-link', state.accent);
root.style.setProperty('--color-link-hover', shade(state.accent, -18));
root.style.setProperty('--color-text', state.text);
root.style.setProperty('--color-bg', state.background);
root.style.setProperty('--font-stack', state.font);
root.style.setProperty('--color-sidebar-bg', state.sidebar);
document.getElementById('preview-posts').style.gridTemplateColumns = 'repeat(' + state.posts + ', 1fr)';
document.getElementById('preview-header').style.flexDirection = state.header === 'stacked' ? 'column' : 'row';
document.getElementById('preview-header').style.alignItems = state.header === 'stacked' ? 'stretch' : 'center';
var r = Math.round(Number(state.radius));
['post-card', '.site-header', '#preview-logo'].forEach(function (sel) {
var el = document.querySelector(sel);
if (el) el.style.borderRadius = r + 'px';
});
document.getElementById('theme-preview').classList.toggle('dark', state.dark);
}
function shade(hex, pct) {
var m = hex.replace('#', '');
if (m.length === 3) m = m.split('').map(function (c) { return c + c; }).join('');
var num = parseInt(m, 16);
var r = Math.max(0, Math.min(255, ((num >> 16) & 255) + pct));
var g = Math.max(0, Math.min(255, ((num >> 8) & 255) + pct));
var b = Math.max(0, Math.min(255, (num & 255) + pct));
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function lerpAlpha(hex, alpha) {
var m = hex.replace('#', '');
if (m.length === 3) m = m.split('').map(function (c) { return c + c; }).join('');
var num = parseInt(m, 16);
var r = (num >> 16) & 255, g = (num >> 8) & 255, b = num & 255;
return 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')';
}
function syncRadiusLabel() { document.getElementById('radius-val').textContent = state.radius + 'px'; }
function bindPair(pickerId, hexId) {
var picker = document.getElementById(pickerId), hex = document.getElementById(hexId);
picker.addEventListener('input', function () {
state[pickerId.slice(2)] = picker.value;
hex.value = picker.value;
setHidden(); renderVars(); applyPreview();
});
hex.addEventListener('input', function () {
var v = hex.value.trim().replace(/^#/, '');
if (/^[\da-fA-F]{6}$/.test(v)) {
state[pickerId.slice(2)] = '#' + v;
picker.value = '#' + v;
setHidden(); renderVars(); applyPreview();
}
});
}
function bind() {
bindPair('t-accent', 't-accent-hex');
bindPair('t-bg', 't-bg-hex');
bindPair('t-text', 't-text-hex');
bindPair('t-sidebar', 't-sidebar-hex');
['t-font', 't-posts', 't-header'].forEach(function (id) {
document.getElementById(id).addEventListener('change', function () {
state[id.slice(2)] = document.getElementById(id).value;
setHidden(); renderVars(); applyPreview(); syncRadiusLabel();
});
});
document.getElementById('t-radius').addEventListener('input', function () {
state.radius = this.value;
document.getElementById('radius-val').textContent = this.value + 'px';
setHidden(); renderVars(); applyPreview();
});
document.getElementById('t-dark').addEventListener('change', function () {
state.dark = this.checked;
setHidden(); renderVars(); applyPreview();
});
// Reset theme controls (and the raw-CSS textarea's generated block) to defaults.
document.getElementById('reset-theme').addEventListener('click', function () {
var form = document.getElementById('appearance-form');
var set = function (id, v) { var el = document.getElementById(id); if (el) el.value = v; };
set('site-title', 'My Blog');
set('site-tagline', 'Thoughts on tech, self-hosting and life.');
set('t-accent', DEFAULTS.accent);
set('t-bg', DEFAULTS.background);
set('t-text', DEFAULTS.text);
set('t-sidebar', DEFAULTS.sidebar);
set('t-font', DEFAULTS.font);
set('t-posts', DEFAULTS.posts);
set('t-header', DEFAULTS.header);
set('t-radius', DEFAULTS.radius);
var dark = document.getElementById('t-dark'); if (dark) dark.checked = DEFAULTS.dark;
// Clear any manual CSS the user typed, then rebuild the hidden field from defaults.
document.getElementById('custom-css-editor').value = '';
setHidden(); renderVars(); applyPreview();
});
}
bind();
renderVars();
loadEditor();
applyPreview();
})();
</script>
{% endblock %}