@open-slide/core 2.0.0-beta.3 → 2.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{build-Bzy-qlGc.js → build-Cu5_qRyE.js} +1 -1
- package/dist/cli/bin.js +3 -3
- package/dist/{config-nbSvY7z_.js → config-BQ6rD2iE.js} +226 -27
- package/dist/{config-DjWifhaA.d.ts → config-GMR1QUlM.d.ts} +1 -1
- package/dist/{dev-MXa5Tod4.js → dev-BK641O5e.js} +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/locale/index.d.ts +1 -1
- package/dist/locale/index.js +1 -1
- package/dist/{preview-DEpQcKz3.js → preview-BuCI-RMW.js} +1 -1
- package/dist/{types-BWg-Dtbg.d.ts → types-BcbaKTNY.d.ts} +60 -0
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +1 -1
- package/dist/{zh-tw-DlKyeNX7.js → zh-tw-B51sMTg3.js} +267 -31
- package/package.json +4 -4
- package/src/app/components/asset-view.tsx +448 -0
- package/src/app/components/history-provider.tsx +68 -44
- package/src/app/components/icon-switcher-indicator.tsx +10 -0
- package/src/app/components/inspector/arrange-panel.tsx +449 -0
- package/src/app/components/inspector/inline-text-editor.tsx +170 -284
- package/src/app/components/inspector/inspect-overlay.tsx +642 -295
- package/src/app/components/inspector/inspector-panel.tsx +434 -427
- package/src/app/components/inspector/inspector-provider.tsx +564 -293
- package/src/app/components/inspector/save-bar.tsx +36 -0
- package/src/app/components/panel/editor-sidebar.tsx +50 -0
- package/src/app/components/panel/panel-fields.tsx +204 -16
- package/src/app/components/panel/panel-shell.tsx +3 -18
- package/src/app/components/style-panel/style-panel.tsx +4 -69
- package/src/app/lib/assets.ts +33 -0
- package/src/app/lib/inspector/geometry.test.ts +324 -0
- package/src/app/lib/inspector/geometry.ts +207 -0
- package/src/app/lib/inspector/text-edit-timeline.test.ts +121 -0
- package/src/app/lib/inspector/text-edit-timeline.ts +12 -0
- package/src/app/lib/inspector/text-selection.ts +117 -0
- package/src/app/lib/inspector/use-editor.ts +1 -1
- package/src/app/lib/inspector/use-visual-editor.ts +429 -0
- package/src/app/lib/inspector/visual-dom.ts +255 -0
- package/src/app/lib/keys.ts +9 -0
- package/src/app/routes/slide.tsx +34 -9
- package/src/locale/en.ts +70 -8
- package/src/locale/ja.ts +72 -7
- package/src/locale/types.ts +60 -0
- package/src/locale/zh-cn.ts +68 -8
- package/src/locale/zh-tw.ts +68 -8
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
Search,
|
|
18
18
|
SearchX,
|
|
19
19
|
Trash2,
|
|
20
|
+
Type,
|
|
20
21
|
Upload,
|
|
21
22
|
X,
|
|
22
23
|
} from 'lucide-react';
|
|
@@ -59,12 +60,15 @@ import {
|
|
|
59
60
|
import {
|
|
60
61
|
type AssetEntry,
|
|
61
62
|
type AssetUsage,
|
|
63
|
+
fetchFontAsFile,
|
|
62
64
|
fetchSvgAsFile,
|
|
63
65
|
GLOBAL_ASSET_SCOPE,
|
|
66
|
+
type GoogleFontItem,
|
|
64
67
|
listAssetUsages,
|
|
65
68
|
renamedCopy,
|
|
66
69
|
revertAssetUsage,
|
|
67
70
|
type SvglItem,
|
|
71
|
+
searchGoogleFonts,
|
|
68
72
|
searchSvgl,
|
|
69
73
|
useAssets,
|
|
70
74
|
} from '@/lib/assets';
|
|
@@ -180,6 +184,7 @@ export function AssetView({ slideId }: Props) {
|
|
|
180
184
|
const [viewMode, setViewMode] = useViewMode();
|
|
181
185
|
const [sort, setSort] = useSortPreference();
|
|
182
186
|
const [gridColumns, setGridColumns] = useGridColumns();
|
|
187
|
+
const [fontSearchOpen, setFontSearchOpen] = useState(false);
|
|
183
188
|
const dragDepth = useRef(0);
|
|
184
189
|
const inputId = useId();
|
|
185
190
|
const t = useLocale();
|
|
@@ -361,6 +366,17 @@ export function AssetView({ slideId }: Props) {
|
|
|
361
366
|
<Search className="size-3.5" />
|
|
362
367
|
<span>{t.asset.searchLogos}</span>
|
|
363
368
|
</button>
|
|
369
|
+
<button
|
|
370
|
+
type="button"
|
|
371
|
+
onClick={() => setFontSearchOpen(true)}
|
|
372
|
+
className={cn(
|
|
373
|
+
'inline-flex h-8 cursor-pointer items-center gap-1.5 rounded-[5px] border border-border bg-card px-2.5 text-[12.5px] font-medium transition-colors',
|
|
374
|
+
'hover:bg-muted/60 hover:border-foreground/20 active:translate-y-px',
|
|
375
|
+
)}
|
|
376
|
+
>
|
|
377
|
+
<Type className="size-3.5" />
|
|
378
|
+
<span>{t.asset.searchFonts}</span>
|
|
379
|
+
</button>
|
|
364
380
|
<label
|
|
365
381
|
htmlFor={inputId}
|
|
366
382
|
className={cn(
|
|
@@ -631,6 +647,13 @@ export function AssetView({ slideId }: Props) {
|
|
|
631
647
|
onPick={(file) => handleFile(file)}
|
|
632
648
|
/>
|
|
633
649
|
)}
|
|
650
|
+
|
|
651
|
+
{fontSearchOpen && (
|
|
652
|
+
<FontSearchDialog
|
|
653
|
+
onClose={() => setFontSearchOpen(false)}
|
|
654
|
+
onPick={(file) => handleFile(file)}
|
|
655
|
+
/>
|
|
656
|
+
)}
|
|
634
657
|
</section>
|
|
635
658
|
);
|
|
636
659
|
}
|
|
@@ -1336,6 +1359,8 @@ function PreviewDialog({
|
|
|
1336
1359
|
className="max-h-[60vh] max-w-full object-contain"
|
|
1337
1360
|
/>
|
|
1338
1361
|
</div>
|
|
1362
|
+
) : isFontAsset(asset) ? (
|
|
1363
|
+
<FontPreview asset={asset} />
|
|
1339
1364
|
) : (
|
|
1340
1365
|
<div className="flex items-center justify-center rounded-md border bg-muted/40 py-12 text-muted-foreground">
|
|
1341
1366
|
<FileImage className="mr-2 size-5" />
|
|
@@ -1356,6 +1381,85 @@ function PreviewDialog({
|
|
|
1356
1381
|
);
|
|
1357
1382
|
}
|
|
1358
1383
|
|
|
1384
|
+
function isFontAsset(asset: AssetEntry): boolean {
|
|
1385
|
+
if (asset.mime.startsWith('font/') || /font/i.test(asset.mime)) return true;
|
|
1386
|
+
return /\.(ttf|otf|woff2?)$/i.test(asset.name);
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
const FONT_PREVIEW_SIZES = [40, 28, 20, 14] as const;
|
|
1390
|
+
|
|
1391
|
+
function FontPreview({ asset }: { asset: AssetEntry }) {
|
|
1392
|
+
const t = useLocale();
|
|
1393
|
+
const family = useMemo(
|
|
1394
|
+
() => `os-fontpreview-${asset.name.replace(/[^a-zA-Z0-9]+/g, '-')}`,
|
|
1395
|
+
[asset.name],
|
|
1396
|
+
);
|
|
1397
|
+
const [status, setStatus] = useState<'loading' | 'ready' | 'error'>('loading');
|
|
1398
|
+
const [sample, setSample] = useState('');
|
|
1399
|
+
|
|
1400
|
+
useEffect(() => {
|
|
1401
|
+
let cancelled = false;
|
|
1402
|
+
setStatus('loading');
|
|
1403
|
+
const face = new FontFace(family, `url("${asset.url}")`);
|
|
1404
|
+
face
|
|
1405
|
+
.load()
|
|
1406
|
+
.then((loaded) => {
|
|
1407
|
+
if (cancelled) return;
|
|
1408
|
+
document.fonts.add(loaded);
|
|
1409
|
+
setStatus('ready');
|
|
1410
|
+
})
|
|
1411
|
+
.catch(() => {
|
|
1412
|
+
if (!cancelled) setStatus('error');
|
|
1413
|
+
});
|
|
1414
|
+
return () => {
|
|
1415
|
+
cancelled = true;
|
|
1416
|
+
document.fonts.delete(face);
|
|
1417
|
+
};
|
|
1418
|
+
}, [family, asset.url]);
|
|
1419
|
+
|
|
1420
|
+
if (status === 'error') {
|
|
1421
|
+
return (
|
|
1422
|
+
<div className="flex items-center justify-center rounded-md border bg-muted/40 py-12 text-muted-foreground">
|
|
1423
|
+
<FileImage className="mr-2 size-5" />
|
|
1424
|
+
<span className="text-sm">{t.asset.noPreview}</span>
|
|
1425
|
+
</div>
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
const text = sample.trim() || t.asset.fontPreviewSample;
|
|
1430
|
+
return (
|
|
1431
|
+
<div className="flex flex-col gap-3">
|
|
1432
|
+
<div className="relative">
|
|
1433
|
+
<Type className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground" />
|
|
1434
|
+
<input
|
|
1435
|
+
value={sample}
|
|
1436
|
+
onChange={(e) => setSample(e.target.value)}
|
|
1437
|
+
placeholder={t.asset.fontSearchPreviewPlaceholder}
|
|
1438
|
+
className="h-9 w-full rounded-[6px] border border-border bg-background py-2 pl-8 pr-3 text-[13px] outline-none focus-visible:border-foreground/40 focus-visible:ring-2 focus-visible:ring-ring/30"
|
|
1439
|
+
/>
|
|
1440
|
+
</div>
|
|
1441
|
+
<div
|
|
1442
|
+
className={cn(
|
|
1443
|
+
'flex max-h-[50vh] flex-col gap-4 overflow-y-auto rounded-md border bg-card px-5 py-5 transition-opacity',
|
|
1444
|
+
status === 'loading' && 'opacity-40',
|
|
1445
|
+
)}
|
|
1446
|
+
>
|
|
1447
|
+
{FONT_PREVIEW_SIZES.map((size) => (
|
|
1448
|
+
<div key={size} className="flex items-baseline gap-3">
|
|
1449
|
+
<span className="w-7 shrink-0 font-mono text-[10px] text-muted-foreground">{size}</span>
|
|
1450
|
+
<span
|
|
1451
|
+
className="min-w-0 break-words"
|
|
1452
|
+
style={{ fontFamily: `'${family}', sans-serif`, fontSize: size, lineHeight: 1.25 }}
|
|
1453
|
+
>
|
|
1454
|
+
{text}
|
|
1455
|
+
</span>
|
|
1456
|
+
</div>
|
|
1457
|
+
))}
|
|
1458
|
+
</div>
|
|
1459
|
+
</div>
|
|
1460
|
+
);
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1359
1463
|
const SKELETON_SLOTS = ['s0', 's1', 's2', 's3', 's4', 's5'] as const;
|
|
1360
1464
|
|
|
1361
1465
|
function LogoSearchDialog({
|
|
@@ -1628,6 +1732,350 @@ function LogoResultCard({
|
|
|
1628
1732
|
);
|
|
1629
1733
|
}
|
|
1630
1734
|
|
|
1735
|
+
function FontNoResultsMessage({ query, t }: { query: string; t: ReturnType<typeof useLocale> }) {
|
|
1736
|
+
const [prefix, suffix] = t.asset.fontSearchNoResults.split('{query}');
|
|
1737
|
+
return (
|
|
1738
|
+
<>
|
|
1739
|
+
{prefix}
|
|
1740
|
+
<span className="font-mono text-foreground">{query}</span>
|
|
1741
|
+
{suffix}
|
|
1742
|
+
</>
|
|
1743
|
+
);
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
function FontSearchDialog({
|
|
1747
|
+
onClose,
|
|
1748
|
+
onPick,
|
|
1749
|
+
}: {
|
|
1750
|
+
onClose: () => void;
|
|
1751
|
+
onPick: (file: File) => Promise<void> | void;
|
|
1752
|
+
}) {
|
|
1753
|
+
const [query, setQuery] = useState('');
|
|
1754
|
+
const [previewText, setPreviewText] = useState('');
|
|
1755
|
+
const [results, setResults] = useState<GoogleFontItem[] | null>(null);
|
|
1756
|
+
const [loading, setLoading] = useState(true);
|
|
1757
|
+
const [error, setError] = useState<string | null>(null);
|
|
1758
|
+
const [pending, setPending] = useState<Set<string>>(() => new Set());
|
|
1759
|
+
const [retryToken, setRetryToken] = useState(0);
|
|
1760
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
1761
|
+
const t = useLocale();
|
|
1762
|
+
|
|
1763
|
+
useEffect(() => {
|
|
1764
|
+
queueMicrotask(() => inputRef.current?.focus());
|
|
1765
|
+
}, []);
|
|
1766
|
+
|
|
1767
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: retryToken is a bump-to-refetch trigger
|
|
1768
|
+
useEffect(() => {
|
|
1769
|
+
const ctrl = new AbortController();
|
|
1770
|
+
const timer = setTimeout(() => {
|
|
1771
|
+
setLoading(true);
|
|
1772
|
+
setError(null);
|
|
1773
|
+
searchGoogleFonts(query, ctrl.signal)
|
|
1774
|
+
.then((next) => {
|
|
1775
|
+
setResults(next);
|
|
1776
|
+
setLoading(false);
|
|
1777
|
+
})
|
|
1778
|
+
.catch((err: unknown) => {
|
|
1779
|
+
if (ctrl.signal.aborted) return;
|
|
1780
|
+
setError(err instanceof Error ? err.message : t.asset.toastSearchFailed);
|
|
1781
|
+
setLoading(false);
|
|
1782
|
+
});
|
|
1783
|
+
}, 200);
|
|
1784
|
+
return () => {
|
|
1785
|
+
clearTimeout(timer);
|
|
1786
|
+
ctrl.abort();
|
|
1787
|
+
};
|
|
1788
|
+
}, [query, retryToken]);
|
|
1789
|
+
|
|
1790
|
+
// Pull the actual web fonts so previews render in their real typeface.
|
|
1791
|
+
useEffect(() => {
|
|
1792
|
+
if (!results || results.length === 0) return;
|
|
1793
|
+
const link = document.createElement('link');
|
|
1794
|
+
link.rel = 'stylesheet';
|
|
1795
|
+
link.href = googleFontsCssUrl(results);
|
|
1796
|
+
document.head.appendChild(link);
|
|
1797
|
+
return () => {
|
|
1798
|
+
link.remove();
|
|
1799
|
+
};
|
|
1800
|
+
}, [results]);
|
|
1801
|
+
|
|
1802
|
+
return (
|
|
1803
|
+
<Dialog open onOpenChange={(open) => !open && onClose()}>
|
|
1804
|
+
<DialogContent className="sm:max-w-2xl">
|
|
1805
|
+
<DialogHeader>
|
|
1806
|
+
<DialogTitle>{t.asset.fontSearchTitle}</DialogTitle>
|
|
1807
|
+
<DialogDescription>
|
|
1808
|
+
{t.asset.fontSearchPoweredByPrefix}
|
|
1809
|
+
<a
|
|
1810
|
+
href="https://fonts.google.com"
|
|
1811
|
+
target="_blank"
|
|
1812
|
+
rel="noopener noreferrer"
|
|
1813
|
+
className="underline underline-offset-2"
|
|
1814
|
+
>
|
|
1815
|
+
Google Fonts
|
|
1816
|
+
</a>
|
|
1817
|
+
.
|
|
1818
|
+
</DialogDescription>
|
|
1819
|
+
</DialogHeader>
|
|
1820
|
+
|
|
1821
|
+
<div className="flex flex-col gap-2 sm:flex-row">
|
|
1822
|
+
<div className="relative sm:flex-1">
|
|
1823
|
+
<Search className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground" />
|
|
1824
|
+
<input
|
|
1825
|
+
ref={inputRef}
|
|
1826
|
+
value={query}
|
|
1827
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
1828
|
+
placeholder={t.asset.fontSearchPlaceholder}
|
|
1829
|
+
className="h-9 w-full rounded-[6px] border border-border bg-background py-2 pl-8 pr-3 text-[13px] outline-none focus-visible:border-foreground/40 focus-visible:ring-2 focus-visible:ring-ring/30"
|
|
1830
|
+
/>
|
|
1831
|
+
</div>
|
|
1832
|
+
<div className="relative sm:flex-1">
|
|
1833
|
+
<Type className="pointer-events-none absolute left-2.5 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground" />
|
|
1834
|
+
<input
|
|
1835
|
+
value={previewText}
|
|
1836
|
+
onChange={(e) => setPreviewText(e.target.value)}
|
|
1837
|
+
placeholder={t.asset.fontSearchPreviewPlaceholder}
|
|
1838
|
+
className="h-9 w-full rounded-[6px] border border-border bg-background py-2 pl-8 pr-3 text-[13px] outline-none focus-visible:border-foreground/40 focus-visible:ring-2 focus-visible:ring-ring/30"
|
|
1839
|
+
/>
|
|
1840
|
+
</div>
|
|
1841
|
+
</div>
|
|
1842
|
+
|
|
1843
|
+
<div className="max-h-[60vh] min-h-[16rem] overflow-y-auto">
|
|
1844
|
+
{error ? (
|
|
1845
|
+
<div className="flex h-64 flex-col items-center justify-center gap-3 px-6 text-center">
|
|
1846
|
+
<div className="flex size-12 items-center justify-center rounded-full bg-muted">
|
|
1847
|
+
<CloudOff className="size-5 text-muted-foreground" />
|
|
1848
|
+
</div>
|
|
1849
|
+
<div>
|
|
1850
|
+
<p className="text-sm font-medium">{t.asset.fontSearchErrorTitle}</p>
|
|
1851
|
+
<p className="mt-1 text-xs text-muted-foreground">{t.asset.fontSearchErrorBody}</p>
|
|
1852
|
+
</div>
|
|
1853
|
+
<Button
|
|
1854
|
+
variant="outline"
|
|
1855
|
+
size="sm"
|
|
1856
|
+
onClick={() => setRetryToken((n) => n + 1)}
|
|
1857
|
+
className="gap-1.5"
|
|
1858
|
+
>
|
|
1859
|
+
<RotateCw className="size-3.5" />
|
|
1860
|
+
{t.common.tryAgain}
|
|
1861
|
+
</Button>
|
|
1862
|
+
</div>
|
|
1863
|
+
) : loading && !results ? (
|
|
1864
|
+
<div className="flex flex-col gap-2.5">
|
|
1865
|
+
{SKELETON_SLOTS.map((slot) => (
|
|
1866
|
+
<div
|
|
1867
|
+
key={slot}
|
|
1868
|
+
className="h-[4.5rem] animate-pulse rounded-lg border bg-muted/40"
|
|
1869
|
+
/>
|
|
1870
|
+
))}
|
|
1871
|
+
</div>
|
|
1872
|
+
) : results && results.length === 0 ? (
|
|
1873
|
+
<div className="flex h-64 flex-col items-center justify-center gap-3 px-6 text-center">
|
|
1874
|
+
<div className="flex size-12 items-center justify-center rounded-full bg-muted">
|
|
1875
|
+
<SearchX className="size-5 text-muted-foreground" />
|
|
1876
|
+
</div>
|
|
1877
|
+
<div>
|
|
1878
|
+
<p className="text-sm font-medium">
|
|
1879
|
+
{query.trim() ? (
|
|
1880
|
+
<FontNoResultsMessage query={query.trim()} t={t} />
|
|
1881
|
+
) : (
|
|
1882
|
+
t.asset.fontSearchEmpty
|
|
1883
|
+
)}
|
|
1884
|
+
</p>
|
|
1885
|
+
<p className="mt-1 text-xs text-muted-foreground">
|
|
1886
|
+
{t.asset.fontSearchEmptyHintPrefix}
|
|
1887
|
+
<a
|
|
1888
|
+
href="https://fonts.google.com"
|
|
1889
|
+
target="_blank"
|
|
1890
|
+
rel="noopener noreferrer"
|
|
1891
|
+
className="underline underline-offset-2 hover:text-foreground"
|
|
1892
|
+
>
|
|
1893
|
+
Google Fonts
|
|
1894
|
+
</a>
|
|
1895
|
+
{t.asset.fontSearchEmptyHintSuffix}
|
|
1896
|
+
</p>
|
|
1897
|
+
</div>
|
|
1898
|
+
</div>
|
|
1899
|
+
) : (
|
|
1900
|
+
<div className="flex flex-col gap-2.5">
|
|
1901
|
+
{results?.map((item) => (
|
|
1902
|
+
<FontResultCard
|
|
1903
|
+
key={item.family}
|
|
1904
|
+
item={item}
|
|
1905
|
+
previewText={previewText.trim()}
|
|
1906
|
+
pending={pending.has(item.family)}
|
|
1907
|
+
onAdd={async (file) => {
|
|
1908
|
+
setPending((prev) => {
|
|
1909
|
+
const next = new Set(prev);
|
|
1910
|
+
next.add(item.family);
|
|
1911
|
+
return next;
|
|
1912
|
+
});
|
|
1913
|
+
try {
|
|
1914
|
+
await onPick(file);
|
|
1915
|
+
} catch (err) {
|
|
1916
|
+
toast.error(
|
|
1917
|
+
err instanceof Error ? err.message : t.asset.toastFontDownloadFailed,
|
|
1918
|
+
);
|
|
1919
|
+
} finally {
|
|
1920
|
+
setPending((prev) => {
|
|
1921
|
+
const next = new Set(prev);
|
|
1922
|
+
next.delete(item.family);
|
|
1923
|
+
return next;
|
|
1924
|
+
});
|
|
1925
|
+
}
|
|
1926
|
+
}}
|
|
1927
|
+
/>
|
|
1928
|
+
))}
|
|
1929
|
+
</div>
|
|
1930
|
+
)}
|
|
1931
|
+
</div>
|
|
1932
|
+
|
|
1933
|
+
<DialogFooter>
|
|
1934
|
+
<Button variant="outline" onClick={onClose}>
|
|
1935
|
+
{t.common.done}
|
|
1936
|
+
</Button>
|
|
1937
|
+
</DialogFooter>
|
|
1938
|
+
</DialogContent>
|
|
1939
|
+
</Dialog>
|
|
1940
|
+
);
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
function FontResultCard({
|
|
1944
|
+
item,
|
|
1945
|
+
previewText,
|
|
1946
|
+
pending,
|
|
1947
|
+
onAdd,
|
|
1948
|
+
}: {
|
|
1949
|
+
item: GoogleFontItem;
|
|
1950
|
+
previewText: string;
|
|
1951
|
+
pending: boolean;
|
|
1952
|
+
onAdd: (file: File) => Promise<void> | void;
|
|
1953
|
+
}) {
|
|
1954
|
+
const [variant, setVariant] = useState(() => defaultVariant(item.variants));
|
|
1955
|
+
const t = useLocale();
|
|
1956
|
+
const sample = previewText || item.family;
|
|
1957
|
+
const filenameBase = `${item.family.replace(/\s+/g, '')}-${variantLabel(variant).replace(/\s+/g, '')}`;
|
|
1958
|
+
const italic = variant.endsWith('i');
|
|
1959
|
+
const weight = (italic ? variant.slice(0, -1) : variant) || '400';
|
|
1960
|
+
const isDefaultVariant = variant === defaultVariant(item.variants);
|
|
1961
|
+
|
|
1962
|
+
// The dialog-level stylesheet only carries each family's default face, so
|
|
1963
|
+
// fetch the selected weight/style on demand for the preview.
|
|
1964
|
+
useEffect(() => {
|
|
1965
|
+
if (isDefaultVariant) return;
|
|
1966
|
+
const link = document.createElement('link');
|
|
1967
|
+
link.rel = 'stylesheet';
|
|
1968
|
+
link.href = googleFontsVariantCssUrl(item.family, weight, italic);
|
|
1969
|
+
document.head.appendChild(link);
|
|
1970
|
+
return () => {
|
|
1971
|
+
link.remove();
|
|
1972
|
+
};
|
|
1973
|
+
}, [isDefaultVariant, item.family, weight, italic]);
|
|
1974
|
+
|
|
1975
|
+
return (
|
|
1976
|
+
<div className="group flex items-center gap-3 rounded-lg border bg-card px-3 py-2.5">
|
|
1977
|
+
<div className="min-w-0 flex-1">
|
|
1978
|
+
<div className="flex items-center gap-2">
|
|
1979
|
+
<span className="truncate text-[11px] font-medium" title={item.family}>
|
|
1980
|
+
{item.family}
|
|
1981
|
+
</span>
|
|
1982
|
+
{item.category ? (
|
|
1983
|
+
<span className="shrink-0 text-[10px] text-muted-foreground">{item.category}</span>
|
|
1984
|
+
) : null}
|
|
1985
|
+
</div>
|
|
1986
|
+
<div
|
|
1987
|
+
className="mt-1 truncate text-2xl leading-tight"
|
|
1988
|
+
style={{
|
|
1989
|
+
fontFamily: `'${item.family}', sans-serif`,
|
|
1990
|
+
fontWeight: Number(weight) || 400,
|
|
1991
|
+
fontStyle: italic ? 'italic' : 'normal',
|
|
1992
|
+
}}
|
|
1993
|
+
title={sample}
|
|
1994
|
+
>
|
|
1995
|
+
{sample}
|
|
1996
|
+
</div>
|
|
1997
|
+
</div>
|
|
1998
|
+
<div className="flex shrink-0 items-center gap-1.5">
|
|
1999
|
+
{item.variants.length > 1 ? (
|
|
2000
|
+
<select
|
|
2001
|
+
aria-label={t.asset.fontWeightLabel}
|
|
2002
|
+
value={variant}
|
|
2003
|
+
onChange={(e) => setVariant(e.target.value)}
|
|
2004
|
+
className="h-6 w-30 rounded-md border bg-background px-1.5 text-[11px] outline-none focus-visible:ring-2 focus-visible:ring-ring/30"
|
|
2005
|
+
>
|
|
2006
|
+
{item.variants.map((v) => (
|
|
2007
|
+
<option key={v} value={v}>
|
|
2008
|
+
{variantLabel(v)}
|
|
2009
|
+
</option>
|
|
2010
|
+
))}
|
|
2011
|
+
</select>
|
|
2012
|
+
) : null}
|
|
2013
|
+
<Button
|
|
2014
|
+
size="sm"
|
|
2015
|
+
variant="outline"
|
|
2016
|
+
disabled={pending}
|
|
2017
|
+
onClick={async () => {
|
|
2018
|
+
try {
|
|
2019
|
+
const file = await fetchFontAsFile(item.family, variant, filenameBase);
|
|
2020
|
+
await onAdd(file);
|
|
2021
|
+
} catch (err) {
|
|
2022
|
+
toast.error(err instanceof Error ? err.message : t.asset.toastFontDownloadFailed);
|
|
2023
|
+
}
|
|
2024
|
+
}}
|
|
2025
|
+
className="h-6 px-2 text-[11px]"
|
|
2026
|
+
>
|
|
2027
|
+
{pending ? <Loader2 className="size-3 animate-spin" /> : t.common.add}
|
|
2028
|
+
</Button>
|
|
2029
|
+
</div>
|
|
2030
|
+
</div>
|
|
2031
|
+
);
|
|
2032
|
+
}
|
|
2033
|
+
|
|
2034
|
+
const WEIGHT_NAMES: Record<string, string> = {
|
|
2035
|
+
'100': 'Thin',
|
|
2036
|
+
'200': 'ExtraLight',
|
|
2037
|
+
'300': 'Light',
|
|
2038
|
+
'400': 'Regular',
|
|
2039
|
+
'500': 'Medium',
|
|
2040
|
+
'600': 'SemiBold',
|
|
2041
|
+
'700': 'Bold',
|
|
2042
|
+
'800': 'ExtraBold',
|
|
2043
|
+
'900': 'Black',
|
|
2044
|
+
};
|
|
2045
|
+
|
|
2046
|
+
function variantLabel(variant: string): string {
|
|
2047
|
+
const italic = variant.endsWith('i');
|
|
2048
|
+
const weight = italic ? variant.slice(0, -1) : variant;
|
|
2049
|
+
const base = WEIGHT_NAMES[weight] ?? weight;
|
|
2050
|
+
if (!italic) return base;
|
|
2051
|
+
return base === 'Regular' ? 'Italic' : `${base} Italic`;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
function defaultVariant(variants: string[]): string {
|
|
2055
|
+
if (variants.includes('400')) return '400';
|
|
2056
|
+
const firstUpright = variants.find((v) => !v.endsWith('i'));
|
|
2057
|
+
return firstUpright ?? variants[0] ?? '400';
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
function googleFontsVariantCssUrl(family: string, weight: string, italic: boolean): string {
|
|
2061
|
+
const name = encodeURIComponent(family).replace(/%20/g, '+');
|
|
2062
|
+
const axis = italic ? `ital,wght@1,${weight}` : `wght@${weight}`;
|
|
2063
|
+
return `https://fonts.googleapis.com/css2?family=${name}:${axis}&display=swap`;
|
|
2064
|
+
}
|
|
2065
|
+
|
|
2066
|
+
function googleFontsCssUrl(items: GoogleFontItem[]): string {
|
|
2067
|
+
const params = items
|
|
2068
|
+
.map((item) => {
|
|
2069
|
+
const name = encodeURIComponent(item.family).replace(/%20/g, '+');
|
|
2070
|
+
// css2 without an ital axis 400s on (or silently drops) italic-only
|
|
2071
|
+
// families like Molle — request their italic face explicitly.
|
|
2072
|
+
const italicOnly = item.variants.length > 0 && item.variants.every((v) => v.endsWith('i'));
|
|
2073
|
+
return italicOnly ? `family=${name}:ital@1` : `family=${name}`;
|
|
2074
|
+
})
|
|
2075
|
+
.join('&');
|
|
2076
|
+
return `https://fonts.googleapis.com/css2?${params}&display=swap`;
|
|
2077
|
+
}
|
|
2078
|
+
|
|
1631
2079
|
function basenameFromUrl(u: string): string {
|
|
1632
2080
|
try {
|
|
1633
2081
|
return new URL(u).pathname.split('/').pop() || '';
|
|
@@ -35,17 +35,31 @@ export function useHistory(): HistoryCtx {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export function HistoryProvider({ children }: { children: ReactNode }) {
|
|
38
|
-
const
|
|
39
|
-
|
|
38
|
+
const stacksRef = useRef<{ past: HistoryEntry[]; future: HistoryEntry[] }>({
|
|
39
|
+
past: [],
|
|
40
|
+
future: [],
|
|
41
|
+
});
|
|
42
|
+
const [availability, setAvailability] = useState({ canUndo: false, canRedo: false });
|
|
40
43
|
// Set while invoking an entry's undo/redo so providers can skip
|
|
41
44
|
// re-recording the resulting state mutation.
|
|
42
45
|
const suppressedRef = useRef(false);
|
|
43
46
|
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const syncAvailability = useCallback(() => {
|
|
48
|
+
const canUndo = stacksRef.current.past.length > 0;
|
|
49
|
+
const canRedo = stacksRef.current.future.length > 0;
|
|
50
|
+
setAvailability((previous) =>
|
|
51
|
+
previous.canUndo === canUndo && previous.canRedo === canRedo
|
|
52
|
+
? previous
|
|
53
|
+
: { canUndo, canRedo },
|
|
54
|
+
);
|
|
55
|
+
}, []);
|
|
56
|
+
|
|
57
|
+
const record = useCallback(
|
|
58
|
+
(entry: Omit<HistoryEntry, 'ts'>) => {
|
|
59
|
+
if (suppressedRef.current) return;
|
|
60
|
+
const ts = Date.now();
|
|
61
|
+
const { past } = stacksRef.current;
|
|
62
|
+
const top = past.at(-1);
|
|
49
63
|
if (
|
|
50
64
|
top &&
|
|
51
65
|
entry.coalesceKey !== undefined &&
|
|
@@ -58,58 +72,68 @@ export function HistoryProvider({ children }: { children: ReactNode }) {
|
|
|
58
72
|
coalesceKey: entry.coalesceKey,
|
|
59
73
|
ts,
|
|
60
74
|
};
|
|
61
|
-
|
|
75
|
+
stacksRef.current = { past: [...past.slice(0, -1), merged], future: [] };
|
|
76
|
+
} else {
|
|
77
|
+
stacksRef.current = { past: [...past, { ...entry, ts }], future: [] };
|
|
62
78
|
}
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
79
|
+
syncAvailability();
|
|
80
|
+
},
|
|
81
|
+
[syncAvailability],
|
|
82
|
+
);
|
|
67
83
|
|
|
68
84
|
const undo = useCallback(() => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
if (suppressedRef.current) return;
|
|
86
|
+
const previous = stacksRef.current;
|
|
87
|
+
const top = previous.past.at(-1);
|
|
88
|
+
if (!top) return;
|
|
89
|
+
const next = { past: previous.past.slice(0, -1), future: [...previous.future, top] };
|
|
90
|
+
stacksRef.current = next;
|
|
91
|
+
suppressedRef.current = true;
|
|
92
|
+
try {
|
|
93
|
+
top.undo();
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (stacksRef.current === next) stacksRef.current = previous;
|
|
96
|
+
throw error;
|
|
97
|
+
} finally {
|
|
98
|
+
suppressedRef.current = false;
|
|
99
|
+
syncAvailability();
|
|
100
|
+
}
|
|
101
|
+
}, [syncAvailability]);
|
|
82
102
|
|
|
83
103
|
const redo = useCallback(() => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
104
|
+
if (suppressedRef.current) return;
|
|
105
|
+
const previous = stacksRef.current;
|
|
106
|
+
const top = previous.future.at(-1);
|
|
107
|
+
if (!top) return;
|
|
108
|
+
const next = { past: [...previous.past, top], future: previous.future.slice(0, -1) };
|
|
109
|
+
stacksRef.current = next;
|
|
110
|
+
suppressedRef.current = true;
|
|
111
|
+
try {
|
|
112
|
+
top.redo();
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (stacksRef.current === next) stacksRef.current = previous;
|
|
115
|
+
throw error;
|
|
116
|
+
} finally {
|
|
117
|
+
suppressedRef.current = false;
|
|
118
|
+
syncAvailability();
|
|
119
|
+
}
|
|
120
|
+
}, [syncAvailability]);
|
|
97
121
|
|
|
98
122
|
const clear = useCallback(() => {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}, []);
|
|
123
|
+
stacksRef.current = { past: [], future: [] };
|
|
124
|
+
syncAvailability();
|
|
125
|
+
}, [syncAvailability]);
|
|
102
126
|
|
|
103
127
|
const value = useMemo<HistoryCtx>(
|
|
104
128
|
() => ({
|
|
105
|
-
canUndo:
|
|
106
|
-
canRedo:
|
|
129
|
+
canUndo: availability.canUndo,
|
|
130
|
+
canRedo: availability.canRedo,
|
|
107
131
|
record,
|
|
108
132
|
undo,
|
|
109
133
|
redo,
|
|
110
134
|
clear,
|
|
111
135
|
}),
|
|
112
|
-
[
|
|
136
|
+
[availability.canUndo, availability.canRedo, record, undo, redo, clear],
|
|
113
137
|
);
|
|
114
138
|
|
|
115
139
|
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function IconSwitcherIndicator({ index }: { index: 0 | 1 }) {
|
|
2
|
+
return (
|
|
3
|
+
<span
|
|
4
|
+
aria-hidden
|
|
5
|
+
data-icon-switcher-indicator
|
|
6
|
+
className="pointer-events-none absolute inset-y-0.5 left-0.5 w-8 rounded-md bg-card shadow-edge transition-transform duration-200 ease-swift motion-reduce:transition-none"
|
|
7
|
+
style={{ transform: `translateX(${index * 100}%)` }}
|
|
8
|
+
/>
|
|
9
|
+
);
|
|
10
|
+
}
|