Skip to content

Commit

Permalink
refactor: add components-popover
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed May 17, 2024
1 parent c1c2c80 commit 48b4946
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 201 deletions.
15 changes: 4 additions & 11 deletions apps/playground/src/helpers/prototypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ const Snippet2ColumnLayout: IComponentPrototype = {
package: '@music163/antd',
initChildren: `
<Columns columns={12}>
<Column colSpan={6}>
<Box />
</Column>
<Column colSpan={6}>
<Box />
</Column>
<Column colSpan={6}></Column>
<Column colSpan={6}></Column>
</Columns>
`,
relatedImports: ['Columns', 'Column', 'Box'],
relatedImports: ['Columns', 'Column'],
};

const Snippet3ColumnLayout: IComponentPrototype = {
Expand All @@ -56,17 +52,14 @@ const Snippet3ColumnLayout: IComponentPrototype = {
initChildren: `
<Columns columns={12}>
<Column colSpan={4}>
<Box />
</Column>
<Column colSpan={4}>
<Box />
</Column>
<Column colSpan={4}>
<Box />
</Column>
</Columns>
`,
relatedImports: ['Columns', 'Column', 'Box'],
relatedImports: ['Columns', 'Column'],
};

const SnippetButtonGroup: IComponentPrototype = {
Expand Down
59 changes: 53 additions & 6 deletions packages/core/src/models/designer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ interface IDesignerOptions {
workspace: IWorkspace;
simulator?: SimulatorNameType | ISimulatorType;
activeSidebarPanel?: string;
/**
* 默认激活的视图模式
*/
activeView?: DesignerViewType;
}

const ISimulatorTypes: Record<string, ISimulatorType> = {
Expand Down Expand Up @@ -64,6 +68,16 @@ export class Designer {
*/
_showSmartWizard = false;

/**
* 是否显示添加组件面板
*/
_showAddComponentPopover = false;

/**
* 添加组件面板的位置
*/
_addComponentPopoverPosition = { clientX: 0, clientY: 0 };

/**
* 是否显示右侧面板
*/
Expand All @@ -74,11 +88,6 @@ export class Designer {
*/
_isPreview = false;

/**
* 默认展开的侧边栏
*/
defaultActiveSidebarPanel?: string;

private readonly workspace: IWorkspace;

get simulator(): ISimulatorType {
Expand Down Expand Up @@ -109,10 +118,22 @@ export class Designer {
return this._showRightPanel;
}

get showAddComponentPopover() {
return this._showAddComponentPopover;
}

get addComponentPopoverPosition() {
return this._addComponentPopoverPosition;
}

constructor(options: IDesignerOptions) {
this.workspace = options.workspace;

const { simulator, activeSidebarPanel: defaultActiveSidebarPanel } = options;
const {
simulator,
activeSidebarPanel: defaultActiveSidebarPanel,
activeView: defaultActiveView,
} = options;

// 默认设计器模式
if (simulator) {
Expand All @@ -124,13 +145,20 @@ export class Designer {
this.setActiveSidebarPanel(defaultActiveSidebarPanel);
}

// 默认激活的视图
if (defaultActiveView) {
this.setActiveView(defaultActiveView);
}

makeObservable(this, {
_simulator: observable,
_viewport: observable,
_activeView: observable,
_activeSidebarPanel: observable,
_showSmartWizard: observable,
_showRightPanel: observable,
_showAddComponentPopover: observable,
_addComponentPopoverPosition: observable,
_isPreview: observable,
simulator: computed,
viewport: computed,
Expand All @@ -139,6 +167,8 @@ export class Designer {
isPreview: computed,
showRightPanel: computed,
showSmartWizard: computed,
showAddComponentPopover: computed,
addComponentPopoverPosition: computed,
setSimulator: action,
setViewport: action,
setActiveView: action,
Expand All @@ -147,6 +177,7 @@ export class Designer {
toggleRightPanel: action,
toggleSmartWizard: action,
toggleIsPreview: action,
toggleAddComponentPopover: action,
});
}

Expand Down Expand Up @@ -185,6 +216,22 @@ export class Designer {
this._showRightPanel = value ?? !this._showRightPanel;
}

/**
* 显示添加组件面板
* @param value 是否显示
* @param position 坐标
*/
toggleAddComponentPopover(
value: boolean,
position: {
clientX: number;
clientY: number;
} = this.addComponentPopoverPosition,
) {
this._showAddComponentPopover = value;
this._addComponentPopoverPosition = position;
}

toggleIsPreview(value: boolean) {
this._isPreview = value ?? !this._isPreview;
if (value) {
Expand Down
143 changes: 143 additions & 0 deletions packages/designer/src/components/components-popover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import React, { useCallback, useMemo, useState } from 'react';
import { Box } from 'coral-system';
import { observer, useDesigner, useWorkspace } from '@music163/tango-context';
import { IconFont, DragPanel } from '@music163/tango-ui';
import { ComponentsPanel, ComponentsPanelProps } from '../sidebar';
import { IComponentPrototype } from '@music163/tango-helpers';

interface ComponentsPopoverProps {
// 添加组件位置
type?: 'inner' | 'before' | 'after';
// 弹出方式 手动触发/DOM 触发
isControlled?: boolean;
title?: string;
prototype?: IComponentPrototype;
children?: React.ReactNode;
}

export const ComponentsPopover = observer(
({
type = 'inner',
title = '添加组件',
isControlled = false,
prototype,
children,
...popoverProps
}: ComponentsPopoverProps) => {
const [layout, setLayout] = useState<ComponentsPanelProps['layout']>('grid');
const workspace = useWorkspace();
const designer = useDesigner();

const { addComponentPopoverPosition, showAddComponentPopover } = designer;
const selectedNodeName = workspace.selectSource.selected?.[0]?.codeId ?? '未选中';

// 推荐使用的子组件
const insertedList = useMemo(
() =>
Array.isArray(prototype?.childrenName)
? prototype.childrenName
: [prototype.childrenName].filter(Boolean),
[prototype.childrenName],
);

// 推荐使用的代码片段
const siblingList = useMemo(() => prototype.siblingNames ?? [], [prototype.siblingNames]);

const tipsTextMap = useMemo(
() => ({
before: `点击,在 ${selectedNodeName} 的前方添加节点`,
after: `点击,在 ${selectedNodeName} 的后方添加节点`,
inner: `点击,在 ${selectedNodeName} 内部添加节点`,
}),
[selectedNodeName],
);

const handleSelect = useCallback(
(name: string) => {
switch (type) {
case 'before':
workspace.insertBeforeSelectedNode(name);
break;
case 'after':
workspace.insertAfterSelectedNode(name);
break;
case 'inner':
workspace.insertToSelectedNode(name);
break;
default:
break;
}
},
[type, workspace],
);

const changeLayout = useCallback(() => {
setLayout(layout === 'grid' ? 'line' : 'grid');
}, [layout]);

const menuData = useMemo(() => {
const menuList = JSON.parse(JSON.stringify(workspace.menuData));
const commonList = menuList['common'];
if (siblingList?.length) {
commonList.unshift({
title: '代码片段',
items: siblingList,
});
}

if (insertedList?.length) {
commonList.unshift({
title: '推荐使用',
items: insertedList,
});
}

return menuList;
}, [insertedList, siblingList, workspace.menuData]);

const innerTypeProps =
// 手动触发 适用于 点击添加组件
type === 'inner' && isControlled
? {
open: showAddComponentPopover,
onOpenChange: (open: boolean) => designer.toggleAddComponentPopover(open),
left: addComponentPopoverPosition.clientX,
top: addComponentPopoverPosition.clientY,
}
: {};

return (
<DragPanel
{...innerTypeProps}
title={title}
extra={
<Box fontSize="12px">
{layout === 'grid' ? (
<IconFont type="icon-liebiaoitem" onClick={changeLayout} />
) : (
<IconFont type="icon-grid1" onClick={changeLayout} />
)}
</Box>
}
footer={tipsTextMap[type]}
width="330px"
body={
<ComponentsPanel
isScope
showBizComps={false}
menuData={menuData}
layout={layout}
onItemSelect={handleSelect}
style={{
maxHeight: '400px',
overflow: 'auto',
}}
/>
}
{...popoverProps}
>
{children}
</DragPanel>
);
},
);
1 change: 1 addition & 0 deletions packages/designer/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './drag-box';
export * from './input-kv';
export * from './variable-tree';
export * from './variable-tree-modal';
export * from './components-popover';
7 changes: 7 additions & 0 deletions packages/designer/src/dnd/use-dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ export function useDnd({
// 打开智能向导弹窗
designer.toggleSmartWizard(true);
break;
case 'addComponent':
// 打开添加组件面板
designer.toggleAddComponentPopover(true, {
clientX: (e.detail.meta as any).clientX + 40,
clientY: (e.detail.meta as any).clientY + 110,
});
break;
default:
break;
}
Expand Down
Loading

0 comments on commit 48b4946

Please sign in to comment.