fix(studio): show a small spinner while applying fonts

This commit is contained in:
罗高
2026-11-12 15:34:05 +08:00
parent a3ef5ef08b
commit 4e7990a460
3 changed files with 66 additions and 4 deletions

View File

@@ -215,6 +215,43 @@ describe("FontFamilyField session font lists", () => {
});
describe("FontFamilyField installed fonts", () => {
it.each([[true, true], [false, true], [true, false], [false, false]])("shows a small loading indicator through font import and save (flat: %s, saved: %s)", async (flat, saved) => {
vi.resetModules();
const { FontFamilyField } = await import("./propertyPanelFont");
let imported!: (fonts: Array<{ family: string; path: string; url: string }>) => void;
let finish!: () => void, fail!: (reason: Error) => void;
const onImportFonts = vi.fn(() => new Promise<Array<{ family: string; path: string; url: string }>>(resolve => { imported = resolve; }));
const onCommit = vi.fn(() => new Promise<void>((resolve, reject) => { finish = resolve; fail = reject; }));
const log = vi.spyOn(console, "error").mockImplementation(() => {});
const host = document.createElement("div");
document.body.append(host);
const root = createRoot(host);
try {
await act(async () => root.render(<FontFamilyField flat={flat} value="Arial" importedFonts={[]} onImportFonts={onImportFonts} onCommit={onCommit} />));
await act(async () => new Promise(resolve => setTimeout(resolve, 10)));
const trigger = host.querySelector<HTMLButtonElement>("button")!;
await act(async () => trigger.click());
const option = Array.from(document.querySelectorAll<HTMLButtonElement>("[data-font-option-index]")).find(button => button.textContent?.includes("Menlo"))!;
await act(async () => option.click());
expect(trigger.disabled).toBe(true);
expect(trigger.getAttribute("aria-busy")).toBe("true");
expect(trigger.querySelector('[role="status"]')?.getAttribute("aria-label")).toBe("Loading");
expect(trigger.querySelector('[data-remix-icon="loader-4-line"]')).not.toBeNull();
expect(onCommit).not.toHaveBeenCalled();
await act(async () => imported([{ family: "Menlo", path: "font.ttf", url: "/font.ttf" }]));
expect(onCommit).toHaveBeenCalledOnce();
expect(trigger.querySelector('[role="status"]')).not.toBeNull();
await act(async () => saved ? finish() : fail(new Error("Save failed")));
expect(trigger.disabled).toBe(false);
expect(trigger.getAttribute("aria-busy")).toBe("false");
expect(trigger.querySelector('[role="status"]')).toBeNull();
if (!saved) {
expect(trigger.textContent).toContain("Arial");
expect(document.body.textContent).toContain('Couldn\'t import "Menlo"');
}
} finally { act(() => root.unmount()); log.mockRestore(); }
});
it.each([true, false])("uses the server font file without browser font access (available: %s)", async (available) => {
vi.resetModules();
const { FontFamilyField } = await import("./propertyPanelFont");

View File

@@ -17,6 +17,7 @@ import { useTrackDesignInput } from "../../contexts/DesignPanelInputContext";
import { runWhenInputIdle } from "./overlayFrameLoop";
import { studioApiFetch } from "../../utils/studioApiFetch";
import { useStudioShellContextOptional } from "../../contexts/StudioContext";
import { RemixIcon } from "../../icons/RemixIcons";
/* ------------------------------------------------------------------ */
/* Font helper functions */
@@ -426,6 +427,12 @@ export function FontFamilyField({
</Popover>
);
const fontLoadingIndicator = importingFonts ? (
<span role="status" aria-label={t("Loading")} className="flex shrink-0 text-panel-text-5">
<RemixIcon name="loader-4-line" size={10} className="animate-spin motion-reduce:animate-none" />
</span>
) : null;
if (flat) {
return (
<div className="relative flex min-h-[30px] items-center justify-between">
@@ -434,6 +441,7 @@ export function FontFamilyField({
type="button"
data-flat-font-trigger="true"
disabled={disabled || importingFonts}
aria-busy={importingFonts}
className="flex items-center gap-1.5 disabled:cursor-not-allowed"
>
<span
@@ -442,7 +450,7 @@ export function FontFamilyField({
>
{currentFamily}
</span>
<svg
{fontLoadingIndicator ?? <svg
width="10"
height="10"
viewBox="0 0 10 10"
@@ -450,7 +458,7 @@ export function FontFamilyField({
className="shrink-0 text-panel-text-5"
>
<path d="M2 3l3 4 3-4z" />
</svg>
</svg>}
</button>)}
</div>
);
@@ -462,6 +470,7 @@ export function FontFamilyField({
{renderPicker(<button
type="button"
disabled={disabled || importingFonts}
aria-busy={importingFonts}
className={`${FIELD} flex h-10 items-center justify-between gap-3 text-left hover:border-neutral-700 disabled:cursor-not-allowed`}
>
<span
@@ -470,8 +479,8 @@ export function FontFamilyField({
>
{currentFamily}
</span>
<span className="shrink-0 text-[10px] uppercase tracking-[0.14em] text-neutral-600">
{t("Font")}</span>
{fontLoadingIndicator ?? <span className="shrink-0 text-[10px] uppercase tracking-[0.14em] text-neutral-600">
{t("Font")}</span>}
</button>)}
</div>
);

View File

@@ -157,6 +157,22 @@ try {
results.pickerHeight = pickerHeight;
page.off('request', holdCatalog);
await page.setRequestInterception(false);
await page.setRequestInterception(true);
const holdImport = request => {
if (!new URL(request.url()).pathname.endsWith('/fonts/import')) void request.continue();
};
page.on('request', holdImport);
const firstImport = page.waitForRequest(request => new URL(request.url()).pathname.endsWith('/fonts/import'));
await choose(results.fonts[0].family);
const importing = await firstImport;
await page.waitForSelector('[data-flat-font-trigger][aria-busy="true"] [role="status"]');
await capture('font-applying.png');
await importing.continue();
page.off('request', holdImport);
await page.setRequestInterception(false);
await applied(results.fonts[0].family);
assert.equal(await page.$('[data-flat-font-trigger] [role="status"]'), null);
results.fontApplyingIndicator = true;
for (const font of results.fonts) {
await choose(font.family); await applied(font.family);
await page.waitForFunction(family => document.querySelector('hyperframes-player').shadowRoot.querySelector('iframe').contentDocument.querySelector('#title').style.fontFamily.includes(family), {}, font.family);