waybetter-for-x/scripts/store/preview.mjs
2026-09-30 23:01:13 +00:00

141 lines
11 KiB
JavaScript

// Store previews as SVG, drawn from the same data the app uses: a theme's colors, shape, layout
// and background become a miniature X timeline (left rail, column of posts, sidebar); a plugin gets
// a card with its name, category glyph and permissions. Used by scripts/build-store.mjs; the site's
// /themes/ and /plugins/ pages and the in-app store show the files it writes.
const esc = value => String(value).replace(/[&<>"']/g, ch => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[ch]);
const hex = value => (/^#[0-9a-f]{6}$/i.test(value ?? '') ? value : null);
/** X's own default (light) palette, for a theme that keeps X's colors. */
const X_LIGHT = { page: '#ffffff', bg: '#ffffff', surface: '#f7f9f9', elevated: '#eff3f4', border: '#eff3f4', text: '#0f1419', muted: '#536471', accent: '#1d9bf0', inverse: '#0f1419', onInverse: '#ffffff' };
function palette(theme) {
const c = theme.colors ?? X_LIGHT;
const pick = (key, fallback) => hex(c[key]) ?? fallback;
const bg = pick('bg', X_LIGHT.bg);
const text = pick('text', X_LIGHT.text);
const accent = pick('accent', X_LIGHT.accent);
return {
page: pick('page', bg), bg, surface: pick('surface', bg), elevated: pick('elevated', bg), border: pick('border', pick('surface', bg)),
text, muted: pick('muted', text), accent, inverse: pick('inverse', accent), onInverse: pick('onInverse', bg),
};
}
function backgroundDefs(theme, p, W, H) {
const bg = theme.background ?? { kind: 'none' };
if (bg.kind === 'gradient' && hex(bg.from) && hex(bg.to)) {
const angle = ((Number(bg.angle) || 180) - 90) * Math.PI / 180;
const x = Math.cos(angle) / 2, y = Math.sin(angle) / 2;
const glow = hex(bg.glow);
return {
defs: `<linearGradient id="g" x1="${0.5 - x}" y1="${0.5 - y}" x2="${0.5 + x}" y2="${0.5 + y}"><stop offset="0" stop-color="${bg.from}"/><stop offset="1" stop-color="${bg.to}"/></linearGradient>`
+ (glow ? `<radialGradient id="glow" cx="0.5" cy="0.15" r="0.6"><stop offset="0" stop-color="${glow}" stop-opacity="0.55"/><stop offset="1" stop-color="${glow}" stop-opacity="0"/></radialGradient>` : ''),
fill: `<rect width="${W}" height="${H}" fill="url(#g)"/>${glow ? `<rect width="${W}" height="${H}" fill="url(#glow)"/>` : ''}`,
};
}
if (bg.kind === 'image' && typeof bg.url === 'string' && /^data:image\/(png|jpeg|webp|gif|avif);base64,/.test(bg.url)) {
const dim = Math.min(0.9, Math.max(0, Number(bg.dim) || 0));
return { defs: '', fill: `<rect width="${W}" height="${H}" fill="${p.page}"/><image href="${bg.url}" width="${W}" height="${H}" preserveAspectRatio="xMidYMid slice"/><rect width="${W}" height="${H}" fill="${p.page}" opacity="${dim}"/>` };
}
if (bg.kind === 'pattern' && hex(bg.ink)) {
const opacity = Math.min(0.6, Math.max(0.04, Number(bg.opacity) || 0.1));
const shapes = {
dots: `<circle cx="6" cy="6" r="1.4" fill="${bg.ink}"/>`,
grid: `<path d="M12 0V12H0" fill="none" stroke="${bg.ink}" stroke-width="0.8"/>`,
diagonal: `<path d="M0 12L12 0" stroke="${bg.ink}" stroke-width="0.8"/>`,
scanlines: `<rect width="12" height="1" y="0" fill="${bg.ink}"/><rect width="12" height="1" y="6" fill="${bg.ink}"/>`,
paper: `<circle cx="3" cy="4" r="0.6" fill="${bg.ink}"/><circle cx="9" cy="9" r="0.5" fill="${bg.ink}"/>`,
clouds: `<ellipse cx="6" cy="7" rx="5" ry="2.4" fill="${bg.ink}"/>`,
};
const size = bg.pattern === 'clouds' ? 48 : 12;
return {
defs: `<pattern id="pt" width="${size}" height="${size}" patternUnits="userSpaceOnUse"><g transform="scale(${size / 12})" opacity="${opacity}">${shapes[bg.pattern] ?? shapes.dots}</g></pattern>`,
fill: `<rect width="${W}" height="${H}" fill="${p.page}"/><rect width="${W}" height="${H}" fill="url(#pt)"/>`,
};
}
return { defs: '', fill: `<rect width="${W}" height="${H}" fill="${p.page}"/>` };
}
/** A 640×400 miniature of X in `theme` (a sanitized ThemeSpec). */
export function themePreviewSvg(theme) {
const W = 640, H = 400;
const p = palette(theme);
const radius = Math.min(18, Math.max(0, Number(theme.shape?.radius ?? 16))) * 0.7;
const pill = Math.min(20, Number(theme.shape?.button ?? 9999)) ;
const avatar = theme.shape?.avatar ?? 'circle';
const cards = Boolean(theme.layout?.cards);
const frame = Boolean(theme.layout?.frame);
const density = theme.layout?.density ?? 'default';
const postH = density === 'compact' ? 88 : density === 'comfortable' ? 110 : 98;
const { defs, fill } = backgroundDefs(theme, p, W, H);
const colX = 170, colW = 300, sideX = 490, sideW = 130;
const out = [];
out.push(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}" width="${W}" height="${H}" role="img" aria-label="${esc(theme.name)} theme preview">`);
out.push(`<title>${esc(theme.name)}</title><defs>${defs}<clipPath id="clip"><rect width="${W}" height="${H}" rx="14"/></clipPath></defs><g clip-path="url(#clip)">${fill}`);
// Left rail: logo, nav items, the Post button.
out.push(`<circle cx="118" cy="34" r="11" fill="${p.text}"/>`);
for (let i = 0; i < 6; i += 1) {
const y = 72 + i * 34;
out.push(`<circle cx="104" cy="${y}" r="7" fill="none" stroke="${p.text}" stroke-width="2"/><rect x="118" y="${y - 4}" width="${28 + (i * 13) % 24}" height="8" rx="4" fill="${p.text}" opacity="${i === 0 ? 1 : 0.8}"/>`);
}
out.push(`<rect x="92" y="290" width="68" height="30" rx="${Math.min(15, pill)}" fill="${p.inverse}"/><rect x="112" y="301" width="28" height="8" rx="4" fill="${p.onInverse}"/>`);
// Timeline column.
if (!cards) out.push(`<rect x="${colX}" y="0" width="${colW}" height="${H}" fill="${p.bg}"${frame ? ` stroke="${p.border}"` : ''}/>`);
out.push(`<rect x="${colX + 16}" y="16" width="70" height="10" rx="5" fill="${p.text}"/><rect x="${colX + 150}" y="40" width="70" height="3" rx="1.5" fill="${p.accent}"/>`);
out.push(`<rect x="${colX}" y="48" width="${colW}" height="1" fill="${p.border}"/>`);
const avatarShape = (x, y, color) => avatar === 'circle' ? `<circle cx="${x + 15}" cy="${y + 15}" r="15" fill="${color}"/>` : `<rect x="${x}" y="${y}" width="30" height="30" rx="${avatar === 'rounded' ? 8 : 2}" fill="${color}"/>`;
const tones = [p.accent, p.muted, p.inverse];
for (let i = 0; i < 3; i += 1) {
const top = 58 + i * (postH + (cards ? 10 : 0));
if (cards) out.push(`<rect x="${colX + 4}" y="${top - 4}" width="${colW - 8}" height="${postH}" rx="${radius}" fill="${p.bg}" stroke="${p.border}"/>`);
out.push(avatarShape(colX + 14, top + 6, tones[i % tones.length]));
out.push(`<rect x="${colX + 54}" y="${top + 8}" width="${60 + i * 14}" height="8" rx="4" fill="${p.text}"/><rect x="${colX + 124 + i * 14}" y="${top + 8}" width="46" height="8" rx="4" fill="${p.muted}" opacity="0.7"/>`);
const lines = [210, 226 - i * 30, 150 + i * 20].slice(0, density === 'compact' ? 2 : 3);
lines.forEach((width, line) => out.push(`<rect x="${colX + 54}" y="${top + 26 + line * 14}" width="${width}" height="7" rx="3.5" fill="${p.text}" opacity="0.72"/>`));
const actions = top + postH - 20;
for (let a = 0; a < 4; a += 1) {
const liked = i === 1 && a === 2;
out.push(`<circle cx="${colX + 60 + a * 58}" cy="${actions}" r="4.5" fill="${liked ? p.accent : 'none'}" stroke="${liked ? p.accent : p.muted}" stroke-width="1.6"/><rect x="${colX + 69 + a * 58}" y="${actions - 3}" width="14" height="6" rx="3" fill="${liked ? p.accent : p.muted}" opacity="0.8"/>`);
}
if (!cards) out.push(`<rect x="${colX}" y="${top + postH - 6}" width="${colW}" height="1" fill="${p.border}"/>`);
}
// Sidebar: search pill and a "What's happening" card.
out.push(`<rect x="${sideX}" y="12" width="${sideW}" height="28" rx="14" fill="${p.surface}" stroke="${p.border}"/><circle cx="${sideX + 16}" cy="26" r="5" fill="none" stroke="${p.muted}" stroke-width="1.6"/>`);
out.push(`<rect x="${sideX}" y="54" width="${sideW}" height="170" rx="${radius}" fill="${p.surface}" stroke="${p.border}"/><rect x="${sideX + 12}" y="68" width="86" height="9" rx="4.5" fill="${p.text}"/>`);
for (let i = 0; i < 4; i += 1) {
out.push(`<rect x="${sideX + 12}" y="${92 + i * 32}" width="44" height="6" rx="3" fill="${p.muted}" opacity="0.8"/><rect x="${sideX + 12}" y="${103 + i * 32}" width="${70 + (i * 17) % 30}" height="7" rx="3.5" fill="${p.text}"/>`);
}
out.push(`<rect x="${sideX}" y="236" width="${sideW}" height="52" rx="${radius}" fill="${p.elevated}" stroke="${p.border}"/><rect x="${sideX + 12}" y="252" width="60" height="7" rx="3.5" fill="${p.text}"/><rect x="${sideX + 84}" y="248" width="34" height="16" rx="${Math.min(8, pill)}" fill="${p.inverse}"/>`);
out.push('</g></svg>');
return out.join('');
}
const GLYPHS = {
reading: 'M4 6h9a3 3 0 0 1 3 3v11a2 2 0 0 0-2-2H4zM28 6h-9a3 3 0 0 0-3 3v11a2 2 0 0 1 2-2h10z',
writing: 'M6 26l1.5-6L21 6.5l4.5 4.5L12 24.5zM18.5 9l4.5 4.5',
filters: 'M5 7h22l-8.5 10v7l-5 2.5V17z',
focus: 'M16 9a7 7 0 1 0 0 14 7 7 0 0 0 0-14zM16 4v3M16 25v3M4 16h3M25 16h3',
tools: 'M20 5a6 6 0 0 0-5.6 8.2L5 22.6 9.4 27l9.4-9.4A6 6 0 0 0 27 12l-4 4-4-1-1-4 4-4a6 6 0 0 0-2-2z',
};
const HUES = { reading: '#5b8cff', writing: '#c084fc', filters: '#f472b6', focus: '#2dd4bf', tools: '#fbbf24' };
const CATEGORY = { reading: 'Reading', writing: 'Writing', filters: 'Filters', focus: 'Focus', tools: 'Tools' };
/** A 640×400 banner for a plugin: Night Ink, a large category glyph, the name and category. */
export function pluginPreviewSvg(plugin) {
const W = 640, H = 400;
const glyph = GLYPHS[plugin.category] ?? GLYPHS.tools;
const hue = HUES[plugin.category] ?? HUES.tools;
const name = esc(plugin.name);
const size = plugin.name.length > 18 ? 34 : 40;
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}" width="${W}" height="${H}" role="img" aria-label="${name} plugin">`
+ `<title>${name}</title><defs>`
+ `<radialGradient id="glow" cx="0.3" cy="0.35" r="0.8"><stop offset="0" stop-color="${hue}" stop-opacity="0.34"/><stop offset="1" stop-color="#07080c" stop-opacity="0"/></radialGradient>`
+ `<pattern id="grid" width="32" height="32" patternUnits="userSpaceOnUse"><path d="M32 0H0V32" fill="none" stroke="#ffffff" stroke-opacity="0.045"/></pattern></defs>`
+ `<rect width="${W}" height="${H}" rx="14" fill="#07080c"/><rect width="${W}" height="${H}" rx="14" fill="url(#grid)"/><rect width="${W}" height="${H}" rx="14" fill="url(#glow)"/>`
+ `<rect x="64" y="96" width="128" height="128" rx="32" fill="#0d1017" stroke="${hue}" stroke-opacity="0.55" stroke-width="1.5"/>`
+ `<g transform="translate(80 112) scale(3)"><path d="${glyph}" fill="none" stroke="${hue}" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/></g>`
+ `<text x="64" y="${278 + (40 - size) / 2}" font-family="Geist, 'Segoe UI', system-ui, sans-serif" font-size="${size}" font-weight="650" fill="#eef0f7" letter-spacing="-0.5">${name}</text>`
+ `<text x="64" y="316" font-family="'Geist Mono', Consolas, ui-monospace, monospace" font-size="15" fill="${hue}" letter-spacing="1.5">${esc((CATEGORY[plugin.category] ?? 'Plugin').toUpperCase())} · WAYBETTER PLUGIN</text>`
+ `</svg>`;
}