tachybase_todo/packages/client/src/plugin-manager/PluginManager.tsx

99 lines
2.9 KiB
TypeScript
Raw Normal View History

import React, { createContext, useContext } from 'react';
import { Menu, Space, Tooltip, MenuItemProps } from 'antd';
2022-01-19 15:14:00 +08:00
import { SettingOutlined, MoreOutlined, DesktopOutlined } from '@ant-design/icons';
import { get } from 'lodash';
import { PluginManagerContext } from './context';
import cls from 'classnames';
import { ConfigProvider } from 'antd';
2022-01-19 15:14:00 +08:00
export const usePrefixCls = (
tag?: string,
props?: {
prefixCls?: string;
},
) => {
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
return getPrefixCls(tag, props?.prefixCls);
};
type PluginManagerType = {
Toolbar?: React.FC<ToolbarProps> & {
Item?: React.FC<MenuItemProps & { selected?: boolean }>;
};
};
export const PluginManager: PluginManagerType = () => null;
const ToolbarItemContext = createContext<ToolbarItemProps>(null);
2022-01-19 15:14:00 +08:00
interface ToolbarProps {
items?: ToolbarItemProps[];
}
2022-01-19 15:14:00 +08:00
interface ToolbarItemProps {
component: string;
pin?: boolean;
}
PluginManager.Toolbar = (props: ToolbarProps) => {
const { components } = useContext(PluginManagerContext);
const { items = [] } = props;
2022-01-19 15:14:00 +08:00
return (
<div style={{ display: 'inline-block' }}>
<Menu style={{ width: '100%' }} selectable={false} mode={'horizontal'} theme={'dark'}>
2022-01-19 15:14:00 +08:00
{items
.filter((item) => item.pin)
2022-01-21 14:04:15 +08:00
.map((item, index) => {
const Action = get(components, item.component);
return (
Action && (
2022-01-21 14:04:15 +08:00
<ToolbarItemContext.Provider key={index} value={item}>
<Action />
</ToolbarItemContext.Provider>
)
);
2022-01-19 15:14:00 +08:00
})}
<Menu.SubMenu key={'more'} title={<MoreOutlined />}>
{items
.filter((item) => !item.pin)
2022-01-21 14:04:15 +08:00
.map((item, index) => {
const Action = get(components, item.component);
return (
Action && (
2022-01-21 14:04:15 +08:00
<ToolbarItemContext.Provider key={index} value={item}>
<Action />
</ToolbarItemContext.Provider>
)
);
})}
2022-01-21 14:04:15 +08:00
<Menu.Divider key={'divider'}></Menu.Divider>
<Menu.Item key={'plugins'} disabled icon={<SettingOutlined />}>
</Menu.Item>
</Menu.SubMenu>
</Menu>
</div>
);
};
PluginManager.Toolbar.Item = (props) => {
const item = useContext(ToolbarItemContext);
const { selected, icon, title, ...others } = props;
const prefix = usePrefixCls();
const className = cls({ [`${prefix}-menu-item-selected`]: selected });
if (item.pin) {
return (
<Tooltip title={title}>
<Menu.Item {...others} className={className} eventKey={item.component}>
{icon}
</Menu.Item>
</Tooltip>
);
}
return (
<Menu.Item {...others} className={className} eventKey={item.component} icon={icon}>
{title}
</Menu.Item>
2022-01-19 15:14:00 +08:00
);
};