Skip to content

Commit 6ca2ee9

Browse files
authored
Merge pull request #1877 from merico-dev/panel-loaded-event
feat(dashboard): add panel loaded event and refactor viz instance API
2 parents 7dab4f6 + ad9c084 commit 6ca2ee9

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtable/api",
3-
"version": "14.60.5",
3+
"version": "14.60.6",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

dashboard/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtable/dashboard",
3-
"version": "14.60.5",
3+
"version": "14.60.6",
44
"license": "Apache-2.0",
55
"repository": {
66
"url": "https://github.com/merico-dev/table"

dashboard/src/components/plugins/viz-components/horizontal-bar-chart/viz-horizontal-bar-chart.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ function Chart({ conf, data, width, height, interactionManager, variables, onCha
5656
const echartsRef = React.useRef<EChartsInstance>();
5757
const onRenderFinishedRef = useLatest(onChartRenderFinished);
5858

59-
useEffect(() => {
60-
setTimeout(() => {
61-
onRenderFinishedRef.current?.(echartsRef.current?.getOption());
62-
}, 100);
63-
}, [option]);
64-
6559
const onEvents = useMemo(() => {
6660
return {
6761
click: handleSeriesClick,
@@ -76,6 +70,12 @@ function Chart({ conf, data, width, height, interactionManager, variables, onCha
7670
return getOption(conf, data, variables);
7771
}, [conf, data]);
7872

73+
useEffect(() => {
74+
setTimeout(() => {
75+
onRenderFinishedRef.current?.(echartsRef.current?.getOption());
76+
}, 100);
77+
}, [option]);
78+
7979
if (!width || !height) {
8080
return null;
8181
}

dashboard/src/components/plugins/viz-components/viz-instance-api.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { VizInstance } from '~/types/plugin';
21
import { IPanelInfo } from '~/components/plugins';
2+
import { VizInstance } from '~/types/plugin';
33

44
/**
55
* Emit viz rendered event
@@ -20,8 +20,21 @@ export interface IVizRenderedPayload {
2020
* @param callback
2121
*/
2222
export function onVizRendered(instance: VizInstance, callback: (data: unknown) => void) {
23-
instance.messageChannels.getChannel('viz').on('rendered', callback);
23+
const channel = instance.messageChannels.getChannel('viz');
24+
channel.on('rendered', callback);
25+
return () => {
26+
channel.off('rendered', callback);
27+
};
28+
}
29+
30+
export function notifyPanelLoaded(instance: VizInstance, data: unknown) {
31+
instance.messageChannels.getChannel('panel').emit('loaded', data);
32+
}
33+
34+
export function onPanelLoaded(instance: VizInstance, callback: (data: unknown) => void) {
35+
const channel = instance.messageChannels.getChannel('panel');
36+
channel.on('loaded', callback);
2437
return () => {
25-
instance.messageChannels.getChannel('viz').off('rendered', callback);
38+
channel.off('loaded', callback);
2639
};
2740
}

dashboard/src/components/plugins/viz-manager/components.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { omit } from 'lodash';
2+
import { useEffect } from 'react';
23
import { VizConfigContext, VizContext, VizInstance, VizViewContext } from '~/types/plugin';
34
import { ITemplateVariable } from '~/utils';
45
import { JsonPluginStorage } from '../json-plugin-storage';
56
import { IPanelInfo, IVizManager } from './types';
7+
import { notifyPanelLoaded } from '../viz-components/viz-instance-api';
68

79
function createCommonContext(
810
instance: VizInstance,
@@ -53,6 +55,9 @@ export const VizViewComponent = <T,>(props: IViewComponentProps<T>) => {
5355
...createCommonContext(instance, data, vizManager, variables),
5456
viewport: { width: measure.w, height: measure.h },
5557
};
58+
useEffect(() => {
59+
notifyPanelLoaded(instance, data);
60+
}, [data, instance]);
5661
const Comp = comp.viewRender;
5762
return <Comp context={context} instance={instance} {...omit(props, ['panel', 'vizManager', 'data'])} />;
5863
};

settings-form/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtable/settings-form",
3-
"version": "14.60.5",
3+
"version": "14.60.6",
44
"license": "Apache-2.0",
55
"repository": {
66
"url": "https://github.com/merico-dev/table"

0 commit comments

Comments
 (0)