Skip to content

Commit edf18f8

Browse files
authored
Merge pull request #752 from GraphScope/feat-provider-extend
fix: modify provider
2 parents 73eb148 + 4645623 commit edf18f8

File tree

21 files changed

+284
-106
lines changed

21 files changed

+284
-106
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { useDynamicStyle } from './useDynamicStyle';
22
export { useIsMobile } from './useIsMobile';
33
export { useTheme } from './useTheme';
4+
export { useLocale } from './useLocale';
45
export { EnvProvider, useEnv } from './useEnv';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useState, useEffect } from 'react';
2+
3+
export const useLocale = () => {
4+
const [value, setValue] = useState("en-US");
5+
6+
useEffect(() => {
7+
const targetElement = document.querySelector('html'); // 目标元素
8+
const observer = new MutationObserver((mutationsList, observer) => {
9+
for (let mutation of mutationsList) {
10+
if (mutation.type === 'attributes' && mutation.attributeName === 'data-locale') {
11+
// const { cssText } = mutation.target.style;
12+
const locale_value = localStorage.getItem('locale') || 'en-US';
13+
console.log('locale_value::: ', locale_value);
14+
setValue(JSON.parse(locale_value));
15+
}
16+
}
17+
});
18+
//@ts-ignore
19+
observer.observe(targetElement, {
20+
attributes: true,
21+
attributeFilter: ['data-locale'], // 仅观察 'style' 属性
22+
});
23+
const locale_value = localStorage.getItem('locale') || 'en-US';
24+
25+
setValue(JSON.parse(locale_value));
26+
}, []);
27+
28+
return value || 'en-US';
29+
};
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
import { useState, useEffect } from 'react';
22

3-
const KEY = 'theme';
43

54
const getThemeByAlgo = () => {
65
const algo = localStorage.getItem('algorithm');
76
if (algo === 'darkAlgorithm') {
8-
return 'dark';
7+
return 'darkAlgorithm';
98
}
10-
return 'light';
9+
return 'defaultAlgorithm';
1110
};
1211

1312
const getThemeByDumi = () => {
1413
return localStorage.getItem('dumi:prefers-color');
1514
};
1615
export const useTheme = () => {
17-
const [value, setValue] = useState('');
16+
const [value, setValue] = useState("defaultAlgorithm");
1817

1918
useEffect(() => {
2019
const targetElement = document.querySelector('html'); // 目标元素
2120
const observer = new MutationObserver((mutationsList, observer) => {
2221
for (let mutation of mutationsList) {
23-
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
22+
if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
2423
// const { cssText } = mutation.target.style;
25-
const theme_value = localStorage.getItem('theme') || getThemeByDumi() || getThemeByAlgo() || 'light';
26-
setValue(theme_value);
24+
const theme_value = localStorage.getItem('algorithm') || 'defaultAlgorithm';
25+
setValue(JSON.parse(theme_value));
2726
}
2827
}
2928
});
3029
//@ts-ignore
3130
observer.observe(targetElement, {
3231
attributes: true,
33-
attributeFilter: ['style'], // 仅观察 'style' 属性
32+
attributeFilter: ['data-theme'], // 仅观察 'style' 属性
3433
});
35-
const theme_value = localStorage.getItem('theme') || getThemeByDumi() || getThemeByAlgo() || 'light';
34+
const theme_value = localStorage.getItem('algorithm') || 'defaultAlgorithm';
3635

37-
setValue(theme_value);
36+
setValue(JSON.parse(theme_value));
3837
}, []);
3938

40-
return value || 'light';
39+
return value || 'defaultAlgorithm';
4140
};

packages/studio-components/src/Provider/getThemeConfig.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const getThemeConfig = (algorithm: ThemeProviderType['algorithm']) => {
4646
colorBorder: isLight ? '#F0F0F0' : '#303030',
4747
colorBgBase: isLight ? '#fff' : '#1d1d1d',
4848
colorBgLayout: isLight ? '#f5f7f9' : 'rgba(43,43,43,1)',
49+
fontFamily: "'Geist Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
4950
};
5051
return { componentsConfig, tokenConfig };
5152
};

