Skip to content

Commit 18f1e12

Browse files
committed
feat(platform): add amap route
1 parent 7d8ca9f commit 18f1e12

File tree

30 files changed

+433
-188
lines changed

30 files changed

+433
-188
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
},
2626
"dependencies": {},
2727
"devDependencies": {
28+
"@amap/amap-jsapi-loader": "^1.0.1",
29+
"@amap/amap-jsapi-types": "^0.0.8",
2830
"@ant-design/icons-svg": "^4.2.1",
2931
"@commitlint/cli": "^17.1.2",
3032
"@commitlint/config-conventional": "^17.1.0",

packages/hooks/src/useForkRef.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isFunction, isObject } from 'lodash';
22
import { useCallback } from 'react';
33

4-
function setRef(ref: any, value: any): void {
4+
export function setRef(ref: any, value: any): void {
55
if (isFunction(ref)) {
66
ref(value);
77
} else if (isObject(ref) && 'current' in ref) {

packages/platform/src/app/Routes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import AppExceptionRoute from './routes/exception/Exception';
1717
import AppLayout from './routes/layout/Layout';
1818
import AppLoginRoute from './routes/login/Login';
1919

20-
const AppAMapRoute = React.lazy(() => import('./routes/dashboard/AMap'));
20+
const AppAMapRoute = React.lazy(() => import('./routes/dashboard/amap/AMap'));
2121
const AppEChartsRoute = React.lazy(() => import('./routes/dashboard/echarts/ECharts'));
2222

2323
const AppACLRoute = React.lazy(() => import('./routes/test/acl/ACL'));
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import AMapLoader from '@amap/amap-jsapi-loader';
2+
import { isNull, isUndefined } from 'lodash';
3+
import React, { useCallback, useRef } from 'react';
4+
5+
import { setRef } from '@react-devui/hooks/useForkRef';
6+
import { getClassName } from '@react-devui/utils';
7+
8+
const CONFIG: {
9+
key?: string;
10+
securityJsCode?: string | null;
11+
version?: string;
12+
} = {
13+
version: '2.0',
14+
};
15+
16+
export interface AppAMapProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
17+
aKey?: string;
18+
aSecurityJsCode?: string | null;
19+
aVersion?: string;
20+
aPlugins?: string[];
21+
aOptions?: AMap.MapOptions;
22+
}
23+
24+
function AMap(props: AppAMapProps, ref: React.ForwardedRef<AMap.Map>): JSX.Element | null {
25+
const {
26+
aKey = CONFIG.key,
27+
aSecurityJsCode = CONFIG.securityJsCode,
28+
aVersion = CONFIG.version,
29+
aPlugins,
30+
aOptions,
31+
32+
...restProps
33+
} = props;
34+
35+
//#region Ref
36+
const elRef = useRef<HTMLDivElement>(null);
37+
//#endregion
38+
39+
const instanceRef = useRef<AMap.Map | null>(null);
40+
const containerRef = useCallback(
41+
(el: HTMLElement | null) => {
42+
instanceRef.current?.destroy();
43+
instanceRef.current = null;
44+
setRef(ref, instanceRef.current);
45+
46+
if (el) {
47+
if (isUndefined(aKey) || isUndefined(aSecurityJsCode) || isUndefined(aVersion)) {
48+
throw new Error('Configs of amap is required!');
49+
}
50+
51+
if (!isNull(aSecurityJsCode)) {
52+
window['_AMapSecurityConfig'] = {
53+
// serviceHost: 'http://1.1.1.1:80/_AMapService',
54+
55+
securityJsCode: aSecurityJsCode,
56+
};
57+
}
58+
59+
AMapLoader.load({
60+
key: aKey,
61+
version: aVersion,
62+
plugins: aPlugins,
63+
})
64+
.then((AMap) => {
65+
instanceRef.current = new AMap.Map(el, aOptions);
66+
setRef(ref, instanceRef.current);
67+
})
68+
.catch((e) => {
69+
console.error(e);
70+
});
71+
}
72+
},
73+
[aKey, aOptions, aPlugins, aSecurityJsCode, aVersion, ref]
74+
);
75+
76+
return (
77+
<div {...restProps} ref={elRef} className={getClassName(restProps.className, 'app-amap')}>
78+
<div ref={containerRef} className="app-amap__container"></div>
79+
</div>
80+
);
81+
}
82+
83+
export const AppAMap = React.forwardRef(AMap);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './AMap';

packages/platform/src/app/components/chart/Chart.tsx

Lines changed: 0 additions & 65 deletions
This file was deleted.

packages/platform/src/app/components/chart/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import type { AppTheme } from '../../App';
2+
3+
import * as echarts from 'echarts';
4+
import { cloneDeep, merge } from 'lodash';
5+
import React, { useCallback, useEffect, useRef, useState } from 'react';
6+
7+
import { useAsync, useResize, useStorage } from '@react-devui/hooks';
8+
import { setRef } from '@react-devui/hooks/useForkRef';
9+
import { getClassName } from '@react-devui/utils';
10+
11+
import { STORAGE_KEY } from '../../../config/storage';
12+
import chartTheme from './theme.json';
13+
14+
echarts.registerTheme('light', chartTheme.light);
15+
echarts.registerTheme('dark', merge(cloneDeep(chartTheme.light), chartTheme.dark));
16+
17+
export interface AppEChartsProps<O extends echarts.EChartsOption> extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
18+
aOption: O | null;
19+
}
20+
21+
function ECharts<O extends echarts.EChartsOption>(props: AppEChartsProps<O>, ref: React.ForwardedRef<echarts.ECharts>): JSX.Element | null {
22+
const {
23+
aOption,
24+
25+
...restProps
26+
} = props;
27+
28+
//#region Ref
29+
const elRef = useRef<HTMLDivElement>(null);
30+
//#endregion
31+
32+
const dataRef = useRef<{
33+
clearTid?: () => void;
34+
}>({});
35+
36+
const async = useAsync();
37+
38+
const themeStorage = useStorage<AppTheme>(...STORAGE_KEY.theme);
39+
40+
const [instance, setInstance] = useState<echarts.ECharts | null>(null);
41+
const containerRef = useCallback(
42+
(el: HTMLElement | null) => {
43+
setInstance((draft) => {
44+
draft?.dispose();
45+
const instance = el ? echarts.init(el, themeStorage.value, { renderer: 'svg' }) : null;
46+
setRef(ref, instance);
47+
return instance;
48+
});
49+
},
50+
[ref, themeStorage.value]
51+
);
52+
53+
useEffect(() => {
54+
if (instance && aOption) {
55+
instance.setOption(aOption);
56+
}
57+
}, [aOption, instance]);
58+
59+
useResize(elRef, () => {
60+
if (instance) {
61+
dataRef.current.clearTid?.();
62+
dataRef.current.clearTid = async.setTimeout(() => {
63+
dataRef.current.clearTid = undefined;
64+
instance.resize({ animation: { duration: 200 } });
65+
}, 100);
66+
}
67+
});
68+
69+
return (
70+
<div {...restProps} ref={elRef} className={getClassName(restProps.className, 'app-echarts')}>
71+
<div ref={containerRef} className="app-echarts__container"></div>
72+
</div>
73+
);
74+
}
75+
76+
export const AppECharts = React.forwardRef(ECharts);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ECharts';

packages/platform/src/app/components/chart/theme.json renamed to packages/platform/src/app/components/echarts/theme.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@
154154
"breadcrumb": {
155155
"itemStyle": {
156156
"color": "var(--d-color-icon-decorator)"
157+
},
158+
"emphasis": {
159+
"itemStyle": {
160+
"color": "var(--d-color-primary-lighter)"
161+
}
157162
}
158163
}
159164
},
@@ -283,7 +288,7 @@
283288
},
284289
"emphasis": {
285290
"iconStyle": {
286-
"borderColor": "var(--d-text-color)"
291+
"borderColor": "var(--d-color-primary-lighter)"
287292
}
288293
}
289294
},

0 commit comments

Comments
 (0)