forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnormalizeStyleOptions.ts
More file actions
232 lines (184 loc) · 8.88 KB
/
normalizeStyleOptions.ts
File metadata and controls
232 lines (184 loc) · 8.88 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { warnOnce } from '@msinternal/botframework-webchat-base/utils';
import defaultStyleOptions from './defaultStyleOptions';
import StyleOptions, { StrictStyleOptions } from './StyleOptions';
const bubbleImageHeightDeprecation = warnOnce(
'"styleOptions.bubbleImageHeight" has been deprecated. Use "styleOptions.bubbleImageMaxHeight" and "styleOptions.bubbleImageMinHeight" instead. This deprecation migration will be removed on or after 2026-07-05.'
);
const bubbleMaxWidthDeprecation = warnOnce(
'"styleOptions.bubbleMaxWidth" has been deprecated. Use "styleOptions.bubbleAttachmentMaxWidth" and "styleOptions.bubbleMessageMaxWidth" instead. This deprecation migration will be removed on or after 2026-07-05.'
);
const bubbleMinWidthDeprecation = warnOnce(
'"styleOptions.bubbleMinWidth" has been deprecated. Use "styleOptions.bubbleAttachmentMinWidth" and "styleOptions.bubbleMessageMinWidth" instead. This deprecation migration will be removed on or after 2026-07-05.'
);
const hideScrollToEndButtonDeprecation = warnOnce(
'"styleOptions.hideScrollToEndButton" has been deprecated. To hide scroll to end button, set "scrollToEndBehavior" to false. This deprecation migration will be removed on or after 2023-06-02.'
);
const newMessagesButtonFontSizeDeprecation = warnOnce(
'"styleOptions.newMessagesButtonFontSize" has been renamed to "styleOptions.scrollToEndButtonFontSize". This deprecation migration will be removed on or after 2023-06-02.'
);
const suggestedActionBackgroundDeprecation = warnOnce(
'"styleOptions.suggestedActionBackground" has been deprecated. Please use "styleOptions.suggestedActionBackgroundColor" instead. This deprecation migration will be removed on or after 2021-09-16.'
);
const suggestedActionXXXBackgroundDeprecation = warnOnce(
'"styleOptions.suggestedActionXXXBackground" has been deprecated. Please use "styleOptions.suggestedActionBackgroundColorOnXXX" instead. This deprecation migration will be removed on or after 2021-09-16.'
);
const suggestedActionDisabledDeprecation = warnOnce(
'"styleOptions.suggestedActionDisabledXXX" has been renamed to "styleOptions.suggestedActionXXXOnDisabled". This deprecation migration will be removed on or after 2021-09-16.'
);
// TODO: [P4] We should add a notice for people who want to use "styleSet" instead of "styleOptions".
// "styleSet" is actually CSS stylesheet and it is based on the DOM tree.
// DOM tree may change from time to time, thus, maintaining "styleSet" becomes a constant effort.
// eslint-disable-next-line complexity
export default function normalizeStyleOptions({
hideScrollToEndButton,
newMessagesButtonFontSize,
...options
}: StyleOptions = {}): StrictStyleOptions {
const filledOptions: Required<StyleOptions> = { ...defaultStyleOptions, ...options };
// Keep this list flat (no nested style) and serializable (no functions)
const { bubbleFromUserNubOffset, bubbleNubOffset, emojiSet } = filledOptions;
let normalizedBubbleFromUserNubOffset: number;
let normalizedBubbleNubOffset: number;
let normalizedEmojiSet: false | Record<string, string>;
if (bubbleFromUserNubOffset === 'top') {
normalizedBubbleFromUserNubOffset = 0;
} else if (typeof bubbleFromUserNubOffset !== 'number') {
normalizedBubbleFromUserNubOffset = -0;
} else {
normalizedBubbleFromUserNubOffset = bubbleFromUserNubOffset;
}
if (bubbleNubOffset === 'top') {
normalizedBubbleNubOffset = 0;
} else if (typeof bubbleNubOffset !== 'number') {
normalizedBubbleNubOffset = -0;
} else {
normalizedBubbleNubOffset = bubbleNubOffset;
}
if (emojiSet === true) {
normalizedEmojiSet = {
':)': '😊',
':-)': '😊',
'(:': '😊',
'(-:': '😊',
':-|': '😐',
':|': '😐',
':-(': '☹️',
':(': '☹️',
':-D': '😀',
':D': '😀',
':-p': '😛',
':p': '😛',
':-P': '😛',
':P': '😛',
':-o': '😲',
':o': '😲',
':O': '😲',
':-O': '😲',
':-0': '😲',
':0': '😲',
';-)': '😉',
';)': '😉',
'<3': '❤️',
'</3': '💔',
'<\\3': '💔'
};
} else if (Object.prototype.toString.call(emojiSet) !== '[object Object]') {
console.warn('botframework-webchat: emojiSet must be a boolean or an object with emoticon: emojiValues');
normalizedEmojiSet = false;
} else {
normalizedEmojiSet = emojiSet;
}
if (hideScrollToEndButton) {
hideScrollToEndButtonDeprecation();
// Only set if the "scrollToEndButtonBehavior" is not set.
// If it has been set, the developer should know the older "hideScrollToEndButton" option is deprecated.
filledOptions.scrollToEndButtonBehavior = options.scrollToEndButtonBehavior || false;
}
let patchedScrollToEndButtonBehavior = filledOptions.scrollToEndButtonBehavior;
if (patchedScrollToEndButtonBehavior !== 'any' && patchedScrollToEndButtonBehavior !== false) {
patchedScrollToEndButtonBehavior === 'unread' ||
console.warn(
'Web Chat: "scrollToEndButtonBehavior" must be either "unread", "any", or false, will set to "unread".'
);
patchedScrollToEndButtonBehavior = 'unread';
}
if (newMessagesButtonFontSize) {
newMessagesButtonFontSizeDeprecation();
// Only set if the "scrollToEndButtonFontSize" is not set.
filledOptions.scrollToEndButtonFontSize = options.scrollToEndButtonFontSize || newMessagesButtonFontSize;
}
options.suggestedActionBackground && suggestedActionBackgroundDeprecation();
if (options.suggestedActionActiveBackground) {
suggestedActionXXXBackgroundDeprecation();
filledOptions.suggestedActionBackgroundColorOnActive =
options.suggestedActionBackgroundColorOnActive || options.suggestedActionActiveBackground;
}
if (options.suggestedActionFocusBackground) {
suggestedActionXXXBackgroundDeprecation();
filledOptions.suggestedActionBackgroundColorOnFocus =
options.suggestedActionBackgroundColorOnFocus || options.suggestedActionFocusBackground;
}
if (options.suggestedActionHoverBackground) {
suggestedActionXXXBackgroundDeprecation();
filledOptions.suggestedActionBackgroundColorOnHover =
options.suggestedActionBackgroundColorOnHover || options.suggestedActionHoverBackground;
}
if (options.suggestedActionDisabledBackground) {
suggestedActionXXXBackgroundDeprecation();
filledOptions.suggestedActionBackgroundColorOnDisabled =
options.suggestedActionBackgroundColorOnDisabled || options.suggestedActionDisabledBackground;
}
if (options.suggestedActionDisabledBorderColor) {
suggestedActionDisabledDeprecation();
filledOptions.suggestedActionBorderColorOnDisabled =
options.suggestedActionBorderColorOnDisabled || options.suggestedActionDisabledBorderColor;
}
if (options.suggestedActionDisabledBorderStyle) {
suggestedActionDisabledDeprecation();
filledOptions.suggestedActionBorderStyleOnDisabled =
options.suggestedActionBorderStyleOnDisabled || options.suggestedActionDisabledBorderStyle;
}
if (options.suggestedActionDisabledBorderWidth) {
suggestedActionDisabledDeprecation();
filledOptions.suggestedActionBorderWidthOnDisabled =
options.suggestedActionBorderWidthOnDisabled || options.suggestedActionDisabledBorderWidth;
}
if (options.suggestedActionDisabledTextColor) {
suggestedActionDisabledDeprecation();
filledOptions.suggestedActionTextColorOnDisabled =
options.suggestedActionTextColorOnDisabled || options.suggestedActionDisabledTextColor;
}
if (options.bubbleImageHeight) {
bubbleImageHeightDeprecation();
filledOptions.bubbleImageMaxHeight = options.bubbleImageHeight;
filledOptions.bubbleImageMinHeight = options.bubbleImageHeight;
filledOptions.bubbleImageHeight = undefined;
}
if (typeof options.bubbleAttachmentMaxWidth === 'undefined' && options.bubbleMaxWidth) {
bubbleMaxWidthDeprecation();
filledOptions.bubbleAttachmentMaxWidth = options.bubbleMaxWidth;
filledOptions.bubbleMaxWidth = undefined;
}
if (typeof options.bubbleAttachmentMinWidth === 'undefined' && options.bubbleMinWidth) {
bubbleMinWidthDeprecation();
filledOptions.bubbleAttachmentMinWidth = options.bubbleMinWidth;
filledOptions.bubbleMinWidth = undefined;
}
if (typeof options.bubbleMessageMaxWidth === 'undefined' && options.bubbleMaxWidth) {
bubbleMaxWidthDeprecation();
filledOptions.bubbleMessageMaxWidth = options.bubbleMaxWidth;
filledOptions.bubbleMaxWidth = undefined;
}
if (typeof options.bubbleMessageMinWidth === 'undefined' && options.bubbleMinWidth) {
bubbleMinWidthDeprecation();
filledOptions.bubbleMessageMinWidth = options.bubbleMinWidth;
filledOptions.bubbleMinWidth = undefined;
}
return {
...filledOptions,
bubbleFromUserNubOffset: normalizedBubbleFromUserNubOffset,
bubbleNubOffset: normalizedBubbleNubOffset,
emojiSet: normalizedEmojiSet,
scrollToEndButtonBehavior: patchedScrollToEndButtonBehavior
};
}