mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 01:21:18 -08:00
redo settings and formatting things
This commit is contained in:
18 files changed
+1004
-515
No files matched your search
+1
-3
@@ -16,9 +16,7 @@
|
||||
"settingsLinkBaseUrl": "https://app.sable.moe",
|
||||
|
||||
"themeCatalogBaseUrl": "https://raw.githubusercontent.com/SableClient/themes/main/",
|
||||
"themeCatalogApprovedHostPrefixes": [
|
||||
"https://raw.githubusercontent.com/SableClient/themes/"
|
||||
],
|
||||
"themeCatalogApprovedHostPrefixes": ["https://raw.githubusercontent.com/SableClient/themes/"],
|
||||
|
||||
"slidingSync": {
|
||||
"enabled": true
|
||||
|
||||
@@ -258,17 +258,23 @@ type CosmeticsProps = {
|
||||
};
|
||||
|
||||
export function Cosmetics({ requestBack, requestClose }: CosmeticsProps) {
|
||||
const [themeBrowserOpen, setThemeBrowserOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<SettingsSectionPage title="Appearance" requestBack={requestBack} requestClose={requestClose}>
|
||||
<Box grow="Yes">
|
||||
<Scroll hideTrack visibility="Hover">
|
||||
<PageContent>
|
||||
<Box direction="Column" gap="700">
|
||||
<Appearance />
|
||||
<IdentityCosmetics />
|
||||
<JumboEmoji />
|
||||
<Privacy />
|
||||
<LanguageSpecificPronouns />
|
||||
<Appearance onThemeBrowserOpenChange={setThemeBrowserOpen} />
|
||||
{!themeBrowserOpen && (
|
||||
<>
|
||||
<IdentityCosmetics />
|
||||
<JumboEmoji />
|
||||
<Privacy />
|
||||
<LanguageSpecificPronouns />
|
||||
</>
|
||||
)}
|
||||
</Box>
|
||||
</PageContent>
|
||||
</Scroll>
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { Box, Button, Switch, Text } from 'folds';
|
||||
|
||||
import { SettingMenuSelector } from '$components/setting-menu-selector';
|
||||
import { SequenceCard } from '$components/sequence-card';
|
||||
import { SettingTile } from '$components/setting-tile';
|
||||
import { DarkTheme, LightTheme, Theme, ThemeKind, useThemeNames, useThemes } from '$hooks/useTheme';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { SequenceCardStyle } from '$features/settings/styles.css';
|
||||
import { useStore } from 'jotai/react';
|
||||
|
||||
import { useThemeCatalogOnboardingGate } from './ThemeCatalogOnboarding';
|
||||
import { ThemeCatalogSettings, usePatchSettings } from './ThemeCatalogSettings';
|
||||
|
||||
function makeThemeOptions(themes: Theme[], themeNames: Record<string, string>) {
|
||||
return themes.map((theme) => ({
|
||||
value: theme.id,
|
||||
label: themeNames[theme.id] ?? theme.id,
|
||||
}));
|
||||
}
|
||||
|
||||
function SelectTheme({ disabled }: Readonly<{ disabled?: boolean }>) {
|
||||
const themes = useThemes();
|
||||
const themeNames = useThemeNames();
|
||||
const [themeId, setThemeId] = useSetting(settingsAtom, 'themeId');
|
||||
|
||||
const themeOptions = makeThemeOptions(themes, themeNames);
|
||||
const selectedThemeId =
|
||||
themeOptions.find((theme) => theme.value === themeId)?.value ?? LightTheme.id;
|
||||
|
||||
return (
|
||||
<SettingMenuSelector
|
||||
value={selectedThemeId}
|
||||
options={themeOptions}
|
||||
onSelect={setThemeId}
|
||||
disabled={disabled}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function SystemThemePreferences() {
|
||||
const themeNames = useThemeNames();
|
||||
const themes = useThemes();
|
||||
const [lightThemeId, setLightThemeId] = useSetting(settingsAtom, 'lightThemeId');
|
||||
const [darkThemeId, setDarkThemeId] = useSetting(settingsAtom, 'darkThemeId');
|
||||
|
||||
const lightThemes = themes.filter((theme) => theme.kind === ThemeKind.Light);
|
||||
const darkThemes = themes.filter((theme) => theme.kind === ThemeKind.Dark);
|
||||
const lightThemeOptions = makeThemeOptions(lightThemes, themeNames);
|
||||
const darkThemeOptions = makeThemeOptions(darkThemes, themeNames);
|
||||
|
||||
const selectedLightThemeId =
|
||||
lightThemeOptions.find((theme) => theme.value === lightThemeId)?.value ?? LightTheme.id;
|
||||
const selectedDarkThemeId =
|
||||
darkThemeOptions.find((theme) => theme.value === darkThemeId)?.value ?? DarkTheme.id;
|
||||
|
||||
return (
|
||||
<Box wrap="Wrap" gap="400">
|
||||
<SettingTile
|
||||
title="Light Theme:"
|
||||
focusId="light-theme"
|
||||
after={
|
||||
<SettingMenuSelector
|
||||
value={selectedLightThemeId}
|
||||
options={lightThemeOptions}
|
||||
onSelect={setLightThemeId}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<SettingTile
|
||||
title="Dark Theme:"
|
||||
focusId="dark-theme"
|
||||
after={
|
||||
<SettingMenuSelector
|
||||
value={selectedDarkThemeId}
|
||||
options={darkThemeOptions}
|
||||
onSelect={setDarkThemeId}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
function ClassicThemeSection({ onBrowseCatalog }: { onBrowseCatalog: () => void }) {
|
||||
const [systemTheme, setSystemTheme] = useSetting(settingsAtom, 'useSystemTheme');
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="System Theme"
|
||||
focusId="system-theme"
|
||||
description="Sync with your device's light/dark mode."
|
||||
after={<Switch variant="Primary" value={systemTheme} onChange={setSystemTheme} />}
|
||||
/>
|
||||
{systemTheme && <SystemThemePreferences />}
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Manual Theme"
|
||||
focusId="manual-theme"
|
||||
description="Active when System Theme is disabled."
|
||||
after={<SelectTheme disabled={systemTheme} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Remote theme catalog"
|
||||
focusId="browse-remote-catalog"
|
||||
description="Browse themes from the official catalog, save favorites, and sync with light and dark mode."
|
||||
after={
|
||||
<Button variant="Secondary" size="300" radii="300" onClick={onBrowseCatalog}>
|
||||
<Text size="B300">Browse catalog…</Text>
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
function RemoteCatalogThemeSection({
|
||||
onBrowseOpenChange,
|
||||
}: {
|
||||
onBrowseOpenChange?: (open: boolean) => void;
|
||||
}) {
|
||||
const [browseOpen, setBrowseOpen] = useState(false);
|
||||
const setBrowseOpenSafe = (open: boolean) => {
|
||||
setBrowseOpen(open);
|
||||
onBrowseOpenChange?.(open);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
onBrowseOpenChange?.(browseOpen);
|
||||
}, [browseOpen, onBrowseOpenChange]);
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="700">
|
||||
{!browseOpen ? (
|
||||
<>
|
||||
<ThemeCatalogSettings mode="local" />
|
||||
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Browse themes"
|
||||
focusId="theme-browse-remote"
|
||||
description="Download themes from the official catalog (star to save locally)."
|
||||
after={
|
||||
<Button
|
||||
variant="Secondary"
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={() => setBrowseOpenSafe(true)}
|
||||
>
|
||||
<Text size="B300">Browse themes</Text>
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<ThemeCatalogSettings mode="chat" />
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Browse themes"
|
||||
focusId="theme-browse-back"
|
||||
description="Download themes from the catalog."
|
||||
after={
|
||||
<Button
|
||||
variant="Secondary"
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={() => setBrowseOpenSafe(false)}
|
||||
>
|
||||
<Text size="B300">Back</Text>
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<ThemeCatalogSettings mode="remote" />
|
||||
</>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
export function ThemeAppearanceSection({
|
||||
onBrowseOpenChange,
|
||||
}: {
|
||||
onBrowseOpenChange?: (open: boolean) => void;
|
||||
} = {}) {
|
||||
const store = useStore();
|
||||
const [onboardingDone] = useSetting(settingsAtom, 'themeCatalogOnboardingDone');
|
||||
const [catalogEnabled] = useSetting(settingsAtom, 'themeRemoteCatalogEnabled');
|
||||
|
||||
useEffect(() => {
|
||||
if (!catalogEnabled) onBrowseOpenChange?.(false);
|
||||
}, [catalogEnabled, onBrowseOpenChange]);
|
||||
|
||||
const completeOnboarding = useCallback(
|
||||
(enabled: boolean) => {
|
||||
const next = { ...store.get(settingsAtom), themeCatalogOnboardingDone: true };
|
||||
next.themeRemoteCatalogEnabled = enabled;
|
||||
store.set(settingsAtom, next);
|
||||
},
|
||||
[store]
|
||||
);
|
||||
|
||||
const { dialog } = useThemeCatalogOnboardingGate(onboardingDone, completeOnboarding);
|
||||
|
||||
const patchSettings = usePatchSettings();
|
||||
const enableCatalog = useCallback(() => {
|
||||
patchSettings({ themeRemoteCatalogEnabled: true, themeCatalogOnboardingDone: true });
|
||||
}, [patchSettings]);
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Theme</Text>
|
||||
{dialog}
|
||||
{catalogEnabled ? (
|
||||
<RemoteCatalogThemeSection onBrowseOpenChange={onBrowseOpenChange} />
|
||||
) : (
|
||||
<ClassicThemeSection onBrowseCatalog={enableCatalog} />
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
config,
|
||||
Dialog,
|
||||
Header,
|
||||
Icon,
|
||||
IconButton,
|
||||
Icons,
|
||||
Overlay,
|
||||
OverlayBackdrop,
|
||||
OverlayCenter,
|
||||
Text,
|
||||
} from 'folds';
|
||||
|
||||
import { stopPropagation } from '$utils/keyboard';
|
||||
|
||||
type ThemeCatalogOnboardingProps = {
|
||||
open: boolean;
|
||||
onEnable: () => void;
|
||||
onDecline: () => void;
|
||||
};
|
||||
|
||||
export function ThemeCatalogOnboarding({ open, onEnable, onDecline }: ThemeCatalogOnboardingProps) {
|
||||
return (
|
||||
<Overlay open={open} backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
onDeactivate: onDecline,
|
||||
clickOutsideDeactivates: false,
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<Dialog variant="Surface">
|
||||
<Header
|
||||
style={{
|
||||
padding: `0 ${config.space.S200} 0 ${config.space.S400}`,
|
||||
borderBottomWidth: config.borderWidth.B300,
|
||||
}}
|
||||
variant="Surface"
|
||||
size="500"
|
||||
>
|
||||
<Box grow="Yes">
|
||||
<Text size="H4">Remote themes</Text>
|
||||
</Box>
|
||||
<IconButton size="300" radii="300" onClick={onDecline} aria-label="Close">
|
||||
<Icon src={Icons.Cross} size="100" />
|
||||
</IconButton>
|
||||
</Header>
|
||||
<Box style={{ padding: config.space.S400 }} direction="Column" gap="400">
|
||||
<Text priority="400">
|
||||
Load themes from the official Sable theme catalog on GitHub? You can browse
|
||||
previews, save favorites locally, and sync them with light and dark mode. If you
|
||||
choose not to, you can keep using the built-in Light and Dark themes only.
|
||||
</Text>
|
||||
<Box direction="Column" gap="200">
|
||||
<Button variant="Primary" fill="Solid" onClick={onEnable}>
|
||||
<Text size="B400">Yes, use the catalog</Text>
|
||||
</Button>
|
||||
<Button variant="Secondary" fill="Soft" onClick={onDecline}>
|
||||
<Text size="B400">No, built-in themes only</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
</Box>
|
||||
</Dialog>
|
||||
</FocusTrap>
|
||||
</OverlayCenter>
|
||||
</Overlay>
|
||||
);
|
||||
}
|
||||
|
||||
export function useThemeCatalogOnboardingGate(
|
||||
onboardingDone: boolean,
|
||||
onComplete: (enabled: boolean) => void
|
||||
) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!onboardingDone) {
|
||||
setOpen(true);
|
||||
}
|
||||
}, [onboardingDone]);
|
||||
|
||||
const handleEnable = useCallback(() => {
|
||||
setOpen(false);
|
||||
onComplete(true);
|
||||
}, [onComplete]);
|
||||
|
||||
const handleDecline = useCallback(() => {
|
||||
setOpen(false);
|
||||
onComplete(false);
|
||||
}, [onComplete]);
|
||||
|
||||
return {
|
||||
open,
|
||||
dialog: (
|
||||
<ThemeCatalogOnboarding open={open} onEnable={handleEnable} onDecline={handleDecline} />
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -1,13 +1,26 @@
|
||||
import { type ChangeEventHandler, useCallback, useMemo, useState } from 'react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { Box, Button, Chip, config, Input, Spinner, Switch, Text, toRem } from 'folds';
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
Chip,
|
||||
config,
|
||||
Icon,
|
||||
IconButton,
|
||||
Icons,
|
||||
Input,
|
||||
Spinner,
|
||||
Switch,
|
||||
Text,
|
||||
toRem,
|
||||
} from 'folds';
|
||||
import { useStore } from 'jotai/react';
|
||||
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import { ThemeKind } from '$hooks/useTheme';
|
||||
import { trimTrailingSlash } from '$utils/common';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom, type Settings } from '$state/settings';
|
||||
import { settingsAtom, type Settings, type ThemeRemoteFavorite } from '$state/settings';
|
||||
import { SequenceCardStyle } from '$features/settings/styles.css';
|
||||
import { SequenceCard } from '$components/sequence-card';
|
||||
import { SettingTile } from '$components/setting-tile';
|
||||
@@ -28,13 +41,26 @@ const DEFAULT_CATALOG_BASE = 'https://raw.githubusercontent.com/SableClient/them
|
||||
export type CatalogPreviewRow = ThemePair & {
|
||||
previewText: string;
|
||||
displayName: string;
|
||||
author?: string;
|
||||
kind: ThemeKind;
|
||||
contrast: SableThemeContrast;
|
||||
tags: string[];
|
||||
fullInstallUrl: string;
|
||||
};
|
||||
|
||||
function usePatchSettings() {
|
||||
export type LocalPreviewRow = ThemeRemoteFavorite & {
|
||||
previewText: string;
|
||||
displayName: string;
|
||||
contrast: SableThemeContrast;
|
||||
tags: string[];
|
||||
};
|
||||
|
||||
function previewUrlFromFullThemeUrl(fullUrl: string): string | undefined {
|
||||
if (!/\.sable\.css$/i.test(fullUrl)) return undefined;
|
||||
return fullUrl.replace(/\.sable\.css$/i, '.preview.sable.css');
|
||||
}
|
||||
|
||||
export function usePatchSettings() {
|
||||
const store = useStore();
|
||||
return useCallback(
|
||||
(partial: Partial<Settings>) => {
|
||||
@@ -105,19 +131,28 @@ function ThemePreviewMini({ scopeClass, styleBlock }: { scopeClass: string; styl
|
||||
);
|
||||
}
|
||||
|
||||
export function ThemeCatalogSettings() {
|
||||
export type ThemeCatalogSettingsMode = 'full' | 'local' | 'chat' | 'remote';
|
||||
|
||||
export function ThemeCatalogSettings({ mode = 'full' }: { mode?: ThemeCatalogSettingsMode }) {
|
||||
const clientConfig = useClientConfig();
|
||||
const patchSettings = usePatchSettings();
|
||||
const configBase = clientConfig.themeCatalogBaseUrl?.trim();
|
||||
const catalogBase = `${trimTrailingSlash(configBase && configBase.length > 0 ? configBase : DEFAULT_CATALOG_BASE)}/`;
|
||||
|
||||
const [catalogEnabled, setCatalogEnabled] = useSetting(settingsAtom, 'themeRemoteCatalogEnabled');
|
||||
const isRemoteMode = mode === 'remote' || mode === 'full';
|
||||
const isChatMode = mode === 'chat' || mode === 'full';
|
||||
const isLocalMode = mode === 'local' || mode === 'full';
|
||||
|
||||
const [favorites] = useSetting(settingsAtom, 'themeRemoteFavorites');
|
||||
const [systemTheme, setSystemTheme] = useSetting(settingsAtom, 'useSystemTheme');
|
||||
const [manualRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteManualFullUrl');
|
||||
const [lightRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteLightFullUrl');
|
||||
const [darkRemoteFullUrl] = useSetting(settingsAtom, 'themeRemoteDarkFullUrl');
|
||||
const [chatAny, setChatAny] = useSetting(settingsAtom, 'themeChatPreviewAnyUrl');
|
||||
const [chatApprovedOnly, setChatApprovedOnly] = useSetting(
|
||||
settingsAtom,
|
||||
'themeChatPreviewApprovedCatalogOnly'
|
||||
);
|
||||
const [systemTheme] = useSetting(settingsAtom, 'useSystemTheme');
|
||||
|
||||
const [search, setSearch] = useState('');
|
||||
const [kindFilter, setKindFilter] = useState<'all' | 'light' | 'dark'>('all');
|
||||
@@ -128,7 +163,7 @@ export function ThemeCatalogSettings() {
|
||||
const pairsQuery = useQuery({
|
||||
queryKey: ['theme-catalog-pairs', catalogBase],
|
||||
queryFn: () => listThemePairsFromCatalog(catalogBase),
|
||||
enabled: catalogEnabled,
|
||||
enabled: isRemoteMode,
|
||||
staleTime: 5 * 60_000,
|
||||
});
|
||||
|
||||
@@ -154,6 +189,7 @@ export function ThemeCatalogSettings() {
|
||||
...pair,
|
||||
previewText,
|
||||
displayName: meta.name?.trim() || pair.basename,
|
||||
author: meta.author?.trim() || undefined,
|
||||
kind,
|
||||
contrast,
|
||||
tags: meta.tags ?? [],
|
||||
@@ -163,7 +199,7 @@ export function ThemeCatalogSettings() {
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
enabled: catalogEnabled && Boolean(pairsQuery.data?.length),
|
||||
enabled: isRemoteMode && Boolean(pairsQuery.data?.length),
|
||||
staleTime: 10 * 60_000,
|
||||
});
|
||||
|
||||
@@ -184,54 +220,152 @@ export function ThemeCatalogSettings() {
|
||||
});
|
||||
}, [previewsQuery.data, search, kindFilter, contrastFilter]);
|
||||
|
||||
const prefetchFull = useCallback((url: string) => {
|
||||
const run = async () => {
|
||||
try {
|
||||
const res = await fetch(url, { mode: 'cors' });
|
||||
if (!res.ok) return;
|
||||
const text = await res.text();
|
||||
await putCachedThemeCss(url, text);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
};
|
||||
run();
|
||||
const localPreviewsQuery = useQuery({
|
||||
queryKey: ['theme-local-previews', favorites.map((f) => f.fullUrl).join('|')],
|
||||
enabled: isLocalMode && favorites.length > 0,
|
||||
staleTime: 10 * 60_000,
|
||||
queryFn: async (): Promise<LocalPreviewRow[]> => {
|
||||
const rows = await Promise.all(
|
||||
favorites.map(async (fav) => {
|
||||
const previewUrl = previewUrlFromFullThemeUrl(fav.fullUrl);
|
||||
if (!previewUrl) return undefined;
|
||||
|
||||
try {
|
||||
const res = await fetch(previewUrl, { mode: 'cors' });
|
||||
if (!res.ok) return undefined;
|
||||
const previewText = await res.text();
|
||||
const meta = parseSableThemeMetadata(previewText);
|
||||
const displayName = meta.name?.trim() || fav.displayName || fav.basename;
|
||||
const contrast: SableThemeContrast = meta.contrast === 'high' ? 'high' : 'low';
|
||||
return {
|
||||
...fav,
|
||||
previewText,
|
||||
displayName,
|
||||
contrast,
|
||||
tags: meta.tags ?? [],
|
||||
};
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return rows.filter((r): r is LocalPreviewRow => Boolean(r));
|
||||
},
|
||||
});
|
||||
|
||||
const removeFavorite = useCallback(
|
||||
(fullUrl: string) => {
|
||||
patchSettings({
|
||||
themeRemoteFavorites: favorites.filter((f) => f.fullUrl !== fullUrl),
|
||||
});
|
||||
},
|
||||
[favorites, patchSettings]
|
||||
);
|
||||
|
||||
const applyFavoriteToLight = useCallback(
|
||||
(row: LocalPreviewRow) => {
|
||||
patchSettings({
|
||||
themeRemoteLightFullUrl: row.fullUrl,
|
||||
themeRemoteLightKind: row.kind,
|
||||
});
|
||||
},
|
||||
[patchSettings]
|
||||
);
|
||||
|
||||
const applyFavoriteToDark = useCallback(
|
||||
(row: LocalPreviewRow) => {
|
||||
patchSettings({
|
||||
themeRemoteDarkFullUrl: row.fullUrl,
|
||||
themeRemoteDarkKind: row.kind,
|
||||
});
|
||||
},
|
||||
[patchSettings]
|
||||
);
|
||||
|
||||
const applyFavoriteToManual = useCallback(
|
||||
(row: LocalPreviewRow) => {
|
||||
patchSettings({
|
||||
themeRemoteManualFullUrl: row.fullUrl,
|
||||
themeRemoteManualKind: row.kind,
|
||||
});
|
||||
},
|
||||
[patchSettings]
|
||||
);
|
||||
|
||||
const useBuiltinForLightSlot = useCallback(
|
||||
() =>
|
||||
patchSettings({
|
||||
themeRemoteLightFullUrl: undefined,
|
||||
themeRemoteLightKind: undefined,
|
||||
}),
|
||||
[patchSettings]
|
||||
);
|
||||
|
||||
const useBuiltinForDarkSlot = useCallback(
|
||||
() =>
|
||||
patchSettings({
|
||||
themeRemoteDarkFullUrl: undefined,
|
||||
themeRemoteDarkKind: undefined,
|
||||
}),
|
||||
[patchSettings]
|
||||
);
|
||||
|
||||
const useBuiltinForManualLight = useCallback(
|
||||
() =>
|
||||
patchSettings({
|
||||
themeRemoteManualFullUrl: undefined,
|
||||
themeRemoteManualKind: undefined,
|
||||
themeId: 'light-theme',
|
||||
}),
|
||||
[patchSettings]
|
||||
);
|
||||
|
||||
const useBuiltinForManualDark = useCallback(
|
||||
() =>
|
||||
patchSettings({
|
||||
themeRemoteManualFullUrl: undefined,
|
||||
themeRemoteManualKind: undefined,
|
||||
themeId: 'dark-theme',
|
||||
}),
|
||||
[patchSettings]
|
||||
);
|
||||
|
||||
const prefetchFull = useCallback(async (url: string): Promise<boolean> => {
|
||||
try {
|
||||
const res = await fetch(url, { mode: 'cors' });
|
||||
if (!res.ok) return false;
|
||||
const text = await res.text();
|
||||
await putCachedThemeCss(url, text);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const installManual = useCallback(
|
||||
(row: CatalogPreviewRow) => {
|
||||
const toggleFavorite = useCallback(
|
||||
async (row: CatalogPreviewRow) => {
|
||||
const existing = favorites.find((f: ThemeRemoteFavorite) => f.fullUrl === row.fullInstallUrl);
|
||||
if (existing) {
|
||||
patchSettings({
|
||||
themeRemoteFavorites: favorites.filter((f) => f.fullUrl !== row.fullInstallUrl),
|
||||
});
|
||||
return;
|
||||
}
|
||||
const ok = await prefetchFull(row.fullInstallUrl);
|
||||
if (!ok) return;
|
||||
const kind: 'light' | 'dark' = row.kind === ThemeKind.Dark ? 'dark' : 'light';
|
||||
const next: ThemeRemoteFavorite = {
|
||||
fullUrl: row.fullInstallUrl,
|
||||
displayName: row.displayName,
|
||||
basename: row.basename,
|
||||
kind,
|
||||
};
|
||||
patchSettings({
|
||||
themeRemoteManualFullUrl: row.fullInstallUrl,
|
||||
themeRemoteManualKind: kind,
|
||||
themeRemoteFavorites: [...favorites, next],
|
||||
});
|
||||
prefetchFull(row.fullInstallUrl);
|
||||
},
|
||||
[patchSettings, prefetchFull]
|
||||
);
|
||||
|
||||
const installLightSlot = useCallback(
|
||||
(row: CatalogPreviewRow) => {
|
||||
const kind: 'light' | 'dark' = row.kind === ThemeKind.Dark ? 'dark' : 'light';
|
||||
patchSettings({
|
||||
themeRemoteLightFullUrl: row.fullInstallUrl,
|
||||
themeRemoteLightKind: kind,
|
||||
});
|
||||
prefetchFull(row.fullInstallUrl);
|
||||
},
|
||||
[patchSettings, prefetchFull]
|
||||
);
|
||||
|
||||
const installDarkSlot = useCallback(
|
||||
(row: CatalogPreviewRow) => {
|
||||
const kind: 'light' | 'dark' = row.kind === ThemeKind.Dark ? 'dark' : 'light';
|
||||
patchSettings({
|
||||
themeRemoteDarkFullUrl: row.fullInstallUrl,
|
||||
themeRemoteDarkKind: kind,
|
||||
});
|
||||
prefetchFull(row.fullInstallUrl);
|
||||
},
|
||||
[patchSettings, prefetchFull]
|
||||
[favorites, patchSettings, prefetchFull]
|
||||
);
|
||||
|
||||
const clearRemote = useCallback(() => {
|
||||
@@ -247,84 +381,22 @@ export function ThemeCatalogSettings() {
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Theme catalog</Text>
|
||||
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Load themes from catalog"
|
||||
focusId="theme-catalog-enabled"
|
||||
description="Fetches the public theme list from the configured GitHub raw URL (see config). Required before browsing or installing remote themes."
|
||||
after={<Switch variant="Primary" value={catalogEnabled} onChange={setCatalogEnabled} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Chat: theme previews from any HTTPS URL"
|
||||
focusId="theme-chat-preview-any"
|
||||
description="When enabled, messages linking to .preview.sable.css may fetch and show a small preview (parsed for safety)."
|
||||
after={<Switch variant="Primary" value={chatAny} onChange={setChatAny} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Chat: only approved catalog URLs"
|
||||
focusId="theme-chat-preview-approved"
|
||||
description="Restricts chat theme embeds to URLs under approved prefixes from config."
|
||||
after={
|
||||
<Switch variant="Primary" value={chatApprovedOnly} onChange={setChatApprovedOnly} />
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
{catalogEnabled && (
|
||||
{isChatMode && (
|
||||
<>
|
||||
<Text size="L400">Chat previews</Text>
|
||||
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<Box direction="Column" gap="200">
|
||||
<Text size="T300" priority="300">
|
||||
{configBase && configBase.length > 0
|
||||
? 'Catalog URL (from config): '
|
||||
: 'Catalog URL (default; set themeCatalogBaseUrl in config to override): '}
|
||||
{catalogBase}
|
||||
</Text>
|
||||
{pairsQuery.isPending && (
|
||||
<Box direction="Row" gap="200" alignItems="Center">
|
||||
<Spinner variant="Primary" size="400" />
|
||||
<Text size="T300">Loading catalog…</Text>
|
||||
</Box>
|
||||
)}
|
||||
{pairsQuery.isError && (
|
||||
<Text size="T300" style={{ color: 'var(--sable-crit-main)' }}>
|
||||
{pairsQuery.error?.message ?? 'Failed to load catalog'}
|
||||
</Text>
|
||||
)}
|
||||
{pairsQuery.isSuccess && pairsQuery.data.length === 0 && (
|
||||
<Text size="T300" priority="300">
|
||||
No paired theme files found (expect *.preview.sable.css + matching *.sable.css).
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
<SettingTile
|
||||
title="Chat: theme previews from any HTTPS URL"
|
||||
focusId="theme-chat-preview-any"
|
||||
description="When enabled, messages linking to .preview.sable.css may fetch and show a small preview (parsed for safety)."
|
||||
after={<Switch variant="Primary" value={chatAny} onChange={setChatAny} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard
|
||||
@@ -334,16 +406,43 @@ export function ThemeCatalogSettings() {
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Clear installed remote themes"
|
||||
focusId="theme-catalog-clear-remote"
|
||||
description="Removes remote full-theme URLs so built-in Light/Dark apply again."
|
||||
title="Chat: only approved catalog URLs"
|
||||
focusId="theme-chat-preview-approved"
|
||||
description="Restricts chat theme embeds to URLs under approved prefixes from config."
|
||||
after={
|
||||
<Button variant="Secondary" size="300" radii="300" onClick={clearRemote}>
|
||||
<Text size="B300">Clear</Text>
|
||||
</Button>
|
||||
<Switch variant="Primary" value={chatApprovedOnly} onChange={setChatApprovedOnly} />
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
</>
|
||||
)}
|
||||
|
||||
{isRemoteMode && (
|
||||
<>
|
||||
<Text size="L400">Browse catalog</Text>
|
||||
|
||||
{(pairsQuery.isPending || pairsQuery.isError) && (
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<Box direction="Column" gap="200">
|
||||
{pairsQuery.isPending && (
|
||||
<Box direction="Row" gap="200" alignItems="Center">
|
||||
<Spinner variant="Primary" size="400" />
|
||||
<Text size="T300">Loading catalog…</Text>
|
||||
</Box>
|
||||
)}
|
||||
{pairsQuery.isError && (
|
||||
<Text size="T300" style={{ color: 'var(--sable-crit-main)' }}>
|
||||
{pairsQuery.error?.message ?? 'Failed to load catalog'}
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
</SequenceCard>
|
||||
)}
|
||||
|
||||
{pairsQuery.isSuccess && pairsQuery.data.length > 0 && (
|
||||
<SequenceCard
|
||||
@@ -411,6 +510,7 @@ export function ThemeCatalogSettings() {
|
||||
const scopeClass = `sable-theme-preview--${slug}`;
|
||||
const styleBlock = buildPreviewStyleBlock(vars, scopeClass);
|
||||
const kindLabel = row.kind === ThemeKind.Dark ? 'Dark' : 'Light';
|
||||
const isFav = favorites.some((f) => f.fullUrl === row.fullInstallUrl);
|
||||
return (
|
||||
<Box
|
||||
key={row.basename}
|
||||
@@ -423,41 +523,33 @@ export function ThemeCatalogSettings() {
|
||||
background: 'var(--sable-surface-container)',
|
||||
}}
|
||||
>
|
||||
<Text size="H6">{row.displayName}</Text>
|
||||
<Text size="T200" priority="300">
|
||||
{kindLabel} · {row.contrast} contrast
|
||||
{row.tags.length > 0 ? ` · ${row.tags.join(', ')}` : ''}
|
||||
</Text>
|
||||
<ThemePreviewMini scopeClass={scopeClass} styleBlock={styleBlock} />
|
||||
{systemTheme ? (
|
||||
<Box direction="Column" gap="200">
|
||||
<Button
|
||||
variant="Secondary"
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={() => installLightSlot(row)}
|
||||
>
|
||||
<Text size="B300">Use when OS light</Text>
|
||||
</Button>
|
||||
<Button
|
||||
variant="Secondary"
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={() => installDarkSlot(row)}
|
||||
>
|
||||
<Text size="B300">Use when OS dark</Text>
|
||||
</Button>
|
||||
<Box
|
||||
direction="Row"
|
||||
alignItems="Start"
|
||||
justifyContent="SpaceBetween"
|
||||
gap="200"
|
||||
>
|
||||
<Box direction="Column" gap="100" grow="Yes">
|
||||
<Text size="H6">{row.displayName}</Text>
|
||||
<Text size="T200" priority="300">
|
||||
{kindLabel} · {row.contrast} contrast
|
||||
{row.author ? ` · by ${row.author}` : ''}
|
||||
{row.tags.length > 0 ? ` · ${row.tags.join(', ')}` : ''}
|
||||
</Text>
|
||||
</Box>
|
||||
) : (
|
||||
<Button
|
||||
variant="Primary"
|
||||
<IconButton
|
||||
size="300"
|
||||
variant={isFav ? 'Primary' : 'Secondary'}
|
||||
radii="300"
|
||||
onClick={() => installManual(row)}
|
||||
aria-label={isFav ? 'Remove from saved' : 'Save theme'}
|
||||
onClick={() => {
|
||||
toggleFavorite(row).catch(() => undefined);
|
||||
}}
|
||||
>
|
||||
<Text size="B300">Install</Text>
|
||||
</Button>
|
||||
)}
|
||||
<Icon size="200" src={Icons.Star} filled={isFav} />
|
||||
</IconButton>
|
||||
</Box>
|
||||
<ThemePreviewMini scopeClass={scopeClass} styleBlock={styleBlock} />
|
||||
</Box>
|
||||
);
|
||||
})}
|
||||
@@ -474,6 +566,189 @@ export function ThemeCatalogSettings() {
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
{isLocalMode && (
|
||||
<>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="Sync with system theme"
|
||||
focusId="theme-local-sync-system"
|
||||
description="When enabled, use different themes for OS light vs dark mode. When disabled, one manual theme is used."
|
||||
after={<Switch variant="Primary" value={systemTheme} onChange={setSystemTheme} />}
|
||||
/>
|
||||
|
||||
{systemTheme ? (
|
||||
<Box direction="Row" gap="200" wrap="Wrap" alignItems="Center">
|
||||
<Button variant="Secondary" size="300" radii="300" onClick={useBuiltinForLightSlot}>
|
||||
<Text size="B300">Built-in (OS light)</Text>
|
||||
</Button>
|
||||
<Button variant="Secondary" size="300" radii="300" onClick={useBuiltinForDarkSlot}>
|
||||
<Text size="B300">Built-in (OS dark)</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
) : (
|
||||
<Box direction="Row" gap="200" wrap="Wrap" alignItems="Center">
|
||||
<Button
|
||||
variant="Secondary"
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={useBuiltinForManualLight}
|
||||
>
|
||||
<Text size="B300">Built-in Light</Text>
|
||||
</Button>
|
||||
<Button
|
||||
variant="Secondary"
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={useBuiltinForManualDark}
|
||||
>
|
||||
<Text size="B300">Built-in Dark</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
)}
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Clear theme assignments"
|
||||
focusId="theme-catalog-clear-remote"
|
||||
description="Clears which saved themes apply to light/dark or manual mode. Saved stars stay available."
|
||||
after={
|
||||
<Button variant="Secondary" size="300" radii="300" onClick={clearRemote}>
|
||||
<Text size="B300">Clear</Text>
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
{localPreviewsQuery.isPending && (
|
||||
<Box direction="Row" gap="200" alignItems="Center">
|
||||
<Spinner variant="Primary" size="400" />
|
||||
<Text size="T300">Loading local previews…</Text>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{favorites.length === 0 && (
|
||||
<Text size="T300" priority="300">
|
||||
No saved themes yet. Star themes in the catalog to download them locally.
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{localPreviewsQuery.isSuccess && favorites.length > 0 && (
|
||||
<>
|
||||
{localPreviewsQuery.data.length === 0 ? (
|
||||
<Text size="T300" priority="300">
|
||||
Could not load local previews. If this happens, the theme preview file may be
|
||||
missing or not paired as `*.preview.sable.css`.
|
||||
</Text>
|
||||
) : (
|
||||
<Box
|
||||
style={{
|
||||
display: 'grid',
|
||||
gridTemplateColumns: 'repeat(auto-fill, minmax(220px, 1fr))',
|
||||
gap: toRem(16),
|
||||
}}
|
||||
>
|
||||
{localPreviewsQuery.data.map((row) => {
|
||||
const vars = extractSafePreviewCustomProperties(row.previewText);
|
||||
const slug = row.basename.replace(/[^a-zA-Z0-9_-]/g, '-') || 'theme';
|
||||
const scopeClass = `sable-theme-preview--${slug}`;
|
||||
const styleBlock = buildPreviewStyleBlock(vars, scopeClass);
|
||||
|
||||
const kindLabel = row.kind === 'dark' ? 'Dark' : 'Light';
|
||||
const isLightSelected = lightRemoteFullUrl === row.fullUrl;
|
||||
const isDarkSelected = darkRemoteFullUrl === row.fullUrl;
|
||||
const isManualSelected = manualRemoteFullUrl === row.fullUrl;
|
||||
|
||||
return (
|
||||
<Box
|
||||
key={row.fullUrl}
|
||||
direction="Column"
|
||||
gap="300"
|
||||
style={{
|
||||
padding: toRem(12),
|
||||
borderRadius: config.radii.R300,
|
||||
border: `${toRem(1)} solid var(--sable-surface-container-line)`,
|
||||
background: 'var(--sable-surface-container)',
|
||||
}}
|
||||
>
|
||||
<Box
|
||||
direction="Row"
|
||||
alignItems="Start"
|
||||
justifyContent="SpaceBetween"
|
||||
gap="200"
|
||||
>
|
||||
<Box direction="Column" gap="100" grow="Yes">
|
||||
<Text size="H6">{row.displayName}</Text>
|
||||
<Text size="T200" priority="300">
|
||||
{kindLabel} · {row.contrast} contrast
|
||||
{row.tags.length > 0 ? ` · ${row.tags.join(', ')}` : ''}
|
||||
</Text>
|
||||
</Box>
|
||||
|
||||
<IconButton
|
||||
size="300"
|
||||
variant="Secondary"
|
||||
radii="300"
|
||||
aria-label="Remove from saved"
|
||||
onClick={() => removeFavorite(row.fullUrl)}
|
||||
>
|
||||
<Icon size="200" src={Icons.Star} filled />
|
||||
</IconButton>
|
||||
</Box>
|
||||
|
||||
<ThemePreviewMini scopeClass={scopeClass} styleBlock={styleBlock} />
|
||||
|
||||
{systemTheme ? (
|
||||
<Box direction="Column" gap="200">
|
||||
<Button
|
||||
variant={isLightSelected ? 'Primary' : 'Secondary'}
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={() => applyFavoriteToLight(row)}
|
||||
>
|
||||
<Text size="B300">Use when OS light</Text>
|
||||
</Button>
|
||||
<Button
|
||||
variant={isDarkSelected ? 'Primary' : 'Secondary'}
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={() => applyFavoriteToDark(row)}
|
||||
>
|
||||
<Text size="B300">Use when OS dark</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
) : (
|
||||
<Button
|
||||
variant={isManualSelected ? 'Primary' : 'Secondary'}
|
||||
size="300"
|
||||
radii="300"
|
||||
onClick={() => applyFavoriteToManual(row)}
|
||||
>
|
||||
<Text size="B300">Use theme</Text>
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
})}
|
||||
</Box>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</SequenceCard>
|
||||
</>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -6,6 +6,9 @@ import { Appearance } from './Themes';
|
||||
type SettingsShape = {
|
||||
themeId?: string;
|
||||
useSystemTheme: boolean;
|
||||
themeCatalogOnboardingDone: boolean;
|
||||
themeRemoteCatalogEnabled: boolean;
|
||||
themeRemoteFavorites: unknown[];
|
||||
lightThemeId?: string;
|
||||
darkThemeId?: string;
|
||||
useSystemArboriumTheme: boolean;
|
||||
@@ -61,6 +64,9 @@ beforeEach(() => {
|
||||
currentSettings = {
|
||||
themeId: 'silver-theme',
|
||||
useSystemTheme: true,
|
||||
themeCatalogOnboardingDone: true,
|
||||
themeRemoteCatalogEnabled: false,
|
||||
themeRemoteFavorites: [],
|
||||
lightThemeId: 'cinny-light-theme',
|
||||
darkThemeId: 'black-theme',
|
||||
useSystemArboriumTheme: true,
|
||||
@@ -94,22 +100,24 @@ const getFirstEnabledButton = (name: string) =>
|
||||
screen.getAllByRole('button', { name }).find((node) => !node.hasAttribute('disabled'));
|
||||
|
||||
describe('Appearance settings', () => {
|
||||
it('renders Theme, Code Block Theme, and Visual Tweaks as separate sections', () => {
|
||||
it('renders Theme, Display, Code Block Theme, and Visual Tweaks as separate sections', () => {
|
||||
render(<Appearance />);
|
||||
|
||||
const themeHeading = screen.getByText('Theme');
|
||||
const displayHeading = screen.getByText('Display');
|
||||
const codeBlockThemeHeading = screen.getByText('Code Block Theme');
|
||||
const visualTweaksHeading = screen.getByText('Visual Tweaks');
|
||||
|
||||
expect(themeHeading.compareDocumentPosition(codeBlockThemeHeading)).toBe(
|
||||
expect(themeHeading.compareDocumentPosition(displayHeading)).toBe(
|
||||
Node.DOCUMENT_POSITION_FOLLOWING
|
||||
);
|
||||
expect(displayHeading.compareDocumentPosition(codeBlockThemeHeading)).toBe(
|
||||
Node.DOCUMENT_POSITION_FOLLOWING
|
||||
);
|
||||
expect(codeBlockThemeHeading.compareDocumentPosition(visualTweaksHeading)).toBe(
|
||||
Node.DOCUMENT_POSITION_FOLLOWING
|
||||
);
|
||||
expect(screen.getByRole('button', { name: 'Silver' })).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Cinny Light' })).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Black' })).toBeInTheDocument();
|
||||
expect(screen.getAllByRole('button', { name: 'Light' }).length).toBeGreaterThan(0);
|
||||
expect(screen.getByRole('button', { name: 'GitHub Light' })).toBeInTheDocument();
|
||||
expect(screen.getAllByRole('button', { name: 'Dracula' })).toHaveLength(2);
|
||||
expect(screen.getAllByRole('button', { name: 'Dracula' }).at(-1)).toBeDisabled();
|
||||
@@ -124,7 +132,7 @@ describe('Appearance settings', () => {
|
||||
|
||||
render(<Appearance />);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Silver' }));
|
||||
fireEvent.click(screen.getAllByRole('button', { name: 'Light' }).at(-1)!);
|
||||
clickLatestButton('Dark');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Dracula' }));
|
||||
@@ -134,19 +142,6 @@ describe('Appearance settings', () => {
|
||||
expect(getSetter('arboriumThemeId')).toHaveBeenCalledWith('ayu-light');
|
||||
});
|
||||
|
||||
it('updates the system theme settings when the chip selectors change', () => {
|
||||
render(<Appearance />);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Cinny Light' }));
|
||||
clickLatestButton('Silver');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Black' }));
|
||||
clickLatestButton('Dark');
|
||||
|
||||
expect(getSetter('lightThemeId')).toHaveBeenCalledWith('silver-theme');
|
||||
expect(getSetter('darkThemeId')).toHaveBeenCalledWith('dark-theme');
|
||||
});
|
||||
|
||||
it('updates the system code block theme settings when the chip selectors change', () => {
|
||||
render(<Appearance />);
|
||||
|
||||
@@ -169,7 +164,7 @@ describe('Appearance settings', () => {
|
||||
|
||||
render(<Appearance />);
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Light' })).toBeInTheDocument();
|
||||
expect(screen.getAllByRole('button', { name: 'Light' }).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('falls back to the active code block system theme when the stored manual theme id is invalid', () => {
|
||||
@@ -194,7 +189,7 @@ describe('Appearance settings', () => {
|
||||
|
||||
render(<Appearance />);
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Light' })).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Dark' })).toBeInTheDocument();
|
||||
expect(screen.getAllByRole('button', { name: 'Light' }).length).toBeGreaterThan(0);
|
||||
expect(screen.getAllByRole('button', { name: 'Dark' }).length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -11,27 +11,11 @@ import {
|
||||
getArboriumThemeLabel,
|
||||
getArboriumThemeOptions,
|
||||
} from '$plugins/arborium';
|
||||
import {
|
||||
DarkTheme,
|
||||
LightTheme,
|
||||
Theme,
|
||||
ThemeKind,
|
||||
useActiveTheme,
|
||||
useSystemThemeKind,
|
||||
useThemeNames,
|
||||
useThemes,
|
||||
} from '$hooks/useTheme';
|
||||
import { ThemeKind, useActiveTheme } from '$hooks/useTheme';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { SequenceCardStyle } from '$features/settings/styles.css';
|
||||
import { ThemeCatalogSettings } from './ThemeCatalogSettings';
|
||||
|
||||
function makeThemeOptions(themes: Theme[], themeNames: Record<string, string>) {
|
||||
return themes.map((theme) => ({
|
||||
value: theme.id,
|
||||
label: themeNames[theme.id] ?? theme.id,
|
||||
}));
|
||||
}
|
||||
import { ThemeAppearanceSection } from './ThemeAppearanceSection';
|
||||
|
||||
function makeArboriumThemeOptions(kind?: 'light' | 'dark') {
|
||||
const themes = kind
|
||||
@@ -70,86 +54,6 @@ function ThemeTrigger({
|
||||
);
|
||||
}
|
||||
|
||||
function SelectTheme({ disabled }: Readonly<{ disabled?: boolean }>) {
|
||||
const themes = useThemes();
|
||||
const themeNames = useThemeNames();
|
||||
const [themeId, setThemeId] = useSetting(settingsAtom, 'themeId');
|
||||
|
||||
const themeOptions = makeThemeOptions(themes, themeNames);
|
||||
const selectedThemeId =
|
||||
themeOptions.find((theme) => theme.value === themeId)?.value ?? LightTheme.id;
|
||||
|
||||
return (
|
||||
<SettingMenuSelector
|
||||
value={selectedThemeId}
|
||||
options={themeOptions}
|
||||
onSelect={setThemeId}
|
||||
disabled={disabled}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function SystemThemePreferences() {
|
||||
const themeKind = useSystemThemeKind();
|
||||
const themeNames = useThemeNames();
|
||||
const themes = useThemes();
|
||||
const [lightThemeId, setLightThemeId] = useSetting(settingsAtom, 'lightThemeId');
|
||||
const [darkThemeId, setDarkThemeId] = useSetting(settingsAtom, 'darkThemeId');
|
||||
|
||||
const lightThemes = themes.filter((theme) => theme.kind === ThemeKind.Light);
|
||||
const darkThemes = themes.filter((theme) => theme.kind === ThemeKind.Dark);
|
||||
const lightThemeOptions = makeThemeOptions(lightThemes, themeNames);
|
||||
const darkThemeOptions = makeThemeOptions(darkThemes, themeNames);
|
||||
|
||||
const selectedLightThemeId =
|
||||
lightThemeOptions.find((theme) => theme.value === lightThemeId)?.value ?? LightTheme.id;
|
||||
const selectedDarkThemeId =
|
||||
darkThemeOptions.find((theme) => theme.value === darkThemeId)?.value ?? DarkTheme.id;
|
||||
|
||||
return (
|
||||
<Box wrap="Wrap" gap="400">
|
||||
<SettingTile
|
||||
title="Light Theme:"
|
||||
focusId="light-theme"
|
||||
after={
|
||||
<SettingMenuSelector
|
||||
value={selectedLightThemeId}
|
||||
options={lightThemeOptions}
|
||||
onSelect={setLightThemeId}
|
||||
renderTrigger={({ selectedOption, openMenu, disabled }) => (
|
||||
<ThemeTrigger
|
||||
selectedLabel={selectedOption.label}
|
||||
onClick={openMenu}
|
||||
active={themeKind === ThemeKind.Light}
|
||||
disabled={disabled}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<SettingTile
|
||||
title="Dark Theme:"
|
||||
focusId="dark-theme"
|
||||
after={
|
||||
<SettingMenuSelector
|
||||
value={selectedDarkThemeId}
|
||||
options={darkThemeOptions}
|
||||
onSelect={setDarkThemeId}
|
||||
renderTrigger={({ selectedOption, openMenu, disabled }) => (
|
||||
<ThemeTrigger
|
||||
selectedLabel={selectedOption.label}
|
||||
onClick={openMenu}
|
||||
active={themeKind === ThemeKind.Dark}
|
||||
disabled={disabled}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
function SelectCodeBlockTheme({ disabled }: Readonly<{ disabled?: boolean }>) {
|
||||
const activeTheme = useActiveTheme();
|
||||
const [arboriumThemeId, setArboriumThemeId] = useSetting(settingsAtom, 'arboriumThemeId');
|
||||
@@ -281,8 +185,7 @@ function CodeBlockThemeSettings() {
|
||||
);
|
||||
}
|
||||
|
||||
function ThemeSettings() {
|
||||
const [systemTheme, setSystemTheme] = useSetting(settingsAtom, 'useSystemTheme');
|
||||
function ThemeVisualPreferences() {
|
||||
const [saturation, setSaturation] = useSetting(settingsAtom, 'saturationLevel');
|
||||
const [underlineLinks, setUnderlineLinks] = useSetting(settingsAtom, 'underlineLinks');
|
||||
const [reducedMotion, setReducedMotion] = useSetting(settingsAtom, 'reducedMotion');
|
||||
@@ -292,31 +195,7 @@ function ThemeSettings() {
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Theme</Text>
|
||||
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
variant="SurfaceVariant"
|
||||
direction="Column"
|
||||
gap="400"
|
||||
>
|
||||
<SettingTile
|
||||
title="System Theme"
|
||||
focusId="system-theme"
|
||||
description="Sync with your device's light/dark mode."
|
||||
after={<Switch variant="Primary" value={systemTheme} onChange={setSystemTheme} />}
|
||||
/>
|
||||
{systemTheme && <SystemThemePreferences />}
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Manual Theme"
|
||||
focusId="manual-theme"
|
||||
description="Active when System Theme is disabled."
|
||||
after={<SelectTheme disabled={systemTheme} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<Text size="L400">Display</Text>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
@@ -386,8 +265,6 @@ function ThemeSettings() {
|
||||
after={<Switch variant="Primary" value={autoplayEmojis} onChange={setAutoplayEmojis} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<ThemeCatalogSettings />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -482,49 +359,66 @@ function PageZoomInput() {
|
||||
);
|
||||
}
|
||||
|
||||
export function Appearance() {
|
||||
export function Appearance({
|
||||
onThemeBrowserOpenChange,
|
||||
}: {
|
||||
onThemeBrowserOpenChange?: (open: boolean) => void;
|
||||
} = {}) {
|
||||
const [twitterEmoji, setTwitterEmoji] = useSetting(settingsAtom, 'twitterEmoji');
|
||||
const [showEasterEggs, setShowEasterEggs] = useSetting(settingsAtom, 'showEasterEggs');
|
||||
const [themeBrowserOpen, setThemeBrowserOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<Box direction="Column" gap="700">
|
||||
<ThemeSettings />
|
||||
<CodeBlockThemeSettings />
|
||||
<ThemeAppearanceSection
|
||||
onBrowseOpenChange={(open) => {
|
||||
setThemeBrowserOpen(open);
|
||||
onThemeBrowserOpenChange?.(open);
|
||||
}}
|
||||
/>
|
||||
{!themeBrowserOpen && (
|
||||
<>
|
||||
<ThemeVisualPreferences />
|
||||
<CodeBlockThemeSettings />
|
||||
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Visual Tweaks</Text>
|
||||
<Box direction="Column" gap="100">
|
||||
<Text size="L400">Visual Tweaks</Text>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Twitter Emoji"
|
||||
focusId="twitter-emoji"
|
||||
description="Use Twitter-style emojis instead of system native ones."
|
||||
after={<Switch variant="Primary" value={twitterEmoji} onChange={setTwitterEmoji} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Twitter Emoji"
|
||||
focusId="twitter-emoji"
|
||||
description="Use Twitter-style emojis instead of system native ones."
|
||||
after={<Switch variant="Primary" value={twitterEmoji} onChange={setTwitterEmoji} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Show Easter Eggs"
|
||||
focusId="show-easter-eggs"
|
||||
description="Lets the interface keep a little mischief turned on."
|
||||
after={<Switch variant="Primary" value={showEasterEggs} onChange={setShowEasterEggs} />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Show Easter Eggs"
|
||||
focusId="show-easter-eggs"
|
||||
description="Lets the interface keep a little mischief turned on."
|
||||
after={
|
||||
<Switch variant="Primary" value={showEasterEggs} onChange={setShowEasterEggs} />
|
||||
}
|
||||
/>
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile title="Page Zoom" focusId="page-zoom" after={<PageZoomInput />} />
|
||||
</SequenceCard>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile title="Page Zoom" focusId="page-zoom" after={<PageZoomInput />} />
|
||||
</SequenceCard>
|
||||
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Subspace Hierarchy Limit"
|
||||
focusId="subspace-hierarchy-limit"
|
||||
description="The maximum nesting depth for Subspaces in the sidebar. Once this limit is reached, deeper Subspaces appear as links instead of nested folders."
|
||||
after={<SubnestedSpaceLinkDepthInput />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
</Box>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Subspace Hierarchy Limit"
|
||||
focusId="subspace-hierarchy-limit"
|
||||
description="The maximum nesting depth for Subspaces in the sidebar. Once this limit is reached, deeper Subspaces appear as links instead of nested folders."
|
||||
after={<SubnestedSpaceLinkDepthInput />}
|
||||
/>
|
||||
</SequenceCard>
|
||||
</Box>
|
||||
</>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -21,10 +21,7 @@ export const REMOTE_THEME_ID = 'sable-remote-theme';
|
||||
const isHttpsThemeUrl = (u: string | undefined): u is string =>
|
||||
Boolean(u && /^https:\/\//i.test(u.trim()));
|
||||
|
||||
function parseRemoteKind(
|
||||
value: 'light' | 'dark' | undefined,
|
||||
fallback: ThemeKind
|
||||
): ThemeKind {
|
||||
function parseRemoteKind(value: 'light' | 'dark' | undefined, fallback: ThemeKind): ThemeKind {
|
||||
if (value === 'dark') return ThemeKind.Dark;
|
||||
if (value === 'light') return ThemeKind.Light;
|
||||
return fallback;
|
||||
@@ -32,8 +29,7 @@ function parseRemoteKind(
|
||||
|
||||
function makeRemoteTheme(url: string, kind: ThemeKind): Theme {
|
||||
const fw = kind === ThemeKind.Dark ? onDarkFontWeight : onLightFontWeight;
|
||||
const kindClass =
|
||||
kind === ThemeKind.Dark ? 'sable-remote-kind-dark' : 'sable-remote-kind-light';
|
||||
const kindClass = kind === ThemeKind.Dark ? 'sable-remote-kind-dark' : 'sable-remote-kind-light';
|
||||
const legacyCssTheme = kind === ThemeKind.Dark ? 'dark-theme' : 'light-theme';
|
||||
const veTheme = kind === ThemeKind.Dark ? darkTheme : lightTheme;
|
||||
return {
|
||||
@@ -107,16 +103,8 @@ export const useActiveTheme = (): Theme => {
|
||||
|
||||
if (!systemTheme) {
|
||||
if (isHttpsThemeUrl(manualRemoteUrl)) {
|
||||
const inferred =
|
||||
themeId === 'dark-theme'
|
||||
? ThemeKind.Dark
|
||||
: themeId === 'light-theme'
|
||||
? ThemeKind.Light
|
||||
: ThemeKind.Light;
|
||||
return makeRemoteTheme(
|
||||
manualRemoteUrl,
|
||||
parseRemoteKind(manualRemoteKind, inferred)
|
||||
);
|
||||
const inferred = themeId === 'dark-theme' ? ThemeKind.Dark : ThemeKind.Light;
|
||||
return makeRemoteTheme(manualRemoteUrl, parseRemoteKind(manualRemoteKind, inferred));
|
||||
}
|
||||
return themes.find((theme) => theme.id === themeId) ?? LightTheme;
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ import {
|
||||
useActiveTheme,
|
||||
useSystemThemeKind,
|
||||
} from '$hooks/useTheme';
|
||||
import { getCachedThemeCss, putCachedThemeCss } from '../theme/cache';
|
||||
import { ArboriumThemeBridge } from '$plugins/arborium';
|
||||
import { useSetting } from '$state/hooks/settings';
|
||||
import { settingsAtom } from '$state/settings';
|
||||
import { getCachedThemeCss, putCachedThemeCss } from '../theme/cache';
|
||||
|
||||
const REMOTE_STYLE_ID = 'sable-remote-theme-style';
|
||||
|
||||
@@ -84,28 +84,27 @@ export function AuthRouteThemeManager({ children }: { children: ReactNode }) {
|
||||
|
||||
useEffect(() => {
|
||||
const url = activeTheme.remoteFullUrl?.trim();
|
||||
if (!url) {
|
||||
document.getElementById(REMOTE_STYLE_ID)?.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
|
||||
(async () => {
|
||||
const text = await loadRemoteThemeCssText(url);
|
||||
if (cancelled) return;
|
||||
if (!text) {
|
||||
document.getElementById(REMOTE_STYLE_ID)?.remove();
|
||||
return;
|
||||
}
|
||||
let node = document.getElementById(REMOTE_STYLE_ID) as HTMLStyleElement | null;
|
||||
if (!node) {
|
||||
node = document.createElement('style');
|
||||
node.id = REMOTE_STYLE_ID;
|
||||
document.head.appendChild(node);
|
||||
}
|
||||
node.textContent = text;
|
||||
})();
|
||||
if (url) {
|
||||
(async () => {
|
||||
const text = await loadRemoteThemeCssText(url);
|
||||
if (cancelled) return;
|
||||
if (!text) {
|
||||
document.getElementById(REMOTE_STYLE_ID)?.remove();
|
||||
return;
|
||||
}
|
||||
let node = document.getElementById(REMOTE_STYLE_ID) as HTMLStyleElement | null;
|
||||
if (!node) {
|
||||
node = document.createElement('style');
|
||||
node.id = REMOTE_STYLE_ID;
|
||||
document.head.appendChild(node);
|
||||
}
|
||||
node.textContent = text;
|
||||
})();
|
||||
} else {
|
||||
document.getElementById(REMOTE_STYLE_ID)?.remove();
|
||||
}
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
|
||||
@@ -23,6 +23,13 @@ export enum CaptionPosition {
|
||||
}
|
||||
export type JumboEmojiSize = 'none' | 'extraSmall' | 'small' | 'normal' | 'large' | 'extraLarge';
|
||||
|
||||
export type ThemeRemoteFavorite = {
|
||||
fullUrl: string;
|
||||
displayName: string;
|
||||
basename: string;
|
||||
kind: 'light' | 'dark';
|
||||
};
|
||||
|
||||
export interface Settings {
|
||||
themeId?: string;
|
||||
useSystemTheme: boolean;
|
||||
@@ -111,9 +118,6 @@ export interface Settings {
|
||||
alwaysShowCallButton: boolean;
|
||||
faviconForMentionsOnly: boolean;
|
||||
highlightMentions: boolean;
|
||||
/**
|
||||
* whether to enable pk compat
|
||||
*/
|
||||
pkCompat: boolean;
|
||||
pmpProxying: boolean;
|
||||
mentionInReplies: boolean;
|
||||
@@ -123,13 +127,14 @@ export interface Settings {
|
||||
renderAnimals: boolean;
|
||||
|
||||
// theme catalog
|
||||
themeCatalogOnboardingDone: boolean;
|
||||
themeRemoteFavorites: ThemeRemoteFavorite[];
|
||||
themeRemoteCatalogEnabled: boolean;
|
||||
themeChatPreviewAnyUrl: boolean;
|
||||
themeChatPreviewApprovedCatalogOnly: boolean;
|
||||
themeRemoteManualFullUrl?: string;
|
||||
themeRemoteLightFullUrl?: string;
|
||||
themeRemoteDarkFullUrl?: string;
|
||||
/** for Arborium / UI kind **/
|
||||
themeRemoteManualKind?: 'light' | 'dark';
|
||||
themeRemoteLightKind?: 'light' | 'dark';
|
||||
themeRemoteDarkKind?: 'light' | 'dark';
|
||||
@@ -233,6 +238,8 @@ const defaultSettings: Settings = {
|
||||
renderAnimals: true,
|
||||
|
||||
// theme catalog
|
||||
themeCatalogOnboardingDone: false,
|
||||
themeRemoteFavorites: [],
|
||||
themeRemoteCatalogEnabled: false,
|
||||
themeChatPreviewAnyUrl: false,
|
||||
themeChatPreviewApprovedCatalogOnly: false,
|
||||
@@ -258,10 +265,19 @@ export const getSettings = () => {
|
||||
}
|
||||
delete parsed.monochromeMode;
|
||||
|
||||
return {
|
||||
const merged = {
|
||||
...defaultSettings,
|
||||
...(parsed as Settings),
|
||||
};
|
||||
|
||||
if (merged.themeCatalogOnboardingDone === undefined) {
|
||||
merged.themeCatalogOnboardingDone = merged.themeRemoteCatalogEnabled === true;
|
||||
}
|
||||
if (!merged.themeRemoteFavorites) {
|
||||
merged.themeRemoteFavorites = [];
|
||||
}
|
||||
|
||||
return merged;
|
||||
};
|
||||
|
||||
export const setSettings = (settings: Settings) => {
|
||||
|
||||
@@ -16,13 +16,15 @@ describe('listThemePairsFromCatalog', () => {
|
||||
name: 'rose-pine.preview.sable.css',
|
||||
path: 'rose-pine.preview.sable.css',
|
||||
type: 'file',
|
||||
download_url: 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css',
|
||||
download_url:
|
||||
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css',
|
||||
},
|
||||
{
|
||||
name: 'rose-pine.sable.css',
|
||||
path: 'rose-pine.sable.css',
|
||||
type: 'file',
|
||||
download_url: 'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css',
|
||||
download_url:
|
||||
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css',
|
||||
},
|
||||
{
|
||||
name: 'README.md',
|
||||
|
||||
+21
-22
@@ -16,28 +16,6 @@ export type ThemePair = {
|
||||
const PREVIEW_SUFFIX = '.preview.sable.css';
|
||||
const FULL_SUFFIX = '.sable.css';
|
||||
|
||||
export async function listThemePairsFromCatalog(baseUrl: string): Promise<ThemePair[]> {
|
||||
const parts = parseGithubRawBaseUrl(baseUrl);
|
||||
if (!parts) return [];
|
||||
const items = await fetchGithubContents(parts);
|
||||
const previewFiles = items.filter(
|
||||
(i) => i.type === 'file' && i.name.endsWith(PREVIEW_SUFFIX)
|
||||
);
|
||||
const pairs: ThemePair[] = [];
|
||||
for (const p of previewFiles) {
|
||||
const basename = p.name.slice(0, -PREVIEW_SUFFIX.length);
|
||||
const fullName = `${basename}${FULL_SUFFIX}`;
|
||||
const full = items.find((i) => i.type === 'file' && i.name === fullName);
|
||||
if (!full?.download_url) continue;
|
||||
pairs.push({
|
||||
basename,
|
||||
previewUrl: rawFileUrl(parts, p.name),
|
||||
fullUrl: full.download_url,
|
||||
});
|
||||
}
|
||||
return pairs;
|
||||
}
|
||||
|
||||
/** Path segments for GET /repos/.../contents/{path} */
|
||||
function directoryPathToApiSegment(directoryPath: string): string {
|
||||
if (!directoryPath) return '';
|
||||
@@ -61,3 +39,24 @@ async function fetchGithubContents(parts: GithubRawParts): Promise<GithubContent
|
||||
const data = (await res.json()) as GithubContentItem | GithubContentItem[];
|
||||
return Array.isArray(data) ? data : [data];
|
||||
}
|
||||
|
||||
export async function listThemePairsFromCatalog(baseUrl: string): Promise<ThemePair[]> {
|
||||
const parts = parseGithubRawBaseUrl(baseUrl);
|
||||
if (!parts) return [];
|
||||
const items = await fetchGithubContents(parts);
|
||||
const previewFiles = items.filter((i) => i.type === 'file' && i.name.endsWith(PREVIEW_SUFFIX));
|
||||
const pairs: ThemePair[] = previewFiles
|
||||
.map((p) => {
|
||||
const basename = p.name.slice(0, -PREVIEW_SUFFIX.length);
|
||||
const fullName = `${basename}${FULL_SUFFIX}`;
|
||||
const full = items.find((i) => i.type === 'file' && i.name === fullName);
|
||||
if (!full?.download_url) return null;
|
||||
return {
|
||||
basename,
|
||||
previewUrl: rawFileUrl(parts, p.name),
|
||||
fullUrl: full.download_url,
|
||||
};
|
||||
})
|
||||
.filter((pair): pair is ThemePair => pair !== null);
|
||||
return pairs;
|
||||
}
|
||||
@@ -9,8 +9,7 @@ export type GithubRawParts = {
|
||||
directoryPath: string;
|
||||
};
|
||||
|
||||
const RAW_RE =
|
||||
/^https:\/\/raw\.githubusercontent\.com\/([^/]+)\/([^/]+)\/([^/]+)(?:\/(.*))?$/;
|
||||
const RAW_RE = /^https:\/\/raw\.githubusercontent\.com\/([^/]+)\/([^/]+)\/([^/]+)(?:\/(.*))?$/;
|
||||
|
||||
export function parseGithubRawBaseUrl(baseUrl: string): GithubRawParts | null {
|
||||
const trimmed = baseUrl.trim().replace(/\/+$/, '');
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
export * from './cache';
|
||||
export * from './catalog';
|
||||
export * from './githubRaw';
|
||||
export * from './legacyThemeMap';
|
||||
export * from './metadata';
|
||||
export * from './previewCss';
|
||||
export * from './themeUrlGuards';
|
||||
@@ -1,23 +0,0 @@
|
||||
/**
|
||||
* Map legacy theme ids to catalog basenames (SableClient/themes repo filenames without suffix) for migration.
|
||||
*/
|
||||
export const LEGACY_THEME_TO_BASENAME: Record<string, string> = {
|
||||
'silver-theme': 'silver',
|
||||
'cinny-light-theme': 'cinny-light',
|
||||
'cinny-silver-theme': 'cinny-silver',
|
||||
'dark-theme': 'dark',
|
||||
'light-theme': 'light',
|
||||
'butter-theme': 'butter',
|
||||
'rose-pine-theme': 'rose-pine',
|
||||
'cinny-dark-theme': 'cinny-dark',
|
||||
'gruvdark-theme': 'gruvdark',
|
||||
'accord-theme': 'accord',
|
||||
'black-theme': 'black',
|
||||
};
|
||||
|
||||
export const LEGACY_THEME_IDS = Object.keys(LEGACY_THEME_TO_BASENAME);
|
||||
|
||||
export function isLegacyBuiltinThemeId(id: string | undefined): boolean {
|
||||
if (!id) return false;
|
||||
return id !== 'light-theme' && id !== 'dark-theme' && id in LEGACY_THEME_TO_BASENAME;
|
||||
}
|
||||
+52
-45
@@ -15,6 +15,14 @@ export type SableThemeMetadata = {
|
||||
|
||||
const META_START = '@sable-theme';
|
||||
|
||||
function extractFirstBlockComment(cssText: string): string {
|
||||
const start = cssText.indexOf('/*');
|
||||
if (start === -1) return '';
|
||||
const end = cssText.indexOf('*/', start + 2);
|
||||
if (end === -1) return '';
|
||||
return cssText.slice(start + 2, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* First block comment must contain @sable-theme and key: value lines.
|
||||
*/
|
||||
@@ -24,54 +32,53 @@ export function parseSableThemeMetadata(cssText: string): Partial<SableThemeMeta
|
||||
|
||||
const lines = block.split(/\r?\n/).map((l) => l.replace(/^\s*\*?\s?/, '').trim());
|
||||
const out: Partial<SableThemeMetadata> = {};
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('@') || line === '---' || line === '') continue;
|
||||
const idx = line.indexOf(':');
|
||||
if (idx === -1) continue;
|
||||
const key = line.slice(0, idx).trim().toLowerCase();
|
||||
const value = line.slice(idx + 1).trim();
|
||||
switch (key) {
|
||||
case 'id':
|
||||
out.id = value;
|
||||
break;
|
||||
case 'name':
|
||||
out.name = value;
|
||||
break;
|
||||
case 'author':
|
||||
out.author = value;
|
||||
break;
|
||||
case 'kind':
|
||||
out.kind = value === 'dark' ? ThemeKind.Dark : ThemeKind.Light;
|
||||
break;
|
||||
case 'contrast':
|
||||
out.contrast = value === 'high' ? 'high' : 'low';
|
||||
break;
|
||||
case 'tags':
|
||||
out.tags = value.split(',').map((t) => t.trim()).filter(Boolean);
|
||||
break;
|
||||
case 'legacyids':
|
||||
case 'legacy_ids':
|
||||
out.legacyIds = value.split(',').map((t) => t.trim()).filter(Boolean);
|
||||
break;
|
||||
case 'fullthemeurl':
|
||||
case 'full_theme_url':
|
||||
out.fullThemeUrl = value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
lines
|
||||
.filter((line) => !(line.startsWith('@') || line === '---' || line === ''))
|
||||
.filter((line) => line.indexOf(':') !== -1)
|
||||
.forEach((line) => {
|
||||
const idx = line.indexOf(':');
|
||||
const key = line.slice(0, idx).trim().toLowerCase();
|
||||
const value = line.slice(idx + 1).trim();
|
||||
switch (key) {
|
||||
case 'id':
|
||||
out.id = value;
|
||||
break;
|
||||
case 'name':
|
||||
out.name = value;
|
||||
break;
|
||||
case 'author':
|
||||
out.author = value;
|
||||
break;
|
||||
case 'kind':
|
||||
out.kind = value === 'dark' ? ThemeKind.Dark : ThemeKind.Light;
|
||||
break;
|
||||
case 'contrast':
|
||||
out.contrast = value === 'high' ? 'high' : 'low';
|
||||
break;
|
||||
case 'tags':
|
||||
out.tags = value
|
||||
.split(',')
|
||||
.map((t) => t.trim())
|
||||
.filter(Boolean);
|
||||
break;
|
||||
case 'legacyids':
|
||||
case 'legacy_ids':
|
||||
out.legacyIds = value
|
||||
.split(',')
|
||||
.map((t) => t.trim())
|
||||
.filter(Boolean);
|
||||
break;
|
||||
case 'fullthemeurl':
|
||||
case 'full_theme_url':
|
||||
out.fullThemeUrl = value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
function extractFirstBlockComment(cssText: string): string {
|
||||
const start = cssText.indexOf('/*');
|
||||
if (start === -1) return '';
|
||||
const end = cssText.indexOf('*/', start + 2);
|
||||
if (end === -1) return '';
|
||||
return cssText.slice(start + 2, end);
|
||||
}
|
||||
|
||||
export function extractFullThemeUrlFromPreview(cssText: string): string | undefined {
|
||||
const meta = parseSableThemeMetadata(cssText);
|
||||
if (meta.fullThemeUrl && /^https:\/\//i.test(meta.fullThemeUrl)) {
|
||||
|
||||
+12
-10
@@ -10,17 +10,19 @@ const DANGEROUS_VALUE = /url\s*\(|@import|expression\s*\(|javascript:|\\0|<!--|-
|
||||
export function extractSafePreviewCustomProperties(cssText: string): Record<string, string> {
|
||||
const noComments = cssText.replace(/\/\*[\s\S]*?\*\//g, '');
|
||||
const vars: Record<string, string> = {};
|
||||
for (const line of noComments.split(/\r?\n/)) {
|
||||
noComments.split(/\r?\n/).forEach((line) => {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith('@')) continue;
|
||||
const m = trimmed.match(PROP_RE);
|
||||
if (!m) continue;
|
||||
const name = m[1];
|
||||
let val = m[2].trim();
|
||||
if (DANGEROUS_VALUE.test(val)) continue;
|
||||
if (val.length > 2000) continue;
|
||||
vars[name] = val;
|
||||
}
|
||||
if (trimmed && !trimmed.startsWith('@')) {
|
||||
const m = trimmed.match(PROP_RE);
|
||||
if (m) {
|
||||
const name = m[1];
|
||||
const val = m[2].trim();
|
||||
if (!DANGEROUS_VALUE.test(val) && val.length <= 2000) {
|
||||
vars[name] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return vars;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
const PREVIEW_RE = /\.preview\.sable\.css(\?|$|#)/i;
|
||||
const FULL_RE = /\.sable\.css(\?|$|#)/i;
|
||||
|
||||
export function isThemePreviewUrl(url: string): boolean {
|
||||
return /^https:\/\//i.test(url) && PREVIEW_RE.test(url) && !isThemeFullUrl(url);
|
||||
}
|
||||
|
||||
export function isThemeFullUrl(url: string): boolean {
|
||||
return /^https:\/\//i.test(url) && /\.sable\.css(\?|$|#)/i.test(url) && !PREVIEW_RE.test(url);
|
||||
}
|
||||
|
||||
export function isApprovedThemeUrl(url: string, approvedPrefixes: string[]): boolean {
|
||||
return approvedPrefixes.some((p) => url.startsWith(p));
|
||||
}
|
||||
Reference in new issue
Block a user