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

TypeScript: Convert text-sets package #12274

Merged
merged 22 commits into from
Oct 19, 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
8 changes: 5 additions & 3 deletions packages/text-sets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@
"type": "module",
"customExports": {
".": {
"default": "./src/index.js"
"default": "./src/index.ts"
}
},
"main": "dist/index.js",
"module": "dist-module/index.js",
"source": "src/index.js",
"types": "dist-types/index.d.ts",
"source": "src/index.ts",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@googleforcreators/migration": "*"
"@googleforcreators/migration": "*",
"@googleforcreators/types": "*"
},
"devDependencies": {
"puppeteer": "*"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
* External dependencies
*/
import { migrate } from '@googleforcreators/migration';
import type { Element } from '@googleforcreators/types';
/**
* Internal dependencies
*/
import type { MinMax, TextSetData, TextSet, TextSets } from './types';
import { TextSetType } from './types';

function updateMinMax(minMax, element) {
function updateMinMax(minMax: MinMax, element: Element): MinMax {
// Purposely mutating object so passed
// in minMax is modified
minMax.minX = Math.min(minMax.minX, element.x);
Expand All @@ -31,23 +37,21 @@ function updateMinMax(minMax, element) {
return minMax;
}

async function loadTextSet(name) {
const data = await import(
async function loadTextSet(name: string) {
const data: TextSetData = (await import(
/* webpackChunkName: "chunk-web-stories-textset-[index]" */ `./raw/${name}.json`
);
const migrated = migrate(data.default, data.default.version);

return migrated.pages.reduce((sets, page) => {
)) as TextSetData;
const migrated = migrate(data, data.version) as TextSetData;
return migrated.pages.reduce((sets: TextSet[], page) => {
const minMax = {
minX: Infinity,
maxX: 0,
minY: Infinity,
maxY: 0,
};

const textElements = page.elements.filter((element) => {
return !element.isBackground && Boolean(updateMinMax(minMax, element));
});
const textElements = page.elements.filter(({ type }) => type === 'text');
textElements.forEach((element) => updateMinMax(minMax, element));

return [
...sets,
Expand All @@ -72,22 +76,13 @@ async function loadTextSet(name) {
}

export default async function loadTextSets() {
const textSets = [
'cover',
'step',
'section_header',
'editorial',
'contact',
'table',
'list',
'quote',
];
const textSets: TextSets = {} as TextSets;

const results = await Promise.all(
textSets.map(async (name) => {
return [name, await loadTextSet(name)];
await Promise.all(
Object.values(TextSetType).map(async (name) => {
textSets[name] = await loadTextSet(name);
})
);

return Object.fromEntries(results);
return textSets;
}
75 changes: 75 additions & 0 deletions packages/text-sets/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.
*/
/**
* External dependencies
*/
import type { Page, Story, Element } from '@googleforcreators/types';

export interface TextSetData extends Omit<Story, 'pages'> {
current: null;
selection: never[];
story: Record<string, never>;
version: number;
pages: TextSetPage[];
}

export interface TextSetPage extends Page {
fonts: string[];
id: string;
}

export interface MinMax {
minX: number;
maxX: number;
minY: number;
maxY: number;
}

export interface TextSetElement extends Element {
normalizedOffsetX: number;
normalizedOffsetY: number;
textSetWidth: number;
textSetHeight: number;
}

export interface TextSet {
id: string;
elements: TextSetElement[];
textSetCategory: string;
textSetFonts: string[];
}

export interface TextSets {
cover: TextSet[];
step: TextSet[];
section_header: TextSet[];
editorial: TextSet[];
contact: TextSet[];
table: TextSet[];
list: TextSet[];
quote: TextSet[];
}

export enum TextSetType {
Cover = 'cover',
Step = 'step',
SectionHeader = 'section_header',
Editorial = 'editorial',
Contact = 'contact',
Table = 'table',
List = 'list',
Quote = 'quote',
}
9 changes: 9 additions & 0 deletions packages/text-sets/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" }, { "path": "../types" }],
"include": ["src/**/*"]
barklund marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{ "path": "packages/rich-text" },
{ "path": "packages/stickers" },
{ "path": "packages/templates" },
{ "path": "packages/text-sets" },
{ "path": "packages/tracking" },
{ "path": "packages/transform" },
{ "path": "packages/types" },
Expand Down