Something went wrong. Try again.
softfork of @juliet.paris parfait forked from juliet.paris/parfait
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import { createSignal } from "solid-js";
/** * Your own CSS, written in Settings › Advanced: kept in this browser only, as the theme is, and * laid over parfait's own styles. Two layers: one for every account, and one for an account alone * on top of it, in effect while a tab of that account's is in front, so each can look its own. * Put in before anything is drawn by index.html (for the account in front when it was last * open), so it never flashes the look it changes; here, kept up to date, and moved after * parfait's own styles, which come after that, so a rule of yours wins over one of theirs as * strong. * * With `?nocss` in the address it's left out, to get back to a page your CSS has broken. */
const KEY = "parfait:css";const STYLE_ID = "parfait-custom-css";/** Where an account's own layer is kept. */const keyOf = (did?: string) => (did ? `${KEY}:${did}` : KEY);
/** Left out for this visit, by `?nocss` in the address. */export const cssSkipped = new URLSearchParams(location.search).has("nocss");
const read = (did?: string) => { try { return localStorage.getItem(keyOf(did)) ?? ""; } catch { return ""; }};
/** Each layer as it is now, by account ("" for every account's), read once and kept. */const [layers, setLayers] = createSignal<Record<string, string>>({ "": read() });/** * The account whose tab is in front, if signed in: its layer is the one in effect. To begin * with, the one in front when parfait was last open, as index.html has it (see tabs). */const lastFront = () => { try { const did = localStorage.getItem("parfait:last-account"); return did?.startsWith("did:") ? did : undefined; } catch { return undefined; }};const [front, setFront] = createSignal(lastFront());
/** A layer: every account's, or with a DID, that account's alone. */export const customCss = (did?: string) => { const key = did ?? ""; const known = layers()[key]; if (known !== undefined) return known; const found = read(did); setLayers((all) => ({ ...all, [key]: found })); return found;};
/** The style it's in, last in the head: after parfait's own, whichever came first. */const styleElement = () => { const found = document.getElementById(STYLE_ID); const style = found instanceof HTMLStyleElement ? found : document.createElement("style"); style.id = STYLE_ID; document.head.append(style); return style;};
/** What's in effect: every account's, then the front account's over it. */const apply = () => { if (cssSkipped) return; const own = front() ? customCss(front()) : ""; styleElement().textContent = own.trim() ? `${customCss()}\n\n${own}` : customCss();};
if (!cssSkipped) { apply(); // Moved after parfait's own again once they're in: this runs as the app's code loads, before // its stylesheet is put in the page (in development, by a module loaded after this one). queueMicrotask(styleElement); addEventListener("load", styleElement, { once: true });}
/** Changes a layer, kept and in effect at once; empty, it's gone. */export const setCustomCss = (next: string, did?: string) => { setLayers((all) => ({ ...all, [did ?? ""]: next })); try { if (next.trim()) localStorage.setItem(keyOf(did), next); else localStorage.removeItem(keyOf(did)); } catch {} apply();};
/** The account in front changed: its own layer, if it has one, comes in, and the last one's goes. */export const setFrontAccount = (did: string | undefined) => { if (did === front()) return; setFront(did); apply();};
/** The block a theme's colours are set in: the light theme's, or the dark one's. */export const themeBlock = (dark: boolean) => (dark ? ':root[data-theme="dark"]' : ":root");
/** * CSS with a colour set in a block: its line there given this value if it has one, else added at * the block's end, the block itself added at the end if there's none yet. With where the value * now is in it, to be picked and typed over. */export const withColour = (css: string, block: string, name: string, value: string) => { const escape = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // The block on its own: `:root` isn't `:root[data-theme="dark"]`, which only starts like it. const found = new RegExp(`(^|[\\s}])${escape(block)}\\s*\\{([^}]*)\\}`).exec(css); if (!found) { const before = css.trimEnd() ? `${css.trimEnd()}\n\n` : ""; const start = before.length + `${block} {\n ${name}: `.length; return { css: `${before}${block} {\n ${name}: ${value};\n}\n`, start, end: start + value.length, }; } const bodyStart = found.index + found[0].indexOf("{") + 1; const body = found[2]; const line = new RegExp(`(^|[\\s;])(${escape(name)}\\s*:\\s*)([^;}]*)`).exec(body); if (line) { const start = bodyStart + line.index + line[1].length + line[2].length; const end = start + line[3].trimEnd().length; return { css: css.slice(0, start) + value + css.slice(end), start, end: start + value.length }; } // A new line at the block's end, as the lines before it are set in. const trimmed = body.trimEnd(); const at = bodyStart + trimmed.length; const insert = `${trimmed && !trimmed.endsWith(";") ? ";" : ""}\n ${name}: ${value};\n`; const start = at + insert.indexOf(": ") + 2; return { css: css.slice(0, at) + insert + css.slice(bodyStart + body.length), start, end: start + value.length, };};