HifiPlanet-Website/hifi-src/src/context/CustomFontsContext.jsx
Maaxxs 326d124a60 Let admins upload their own custom fonts
Adds a new custom_fonts table (name, a server-generated collision-free
font_family identifier, file_path) with its own upload endpoint
(CustomFontController) separate from the generic image-only /uploads,
since it needs to accept WOFF2/WOFF/TTF/OTF instead. New management UI in
WebsiteSettings.jsx ("Eigene Schriftarten"): upload with a display name,
list with delete.

CustomFontsContext.jsx fetches the list once per layout (PublicLayout.jsx
for visitors, AdminLayout.jsx for the dashboard) and exposes it two ways:
useFontOptionsWithCustom() appends the uploaded fonts to the existing
static FONT_OPTIONS list for every font picker (RichTextEditor,
TextStyleWidget, the per-section and global site-font dropdowns), and a
new CustomFontFaces.jsx component renders the @font-face rules that
actually make an uploaded font usable - without it, picking a custom font
would just reference a name the browser has no file for. refresh() lets
WebsiteSettings.jsx update every picker immediately after an
upload/delete, not just on next page load.

Fonts::isValid() replaces the three places that validated a font_family
value against the fixed Fonts::VALUES list (site-wide default, per-section
override, per-field text style) - it now also accepts any uploaded
font's generated identifier, so those endpoints don't reject a custom
font as an unknown value.

Verified locally end-to-end: migration creates the table, a simulated
upload's font_family passes validation while a nonexistent one doesn't,
index() returns the expected shape, and destroy() removes both the row
and the file from uploads/.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-10 03:28:57 +02:00

32 lines
1.5 KiB
JavaScript

import { createContext, useContext } from 'react';
import { FONT_OPTIONS } from '../utils/fontOptions.js';
// Vom Admin selbst hochgeladene Schriftarten (siehe CustomFontController) - ein Request pro
// Seitenaufruf (siehe PublicLayout.jsx/AdminLayout.jsx), von dort ueber Context verteilt statt
// dass jede einzelne Schriftart-Auswahl (RichTextEditor, TextStyleWidget, ...) selbst laedt.
// refresh() laedt neu, ohne die ganze Seite neu zu laden - nach Hochladen/Loeschen einer
// Schriftart in WebsiteSettings.jsx, damit alle Schriftart-Auswahlen sofort den neuen Stand
// zeigen (auch auf derselben Seite), nicht erst nach Navigation/Reload.
const CustomFontsContext = createContext({ fonts: [], refresh: () => {} });
export const CustomFontsProvider = CustomFontsContext.Provider;
/** Die rohe Liste der hochgeladenen Schriftarten ({id, name, font_family, file_path}). */
export function useCustomFonts() {
return useContext(CustomFontsContext).fonts;
}
/** Laedt die Liste neu - nach Hochladen/Loeschen einer eigenen Schriftart aufrufen. */
export function useCustomFontsRefresh() {
return useContext(CustomFontsContext).refresh;
}
/** FONT_OPTIONS ergaenzt um die hochgeladenen Schriftarten - fuer jede Schriftart-Auswahl im
* Dashboard, damit eigene Schriftarten dort ueberall gleichermassen zur Wahl stehen. */
export function useFontOptionsWithCustom() {
const customFonts = useCustomFonts();
return [
...FONT_OPTIONS,
...customFonts.map((f) => ({ label: f.name, value: f.font_family })),
];
}