-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathWidgetPlugin.tsx
More file actions
97 lines (84 loc) · 3.78 KB
/
WidgetPlugin.tsx
File metadata and controls
97 lines (84 loc) · 3.78 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
import React from 'react';
import type {Plugin} from '@gravity-ui/dashkit';
import type {DashTabItemWidget} from 'shared';
import {CustomPaletteBgColors} from 'shared/constants';
import {isOldBackgroundSettings} from 'shared/utils';
import type {ChartWidgetWithWrapRefProps} from 'ui/components/Widgets/Chart/types';
import MarkdownProvider from '../../../../modules/markdownProvider';
import {ChartWrapper} from '../../../Widgets/Chart/ChartWidgetWithProvider';
import type {CommonPluginSettings} from '../../DashKit';
import {useWidgetContext} from '../../context/WidgetContext';
import {usePreparedWrapSettings} from '../../utils';
import {RendererWrapper} from '../RendererWrapper/RendererWrapper';
import type {WidgetPluginProps} from './types';
type Props = WidgetPluginProps;
type PluginWidgetObjectSettings = CommonPluginSettings;
type PluginWidget = Plugin<Props> &
CommonPluginSettings & {
setSettings: (settings: PluginWidgetObjectSettings) => PluginWidget;
};
const widgetPlugin: PluginWidget = {
type: 'widget',
defaultLayout: {w: 12, h: 12},
setSettings: (settings: PluginWidgetObjectSettings) => {
widgetPlugin.scope = settings.scope;
widgetPlugin.globalWidgetSettings = settings.globalWidgetSettings;
return widgetPlugin;
},
renderer: function Wrapper(
props: Props,
forwardedRef?: React.RefCallback<ChartWidgetWithWrapRefProps>,
) {
const rootNodeRef = React.useRef<HTMLDivElement>(null);
const {onWidgetLoadData} = useWidgetContext({
id: props.id,
elementRef: rootNodeRef,
});
// @ts-expect-error TS2352: hideTitle is required in DashTabItemWidget['data'].
const data = props.data as DashTabItemWidget['data'];
const workbookId = props.context.workbookId;
const enableAssistant = props.context.enableAssistant;
const propsBg = data.tabs?.[0]?.background;
let oldWidgetBg = isOldBackgroundSettings(propsBg) ? propsBg : undefined;
if (widgetPlugin.scope === 'dash' && !data.backgroundSettings) {
oldWidgetBg = {color: CustomPaletteBgColors.LIKE_CHART};
}
const {style, hasInternalMargins: hasInternalMarginsComputed} = usePreparedWrapSettings({
ownWidgetSettings: {
background: oldWidgetBg,
backgroundSettings: data.backgroundSettings,
borderRadius: data.borderRadius,
internalMarginsEnabled: data.internalMarginsEnabled,
},
dashVisualSettings: {
background: undefined,
backgroundSettings: undefined,
widgetsSettings: widgetPlugin.globalWidgetSettings,
},
defaultOldColor:
widgetPlugin.scope === 'dash'
? CustomPaletteBgColors.LIKE_CHART
: CustomPaletteBgColors.NONE,
});
const hasInternalMargins =
(data.internalMarginsEnabled === undefined &&
widgetPlugin.globalWidgetSettings?.internalMarginsEnabled === undefined) ||
hasInternalMarginsComputed;
return (
<RendererWrapper type="widget" nodeRef={rootNodeRef} id={props.id} style={style}>
<ChartWrapper
{...props}
usageType="widget"
forwardedRef={forwardedRef}
getMarkdown={MarkdownProvider.getMarkdown}
workbookId={workbookId}
enableAssistant={enableAssistant}
onWidgetLoadData={onWidgetLoadData}
backgroundColor={style?.backgroundColor}
hasInternalMargins={hasInternalMargins}
/>
</RendererWrapper>
);
},
};
export default widgetPlugin;