-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathConfigProvider.vue
More file actions
178 lines (153 loc) · 4.45 KB
/
Copy pathConfigProvider.vue
File metadata and controls
178 lines (153 loc) · 4.45 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
<script setup lang="ts">
import { ThemeProvider } from '@opentiny/tiny-robot';
import {
type IMaterials,
type IMaterialsTheme,
type ThemeApplyResult,
type ThemeColorScheme,
} from '@opentiny/genui-sdk-core';
import {
watch,
provide,
inject,
computed,
ref,
shallowRef,
onBeforeUnmount,
defineComponent,
h,
type Component,
type PropType,
type VNode,
} from 'vue';
import { RENDERER_SETTINGS_KEY } from '@opentiny/tiny-schema-renderer';
import { I18nMessages, useI18n } from '../chat/i18n';
import { GENUI_I18N, GENUI_CONFIG, GENUI_MATERIALS } from './injection-tokens';
import { useMediaTheme } from './use-media-theme';
import type { NotifyHandler } from './notify.types';
export type { NotifyHandler };
export interface ConfigProviderProps {
theme?: string;
id?: string;
locale?: string;
i18n?: I18nMessages;
materials?: IMaterials;
notify?: NotifyHandler;
}
const props = withDefaults(defineProps<ConfigProviderProps>(), {
id: 'tiny-genui-config-provider',
locale: 'zh_CN',
});
const i18n = useI18n();
provide(GENUI_I18N, i18n);
const { theme: mediaTheme } = useMediaTheme();
const materialThemes = computed<IMaterialsTheme[]>(() => {
const theme = props.materials?.theme;
if (!theme) {
return [];
}
return Array.isArray(theme) ? theme : [theme];
});
const theme = computed(() => props.theme || 'light');
const colorScheme = ref<ThemeColorScheme>('light');
const genuiConfig = computed(() => ({
colorScheme: colorScheme.value,
id: props.id,
}));
provide(GENUI_CONFIG, genuiConfig);
const internalMaterials = {};
watch(() => props.materials, (newVal) => {
Object.assign(internalMaterials, newVal);
}, { immediate: true });
provide(GENUI_MATERIALS, internalMaterials);
const parentRendererSettings = inject(RENDERER_SETTINGS_KEY, {}) as Record<string, any>;
const rendererSettings = {
...(parentRendererSettings && typeof parentRendererSettings === 'object' ? parentRendererSettings : {}),
};
watch(
() => props.notify,
(notify) => {
rendererSettings.notify = notify ?? parentRendererSettings?.notify;
},
{ immediate: true },
);
provide(RENDERER_SETTINGS_KEY, rendererSettings);
watch(
() => [props.locale, props.i18n] as const,
() => {
if (props.locale && props.locale !== i18n.locale.value) {
i18n.setLocale(props.locale);
}
props.i18n && i18n.mergeMessages(props.i18n);
},
{ immediate: true },
);
const ThemeRoots = defineComponent({
name: 'ThemeRoots',
props: {
roots: { type: Array as PropType<Component[]>, required: true },
},
setup(props, { slots }) {
return () => {
const children = slots.default?.() ?? [];
return props.roots.reduceRight<VNode | VNode[]>(
(acc, root) => h(root, {}, () => acc),
children,
);
};
},
});
const themeRoots = shallowRef<Component[]>([]);
let applied: ThemeApplyResult[] = [];
function clearTheme() {
const pending = applied;
applied = [];
pending.forEach((item) => item.dispose());
}
watch(
() => [materialThemes.value, theme.value, mediaTheme.value, props.id] as const,
([apis, themeValue, systemColorScheme]) => {
clearTheme();
// 原始 theme(含 auto)原样下发,物料用 ctx.systemColorScheme 自行解析
const results: ThemeApplyResult[] = [];
const roots: Component[] = [];
for (const api of apis) {
const result = api.apply(themeValue, { systemColorScheme });
if (result.Root) {
roots.push(result.Root as Component);
}
results.push(result);
applied.push(result);
}
themeRoots.value = roots;
// 取第一个声明了 colorScheme 的落地结果(first-wins),否则跟随系统
colorScheme.value =
results.find((result) => result.descriptor.colorScheme)?.descriptor.colorScheme ??
systemColorScheme;
},
{ immediate: true },
);
onBeforeUnmount(clearTheme);
const robotProviderProps = computed(() => ({
colorMode: colorScheme.value,
targetElement: `#${props.id}`,
}));
</script>
<template>
<div :id="props.id" class="tg-config-provider">
<ThemeProvider v-bind="robotProviderProps">
<ThemeRoots :roots="themeRoots">
<slot />
</ThemeRoots>
</ThemeProvider>
</div>
</template>
<style scoped>
.tg-config-provider {
--tr-sender-bg-color: var(--tr-container-bg-default);
--tr-sender-text-color: var(--tr-text-primary);
--tr-sender-action-buttons-icon-color: var(--tr-text-secondary);
--tr-sender-action-buttons-send-bg-color: var(--tr-color-primary);
height: 100%;
}
</style>