Files
AFFiNE-Mirror/blocksuite/affine/blocks/surface/src/utils/font.ts
T
Aisha Roslan a3379c8979 fix(editor): font weight dropdown empty on Chrome/Safari in edgeless mode (#14725)
### Summary
Resolves #14528. Normalizes font family string before comparison.
Removes browser dependency.

### What Changed
On Edgeless mode, font dropdown was not appearing for shapes on Chrome.
Now, it appears on Chrome, Safari, and Firefox.

### Screenshot Verification
**Google Chrome**
<img width="623" height="322" alt="Screenshot 2026-03-25 at 1 24 51 PM"
src="https://github.com/user-attachments/assets/fb05790d-6842-43ce-a014-2b24d15bc80d"
/>

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Improved font matching to be more tolerant of whitespace, quotes, and
casing so font family comparisons are consistent across browsers,
reducing incorrect font fallbacks and visual mismatches.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-03-26 06:49:10 +00:00

57 lines
1.5 KiB
TypeScript

import type { FontFamily } from '@blocksuite/affine-model';
import { IS_FIREFOX } from '@blocksuite/global/env';
function normalizeFontFamily(fontFamily: FontFamily | string): string {
let s = fontFamily.trim();
if (
(s.startsWith('"') && s.endsWith('"')) ||
(s.startsWith("'") && s.endsWith("'"))
) {
s = s.slice(1, -1);
}
return s.toLowerCase();
}
export function wrapFontFamily(fontFamily: FontFamily | string): string {
return `"${fontFamily}"`;
}
export const getFontFaces = IS_FIREFOX
? () => {
const keys = document.fonts.keys();
const fonts = [];
let done = false;
while (!done) {
const item = keys.next();
done = !!item.done;
if (item.value) {
fonts.push(item.value);
}
}
return fonts;
}
: () => [...document.fonts.keys()];
export const isSameFontFamily =
(fontFamily: FontFamily | string) => (fontFace: FontFace) =>
normalizeFontFamily(fontFace.family) === normalizeFontFamily(fontFamily);
export function getFontFacesByFontFamily(
fontFamily: FontFamily | string
): FontFace[] {
return (
getFontFaces()
.filter(isSameFontFamily(fontFamily))
// remove duplicate font faces
.filter(
(item, index, arr) =>
arr.findIndex(
fontFace =>
fontFace.family === item.family &&
fontFace.weight === item.weight &&
fontFace.style === item.style
) === index
)
);
}