Skip to content

Commit

Permalink
feat: 优化组件加载
Browse files Browse the repository at this point in the history
  • Loading branch information
JackySoft authored and jianbing.chen committed Dec 18, 2024
1 parent e03cffd commit 28b2966
Show file tree
Hide file tree
Showing 84 changed files with 291 additions and 221 deletions.
4 changes: 2 additions & 2 deletions packages/editor/src/components/SetterRender/SetterRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const SetterRender = memo(({ attrs, form }: IAttrs) => {
if (item.type == 'Title') {
if (item.popover) {
return (
<Popover title={item.popover?.title} content={item.popover.content} placement={item.popover.placement || 'left'}>
<h2 className={styles.title} key={key}>
<Popover title={item.popover?.title} content={item.popover.content} placement={item.popover.placement || 'left'} key={key}>
<h2 className={styles.title}>
<span style={{ marginRight: 10 }}>{item.label}</span>
<QuestionCircleOutlined />
</h2>
Expand Down
32 changes: 20 additions & 12 deletions packages/editor/src/config/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
export interface SysComItem {
type: string;
title: string;
hidden?: boolean;
data: Array<{
icon: string;
name: string;
type: string;
hidden?: boolean;
}>;
}
const components = [
{
type: 'Page',
title: '页面组件',
hidden: true,
data: [],
},
{
type: 'Scene',
title: '场景组件',
Expand All @@ -28,7 +36,7 @@ const components = [
],
},
{
type: 'Layouts',
type: 'Container',
title: '容器组件',
data: [
{
Expand All @@ -54,7 +62,7 @@ const components = [
],
},
{
type: 'LayoutComp',
type: 'Layout',
title: '布局组件',
data: [
{
Expand All @@ -80,7 +88,7 @@ const components = [
],
},
{
type: 'Form',
type: 'FormItems',
title: '表单组件',
data: [
{
Expand Down Expand Up @@ -201,7 +209,7 @@ const components = [
],
},
{
type: 'Echarts',
type: 'Echart',
title: '图表组件',
data: [
{
Expand Down Expand Up @@ -281,6 +289,12 @@ const components = [
name: '标签页',
type: 'Tabs',
},
{
icon: '',
name: '子标签页',
type: 'Tab',
hidden: true,
},
],
},
{
Expand Down Expand Up @@ -310,7 +324,7 @@ const components = [
],
},
{
type: 'BasicComp',
type: 'Basic',
title: '基础组件',
data: [
{
Expand Down Expand Up @@ -361,20 +375,14 @@ const components = [
],
},
{
type: 'Map',
type: 'Other',
title: '地图',
data: [
{
icon: '',
name: 'BMap',
type: 'BMap',
},
],
},
{
type: 'MicroApp',
title: '微服务',
data: [
{
icon: '',
name: 'IFrame',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Suspense, lazy, memo, useEffect, useState } from 'react';
import { ConfigProvider, Form, Tabs } from 'antd';
import type { TabsProps } from 'antd';
import { useDebounceFn } from 'ahooks';
import * as Components from '@/packages/index';
import { usePageStore } from '@/stores/pageStore';
import { CheckOutlined, CopyOutlined } from '@ant-design/icons';
import { message } from '@/utils/AntdGlobal';
import { defaultsDeep } from 'lodash-es';
import copy from 'copy-to-clipboard';
import SpinLoading from '@/components/SpinLoading';
import { getComponent } from '@/packages/index';
import styles from './index.module.less';

// 属性设置器
Expand Down Expand Up @@ -54,15 +54,15 @@ const ConfigPanel = memo(() => {
});
} else {
// 生成组件
const item: any = Components[(selectedElement.type + 'Config') as keyof typeof Components];
const item: any = getComponent(selectedElement.type + 'Config');
setComponentConfig(item);
// defaults是为了继承页面中新增的配置项
form.setFieldsValue(elementsMap[selectedElement.id]?.config.props);
}
form.setFieldValue('id', selectedElement.id);
} else {
// 获取页面配置
const item: any = Components['PageConfig' as keyof typeof Components];
const item: any = getComponent('PageConfig');
setComponentConfig(item);
// defaults是为了继承页面中新增的配置项
form.setFieldsValue({ pageName, ...defaultsDeep({ ...pageProps }, item.config.props) });
Expand Down
24 changes: 13 additions & 11 deletions packages/editor/src/layout/components/Menu/ComponentPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ const ComponentPanel = () => {
const [list, setList] = useState<Array<{ key: string; label: string; children: JSX.Element }>>([]);
useEffect(() => {
// 系统自带组件
const items: Array<{ key: string; label: string; children: React.JSX.Element }> = (keyword ? searchByName(components, keyword) : components).map(
(item: SysComItem) => {
const items: Array<{ key: string; label: string; children: React.JSX.Element }> = (keyword ? searchByName(components, keyword) : components)
.filter((item: SysComItem) => !item.hidden)
.map((item: SysComItem) => {
return {
key: item.type,
label: item.title,
children: (
<Row gutter={[10, 10]}>
{item.data.map((subItem) => {
return (
<Col span={12} key={subItem.type}>
<DragMenuItem {...subItem} />
</Col>
);
})}
{item.data
.filter((sub) => !sub.hidden)
.map((subItem) => {
return (
<Col span={12} key={subItem.type}>
<DragMenuItem {...subItem} />
</Col>
);
})}
</Row>
),
};
},
);
});
setActiveKeys(items.map((item) => item.key));
setList(items);
}, [keyword]);
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/layout/components/Menu/DragMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState } from 'react';
import { DragSourceMonitor, useDrag } from 'react-dnd';
import { IDragTarget } from '@/packages/types/index';
import { checkComponentType, createId } from '@/utils/util';
import * as Components from '@/packages/index';
import { getComponent } from '@/packages/index';
import { usePageStore } from '@/stores/pageStore';
import styles from './index.module.less';
import { message } from '@/utils/AntdGlobal';
Expand Down Expand Up @@ -44,15 +44,15 @@ const DragMenuItem = (props: IDragTarget) => {

const handleClick = (item: IDragTarget) => {
// 生成默认配置
const { config, events, methods = [], elements = [] }: any = Components[(item.type + 'Config') as keyof typeof Components] || {};
const { config, events, methods = [], elements = [] }: any = getComponent(item.type + 'Config') || {};
const newId = createId(item.type);
if (!checkComponentType(item.type, selectedElement?.id, selectedElement?.type, elementsMap)) {
message.info('请把表单项放在Form容器内');
return;
}
const childElement =
elements.map((child: IDragTarget & { id: string }) => {
const { config, events, methods = [] }: any = Components[(child.type + 'Config') as keyof typeof Components] || {};
const { config, events, methods = [] }: any = getComponent(child.type + 'Config') || {};
return {
id: child.id || createId(child.type),
name: child.name,
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/Avatar/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/CountDown/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/Icon/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/Image/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/Link/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/QRCode/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/Statistic/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/Text/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/Basic/Title/index.ts

This file was deleted.

32 changes: 23 additions & 9 deletions packages/editor/src/packages/Basic/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
export { Image, ImageConfig } from './Image';
export { Text, TextConfig } from './Text';
export { Title, TitleConfig } from './Title';
export { Link, LinkConfig } from './Link';
export { Icon, IconConfig } from './Icon';
export { Avatar, AvatarConfig } from './Avatar';
export { Statistic, StatisticConfig } from './Statistic';
export { CountDown, CountDownConfig } from './CountDown';
export { QRCode, QRCodeConfig } from './QRCode';
import { lazy } from 'react';

export { default as ImageConfig } from './Image/Schema';
export { default as TextConfig } from './Text/Schema';
export { default as TitleConfig } from './Title/Schema';
export { default as LinkConfig } from './Link/Schema';
export { default as IconConfig } from './Icon/Schema';
export { default as AvatarConfig } from './Avatar/Schema';
export { default as StatisticConfig } from './Statistic/Schema';
export { default as CountDownConfig } from './CountDown/Schema';
export { default as QRCodeConfig } from './QRCode/Schema';

const Image = lazy(() => import('./Image/Image'));
const Text = lazy(() => import('./Text/Text'));
const Title = lazy(() => import('./Title/Title'));
const Link = lazy(() => import('./Link/Link'));
const Icon = lazy(() => import('./Icon/Icon'));
const Avatar = lazy(() => import('./Avatar/Avatar'));
const Statistic = lazy(() => import('./Statistic/Statistic'));
const CountDown = lazy(() => import('./CountDown/CountDown'));
const QRCode = lazy(() => import('./QRCode/QRCode'));

export { Image, Text, Title, Link, Icon, Avatar, Statistic, CountDown, QRCode };
4 changes: 2 additions & 2 deletions packages/editor/src/packages/Container/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentType, IDragTargetItem } from '@/packages/types';
import { Button, Card } from 'antd';
import { useDrop } from 'react-dnd';
import * as Components from '@/packages/index';
import { getComponent } from '@/packages/index';
import MarsRender from '@/packages/MarsRender/MarsRender';
import { usePageStore } from '@/stores/pageStore';
import { forwardRef, useImperativeHandle, useState } from 'react';
Expand All @@ -20,7 +20,7 @@ const MCard = ({ id, type, config, elements, onClick }: ComponentType, ref: any)
drop(item: IDragTargetItem, monitor) {
if (monitor.didDrop()) return;
// 生成默认配置
const { config, events, methods = [] }: any = Components[(item.type + 'Config') as keyof typeof Components] || {};
const { config, events, methods = [] }: any = getComponent(item.type + 'Config') || {};
addChildElements({
type: item.type,
name: item.name,
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/src/packages/Container/Card/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/editor/src/packages/Container/Div/Div.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentType, IDragTargetItem } from '@/packages/types';
import { useDrop } from 'react-dnd';
import * as Components from '@/packages/index';
import { getComponent } from '@/packages/index';
import MarsRender from '@/packages/MarsRender/MarsRender';
import { usePageStore } from '@/stores/pageStore';
import { forwardRef, useImperativeHandle, useState } from 'react';
Expand All @@ -24,7 +24,7 @@ const Div = ({ id, type, config, elements }: ComponentType, ref: any) => {
drop(item: IDragTargetItem, monitor) {
if (monitor.didDrop()) return;
// 生成默认配置
const { config, events, methods = [] }: any = Components[(item.type + 'Config') as keyof typeof Components] || {};
const { config, events, methods = [] }: any = getComponent(item.type + 'Config') || {};
addChildElements({
type: item.type,
name: item.name,
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/src/packages/Container/Div/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/editor/src/packages/Container/Flex/Flex.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentType, IDragTargetItem } from '@/packages/types';
import { Flex } from 'antd';
import { useDrop } from 'react-dnd';
import * as Components from '@/packages/index';
import { getComponent } from '@/packages/index';
import MarsRender from '@/packages/MarsRender/MarsRender';
import { usePageStore } from '@/stores/pageStore';
import { forwardRef, useImperativeHandle, useState } from 'react';
Expand All @@ -25,7 +25,7 @@ const MFlex = ({ id, type, config, elements }: ComponentType, ref: any) => {
drop(item: IDragTargetItem, monitor) {
if (monitor.didDrop()) return;
// 生成默认配置
const { config, events, methods = [] }: any = Components[(item.type + 'Config') as keyof typeof Components] || {};
const { config, events, methods = [] }: any = getComponent(item.type + 'Config') || {};
addChildElements({
type: item.type,
name: item.name,
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/src/packages/Container/Flex/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/editor/src/packages/Container/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Form } from 'antd';
import { forwardRef, memo, useImperativeHandle, useState } from 'react';
import { useDrop } from 'react-dnd';
import { useShallow } from 'zustand/react/shallow';
import * as Components from '@/packages/index';
import { getComponent } from '@/packages/index';
import MarsRender from '@/packages/MarsRender/MarsRender';
import { usePageStore } from '@/stores/pageStore';
import { FormContext } from '@/packages/utils/context';
Expand Down Expand Up @@ -33,7 +33,7 @@ const MForm = ({ id, type, config, elements, onFinish, onChange }: ComponentType
drop(item: IDragTargetItem, monitor) {
if (monitor.didDrop()) return;
// 生成默认配置
const { config, events, methods = [] }: any = Components[(item.type + 'Config') as keyof typeof Components] || {};
const { config, events, methods = [] }: any = getComponent(item.type + 'Config') || {};
addChildElements({
type: item.type,
name: item.name,
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/src/packages/Container/Form/index.ts

This file was deleted.

17 changes: 13 additions & 4 deletions packages/editor/src/packages/Container/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export { Flex, FlexConfig } from './Flex';
export { Form, FormConfig } from './Form';
export { Card, CardConfig } from './Card';
export { Div, DivConfig } from './Div';
import { lazy } from 'react';

export { default as FlexConfig } from './Flex/Schema';
export { default as FormConfig } from './Form/Schema';
export { default as CardConfig } from './Card/Schema';
export { default as DivConfig } from './Div/Schema';

const Flex = lazy(() => import('./Flex/Flex'));
const Form = lazy(() => import('./Form/Form'));
const Card = lazy(() => import('./Card/Card'));
const Div = lazy(() => import('./Div/Div'));

export { Flex, Form, Card, Div };
2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/Bar/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/Column/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/Line/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/Pie/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/Progress/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/RingProgress/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/TinyColumn/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/editor/src/packages/EChart/TinyLine/index.ts

This file was deleted.

Loading

0 comments on commit 28b2966

Please sign in to comment.