Skip to content

Commit

Permalink
TypeScript: Convert output package (#13504)
Browse files Browse the repository at this point in the history
Co-authored-by: Anurag Vasanwala <[email protected]>
  • Loading branch information
swissspidy and AnuragVasanwala authored Mar 27, 2024
1 parent e526787 commit ef233cf
Show file tree
Hide file tree
Showing 78 changed files with 1,989 additions and 1,021 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"",
{
"pattern": " \\* Copyright \\d{4} Google LLC",
"template": " * Copyright 2023 Google LLC"
"template": " * Copyright 2024 Google LLC"
},
" *",
" * Licensed under the Apache License, Version 2.0 (the \"License\");",
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/animation/src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface AnimationProviderState {
}

export type AnimationProviderProps = PropsWithChildren<{
animations: StoryAnimation[];
animations?: StoryAnimation[];
elements?: Element[];
onWAAPIFinish?: () => void;
selectedElementIds?: string[];
Expand Down
7 changes: 6 additions & 1 deletion packages/element-library/src/text/outputWithUnits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ function TextOutputWithUnits({
backgroundTextMode,
padding,
borderRadius,
tagName: TagName = 'p',
...rest
} = element;

// It can never be 'auto' here thanks to getTextElementTagNames(),
// but this makes TypeScript happy.
const TagName =
element.tagName && element.tagName !== 'auto' ? element.tagName : 'p';

if (!dataToFontSizeY) {
dataToFontSizeY = dataToStyleY;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/element-library/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
// Adjust tsconfig.json and "types" field in package.json and then
// delete this file once complete.

/**
* External dependencies
*/
import type { ElementDefinition } from '@googleforcreators/elements';

export * from './constants';
export * from './types';
export * from './utils/textMeasurements';

export const elementTypes: ElementDefinition[] = [];

export {};
24 changes: 20 additions & 4 deletions packages/elements/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import type { AudioResource } from '@googleforcreators/media';
*/
import type { Page } from './page';

export type FontFamily = string;

export type FontStyle = 'normal' | 'italic' | 'regular';

export enum FontVariantStyle {
Normal = 0,
Italic = 1,
Expand All @@ -35,6 +38,12 @@ export type FontWeight = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;

export type FontVariant = [FontVariantStyle, FontWeight];

export enum FontService {
Custom = 'custom',
GoogleFonts = 'fonts.google.com',
System = 'system',
}

export interface FontMetrics {
upm: number;
asc: number;
Expand All @@ -54,7 +63,8 @@ export interface FontMetrics {
}

export interface BaseFontData {
family: string;
service: FontService;
family: FontFamily;
weights?: FontWeight[];
styles?: FontStyle[];
variants?: FontVariant[];
Expand All @@ -63,15 +73,15 @@ export interface BaseFontData {
}

export interface GoogleFontData extends BaseFontData {
service: 'fonts.google.com';
service: FontService.GoogleFonts;
}

export interface SystemFontData extends BaseFontData {
service: 'system';
service: FontService.System;
}

export interface CustomFontData extends BaseFontData {
service: 'custom';
service: FontService.Custom;
url: string;
}

Expand All @@ -91,6 +101,12 @@ export interface ProductData {
productPriceCurrency: string;
productTitle: string;
productUrl: string;
productIcon?: string;
aggregateRating?: {
ratingValue: number;
reviewCount: number;
reviewUrl: string;
};
}

// Data retrieved as part of the raw data from the backend, used for example in the templates, in migration.
Expand Down
60 changes: 28 additions & 32 deletions packages/elements/src/types/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
/**
* External dependencies
*/
import type { Solid } from '@googleforcreators/patterns';
import type { Solid, Pattern } from '@googleforcreators/patterns';
import type {
GifResource,
Resource,
SequenceResource,
GifResource,
VideoResource,
} from '@googleforcreators/media';
import type { ElementBox } from '@googleforcreators/units';
Expand All @@ -30,8 +30,13 @@ import type { ElementBox } from '@googleforcreators/units';
* Internal dependencies
*/
import type { ElementType } from './elementType';
import type { FontMetrics, ProductData } from './data';
import type { Track } from './media';
import type {
ProductData,
GoogleFontData,
SystemFontData,
CustomFontData,
} from './data';

export enum LinkType {
Regular = 'regular',
Expand Down Expand Up @@ -101,6 +106,10 @@ export interface Element extends ElementBox {
isHidden?: boolean;
}

export interface LinkableElement extends Element {
link: Link;
}

export interface DefaultBackgroundElement extends Element {
isDefaultBackground: boolean;
backgroundColor: Solid;
Expand All @@ -110,10 +119,6 @@ export interface BackgroundableElement extends Element {
isBackground?: boolean;
}

export interface LinkableElement extends Element {
link: Link;
}

export interface MediaElement extends BackgroundableElement {
resource: Resource;
scale?: number;
Expand All @@ -129,13 +134,23 @@ export interface VideoElement extends SequenceMediaElement {
type: ElementType.Video;
tracks: Track[];
resource: VideoResource;
poster?: string;
loop?: boolean;
}

export interface GifElement extends SequenceMediaElement {
type: ElementType.Gif;
resource: GifResource;
}

export interface ImageElement extends MediaElement {
type: ElementType.Image;
}

export interface OverlayableElement extends Element {
overlay?: Pattern | null;
}

export interface ProductElement extends Element {
type: ElementType.Product;
product: ProductData;
Expand All @@ -148,30 +163,7 @@ export interface StickerElement extends Element {
};
}

interface BaseTextElementFont {
service: string;
family: string;
fallbacks: string[];
metrics?: FontMetrics;
}

export interface GoogleTextElementFont extends BaseTextElementFont {
service: 'fonts.google.com';
}

export interface SystemTextElementFont extends BaseTextElementFont {
service: 'system';
}

export interface CustomTextElementFont extends BaseTextElementFont {
service: 'custom';
url: string;
}

export type TextElementFont =
| GoogleTextElementFont
| SystemTextElementFont
| CustomTextElementFont;
export type TextElementFont = GoogleFontData | SystemFontData | CustomFontData;

export interface Padding {
horizontal: number;
Expand All @@ -188,9 +180,13 @@ export interface TextElement extends Element {
fontSize: number;

backgroundTextMode?: string;
tagName?: 'h1' | 'h2' | 'h3' | 'p';
tagName?: 'h1' | 'h2' | 'h3' | 'p' | 'auto';
padding?: Padding;
marginOffset: number;
lineHeight: number;
textAlign: TextAlign;
}

export interface ShapeElement extends Element {
type: ElementType.Shape;
}
2 changes: 1 addition & 1 deletion packages/elements/src/types/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type Track = {
trackId: number;
trackName: string;
id: string;
srcLang?: string;
srclang?: string;
label?: string;
kind: string;
};
46 changes: 27 additions & 19 deletions packages/elements/src/types/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
import type { Pattern } from '@googleforcreators/patterns';
import type { StoryAnimation } from '@googleforcreators/animation';
import type { AudioResource } from '@googleforcreators/media';

/**
* Internal dependencies
Expand All @@ -32,33 +33,40 @@ export interface Group {
isCollapsed?: boolean;
}

export type BackgroundAudio = {
resource: AudioResource;
tracks?: Track[];
loop?: boolean;
};

export type Groups = Record<string, Group>;

export type PageAttachment = {
url: string;
ctaText?: string;
theme?: 'light' | 'dark';
icon?: string;
rel?: string[];
needsProxy?: boolean;
};

export type ShoppingAttachment = {
ctaText: string;
theme?: 'light' | 'dark';
};

export interface Page {
id: ElementId;
elements: Element[];
defaultBackgroundElement?: DefaultBackgroundElement;
animations?: StoryAnimation[];
backgroundColor: Pattern;
groups?: Groups;
backgroundAudio?: {
resource: {
src: string;
id: number;
mimeType: string;
};
tracks: Track[];
loop: boolean;
};
autoAdvance?: boolean;
defaultPageDuration?: number;
pageAttachment?: {
url: string;
ctaText: string;
theme: string;
};
shoppingAttachment?: {
ctaText: string;
theme: string;
backgroundAudio?: BackgroundAudio;
pageAttachment?: PageAttachment;
shoppingAttachment?: ShoppingAttachment;
advancement?: {
autoAdvance?: boolean;
pageDuration?: number;
};
}
4 changes: 2 additions & 2 deletions packages/elements/src/types/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ interface FeaturedMedia {
height: number;
width: number;
url: string;
needsProxy: boolean;
isExternal: boolean;
needsProxy?: boolean;
isExternal?: boolean;
}
interface PublisherLogo {
id: number;
Expand Down
17 changes: 14 additions & 3 deletions packages/elements/src/utils/elementIs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import type {
BackgroundableElement,
DefaultBackgroundElement,
Element,
GifElement,
LinkableElement,
MediaElement,
ProductElement,
SequenceMediaElement,
StickerElement,
TextElement,
VideoElement,
GifElement,
LinkableElement,
OverlayableElement,
} from '../types';
import { ElementType } from '../types';

Expand Down Expand Up @@ -79,7 +80,10 @@ function isSequenceMediaElement(
e: Draft<MediaElement>
): e is Draft<SequenceMediaElement>;
function isSequenceMediaElement(e: MediaElement): e is SequenceMediaElement {
return 'poster' in e.resource && Boolean(e.resource.poster);
return (
('poster' in e.resource && Boolean(e.resource.poster)) ||
('length' in e.resource && Boolean(e.resource.length))
);
}

function isSticker(e: Element): e is StickerElement {
Expand Down Expand Up @@ -110,6 +114,12 @@ function isLinkable(e: Element): e is LinkableElement {
);
}

function isOverlayable(e: Element): e is OverlayableElement;
function isOverlayable(e: Draft<Element>): e is Draft<OverlayableElement>;
function isOverlayable(e: Element): e is OverlayableElement {
return 'overlay' in e && typeof e.overlay !== 'undefined';
}

const elementIs = {
media: isMediaElement,
text: isTextElement,
Expand All @@ -121,6 +131,7 @@ const elementIs = {
video: isVideo,
gif: isGif,
linkable: isLinkable,
overlayable: isOverlayable,
};

export default elementIs;
Loading

0 comments on commit ef233cf

Please sign in to comment.