clove/pkviewer
web/src/lib/components/manage/CssEditor.svelte
back to web/src/lib/components/manage
<!-- web/src/lib/components/manage/CssEditor.svelte -->
<script lang="ts">
import { CSS_ISSUE_MESSAGES, MAX_CSS_LENGTH, type CssIssue } from "@pkviewer/shared";
import { site } from "$lib/config.ts";
import { saveCss } from "$lib/manage.remote.ts";
import { mutate } from "$lib/mutate.ts";
import Icon from "../Icon.svelte";
import PageHeader from "./PageHeader.svelte";
import SaveBar, { type SaveState } from "./SaveBar.svelte";
import Section from "./Section.svelte";
// CSS is compiled against an allow-list, so this screen must say which rules were dropped and why
let {
systemId,
memberRef,
initialSource,
initialIssues,
}: {
systemId: string;
/** Present for a member's own stylesheet; absent for the system's. */
memberRef?: string;
initialSource: string;
initialIssues: CssIssue[];
} = $props();
// svelte-ignore state_referenced_locally
let source = $state(initialSource);
// svelte-ignore state_referenced_locally
let saved = $state(initialSource);
// svelte-ignore state_referenced_locally
let issues = $state<CssIssue[]>(initialIssues);
let kept = $state<number | null>(null);
let saveState = $state<SaveState>("clean");
const dirty = $derived(source !== saved);
const tooLong = $derived(source.length > MAX_CSS_LENGTH);
const reference = `${site.docsUrl}/projects/pkviewer/css`;
async function save() {
saveState = "saving";
const outcome = await mutate(
saveCss({
systemId,
...(memberRef ? { memberRef } : {}),
css: source,
}),
);
if (!outcome.ok) {
saveState = "error";
return;
}
saved = source;
issues = outcome.issues ?? [];
kept = outcome.kept ?? 0;
saveState = "saved";
}
</script>
<PageHeader
title="Advanced CSS"
description={memberRef
? "Styles this member's page only, layered on top of your system stylesheet."
: "Styles every page in your system. Members can add their own on top."}
>
{#snippet actions()}
<a class="btn" href={reference} rel="noopener">
CSS reference <Icon name="box-arrow-up-right" />
</a>
{/snippet}
</PageHeader>
<SaveBar
state={dirty && saveState !== "saving" ? "dirty" : saveState}
onSave={save}
onDiscard={() => {
source = saved;
saveState = "clean";
}}
disabled={tooLong}
message={tooLong ? `Too long: ${source.length} of ${MAX_CSS_LENGTH} characters.` : null}
/>
<Section
title={memberRef ? "This member's stylesheet" : "Your stylesheet"}
description="Selectors are scoped to your page automatically — write `.card`, not `#pkv-user .card`."
>
<div class="mg-field">
<label for="css-source">CSS</label>
<textarea
id="css-source"
class="mg-code"
bind:value={source}
spellcheck="false"
rows="20"
placeholder={".card {\n border-radius: 18px;\n background-color: #1b1520;\n}"}
oninput={() => (saveState = "dirty")}
></textarea>
<p class="mg-hint">
{source.length.toLocaleString()} of {MAX_CSS_LENGTH.toLocaleString()} characters.
</p>
</div>
</Section>
{#if issues.length > 0}
<Section
title="{issues.length} {issues.length === 1 ? 'line was' : 'lines were'} not applied"
description="Saved, but skipped when the page was built. Everything else still applies."
>
<ul class="mg-list">
{#each issues as issue, index (`${issue.line}-${index}`)}
<li class="mg-item">
<Icon name="exclamation-triangle" />
<div class="grow">
<strong>Line {issue.line}</strong>
<span class="muted">{CSS_ISSUE_MESSAGES[issue.kind] ?? issue.kind}</span>
{#if issue.detail}
<div class="mg-item-meta">
<code>{issue.detail}</code>
</div>
{/if}
</div>
</li>
{/each}
</ul>
</Section>
{:else if kept !== null && kept > 0}
<p class="mg-note" data-tone="ok" role="status" aria-live="polite">
All {kept} {kept === 1 ? "declaration" : "declarations"} applied.
</p>
{/if}
<Section title="What is not available">
<ul class="mg-plain">
<li>
<strong>No <code>url()</code>.</strong> Stylesheets make no network
requests, so nobody visiting your page hands their address to
somewhere else.
</li>
<li>
<strong>No <code>position: absolute</code> or <code>fixed</code>.</strong>
Nothing can be laid over the page — a convincing fake sign-in box is
the thing this prevents.
</li>
<li>
<strong>No <code>!important</code>.</strong> It is reserved for the
rules that keep badges and the site notice in place.
</li>
<li>
<strong>Badges and the site notice cannot be restyled.</strong> They
are pkviewer's statements, not part of your page's appearance.
</li>
<li>
<strong>Links only to Google Fonts and the pkviewer CDN.</strong>
<code>@import</code> a font, or point <code>background-image</code>
at the CDN. Anywhere else is refused, so a page cannot report its
visitors to a third party.
</li>
</ul>
<p class="mg-hint">
Anything not on the supported list is skipped rather than applied, and
says so above. The
<a href={reference} rel="noopener">CSS reference</a>
lists every property and variable you can use.
</p>
</Section>