-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathAdaptiveCardRenderer.js
210 lines (167 loc) · 5.96 KB
/
AdaptiveCardRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* eslint no-magic-numbers: ["error", { "ignore": [0, 2] }] */
import { HostConfig } from 'adaptivecards';
import PropTypes from 'prop-types';
import React from 'react';
import { Components, connectToWebChat, getTabIndex, localize } from 'botframework-webchat-component';
const { ErrorBox } = Components;
function isPlainObject(obj) {
return Object.getPrototypeOf(obj) === Object.prototype;
}
class AdaptiveCardRenderer extends React.PureComponent {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.handleExecuteAction = this.handleExecuteAction.bind(this);
this.contentRef = React.createRef();
this.state = {
error: null
};
}
componentDidMount() {
this.renderCard();
}
componentDidUpdate({ adaptiveCard: prevAdaptiveCard }) {
const { adaptiveCard } = this.props;
prevAdaptiveCard !== adaptiveCard && this.renderCard();
}
handleClick({ target }) {
const { disabled, onCardAction, tapAction } = this.props;
// Some items, e.g. tappable text, cannot be disabled thru DOM attributes
if (!disabled) {
const tabIndex = getTabIndex(target);
// If the user is clicking on something that is already clickable, do not allow them to click the card.
// E.g. a hero card can be tappable, and image and buttons inside the hero card can also be tappable.
if (typeof tabIndex !== 'number' || tabIndex < 0) {
tapAction && onCardAction(tapAction);
}
}
}
handleExecuteAction(action) {
const { disabled, onCardAction } = this.props;
// Some items, e.g. tappable image, cannot be disabled thru DOM attributes
if (disabled) {
return;
}
const actionTypeName = action.getJsonTypeName();
if (actionTypeName === 'Action.OpenUrl') {
onCardAction({
type: 'openUrl',
value: action.url
});
} else if (actionTypeName === 'Action.Submit') {
if (typeof action.data !== 'undefined') {
const { data: actionData } = action;
if (actionData && actionData.__isBotFrameworkCardAction) {
const { cardAction } = actionData;
const { displayText, type, value } = cardAction;
onCardAction({ displayText, type, value });
} else {
onCardAction({
type: typeof action.data === 'string' ? 'imBack' : 'postBack',
value: action.data
});
}
}
} else {
console.error(`Web Chat: received unknown action from Adaptive Cards`);
console.error(action);
}
}
renderCard() {
const {
contentRef: { current },
props: { adaptiveCard, adaptiveCardHostConfig, disabled, renderMarkdown },
state: { error }
} = this;
if (current && adaptiveCard) {
// Currently, the only way to set the Markdown engine is to set it thru static member of AdaptiveCard class
// TODO: [P3] Checks if we could make the "renderMarkdown" per card
// This could be limitations from Adaptive Cards package
// Because there could be timing difference between .parse and .render, we could be using wrong Markdown engine
adaptiveCard.constructor.onProcessMarkdown = (text, result) => {
if (renderMarkdown) {
result.outputHtml = renderMarkdown(text);
result.didProcess = true;
}
};
adaptiveCard.onExecuteAction = this.handleExecuteAction;
if (adaptiveCardHostConfig) {
adaptiveCard.hostConfig = isPlainObject(adaptiveCardHostConfig)
? new HostConfig(adaptiveCardHostConfig)
: adaptiveCardHostConfig;
}
const errors = adaptiveCard.validate();
if (errors.length) {
// TODO: [P3] Since this can be called from `componentDidUpdate` and potentially error, we should fix a better way to propagate the error.
return this.setState(() => ({ error: errors }));
}
let element;
try {
element = adaptiveCard.render();
} catch (error) {
return this.setState(() => ({ error }));
}
if (!element) {
return this.setState(() => ({ error: 'Adaptive Card rendered as empty element' }));
}
error && this.setState(() => ({ error: null }));
if (disabled) {
const hyperlinks = element.querySelectorAll('a');
const inputs = element.querySelectorAll('button, input, select, textarea');
[].forEach.call(inputs, input => {
input.disabled = true;
});
[].forEach.call(hyperlinks, hyperlink => {
hyperlink.addEventListener('click', event => {
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
});
});
}
const [firstChild] = current.children;
if (firstChild) {
current.replaceChild(element, firstChild);
} else {
current.appendChild(element);
}
}
}
render() {
const {
props: { language, styleSet },
state: { error }
} = this;
return error ? (
<ErrorBox message={localize('Adaptive Card render error', language)}>
<pre>{JSON.stringify(error, null, 2)}</pre>
</ErrorBox>
) : (
<div className={styleSet.adaptiveCardRenderer} onClick={this.handleClick} ref={this.contentRef} />
);
}
}
AdaptiveCardRenderer.propTypes = {
adaptiveCard: PropTypes.any.isRequired,
adaptiveCardHostConfig: PropTypes.any.isRequired,
disabled: PropTypes.bool,
language: PropTypes.string.isRequired,
onCardAction: PropTypes.func.isRequired,
renderMarkdown: PropTypes.func.isRequired,
styleSet: PropTypes.shape({
adaptiveCardRenderer: PropTypes.any.isRequired
}).isRequired,
tapAction: PropTypes.func
};
AdaptiveCardRenderer.defaultProps = {
disabled: false,
tapAction: undefined
};
export default connectToWebChat(({ disabled, language, onCardAction, renderMarkdown, styleSet, tapAction }) => ({
disabled,
language,
onCardAction,
renderMarkdown,
styleSet,
tapAction
}))(AdaptiveCardRenderer);