Skip to content

Commit

Permalink
feat(adobe): support multiple kitIds
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerzl committed Mar 12, 2024
1 parent 224e21c commit f1755ce
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
5 changes: 3 additions & 2 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
11 changes: 11 additions & 0 deletions playground/pages/providers/adobe2.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
Nuxt module playground!
</div>
</template>

<style scoped>
div {
font-family: 'barlow-semi-condensed', sans-serif;
}
</style>
64 changes: 48 additions & 16 deletions src/providers/adobe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { cachedData } from '../cache'
import { logger } from '../logger'

interface ProviderOption {
id?: string
id?: string[] | string
}

export default {
Expand Down Expand Up @@ -38,6 +38,10 @@ const fontCSSAPI = $fetch.create({
})

interface AdobeFontMeta {
kit: AdobeFontKit[]
}

interface AdobeFontAPI {
kit: AdobeFontKit
}

Expand All @@ -58,15 +62,34 @@ interface AdobeFontFamily {
let fonts: AdobeFontMeta
const familyMap = new Map<string, string>()

async function initialiseFontMeta (kitId: string) {
fonts = await cachedData('adobe:meta.json', () => fontAPI<AdobeFontMeta>(`/api/v1/json/kits/${kitId}/published`, { responseType: 'json' }), {
async function getAdobeFontMeta (kitId: string | string[]):Promise<AdobeFontMeta> {
if (typeof kitId === "string")
return {
kit: [
(await fontAPI<AdobeFontAPI>(`/api/v1/json/kits/${kitId}/published`, { responseType: 'json' })).kit
]
}

const metadata: AdobeFontMeta = { kit: [] }

for (const kit in kitId) {
metadata.kit.push((await fontAPI<AdobeFontAPI>(`/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)
}
}
}

Expand All @@ -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 []
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f1755ce

Please sign in to comment.