forked from twreporter/twreporter-npm-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-components): add NotoSansTC fontface
This change adds get-fontfaces utility function to get the fontfaces by font-family and font-weight specified in @twreporter/core.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { fonts, fontWeight } from '@twreporter/core/lib/constants/font' | ||
// lodash | ||
import keys from 'lodash/keys' | ||
import reduce from 'lodash/reduce' | ||
|
||
const _ = { | ||
keys, | ||
reduce, | ||
} | ||
|
||
const baseGCSDir = 'https://www.twreporter.org/assets/font/' | ||
|
||
const gcsFontFolder = { | ||
[fonts.notoSansTC]: 'NotoSansTC', | ||
} | ||
|
||
const fontWeightKeys = _.keys(fontWeight) | ||
|
||
// add @font-face to global style to use self-hosted font for performance reasons, to be more precise please check the issue below: | ||
// https://twreporter-org.atlassian.net/browse/TWREPORTER-318?atlOrigin=eyJpIjoiNjg4OTQ2MWU2MGIxNGEzMGE0NDY2ZDNmZGRhOWExZDEiLCJwIjoiaiJ9 | ||
const getFontFaces = ({ font, folder }) => { | ||
const fontFaceCSSTemplate = ({ fontWeightKey }) => ` | ||
@font-face { | ||
font-family: "${font}"; | ||
font-weight: ${fontWeight[fontWeightKey]}; | ||
font-display: swap; | ||
src: url("${baseGCSDir}${folder}/${fontWeightKey}.otf"); | ||
} | ||
` | ||
return _.reduce( | ||
fontWeightKeys, | ||
(fontFaces, fontWeightKey) => { | ||
return fontFaces + fontFaceCSSTemplate({ fontWeightKey }) | ||
}, | ||
'' | ||
) | ||
} | ||
|
||
export default { | ||
[fonts.notoSansTC]: getFontFaces({ | ||
font: fonts.notoSansTC, | ||
folder: gcsFontFolder[fonts.notoSansTC], | ||
}), | ||
} |