Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
* Source is local UI state only — it is inferred from the current
* value on mount and never persisted separately.
*
* Font data comes from window.slashedApp.bricksFonts (populated at
* page load from PHP), so no async REST fetch is needed.
* Font data starts from window.slashedApp.bricksFonts (populated at
* page load from PHP) and is refreshed on mount via the REST endpoint
* so newly added fonts appear without a full page reload.
*/
import { onMount } from 'svelte';
import { meta, tokens, writeField } from '../lib/stores.svelte.js';
import { fetchBricksFonts } from '../lib/api.js';
import FieldRow from './FieldRow.svelte';

let {
Expand Down Expand Up @@ -47,11 +50,29 @@
{ label: 'Handwritten', value: "'Segoe Print', 'Bradley Hand', Chilanka, TSCu_Comic, casual, cursive" },
];

// ── Bricks fonts from PHP bootstrap ─────────────────────────────────
// ── Bricks fonts: bootstrap then live refresh ────────────────────────
// Start from the PHP bootstrap so the dropdown is populated immediately,
// then fetch the live list on mount so fonts added since the page loaded
// (or within the CPT transient window) appear without a full reload.
/** @type {Array<{family:string, label:string, source:string}>} */
const bricksFonts = meta.bricksFonts;
let bricksFonts = $state(meta.bricksFonts);
const bricksEnabled = meta.activeIntegrations?.bricks ?? true;

onMount(async () => {
const initialSource = source;
try {
const fresh = await fetchBricksFonts();
bricksFonts = fresh;
// Re-infer source: a font present only in the live list would have
// been misclassified as 'manual' at init time.
if (source === initialSource && initialSource === 'manual' && detectSource(effectiveValue) === 'bricks') {
source = 'bricks';
}
} catch {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
// Keep bootstrap data if the fetch fails.
}
});

// ── Current value ─────────────────────────────────────────────────
const currentValue = $derived(
tokens[section]?.[fieldKey] === undefined || tokens[section]?.[fieldKey] === null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ export function saveSettings(settings) {
return call('/settings', settings);
}

/**
* Fetch the live Bricks font list from the REST endpoint.
*
* Falls back to the bootstrap snapshot in the Vite dev harness (no real
* backend). On a real WP install this always returns the current state,
* so newly-added fonts appear without a page reload.
*/
export async function fetchBricksFonts() {
const { url, nonce } = meta.rest;
if (!url) {
console.info('[slashed-admin] (dev) would GET /bricks-fonts');
return Array.isArray(meta.bricksFonts) ? meta.bricksFonts : [];
}
const res = await fetch(url + '/bricks-fonts', {
credentials: 'same-origin',
headers: { 'X-WP-Nonce': nonce },
});
if (!res.ok) throw new Error(await res.text() || `HTTP ${res.status}`);
const data = await res.json();
return Array.isArray(data?.fonts) ? data.fonts : [];
}

/**
* Fetch all current token overrides as a portable export envelope.
*
Expand Down
Loading