Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert fonts package to TypeScript #12213

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/fonts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"default": "./src/fonts.json"
},
".": {
"default": "./src/index.js"
"default": "./src/index.ts"
}
},
"exports": {
Expand All @@ -47,7 +47,8 @@
},
"main": "dist/index.js",
"module": "dist-module/index.js",
"source": "src/index.js",
"tyoes": "dist-types/index.d.ts",
"source": "src/index.ts",
"publishConfig": {
"access": "public"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// Curated Fonts list source:
// https://github.com/googleforcreators/web-stories-wp/issues/1989#issuecomment-662253222
export const CURATED_FONT_NAMES = [
export const CURATED_FONT_NAMES: string[] = [
'Karla',
'Lato',
'Lora',
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions packages/fonts/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface Font {
family: string;
variants: Array<[number, number]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
/**
* Get the CSS font format for a given font URL.
*
* @param {string} src Font URL.
* @return {string|null} Font format string or null if the font is unsupported.
* @param src Font URL.
* @return Font format string or null if the font is unsupported.
*/
function getFontFormat(src) {
const fileExtension = src.split(/[#?]/)[0].split('.').pop().trim();
function getFontFormat(src: string): string | null {
const fileExtension = src.split(/[#?]/)?.[0]?.split('.')?.pop()?.trim();

switch (fileExtension) {
case 'woff':
Expand All @@ -39,11 +39,11 @@ function getFontFormat(src) {
/**
* Get the inline stylesheet for a specific font family.
*
* @param {string} name Font family.
* @param {string} src Font URL.
* @return {string|null} Stylesheet or null if the font has an unsupported format.
* @param name Font family.
* @param src Font URL.
* @return Stylesheet or null if the font has an unsupported format.
*/
function getFontCSS(name, src) {
function getFontCSS(name: string, src: string): string | null {
const format = getFontFormat(src);

if (!format) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,29 @@
* limitations under the License.
*/

/**
* Internal dependencies
*/
import type { Font } from '../types';

/**
* Given a list of Google fonts, returns a URL to embed them.
*
* Uses the given list of font variants (axis tuples) to assemble the
* axis tag list and axis tuple list that Google Fonts expects.
*
* @see https://developers.google.com/fonts/docs/css2
* @param {Array<Object<{family: string, variants: Array<number, number>}>>} fonts List of font objects.
* @param {string} [display] Valid font-display value, e.g. 'swap' or 'auto'. Default 'swap'.
* @return {string} Google Fonts embed URL.
* @param fonts List of font objects.
* @param [display] Valid font-display value, e.g. 'swap' or 'auto'. Default 'swap'.
* @return Google Fonts embed URL.
*/
function getGoogleFontURL(fonts, display = 'swap') {
function getGoogleFontURL(fonts: Font[], display: 'swap' | 'auto' = 'swap') {
const url = new URL('https://fonts.googleapis.com/css2');
url.searchParams.append('display', display);

for (const { family: familyName, variants = [] } of fonts) {
// [ [ 1, 400 ], [ 0, 700 ] ] -> [ ital, wght ]
const axes = variants
const axes: string[] = variants
.reduce((acc, [fontStyle, fontWeight]) => {
// Uses axis names as listed on https://developers.google.com/web/fundamentals/design-and-ux/typography/variable-fonts.
if (fontStyle === 1 && !acc.includes('ital')) {
Expand All @@ -41,7 +46,7 @@ function getGoogleFontURL(fonts, display = 'swap') {
acc.push('wght');
}
return acc;
}, [])
}, [] as Array<'ital' | 'wght'>)
.sort(); // Need to be sorted alphabetically.

let family = familyName;
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions packages/fonts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.shared.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "dist-types"
},
"references": [{ "path": "../react" }],
"include": ["src/**/*"]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"references": [
{ "path": "packages/fonts" },
{ "path": "packages/i18n" },
{ "path": "packages/media" },
{ "path": "packages/moveable" },
Expand Down