@asgard-js/react 0.0.33 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context/asgard-service-context.d.ts.map +1 -1
- package/dist/context/asgard-theme-context.d.ts.map +1 -1
- package/dist/hooks/use-react-markdown-renderer.d.ts.map +1 -1
- package/dist/index.js +9283 -9256
- package/dist/utils/color-utils.d.ts +18 -0
- package/dist/utils/color-utils.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/context/asgard-service-context.tsx +10 -1
- package/src/context/asgard-theme-context.tsx +17 -1
- package/src/hooks/use-react-markdown-renderer.tsx +7 -1
- package/src/utils/color-utils.ts +38 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 顏色工具函數
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 為顏色添加透明度
|
|
6
|
+
* @param color 十六進制顏色值 (例如: #FF0000)
|
|
7
|
+
* @param alpha 透明度 (0-1)
|
|
8
|
+
* @returns 帶透明度的十六進制顏色值
|
|
9
|
+
*/
|
|
10
|
+
export declare const addTransparency: (color: string, alpha: number) => string;
|
|
11
|
+
/**
|
|
12
|
+
* 加深顏色
|
|
13
|
+
* @param color 十六進制顏色值 (例如: #FF0000)
|
|
14
|
+
* @param amount 加深程度 (0-1)
|
|
15
|
+
* @returns 加深後的十六進制顏色值
|
|
16
|
+
*/
|
|
17
|
+
export declare const darkenColor: (color: string, amount: number) => string;
|
|
18
|
+
//# sourceMappingURL=color-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"color-utils.d.ts","sourceRoot":"","sources":["../../src/utils/color-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,eAAe,UAAW,MAAM,SAAS,MAAM,KAAG,MAM9D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,UAAU,MAAM,KAAG,MAa3D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asgard-js/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.36",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"vitest": "^1.6.0"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@asgard-js/core": "^0.0.
|
|
57
|
+
"@asgard-js/core": "^0.0.36",
|
|
58
58
|
"react": "^18.0.0",
|
|
59
59
|
"react-dom": "^18.0.0"
|
|
60
60
|
},
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
UseChannelProps,
|
|
20
20
|
UseChannelReturn,
|
|
21
21
|
} from '../hooks';
|
|
22
|
+
import { useAsgardAppInitializationContext } from './asgard-app-initialization-context';
|
|
22
23
|
|
|
23
24
|
export interface AsgardServiceContextValue {
|
|
24
25
|
avatar?: string;
|
|
@@ -68,12 +69,20 @@ export function AsgardServiceContextProvider(
|
|
|
68
69
|
children,
|
|
69
70
|
parentRef,
|
|
70
71
|
config,
|
|
71
|
-
botTypingPlaceholder,
|
|
72
|
+
botTypingPlaceholder: botTypingPlaceholderFromProps,
|
|
72
73
|
customChannelId,
|
|
73
74
|
initMessages,
|
|
74
75
|
onSseMessage,
|
|
75
76
|
} = props;
|
|
76
77
|
|
|
78
|
+
const { data } = useAsgardAppInitializationContext();
|
|
79
|
+
const { annotations } = data;
|
|
80
|
+
|
|
81
|
+
// API 優先,Props 次之,最後才是預設值
|
|
82
|
+
const botTypingPlaceholder =
|
|
83
|
+
annotations?.embedConfig?.botTypingPlaceholder ||
|
|
84
|
+
botTypingPlaceholderFromProps;
|
|
85
|
+
|
|
77
86
|
const messageBoxBottomRef = useRef<HTMLDivElement>(null);
|
|
78
87
|
|
|
79
88
|
const client = useAsgardServiceClient({ config });
|
|
@@ -4,10 +4,12 @@ import {
|
|
|
4
4
|
PropsWithChildren,
|
|
5
5
|
ReactNode,
|
|
6
6
|
useContext,
|
|
7
|
+
useEffect,
|
|
7
8
|
useMemo,
|
|
8
9
|
useCallback,
|
|
9
10
|
} from 'react';
|
|
10
11
|
import { deepMerge } from '../utils/deep-merge';
|
|
12
|
+
import { addTransparency, darkenColor } from '../utils/color-utils';
|
|
11
13
|
import {
|
|
12
14
|
useAsgardAppInitializationContext,
|
|
13
15
|
Annotations,
|
|
@@ -331,7 +333,7 @@ export function AsgardThemeContextProvider(
|
|
|
331
333
|
borderColor: themeFromAnnotations.chatbot?.borderColor,
|
|
332
334
|
backgroundColor: themeFromAnnotations.botMessage
|
|
333
335
|
?.backgroundColor
|
|
334
|
-
?
|
|
336
|
+
? addTransparency(themeFromAnnotations.botMessage.backgroundColor, 0.2)
|
|
335
337
|
: undefined,
|
|
336
338
|
},
|
|
337
339
|
},
|
|
@@ -346,6 +348,9 @@ export function AsgardThemeContextProvider(
|
|
|
346
348
|
// For unset messages
|
|
347
349
|
color:
|
|
348
350
|
themeFromAnnotations.chatbot?.primaryComponent?.secondaryColor,
|
|
351
|
+
backgroundColor: themeFromAnnotations.botMessage?.backgroundColor
|
|
352
|
+
? addTransparency(themeFromAnnotations.botMessage.backgroundColor, 0.2)
|
|
353
|
+
: undefined,
|
|
349
354
|
},
|
|
350
355
|
},
|
|
351
356
|
ButtonMessageTemplate: {
|
|
@@ -389,6 +394,17 @@ export function AsgardThemeContextProvider(
|
|
|
389
394
|
|
|
390
395
|
const value = useMemo(() => deepMergeTheme(), [deepMergeTheme]);
|
|
391
396
|
|
|
397
|
+
// 設置 CSS 自定義屬性
|
|
398
|
+
useEffect(() => {
|
|
399
|
+
const botMessageBgColor = annotations?.embedConfig?.theme?.botMessage?.backgroundColor;
|
|
400
|
+
if (botMessageBgColor) {
|
|
401
|
+
document.documentElement.style.setProperty(
|
|
402
|
+
'--bot-message-hyperlink-color',
|
|
403
|
+
darkenColor(botMessageBgColor, 0.2)
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
}, [annotations?.embedConfig?.theme?.botMessage?.backgroundColor]);
|
|
407
|
+
|
|
392
408
|
return (
|
|
393
409
|
<AsgardThemeContext.Provider value={value}>
|
|
394
410
|
{children}
|
|
@@ -113,7 +113,13 @@ const LinkRenderer = ({ children, href, ...props }: React.ComponentProps<'a'>):
|
|
|
113
113
|
);
|
|
114
114
|
|
|
115
115
|
return (
|
|
116
|
-
<a
|
|
116
|
+
<a
|
|
117
|
+
href={href}
|
|
118
|
+
onClick={handleClick}
|
|
119
|
+
rel="noopener noreferrer"
|
|
120
|
+
style={{ color: 'var(--bot-message-hyperlink-color, #4467eb)' }}
|
|
121
|
+
{...props}
|
|
122
|
+
>
|
|
117
123
|
{children}
|
|
118
124
|
</a>
|
|
119
125
|
);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 顏色工具函數
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 為顏色添加透明度
|
|
7
|
+
* @param color 十六進制顏色值 (例如: #FF0000)
|
|
8
|
+
* @param alpha 透明度 (0-1)
|
|
9
|
+
* @returns 帶透明度的十六進制顏色值
|
|
10
|
+
*/
|
|
11
|
+
export const addTransparency = (color: string, alpha: number): string => {
|
|
12
|
+
if (!color.startsWith('#')) return color;
|
|
13
|
+
|
|
14
|
+
const hex = Math.round(alpha * 255).toString(16).padStart(2, '0');
|
|
15
|
+
|
|
16
|
+
return `${color}${hex}`;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 加深顏色
|
|
21
|
+
* @param color 十六進制顏色值 (例如: #FF0000)
|
|
22
|
+
* @param amount 加深程度 (0-1)
|
|
23
|
+
* @returns 加深後的十六進制顏色值
|
|
24
|
+
*/
|
|
25
|
+
export const darkenColor = (color: string, amount: number): string => {
|
|
26
|
+
if (!color.startsWith('#')) return color;
|
|
27
|
+
|
|
28
|
+
const hex = color.slice(1);
|
|
29
|
+
const r = parseInt(hex.slice(0, 2), 16);
|
|
30
|
+
const g = parseInt(hex.slice(2, 4), 16);
|
|
31
|
+
const b = parseInt(hex.slice(4, 6), 16);
|
|
32
|
+
|
|
33
|
+
const newR = Math.max(0, Math.round(r * (1 - amount)));
|
|
34
|
+
const newG = Math.max(0, Math.round(g * (1 - amount)));
|
|
35
|
+
const newB = Math.max(0, Math.round(b * (1 - amount)));
|
|
36
|
+
|
|
37
|
+
return `#${newR.toString(16).padStart(2, '0')}${newG.toString(16).padStart(2, '0')}${newB.toString(16).padStart(2, '0')}`;
|
|
38
|
+
};
|