This commit is contained in:
pa
2026-01-17 23:14:31 +09:00
committed by Natsumi
parent 5e5abc1141
commit 2d812d7c05
18 changed files with 211 additions and 97 deletions

View File

@@ -1,4 +1,4 @@
const APP_FONT_DEFAULT_KEY = 'noto_sans';
const APP_FONT_DEFAULT_KEY = 'inter';
const APP_FONT_CONFIG = Object.freeze({
inter: {
@@ -9,6 +9,16 @@ const APP_FONT_CONFIG = Object.freeze({
cssName: "'Noto Sans Variable'",
link: null
},
source_sans_3: {
cssName: "'Source Sans 3'",
cssImport:
"@import url('https://fonts.googleapis.com/css2?family=Source+Sans+3:ital,wght@0,200..900;1,200..900&display=swap');"
},
ibm_plex_sans: {
cssName: "'IBM Plex Sans'",
cssImport:
"@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,100..700;1,100..700&family=Source+Sans+3:ital,wght@0,200..900;1,200..900&display=swap');"
},
harmonyos_sans: {
cssName: "'HarmonyOS Sans'",
cssImport:

View File

@@ -152,26 +152,36 @@ function resolveAppFontFamily(fontKey) {
};
}
function ensureAppFontLinks() {
function ensureAppFontLinks(fontKey) {
const head = document.head;
if (!head) {
return;
}
Object.entries(APP_FONT_CONFIG).forEach(([key, config]) => {
if (!config?.cssImport) {
return;
}
const existing = document.querySelector(
`style[${APP_FONT_LINK_ATTR}="${key}"]`
);
if (existing) {
return;
}
const styleEl = document.createElement('style');
styleEl.setAttribute(APP_FONT_LINK_ATTR, key);
styleEl.textContent = config.cssImport;
head.appendChild(styleEl);
});
document
.querySelectorAll(`style[${APP_FONT_LINK_ATTR}]`)
.forEach((styleEl) => {
if (styleEl.getAttribute(APP_FONT_LINK_ATTR) !== fontKey) {
styleEl.remove();
}
});
const config = APP_FONT_CONFIG[fontKey];
if (!config?.cssImport) {
return;
}
const existing = document.querySelector(
`style[${APP_FONT_LINK_ATTR}="${fontKey}"]`
);
if (existing) {
return;
}
const styleEl = document.createElement('style');
styleEl.setAttribute(APP_FONT_LINK_ATTR, fontKey);
styleEl.textContent = config.cssImport;
head.appendChild(styleEl);
}
function applyAppFontFamily(fontKey) {
@@ -179,7 +189,7 @@ function applyAppFontFamily(fontKey) {
const root = document.documentElement;
root.style.setProperty('--font-western-primary', resolved.cssName);
ensureAppFontLinks();
ensureAppFontLinks(resolved.key);
return resolved;
}