diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 1567330..acb27a6 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -16,10 +16,11 @@ export default defineNuxtConfig({ { name: 'MyCustom', src: '/custom-font.woff2' }, { name: 'CustomGlobal', global: true, src: '/font-global.woff2' }, { name: 'Oswald', fallbacks: ['Times New Roman'] }, - { name: 'Aleo', provider: 'adobe'} + { name: 'Aleo', provider: 'adobe'}, + { name: 'barlow-semi-condensed', provider: 'adobe' } ], adobe: { - id: 'sij5ufr', + id: ['sij5ufr', 'grx7wdj'], }, defaults: { fallbacks: { diff --git a/playground/pages/providers/adobe2.vue b/playground/pages/providers/adobe2.vue new file mode 100644 index 0000000..5736d7e --- /dev/null +++ b/playground/pages/providers/adobe2.vue @@ -0,0 +1,11 @@ + + + diff --git a/src/providers/adobe.ts b/src/providers/adobe.ts index fc1d08e..2be8db7 100644 --- a/src/providers/adobe.ts +++ b/src/providers/adobe.ts @@ -7,7 +7,7 @@ import { cachedData } from '../cache' import { logger } from '../logger' interface ProviderOption { - id?: string + id?: string[] | string } export default { @@ -38,6 +38,10 @@ const fontCSSAPI = $fetch.create({ }) interface AdobeFontMeta { + kit: AdobeFontKit[] +} + +interface AdobeFontAPI { kit: AdobeFontKit } @@ -58,15 +62,34 @@ interface AdobeFontFamily { let fonts: AdobeFontMeta const familyMap = new Map() -async function initialiseFontMeta (kitId: string) { - fonts = await cachedData('adobe:meta.json', () => fontAPI(`/api/v1/json/kits/${kitId}/published`, { responseType: 'json' }), { +async function getAdobeFontMeta (kitId: string | string[]):Promise { + if (typeof kitId === "string") + return { + kit: [ + (await fontAPI(`/api/v1/json/kits/${kitId}/published`, { responseType: 'json' })).kit + ] + } + + const metadata: AdobeFontMeta = { kit: [] } + + for (const kit in kitId) { + metadata.kit.push((await fontAPI(`/api/v1/json/kits/${kitId[kit]}/published`, { responseType: 'json' })).kit) + } + + return metadata +} + +async function initialiseFontMeta (kitId: string | string[]) { + fonts = await cachedData('adobe:meta.json', () => getAdobeFontMeta(kitId), { onError () { logger.error('Could not download `adobe` font metadata. `@nuxt/fonts` will not be able to inject `@font-face` rules for adobe.') - return { kit: { id: '', families: [] } } + return { kit: [] } } }) - for (const family in fonts.kit.families) { - familyMap.set(fonts.kit.families[family]!.name, fonts.kit.families[family]!.id) + for (const kit in fonts.kit) { + for (const family in fonts.kit[kit]!.families) { + familyMap.set(fonts.kit[kit]!.families[family]!.slug, fonts.kit[kit]!.families[family]!.id) + } } } @@ -76,16 +99,25 @@ function isAdobeFont (family: string) { async function getFontDetails (family: string, variants: ResolveFontFacesOptions) { variants.weights = variants.weights.map(String) - const font = fonts.kit.families.find(f => f.name === family)! - const styles: string[] = [] - for (const style of font.variations) { - if (style.includes('i') && !variants.styles.includes('italic')) { continue } - if (!variants.weights.includes(String(style.slice(-1)+'00'))) { continue } - styles.push(style) - } - if (styles.length === 0) return [] - const css = await fontCSSAPI(`${fonts.kit.id}.css`) + for (const kit in fonts.kit) { + const font = fonts.kit[kit]!.families.find(f => f.slug === family)! + if (!font) { continue } + + const styles: string[] = [] + for (const style of font.variations) { + if (style.includes('i') && !variants.styles.includes('italic')) { + continue + } + if (!variants.weights.includes(String(style.slice(-1) + '00'))) { + continue + } + styles.push(style) + } + if (styles.length === 0) { continue } + const css = await fontCSSAPI(`${fonts.kit[kit]!.id}.css`) + return addLocalFallbacks(family, extractFontFaceData(css, family)) + } - return addLocalFallbacks(family, extractFontFaceData(css, family)) + return [] } diff --git a/src/types.ts b/src/types.ts index f75caa4..6e6ae0a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -151,7 +151,7 @@ export interface ModuleOptions { local?: {} /** Options passed directly to `adobe` font provider */ adobe?: { - id: string + id: string | string[] } /** * An ordered list of providers to check when resolving font families.