mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-06 19:52:24 +03:00
fix(frontend): Preserve host font configuration
Use default Serif fonts only with the default Desktop font set. Bundle the Playground Serif fonts on native desktop targets.
This commit is contained in:
1 parent
59b28730b7
commit
c61a276d07
3 files changed
+94
-56
No files matched your search
@@ -9,7 +9,7 @@ impl PlaygroundView {
|
||||
|
||||
pub fn set_editor_font_style(&mut self, style: EditorFontStyle, cx: &mut Context<Self>) {
|
||||
if style != self.editor_font_style() {
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
#[cfg(not(any(target_family = "wasm", target_os = "ios", target_os = "android")))]
|
||||
if style == EditorFontStyle::Serif {
|
||||
register_editor_serif(cx);
|
||||
}
|
||||
@@ -33,7 +33,7 @@ impl PlaygroundView {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
#[cfg(not(any(target_family = "wasm", target_os = "ios", target_os = "android")))]
|
||||
fn register_editor_serif(cx: &ui::App) {
|
||||
use std::borrow::Cow;
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ export function desktopFontUrls(manifest, legacyFonts) {
|
||||
const url = manifest?.[key];
|
||||
if (typeof url === 'string' && url.length) {
|
||||
urls.push(url);
|
||||
} else if (!hasManifest) {
|
||||
} else if (!hasManifest && legacyFonts == null) {
|
||||
urls.push(fallback);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,8 +107,11 @@ void test('the static gate rejects Desktop inline scripts, extra CSP sources, an
|
||||
);
|
||||
});
|
||||
|
||||
void test('desktop boot applies the manifest before preview init and falls back to legacy or default assets', async t => {
|
||||
void test('desktop boot loads the configured fonts and workers before mount', async t => {
|
||||
const boot = await readFile(desktopBoot, 'utf8');
|
||||
const desktopStart = JSON.stringify(
|
||||
new URL('../desktop/start.js', import.meta.url).href
|
||||
);
|
||||
const serif = '/assets/fonts/SourceSerif4-VariableFont_opsz,wght.ttf';
|
||||
const serifItalic =
|
||||
'/assets/fonts/SourceSerif4-Italic-VariableFont_opsz,wght.ttf';
|
||||
@@ -123,17 +126,19 @@ void test('desktop boot applies the manifest before preview init and falls back
|
||||
]
|
||||
.map(name => '/assets/fonts/' + name)
|
||||
.concat(serif, serifItalic);
|
||||
const fonts = {
|
||||
serif: '/assets/fonts/serif-hash.ttf',
|
||||
serifItalic: '/assets/fonts/serif-italic-hash.ttf',
|
||||
arabic: '/arabic',
|
||||
hebrew: '/hebrew',
|
||||
const baseFonts = {
|
||||
latin: '/latin',
|
||||
mono: '/mono',
|
||||
monoBold: '/mono-bold',
|
||||
monoBoldItalic: '/mono-bold-italic',
|
||||
monoItalic: '/mono-italic',
|
||||
monoBoldItalic: '/mono-bold-italic',
|
||||
arabic: '/arabic',
|
||||
hebrew: '/hebrew',
|
||||
};
|
||||
const baseUrls = Object.values(baseFonts);
|
||||
const legacyFonts = baseUrls.map(url => '/legacy' + url);
|
||||
const manifestSerif = '/fonts/serif-hash.ttf';
|
||||
const manifestSerifItalic = '/fonts/serif-italic-hash.ttf';
|
||||
const workers = {
|
||||
math: { script: '/math.js', wasm: '/math.wasm' },
|
||||
mermaid: { script: '/mermaid.js', wasm: '/mermaid.wasm' },
|
||||
@@ -143,21 +148,18 @@ void test('desktop boot applies the manifest before preview init and falls back
|
||||
const legacyWorkers = { mathScript: '/legacy-math.js' };
|
||||
const cases = [
|
||||
{
|
||||
name: 'release manifest',
|
||||
manifest: { fonts, workers },
|
||||
legacyFonts: ['/legacy-font'],
|
||||
name: 'release manifest with both serif fonts',
|
||||
manifest: {
|
||||
fonts: {
|
||||
...baseFonts,
|
||||
serif: manifestSerif,
|
||||
serifItalic: manifestSerifItalic,
|
||||
},
|
||||
workers,
|
||||
},
|
||||
legacyFonts,
|
||||
legacyWorkers,
|
||||
expectedFonts: [
|
||||
'/latin',
|
||||
'/mono',
|
||||
'/mono-bold',
|
||||
'/mono-italic',
|
||||
'/mono-bold-italic',
|
||||
'/arabic',
|
||||
'/hebrew',
|
||||
fonts.serif,
|
||||
fonts.serifItalic,
|
||||
],
|
||||
expectedFonts: [...baseUrls, manifestSerif, manifestSerifItalic],
|
||||
expectedWorkers: {
|
||||
mathScript: '/math.js',
|
||||
mathWasm: '/math.wasm',
|
||||
@@ -169,48 +171,76 @@ void test('desktop boot applies the manifest before preview init and falls back
|
||||
typstWasm: '/typst.wasm',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'release manifest with one serif font',
|
||||
manifest: { fonts: { ...baseFonts, serif: manifestSerif } },
|
||||
expectedFonts: [...baseUrls, manifestSerif],
|
||||
},
|
||||
{
|
||||
name: 'older release manifest without serif assets',
|
||||
manifest: {
|
||||
fonts: { ...fonts, serif: undefined, serifItalic: undefined },
|
||||
},
|
||||
expectedFonts: [
|
||||
'/latin',
|
||||
'/mono',
|
||||
'/mono-bold',
|
||||
'/mono-italic',
|
||||
'/mono-bold-italic',
|
||||
'/arabic',
|
||||
'/hebrew',
|
||||
],
|
||||
manifest: { fonts: baseFonts },
|
||||
expectedFonts: baseUrls,
|
||||
},
|
||||
{ name: 'empty manifest', manifest: {}, expectedFonts: fallbackFonts },
|
||||
{ name: 'missing manifest', expectedFonts: fallbackFonts },
|
||||
{
|
||||
name: 'empty manifest with legacy fonts',
|
||||
manifest: {},
|
||||
legacyFonts,
|
||||
expectedFonts: legacyFonts,
|
||||
},
|
||||
{
|
||||
name: 'incomplete fonts and empty workers use legacy overrides',
|
||||
manifest: { fonts: { latin: '/latin' }, workers: {} },
|
||||
legacyFonts: ['/legacy-font', serif, serif],
|
||||
manifest: { fonts: { latin: '/partial-latin' }, workers: {} },
|
||||
legacyFonts,
|
||||
legacyWorkers,
|
||||
expectedFonts: ['/legacy-font', serif, serifItalic],
|
||||
expectedFonts: legacyFonts,
|
||||
expectedWorkers: legacyWorkers,
|
||||
},
|
||||
{
|
||||
name: 'incomplete manifest with a declared serif font and legacy fonts',
|
||||
manifest: { fonts: { serifItalic: manifestSerifItalic } },
|
||||
legacyFonts,
|
||||
expectedFonts: [...legacyFonts, manifestSerifItalic],
|
||||
},
|
||||
{
|
||||
name: 'missing required mono font',
|
||||
manifest: { fonts: baseFonts },
|
||||
expectedFonts: baseUrls,
|
||||
missingFont: baseFonts.mono,
|
||||
},
|
||||
];
|
||||
const moduleUrl = source =>
|
||||
'data:text/javascript,' + encodeURIComponent(source);
|
||||
for (const entry of cases) {
|
||||
await t.test(entry.name, async () => {
|
||||
await t.test(entry.name, async t => {
|
||||
const requestedFonts = [];
|
||||
t.mock.method(globalThis, 'fetch', async url => {
|
||||
requestedFonts.push(url);
|
||||
const index = entry.expectedFonts.indexOf(url);
|
||||
return index === -1 || url === entry.missingFont
|
||||
? new Response(null, { status: 404 })
|
||||
: new Response(Uint8Array.of(index));
|
||||
});
|
||||
const errors = t.mock.method(console, 'error', () => {});
|
||||
const fixtures = moduleUrl(`
|
||||
export { desktopFontUrls } from ${JSON.stringify(new URL('../desktop/start.js', import.meta.url).href)};
|
||||
// ${entry.name}
|
||||
import { startDesktop as mountDesktop } from ${desktopStart};
|
||||
export { desktopFontUrls } from ${desktopStart};
|
||||
export const calls = [];
|
||||
export const window = {
|
||||
__AFFINE_FONT_ASSETS__: ${JSON.stringify(entry.legacyFonts)},
|
||||
__AFFINE_WORKER_ASSETS__: ${JSON.stringify(entry.legacyWorkers)},
|
||||
__playgroundPreview: {},
|
||||
__playgroundPreview: {
|
||||
registerFont: bytes => calls.push(['preview-font', ...bytes]),
|
||||
},
|
||||
};
|
||||
export const document = {
|
||||
getElementById: id => id === 'affine-desktop-manifest'
|
||||
? ${JSON.stringify(entry.manifest && { textContent: JSON.stringify(entry.manifest) }) ?? 'null'}
|
||||
: null,
|
||||
createElement: () => ({ setAttribute() {} }),
|
||||
body: { append: notice => calls.push(['alert', notice.textContent]) },
|
||||
};
|
||||
export const location = { origin: 'https://example.org', search: '?doc=a' };
|
||||
export const matchMedia = () => ({ matches: true });
|
||||
@@ -219,8 +249,12 @@ void test('desktop boot applies the manifest before preview init and falls back
|
||||
}
|
||||
export function startDesktop(wasm, fonts, query, dark, preview) {
|
||||
calls.push(['start', wasm.name, fonts, query, dark, preview]);
|
||||
return 'store';
|
||||
return mountDesktop(wasm, fonts, query, dark, preview, async () => 'store', null, {});
|
||||
}
|
||||
export default function init() {}
|
||||
export const register_web_desktop_font = bytes => calls.push(['font', ...bytes]);
|
||||
export const web_desktop_workspace_ids = () => [];
|
||||
export const wasm_desktop_main = () => calls.push(['mount']);
|
||||
export function flushDesktopOnHide(wasm, store, target) {
|
||||
calls.push(['flush', wasm.name, store, target === window]);
|
||||
}
|
||||
@@ -249,8 +283,24 @@ void test('desktop boot applies the manifest before preview init and falls back
|
||||
true,
|
||||
window.__playgroundPreview,
|
||||
],
|
||||
['flush', 'desktop-wasm', 'store', true],
|
||||
...(entry.missingFont
|
||||
? [
|
||||
[
|
||||
'alert',
|
||||
`AFFiNE Desktop could not start: Font ${entry.missingFont} returned HTTP 404`,
|
||||
],
|
||||
]
|
||||
: [
|
||||
...entry.expectedFonts.flatMap((_, index) => [
|
||||
['font', index],
|
||||
['preview-font', index],
|
||||
]),
|
||||
['mount'],
|
||||
['flush', 'desktop-wasm', 'store', true],
|
||||
]),
|
||||
]);
|
||||
assert.deepEqual(requestedFonts, entry.expectedFonts);
|
||||
assert.equal(errors.mock.callCount(), entry.missingFont ? 1 : 0);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -456,18 +506,6 @@ void test('closing the final split removes only split from the desktop URL', ()
|
||||
);
|
||||
});
|
||||
|
||||
void test('an unavailable font stops desktop startup', async t => {
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async () => new Response(null, { status: 404 })
|
||||
);
|
||||
await assert.rejects(
|
||||
startDesktop({ default() {} }, ['/missing'], '', false, {}),
|
||||
/Font \/missing returned HTTP 404/
|
||||
);
|
||||
});
|
||||
|
||||
void test('the desktop writes queued changes when its page is hidden or unloaded', () => {
|
||||
const calls = [];
|
||||
const win = new EventTarget();
|
||||
|
||||
Reference in new issue
Block a user