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

Add typings for Adaptive Cards #3946

Merged
merged 3 commits into from
Jun 17, 2021
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `"any"` will show when there are any offscreen messages;
- `false` will always hide the button.
- Added new [`scrollToEndButtonMiddleware`](https://github.com/microsoft/BotFramework-WebChat/blob/main/packages/api/src/types/scrollToEndButtonMiddleware.ts) to customize the appearance of the scroll to end button.
- Resolves [#3752](https://github.com/microsoft/BotFramework-WebChat/issues/3752). Added typings (`*.d.ts`) for all public interfaces, by [@compulim](https://github.com), in PR [#3931](https://github.com/microsoft/BotFramework-WebChat/pull/3931)
- Resolves [#3752](https://github.com/microsoft/BotFramework-WebChat/issues/3752). Added typings (`*.d.ts`) for all public interfaces, by [@compulim](https://github.com), in PR [#3931](https://github.com/microsoft/BotFramework-WebChat/pull/3931) and [#3946](https://github.com/microsoft/BotFramework-WebChat/pull/3946)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { hooks } from '../../../../packages/bundle';

const [styleOptions] = hooks.useStyleOptions();

styleOptions.cardEmphasisBackgroundColor = 123;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { hooks } from '../../../../packages/bundle/lib/index-minimal';

const [styleOptions] = hooks.useStyleOptions();

styleOptions.cardEmphasisBackgroundColor = 'black';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { hooks } from '../../../../packages/bundle';

const [styleOptions] = hooks.useStyleOptions();

styleOptions.cardEmphasisBackgroundColor = 'black';
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* Adaptive Cards styling
*/
type AdaptiveCardsStyleOptions = {
/**
* Adaptive Cards styling
*/
/** Adaptive Cards: Specify the maximum schema version supported by the Adaptive Card serializer. */
adaptiveCardsParserMaxVersion?: string;

/**
* Adaptive Cards styling for 'emphasis' container style
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from 'adaptivecards';

import { CardAction } from 'botframework-directlinejs';
import AdaptiveCardsPackage from '../../types/AdaptiveCardsPackage';
import AdaptiveCardsStyleOptions from '../AdaptiveCardsStyleOptions';

export interface BotFrameworkCardAction {
__isBotFrameworkCardAction: boolean;
Expand Down Expand Up @@ -54,9 +56,13 @@ function addCardAction(cardAction: CardAction, includesOAuthButtons?: boolean) {
export default class AdaptiveCardBuilder {
card: AdaptiveCard;
container: Container;
styleOptions: any;
styleOptions: AdaptiveCardsStyleOptions;

constructor(adaptiveCards, styleOptions, direction = 'ltr') {
constructor(
adaptiveCards: AdaptiveCardsPackage,
styleOptions: AdaptiveCardsStyleOptions,
direction: 'ltr' | 'rtl' | 'auto' = 'ltr'
) {
this.card = new adaptiveCards.AdaptiveCard();
this.container = new Container();
this.container.rtl = direction === 'rtl';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint react/no-array-index-key: "off" */

import { DirectLineAnimationCard } from 'botframework-webchat-core';
import { Components, hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC } from 'react';
Expand All @@ -11,7 +12,7 @@ const { useStyleSet } = hooks;

type AnimationCardContentProps = {
actionPerformedClassName?: string;
content: any;
content: DirectLineAnimationCard;
disabled?: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint react/no-array-index-key: "off" */

import { DirectLineAudioCard } from 'botframework-webchat-core';
import { Components, hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC } from 'react';
Expand All @@ -11,7 +12,7 @@ const { useStyleSet } = hooks;

type AudioCardContentProps = {
actionPerformedClassName?: string;
content: any;
content: DirectLineAudioCard;
disabled?: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { hooks } from 'botframework-webchat-component';
import AdaptiveCardBuilder from './AdaptiveCardBuilder';
import AdaptiveCardRenderer from './AdaptiveCardRenderer';
import useAdaptiveCardsPackage from '../hooks/useAdaptiveCardsPackage';
import useStyleOptions from '../../hooks/useStyleOptions';

const { useDirection, useStyleOptions } = hooks;
const { useDirection } = hooks;

const CommonCard = ({ actionPerformedClassName, content, disabled }) => {
const [adaptiveCardsPackage] = useAdaptiveCardsPackage();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { DirectLineHeroCard } from 'botframework-webchat-core';
import { hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC, useMemo } from 'react';

import AdaptiveCardBuilder from './AdaptiveCardBuilder';
import AdaptiveCardRenderer from './AdaptiveCardRenderer';
import useAdaptiveCardsPackage from '../hooks/useAdaptiveCardsPackage';
import useStyleOptions from '../../hooks/useStyleOptions';

const { useDirection, useStyleOptions } = hooks;
const { useDirection } = hooks;

type HeroCardContentProps = {
actionPerformedClassName?: string;
content: any;
content: DirectLineHeroCard;
disabled?: boolean;
};

const HeroCardContent: FC<HeroCardContentProps> = ({ actionPerformedClassName, content, disabled }) => {
const [adaptiveCardsPackage] = useAdaptiveCardsPackage();
const [styleOptions] = useStyleOptions();
const [direction] = useDirection();

const builtCard = useMemo(() => {
const builder = new AdaptiveCardBuilder(adaptiveCardsPackage, styleOptions, direction);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { DirectLineOAuthCard } from 'botframework-webchat-core';
import { hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC, useMemo } from 'react';

import AdaptiveCardBuilder from './AdaptiveCardBuilder';
import AdaptiveCardRenderer from './AdaptiveCardRenderer';
import useAdaptiveCardsPackage from '../hooks/useAdaptiveCardsPackage';
import useStyleOptions from '../../hooks/useStyleOptions';

const { useDirection, useStyleOptions } = hooks;
const { useDirection } = hooks;

type OAuthCardContentProps = {
actionPerformedClassName?: string;
content: any;
content: DirectLineOAuthCard;
disabled?: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
/* eslint no-magic-numbers: ["error", { "ignore": [0, 1, 10, 15, 25, 50, 75] }] */

import { DirectLineReceiptCard } from 'botframework-webchat-core';
import { hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC, useMemo } from 'react';

import { StrictFullBundleStyleOptions } from '../../types/FullBundleStyleOptions';
import AdaptiveCardBuilder from './AdaptiveCardBuilder';
import AdaptiveCardRenderer from './AdaptiveCardRenderer';
import useAdaptiveCardsPackage from '../hooks/useAdaptiveCardsPackage';
import useStyleOptions from '../../hooks/useStyleOptions';

const { useDirection, useLocalizer, useStyleOptions } = hooks;
const { useDirection, useLocalizer } = hooks;

function nullOrUndefined(obj) {
return obj === null || typeof obj === 'undefined';
}

type ReceiptCardContentProps = {
actionPerformedClassName?: string;
content: any;
content: DirectLineReceiptCard;
disabled?: boolean;
};

const ReceiptCardContent: FC<ReceiptCardContentProps> = ({ actionPerformedClassName, content, disabled }) => {
const [adaptiveCardsPackage] = useAdaptiveCardsPackage();
const [direction] = useDirection();

// TODO: Create another useStyleOptions that natively export the correct type.
const [styleOptions] = useStyleOptions() as [StrictFullBundleStyleOptions];
const [styleOptions] = useStyleOptions();
const localize = useLocalizer();

const taxText = localize('RECEIPT_CARD_TAX');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DirectLineSignInCard } from 'botframework-webchat-core';
import { hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC } from 'react';
Expand All @@ -8,7 +9,7 @@ const { useStyleSet } = hooks;

type SignInCardContentProps = {
actionPerformedClassName?: string;
content: any;
content: DirectLineSignInCard;
disabled?: boolean;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
/* eslint no-magic-numbers: ["error", { "ignore": [25, 75] }] */

import { DirectLineThumbnailCard } from 'botframework-webchat-core';
import { hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC, useMemo } from 'react';

import { StrictFullBundleStyleOptions } from '../../types/FullBundleStyleOptions';
import AdaptiveCardBuilder from './AdaptiveCardBuilder';
import AdaptiveCardRenderer from './AdaptiveCardRenderer';
import useAdaptiveCardsPackage from '../hooks/useAdaptiveCardsPackage';
import useStyleOptions from '../../hooks/useStyleOptions';

const { useDirection, useStyleOptions } = hooks;
const { useDirection } = hooks;

type ThumbnailCardContentProps = {
actionPerformedClassName?: string;
content: any;
content: DirectLineThumbnailCard;
disabled?: boolean;
};

const ThumbnailCardContent: FC<ThumbnailCardContentProps> = ({ actionPerformedClassName, content, disabled }) => {
const [adaptiveCardsPackage] = useAdaptiveCardsPackage();
const [direction] = useDirection();

// TODO: Create another useStyleOptions that natively export the correct type.
const [styleOptions] = useStyleOptions() as [StrictFullBundleStyleOptions];
const [styleOptions] = useStyleOptions();

const builtCard = useMemo(() => {
if (content) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint react/no-array-index-key: "off" */

import { DirectLineVideoCard } from 'botframework-webchat-core';
import { Components, hooks } from 'botframework-webchat-component';
import PropTypes from 'prop-types';
import React, { FC } from 'react';
Expand All @@ -11,7 +12,7 @@ const { VideoContent } = Components;

type VideoCardContentProps = {
actionPerformedClassName?: string;
content: {
content: DirectLineVideoCard & {
autoloop?: boolean;
autostart?: boolean;
image?: { url?: string };
Expand All @@ -21,7 +22,7 @@ type VideoCardContentProps = {
};

const VideoCardContent: FC<VideoCardContentProps> = ({ actionPerformedClassName, content, disabled }) => {
const { autoloop, autostart, image: { url: imageURL } = {}, media } = content;
const { autoloop, autostart, image: { url: imageURL } = { url: undefined }, media } = content;
const [{ audioCardAttachment: audioCardAttachmentStyleSet }] = useStyleSet();

return (
Expand Down
1 change: 1 addition & 0 deletions packages/bundle/src/adaptiveCards/defaultStyleOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AdaptiveCardsStyleOptions from './AdaptiveCardsStyleOptions';

const ADAPTIVE_CARDS_DEFAULT_STYLE_OPTIONS: Required<AdaptiveCardsStyleOptions> = {
adaptiveCardsParserMaxVersion: undefined,
cardEmphasisBackgroundColor: '#F0F0F0',
cardPushButtonBackgroundColor: '#0063B1',
cardPushButtonTextColor: 'White',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext } from 'react';

import AdaptiveCardsContext from '../../AdaptiveCardsContext';

export default function useAdaptiveCardsContext() {
export default function useAdaptiveCardsContext(): AdaptiveCardsContext {
const context = useContext(AdaptiveCardsContext);

if (!context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { hooks } from 'botframework-webchat-component';
import { useCallback, useMemo } from 'react';

import useAdaptiveCardsPackage from '../useAdaptiveCardsPackage';
import useStyleOptions from '../../../hooks/useStyleOptions';

const { useDirection, useStyleOptions } = hooks;
const { useDirection } = hooks;

function updateRTLInline(element, rtl, adaptiveCardsPackage) {
if (element instanceof adaptiveCardsPackage.Container) {
Expand Down Expand Up @@ -33,7 +34,9 @@ export default function useParseAdaptiveCardJSON() {
const maxVersion = Version.parse(adaptiveCardsParserMaxVersion, new SerializationContext());

if (maxVersion && !maxVersion.isValid) {
return console.warn('botframework-webchat: "adaptiveCardsParserMaxVersion" specified is not a valid version.');
console.warn('botframework-webchat: "adaptiveCardsParserMaxVersion" specified is not a valid version.');

return;
}

return maxVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useMemo } from 'react';
import random from 'math-random';

export default function useUniqueId(prefix) {
export default function useUniqueId(prefix?: string): string {
const id = useMemo(() => random().toString(36).substr(2, 5), []);

prefix = prefix ? `${prefix}--` : '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { hooks } from 'botframework-webchat-component';
import { useMemo } from 'react';

import createDefaultAdaptiveCardHostConfig from '../Styles/adaptiveCardHostConfig';
import useAdaptiveCardsContext from './internal/useAdaptiveCardsContext';

const { useStyleOptions } = hooks;
import useStyleOptions from '../../hooks/useStyleOptions';

export default function useAdaptiveCardsHostConfig(): [any] {
const { hostConfigFromProps } = useAdaptiveCardsContext();
Expand Down
9 changes: 9 additions & 0 deletions packages/bundle/src/hooks/useStyleOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { hooks } from 'botframework-webchat-component';

import { StrictFullBundleStyleOptions } from '../types/FullBundleStyleOptions';

export default function useStyleOptions(): [StrictFullBundleStyleOptions] {
const [styleOptions] = hooks.useStyleOptions();

return [styleOptions as StrictFullBundleStyleOptions];
}
4 changes: 3 additions & 1 deletion packages/bundle/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import SignInCardContent from './adaptiveCards/Attachment/SignInCardContent';
import ThumbnailCardContent from './adaptiveCards/Attachment/ThumbnailCardContent';
import useAdaptiveCardsHostConfig from './adaptiveCards/hooks/useAdaptiveCardsHostConfig';
import useAdaptiveCardsPackage from './adaptiveCards/hooks/useAdaptiveCardsPackage';
import useStyleOptions from './hooks/useStyleOptions';
import VideoCardContent from './adaptiveCards/Attachment/VideoCardContent';

const renderWebChat = coreRenderWebChat.bind(null, ReactWebChat);
Expand Down Expand Up @@ -54,7 +55,8 @@ export const createDirectLineAppServiceExtension = (
const patchedHooks = {
...hooks,
useAdaptiveCardsHostConfig,
useAdaptiveCardsPackage
useAdaptiveCardsPackage,
useStyleOptions
};

const AdditionalComponents = {
Expand Down
12 changes: 7 additions & 5 deletions packages/bundle/src/types/AdaptiveCardsPackage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class AdaptiveCard {}
import { AdaptiveCard, HorizontalAlignment, TextSize, TextWeight, SerializationContext, Version } from 'adaptivecards';

type AdaptiveCardsPackage = {
AdaptiveCard: AdaptiveCard;
HorizontalAlignment: any;
TextSize: any;
TextWeight: any;
AdaptiveCard: typeof AdaptiveCard;
HorizontalAlignment: typeof HorizontalAlignment;
TextSize: typeof TextSize;
TextWeight: typeof TextWeight;
SerializationContext: typeof SerializationContext;
Version: typeof Version;
};

export default AdaptiveCardsPackage;
Loading