packages/studio-components/src/Provider/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { useStudioProvier } from './useThemeConfigProvider.tsx';
1010
import { components, token } from './const.ts';
1111
/** 修改主题色 */
1212
const ToogleButton = () => {
13-
const { handleThemeOrLocale } = useStudioProvier();
13+
const { updateStudio } = useStudioProvier();
1414
return (
1515
<Button
1616
onClick={() => {
17-
handleThemeOrLocale({
17+
updateStudio({
1818
components,
1919
token,
2020
});

packages/studio-components/src/Provider/index.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import type { ThemeProviderType } from './useThemeConfigProvider';
66
import { storage } from '../Utils';
77
import { getThemeConfig } from './getThemeConfig';
88
import { useCustomToken } from './useCustomToken';
9+
import { useTheme, useLocale } from '../HomePage/Hooks';
910

11+
type AlgorithmType = 'defaultAlgorithm' | 'darkAlgorithm';
1012
type IThemeProvider = {
1113
locales: {
1214
'zh-CN': Record<string, string>;
1315
'en-US': Record<string, string>;
1416
};
1517
children: React.ReactNode;
1618
locale?: 'zh-CN' | 'en-US';
17-
algorithm?: 'defaultAlgorithm' | 'darkAlgorithm';
19+
algorithm?: AlgorithmType;
1820
};
1921

2022
const Provider: React.FC<IThemeProvider> = props => {
@@ -47,13 +49,39 @@ const Provider: React.FC<IThemeProvider> = props => {
4749
const { componentsConfig, tokenConfig } = getThemeConfig(algorithm);
4850
const colorConfig = useCustomToken();
4951
const isLight = algorithm === 'defaultAlgorithm';
52+
const currentTheme = useTheme();
53+
const currentLocale = useLocale();
5054

51-
const handleThemeOrLocale = (themeConfig: Partial<ThemeProviderType>) => {
55+
useEffect(() => {
56+
setState(preState => {
57+
return {
58+
...preState,
59+
algorithm: currentTheme as AlgorithmType,
60+
locale: currentLocale as 'zh-CN' | 'en-US',
61+
};
62+
});
63+
}, [currentTheme, currentLocale]);
64+
65+
useEffect(() => {
66+
const theme = storage.get<AlgorithmType>('algorithm');
67+
const locale = storage.get<'zh-CN' | 'en-US'>('locale');
68+
document.documentElement.setAttribute('data-theme', theme || 'defaultAlgorithm');
69+
document.documentElement.setAttribute('data-locale', locale || 'en-US');
70+
}, []);
71+
72+
const updateStudio = (themeConfig: Partial<ThemeProviderType>) => {
73+
console.log('themeConfig::: ', themeConfig);
5274
const { components, token } = themeConfig;
5375
Object.keys(themeConfig).forEach(key => {
5476
storage.set(key, themeConfig[key]);
55-
});
77+
if (key === 'algorithm') {
78+
document.documentElement.setAttribute('data-theme', themeConfig.algorithm || 'defaultAlgorithm');
79+
}
5680

81+
if (key === 'locale') {
82+
document.documentElement.setAttribute('data-locale', themeConfig.locale || 'en-US');
83+
}
84+
});
5785
setState(preState => {
5886
// 特殊化处理,切token数据需初始化数据做基础
5987
storage.set('token', { ...preState.token, ...token });
@@ -74,7 +102,7 @@ const Provider: React.FC<IThemeProvider> = props => {
74102
value={{
75103
token: { ...tokenConfig, ...token },
76104
components: { ...componentsConfig, ...components },
77-
handleThemeOrLocale,
105+
updateStudio,
78106
algorithm,
79107
locale: locale,
80108
isLight,

packages/studio-components/src/Provider/useThemeConfigProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ export interface ThemeProviderType extends IColorStore {
88
isLight?: boolean;
99
}
1010
export interface IContainerContext extends ThemeProviderType {
11-
handleThemeOrLocale: (value: ThemeProviderType) => void;
11+
updateStudio: (value: ThemeProviderType) => void;
1212
}
1313
export const ContainerContext = createContext<IContainerContext>({
1414
components: {},
1515
token: {},
16-
handleThemeOrLocale: ({}) => {},
16+
updateStudio: ({}) => {},
1717
locale: 'en-US',
1818
algorithm: 'defaultAlgorithm',
1919
isLight: false,

packages/studio-components/src/layout/index.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Outlet } from 'react-router-dom';
33
import Section from '../Section';
44
import Header from './header';
55
import Sidebar from './sidebar';
6-
import { Button, Space, theme } from 'antd';
6+
import { Button, Space, theme, Tooltip } from 'antd';
77
import { ReadOutlined, GithubOutlined } from '@ant-design/icons';
88
import { getCurrentNav } from '../Utils';
99

@@ -17,6 +17,7 @@ interface ILayoutProps {
1717
style?: React.CSSProperties;
1818
collapsedConfig?: Record<string, boolean>;
1919
onMenuClick?: (currentNav: string) => void;
20+
headerSlot?: React.ReactNode | null;
2021
}
2122

2223
const Layout: React.FunctionComponent<ILayoutProps> = props => {
@@ -27,7 +28,9 @@ const Layout: React.FunctionComponent<ILayoutProps> = props => {
2728
style = {},
2829
collapsedConfig = {},
2930
onMenuClick,
31+
headerSlot=null,
3032
} = props;
33+
3134
const { width = 220, collapsedWidth = 56 } = sideStyle;
3235
const activeKey = getCurrentNav();
3336
const { token } = theme.useToken();
@@ -61,20 +64,25 @@ const Layout: React.FunctionComponent<ILayoutProps> = props => {
6164
<div id="header-breadcrumb"></div>
6265
</Space>
6366
<Space>
64-
<Button
65-
onClick={() => {
66-
window.open('https://graphscope.io/', '_blank');
67-
}}
68-
icon={<ReadOutlined />}
69-
type="text"
70-
></Button>
71-
<Button
72-
onClick={() => {
73-
window.open(github, '_blank');
74-
}}
75-
icon={<GithubOutlined />}
76-
type="text"
77-
></Button>
67+
{headerSlot}
68+
<Tooltip title="访问 GraphScope 官网">
69+
<Button
70+
onClick={() => {
71+
window.open('https://graphscope.io/', '_blank');
72+
}}
73+
icon={<ReadOutlined />}
74+
type="text"
75+
></Button>
76+
</Tooltip>
77+
<Tooltip title="GraphScope Github">
78+
<Button
79+
onClick={() => {
80+
window.open(github, '_blank');
81+
}}
82+
icon={<GithubOutlined />}
83+
type="text"
84+
></Button>
85+
</Tooltip>
7886
</Space>
7987
</Header>
8088
<Outlet />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from "react";
2+
3+
const SvgIcon: React.FC<React.SVGProps<SVGElement>> = (props) => (
4+
<svg
5+
xmlns="http://www.w3.org/2000/svg"
6+
width="18"
7+
height="18"
8+
className="icon"
9+
viewBox="0 0 1024 1024"
10+
>
11+
<path d="M335.815 79.644 512 512h-86.244l-48.07-117.93H213.901L165.888 512H79.644L255.83 79.644zm-39.993 113.38-49.948 122.425h99.84zm-22.414 449.65V710.2c0 1.024.398 1.934.398 2.958 0 .967.342 1.934.342 2.901 0 1.024.34 2.105.568 3.13.171.966.285 1.934.57 2.844.227 1.024.568 1.99.853 2.958l.91 2.958 1.138 2.73 1.251 2.732c.455.91.797 1.877 1.309 2.787.455.853 1.137 1.707 1.65 2.56.511.853.853 1.707 1.422 2.56l1.877 2.617c.569.796 1.138 1.593 1.82 2.39l1.992 2.389c.682.796 1.479 1.536 2.218 2.275l2.162 2.219a63.4 63.4 0 0 0 7.339 5.803l2.73 1.706a62 62 0 0 0 5.519 3.13c.91.454 1.763.966 2.73 1.364l2.902 1.195 2.958 1.024a74 74 0 0 0 9.33 2.617c1.024.228 2.048.683 3.128.683 1.024 0 2.048.341 3.072.341 1.081 0 2.276.341 3.3.341l5.12.171h102.969v64.455H342.016c-2.276 0-4.551-.17-6.77-.17-2.275 0-4.55-.342-6.77-.57a142 142 0 0 1-26.34-4.835c-2.16-.569-4.266-1.308-6.428-1.991a140 140 0 0 1-12.401-4.836c-2.048-.91-4.096-1.934-6.03-2.901a138 138 0 0 1-5.86-3.072c-1.934-1.138-3.812-2.105-5.689-3.3-1.877-1.137-3.698-2.503-5.461-3.754-1.82-1.252-3.584-2.446-5.348-3.812a134 134 0 0 1-5.12-4.096c-1.65-1.422-3.129-3.015-4.722-4.494-1.593-1.48-3.242-2.844-4.778-4.437a131 131 0 0 1-4.38-4.836c-1.423-1.65-2.675-3.3-3.983-5.006-1.365-1.707-2.844-3.413-4.096-5.12-1.252-1.764-2.276-3.527-3.413-5.348a126 126 0 0 1-3.357-5.518 130 130 0 0 1-8.249-17.351c-.74-1.934-1.536-3.982-2.161-5.973a124 124 0 0 1-4.38-18.603c-.342-2.048-.57-4.096-.74-6.144-.228-2.105-.57-4.267-.57-6.372l-.17-6.371v-64.455zM685.056 256l6.827.17c2.218 0 4.494.285 6.713.57a142.3 142.3 0 0 1 26.34 4.835 137.5 137.5 0 0 1 18.83 6.827c2.047.853 4.095 1.877 6.086 2.844l5.86 3.13c1.934 1.137 3.755 2.104 5.689 3.299 1.82 1.137 3.64 2.503 5.461 3.754 1.764 1.252 3.584 2.447 5.29 3.812l5.12 4.096c1.708 1.422 3.187 2.958 4.78 4.494 1.592 1.48 3.242 2.845 4.721 4.437a117 117 0 0 1 4.438 4.836c1.422 1.593 2.673 3.3 3.982 4.95 1.365 1.706 2.844 3.413 4.039 5.176 1.252 1.707 2.276 3.527 3.47 5.348a125.6 125.6 0 0 1 11.549 22.812c.74 1.991 1.536 3.982 2.161 6.03a131 131 0 0 1 4.38 18.546c.342 2.105.57 4.096.74 6.258.228 2.048.57 4.21.57 6.315l.17 6.371v64.455h-68.608v-67.641c0-1.024-.341-2.218-.341-3.242s-.399-2.048-.399-3.072c0-1.081-.284-2.105-.568-3.13a58 58 0 0 0-1.593-6.2l-1.138-2.844c-.341-1.024-.797-1.992-1.252-2.959l-1.251-2.958-1.48-2.901a66 66 0 0 0-1.706-2.73l-1.764-2.618-2.048-2.56-1.99-2.56a69 69 0 0 0-2.22-2.446l-2.332-2.219-2.39-2.275a69 69 0 0 0-2.56-1.991l-2.787-1.935c-.853-.568-1.763-1.308-2.73-1.877l-2.731-1.707a69 69 0 0 0-2.958-1.536c-.967-.512-2.048-.91-3.072-1.365a61 61 0 0 0-6.258-2.446l-3.072-1.024a75 75 0 0 0-6.6-1.536c-1.137-.228-2.16-.569-3.299-.569-1.137 0-2.161-.284-3.3-.284-1.137 0-2.161-.342-3.299-.342H582.258V256zm48.128 292.181H870.4v225.622H733.184V870.4h-68.608v-96.597H527.36V548.18h137.216v-64.455h68.608zm-68.608 64.455h-68.608v96.712h68.608zm137.216 0h-68.608v96.712h68.608z"></path>
12+
</svg>
13+
);
14+
15+
export default SvgIcon;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from "react";
2+
3+
const SvgIcon: React.FC<React.SVGProps<SVGElement>> = (props) => (
4+
<svg
5+
xmlns="http://www.w3.org/2000/svg"
6+
width="18"
7+
height="18"
8+
className="icon"
9+
viewBox="0 0 1024 1024"
10+
>
11+
<path d="m770.617 545.963 151.097 375.58h-74.126l-41.131-102.4H666.055l-41.187 102.4h-73.615l150.642-375.58h68.779zm-34.133 98.759-42.838 106.04h85.618l-42.837-106.04zM324.722 716.8v74.524c0 1.081.398 2.22.398 3.243 0 1.138.796 2.276.796 3.3s.399 1.82.399 2.844c0 1.081.853 2.276.853 3.3s1.252 1.877 1.252 2.844c0 1.024.796 2.276.796 3.3s.796 1.934 1.252 2.844c.455 1.024 1.137 1.934 1.65 2.845.454 1.024.682 1.99 1.25 2.9.513.968 1.025 1.594 1.594 2.447.569.91 1.479 2.048 2.105 2.845.569.91.967 1.706 1.65 2.503.625.853 1.365 1.65 2.047 2.446.683.853 1.309 1.707 2.048 2.503l2.447 2.446 2.105 2.048 2.446 2.048c.853.626 1.593 1.422 2.446 2.048.91.57 1.991 1.024 2.901 1.65.91.569 1.536 1.08 2.447 1.593l2.9 1.707c.968.455 1.935 1.137 2.845 1.592l2.902 1.195c1.024.455 1.877 1.252 2.901 1.252s2.276.796 3.3.796 1.877 1.252 2.844 1.252c1.08 0 2.276.398 3.356.398 1.024 0 1.82.853 2.845.853 1.08 0 2.275.398 3.3.398 1.08 0 2.275.399 3.299.399h107.861V921.6H386.503c-2.275 0-4.323-.796-6.599-.796-2.219 0-4.779-.797-6.997-.797a33.7 33.7 0 0 1-6.6-.853c-2.161-.398-4.38-1.081-6.542-1.65-2.218-.569-4.039-.967-6.2-1.593-2.162-.683-4.495-1.308-6.6-2.048a116 116 0 0 1-6.144-2.503c-2.105-.853-4.153-1.877-6.2-2.844-1.992-.968-4.21-1.82-6.145-2.845a132 132 0 0 1-11.548-6.997c-1.877-1.252-3.527-2.73-5.348-4.096a138 138 0 0 1-5.347-4.096c-1.707-1.422-3.698-2.56-5.348-4.096-1.706-1.48-2.958-3.3-4.55-4.893-1.594-1.593-3.414-2.844-4.95-4.55-1.48-1.594-2.674-3.585-4.096-5.291a142 142 0 0 1-4.096-5.348c-1.365-1.764-2.845-3.413-4.153-5.29a142 142 0 0 1-6.997-11.492c-1.024-1.934-1.935-4.096-2.845-6.144-.967-1.991-2.048-4.04-2.901-6.144-.853-2.048-1.707-3.982-2.446-6.144-.797-2.105-1.423-4.38-2.105-6.542-.626-2.105-1.081-3.983-1.65-6.144l-1.65-6.543a33.3 33.3 0 0 1-.796-6.542c0-2.275-.854-4.779-.854-6.997s-.796-4.324-.796-6.542V716.8h68.779zM736.37 307.2h6.599c2.275 0 4.38.796 6.599.796 2.276 0 4.779.854 6.997.854s4.38.398 6.6.796c2.161.455 4.38 1.138 6.542 1.65 2.218.569 4.039.967 6.2 1.65 2.162.626 4.495 1.308 6.6 2.048s4.096 1.593 6.144 2.446c2.105.853 4.153 1.934 6.2 2.844 2.049.968 4.21 1.82 6.145 2.845a131 131 0 0 1 11.548 6.997c1.877 1.252 3.527 2.788 5.348 4.096 1.82 1.366 3.64 2.674 5.347 4.096s3.698 2.617 5.348 4.096c1.706 1.536 2.958 3.357 4.55 4.95 1.594 1.536 3.414 2.844 4.95 4.494 1.48 1.65 2.674 3.584 4.096 5.29 1.422 1.707 2.788 3.528 4.096 5.348 1.365 1.82 2.845 3.413 4.153 5.348a144 144 0 0 1 6.997 11.434c1.024 1.991 1.935 4.153 2.845 6.144.967 2.048 2.048 4.096 2.901 6.144l2.503 6.144c.74 2.105 1.366 4.438 2.048 6.542.626 2.162 1.138 3.983 1.65 6.144.569 2.162 1.195 4.38 1.65 6.6a33.3 33.3 0 0 1 .796 6.542c0 2.218.854 4.722.854 6.94 0 2.276.796 4.324.796 6.542V512H804.75v-74.98c0-1.137-.455-2.161-.455-3.242 0-1.138-.796-2.219-.796-3.3 0-1.138-.854-2.162-.854-3.242 0-1.138-.796-2.276-.796-3.3 0-1.08-.853-2.276-.853-3.3s-.797-1.82-1.195-2.844c-.455-1.024-1.138-2.276-1.707-3.3-.455-.967-.682-1.877-1.138-2.844a64 64 0 0 0-1.706-2.844c-.57-.968-1.422-1.992-2.048-2.845-.626-.967-.967-2.048-1.65-2.901-.683-.91-1.365-1.593-2.048-2.446a90 90 0 0 0-2.503-2.845c-.74-.853-1.252-1.308-2.048-2.105l-2.446-2.446c-.854-.74-2.048-1.365-2.845-2.048-.91-.74-1.593-1.82-2.503-2.446-.91-.683-1.991-1.024-2.844-1.65-.967-.626-1.991-1.48-2.959-2.048a66 66 0 0 0-2.844-1.65c-1.024-.569-1.877-.74-2.844-1.194-1.081-.512-1.878-1.252-2.959-1.707-1.024-.398-2.218-1.138-3.242-1.138-1.138 0-2.276-.853-3.3-.853-1.138 0-2.275-.854-3.3-.854-1.137 0-2.218-.796-3.299-.796-1.138 0-2.162-.796-3.3-.796s-2.161-.456-3.3-.456h-109.51V307.2h102.969zM341.276 85.333v85.334h170.667v298.666H341.276v128h-85.333v-128H85.276V170.667h170.667V85.333zM255.943 256H170.61v128H256V256zm170.667 0h-85.334v128h85.334z"></path>
12+
</svg>
13+
);
14+
15+
export default SvgIcon;

0 commit comments

Comments
 (0)