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 11 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,13 @@
* External dependencies
*/
import { migrate } from '@googleforcreators/migration';
import type { Element } from '@googleforcreators/types';
/**
* Internal dependencies
*/
import type { MinMax, TemplateData, TextSet, TextSets } 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,21 +36,20 @@ function updateMinMax(minMax, element) {
return minMax;
}

async function loadTextSet(name) {
const data = await import(
async function loadTextSet(name: string): Promise<TextSet[]> {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
const data: TemplateData = (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 TemplateData;
const migrated = migrate(data, data.version) as TemplateData;
return migrated.pages.reduce((sets: TextSet[], page) => {
const minMax = {
minX: Infinity,
maxX: 0,
minY: Infinity,
maxY: 0,
};

const textElements = page.elements.filter((element) => {
const textElements = page.elements.filter((element: Element) => {
return !element.isBackground && Boolean(updateMinMax(minMax, element));
});
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -55,7 +59,7 @@ async function loadTextSet(name) {
textSetFonts: page.fonts,
id: page.id,
textSetCategory: name,
elements: textElements.map((e) => ({
elements: textElements.map((e: Element) => ({
...e,
// Offset elements so the text set's
// default position is (0,0)
Expand All @@ -71,7 +75,7 @@ async function loadTextSet(name) {
}, []);
}

export default async function loadTextSets() {
export default async function loadTextSets(): Promise<TextSets> {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
const textSets = [
'cover',
'step',
Expand All @@ -84,10 +88,10 @@ export default async function loadTextSets() {
];

const results = await Promise.all(
textSets.map(async (name) => {
textSets.map(async (name: string): Promise<Array<string | TextSet[]>> => {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
return [name, await loadTextSet(name)];
})
);

return Object.fromEntries(results);
return Object.fromEntries(results) as TextSets;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as is a rather crude tool that should only be applied if no better tool exists, as it will actually ignore and suppress some potential errors. However, moving it to a typed variable or just setting the return type won't do the trick, because fromEntries doesn't play nice (typed as returning any).

This required updating the type system a bit as:

export enum TextSetType {
  Cover = 'cover',
  Step = 'step',
  SectionHeader = 'section_header',
  Editorial = 'editorial',
  Contact = 'contact',
  Table = 'table',
  List = 'list',
  Quote = 'quote',
}

export type TextSets = Partial<Record<TextSetType, TextSet[]>>;

And then loadTextSets becomes:

export default async function loadTextSets(): Promise<TextSets> {
  const results: [TextSetType, TextSet[]][] = await Promise.all(
    Object.values(TextSetType).map(async (name) => [
      name,
      await loadTextSet(name),
    ])
  );
  return Object.fromEntries(results);
}

The Partial<> is a bit annoying but I don't see a clean way around it? I feel this is cleaner, but it's probably just a nit really.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am honest, I don't find your code easier to read or understand. I have pushed up my own version of the code, that is a little easier to follow. See ed4be89

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barklund Can you take a look ?

}
64 changes: 64 additions & 0 deletions packages/text-sets/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 TemplateData extends Omit<Story, 'pages'> {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
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[];
}
8 changes: 8 additions & 0 deletions packages/text-sets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.shared.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "dist-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 @@ -9,6 +9,7 @@
{ "path": "packages/react" },
{ "path": "packages/rich-text" },
{ "path": "packages/stickers" },
{ "path": "packages/text-sets" },
{ "path": "packages/tracking" },
{ "path": "packages/transform" },
{ "path": "packages/types" },
Expand Down