Skip to content

Commit 4f2a586

Browse files
committed
feat(core): support workspace drag source
1 parent bfbb71a commit 4f2a586

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed

packages/core/src/models/DragSource.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export class DragSource {
1212
this.prefix = uid()
1313
}
1414

15+
get size() {
16+
return this.getAllSources().length
17+
}
18+
1519
setSources(sources: Record<string, ITreeNode[]>) {
1620
each(sources, (data, group) => {
1721
this.setSourcesByGroup(group, data)

packages/core/src/models/Workspace.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import { History } from './History'
55
import { uid, ICustomEvent, EventContainer } from '@designable/shared'
66
import { HistoryGotoEvent, HistoryRedoEvent, HistoryUndoEvent } from '../events'
77
import { IEngineContext } from '../types'
8+
import { DragSource } from './DragSource'
89
export interface IViewportMatcher {
910
contentWindow?: Window
1011
viewportElement?: HTMLElement
1112
}
1213

1314
export interface IWorkspace {
15+
id?: string
16+
title?: string
17+
description?: string
1418
operation: IOperation
1519
}
1620

@@ -34,6 +38,8 @@ export class Workspace {
3438

3539
viewport: Viewport
3640

41+
source: DragSource
42+
3743
outline: Viewport
3844

3945
operation: Operation
@@ -48,6 +54,7 @@ export class Workspace {
4854
this.id = props.id || uid()
4955
this.title = props.title
5056
this.description = props.description
57+
this.source = new DragSource()
5158
this.viewport = new Viewport({
5259
engine: this.engine,
5360
workspace: this,
@@ -102,13 +109,26 @@ export class Workspace {
102109

103110
serialize(): IWorkspace {
104111
return {
112+
id: this.id,
113+
title: this.title,
114+
description: this.description,
105115
operation: this.operation.serialize(),
106116
}
107117
}
108118

109119
from(workspace?: IWorkspace) {
110-
if (workspace?.operation) {
120+
if (!workspace) return
121+
if (workspace.operation) {
111122
this.operation.from(workspace.operation)
112123
}
124+
if (workspace.id) {
125+
this.id = workspace.id
126+
}
127+
if (workspace.title) {
128+
this.title = workspace.title
129+
}
130+
if (workspace.description) {
131+
this.description = workspace.description
132+
}
113133
}
114134
}

packages/playground/src/main.tsx

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useState } from 'react'
1+
import React from 'react'
22
import ReactDOM from 'react-dom'
33
import {
44
Designer,
@@ -237,31 +237,23 @@ const Actions = observer(() => (
237237
const engine = createDesigner()
238238

239239
const App = () => {
240-
const [view, setView] = useState('design')
241-
242240
return (
243241
<Designer engine={engine}>
244-
<MainPanel logo={<Logo />} actions={<Actions />}>
245-
<CompositePanel>
246-
<CompositePanel.Item
247-
title="panels.Component"
248-
icon={<IconWidget infer="Component" />}
249-
>
250-
<DragSourceWidget title="sources.Inputs" name="form" />
251-
<DragSourceWidget title="sources.Displays" name="form" />
252-
<DragSourceWidget title="sources.Feedbacks" name="form" />
253-
</CompositePanel.Item>
254-
<CompositePanel.Item
255-
title="panels.OutlinedTree"
256-
icon={<IconWidget infer="Outline" />}
257-
>
258-
<OutlineTreeWidget />
259-
</CompositePanel.Item>
260-
<CompositePanel.Item title="历史记录" icon="History">
261-
<HistoryWidget />
262-
</CompositePanel.Item>
263-
</CompositePanel>
264-
<Workbench>
242+
<Workbench>
243+
<MainPanel logo={<Logo />} actions={<Actions />}>
244+
<CompositePanel>
245+
<CompositePanel.Item title="panels.Component" icon="Component">
246+
<DragSourceWidget title="sources.Inputs" name="form" />
247+
<DragSourceWidget title="sources.Displays" name="form" />
248+
<DragSourceWidget title="sources.Feedbacks" name="form" />
249+
</CompositePanel.Item>
250+
<CompositePanel.Item title="panels.OutlinedTree" icon="Outline">
251+
<OutlineTreeWidget />
252+
</CompositePanel.Item>
253+
<CompositePanel.Item title="panels.History" icon="History">
254+
<HistoryWidget />
255+
</CompositePanel.Item>
256+
</CompositePanel>
265257
<WorkspacePanel>
266258
<ToolbarPanel>
267259
<DesignerToolsWidget />
@@ -284,11 +276,11 @@ const App = () => {
284276
</ViewPanel>
285277
</ViewportPanel>
286278
</WorkspacePanel>
287-
</Workbench>
288-
<SettingsPanel title="panels.PropertySettings">
289-
<SettingsForm uploadAction="https://www.mocky.io/v2/5cc8019d300000980a055e76" />
290-
</SettingsPanel>
291-
</MainPanel>
279+
<SettingsPanel title="panels.PropertySettings">
280+
<SettingsForm uploadAction="https://www.mocky.io/v2/5cc8019d300000980a055e76" />
281+
</SettingsPanel>
282+
</MainPanel>
283+
</Workbench>
292284
</Designer>
293285
)
294286
}

packages/react/src/widgets/DragSourceWidget/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TreeNode } from '@designable/core'
33
import { isFn } from '@designable/shared'
44
import { observer } from '@formily/reactive-react'
55
import cls from 'classnames'
6-
import { useDesigner, usePrefix } from '../../hooks'
6+
import { useDesigner, usePrefix, useWorkspace } from '../../hooks'
77
import { IconWidget } from '../IconWidget'
88
import { TextWidget } from '../TextWidget'
99
import './styles.less'
@@ -22,6 +22,7 @@ export const DragSourceWidget: React.FC<IDragSourceWidgetProps> = observer(
2222
(props) => {
2323
const prefix = usePrefix('drag-source')
2424
const designer = useDesigner()
25+
const workspace = useWorkspace()
2526
const [expand, setExpand] = useState(props.defaultExpand)
2627
const renderNode = (node: TreeNode) => {
2728
return (
@@ -42,6 +43,9 @@ export const DragSourceWidget: React.FC<IDragSourceWidgetProps> = observer(
4243
)
4344
}
4445

46+
const source =
47+
workspace.source.size > 0 ? workspace.source : designer.source
48+
4549
return (
4650
<div
4751
className={cls(prefix, props.className, {
@@ -65,7 +69,7 @@ export const DragSourceWidget: React.FC<IDragSourceWidgetProps> = observer(
6569
</div>
6670
<div className={prefix + '-content-wrapper'}>
6771
<div className={prefix + '-content'}>
68-
{designer.source.mapSourcesByGroup(
72+
{source.mapSourcesByGroup(
6973
props.name,
7074
isFn(props.children) ? props.children : renderNode
7175
)}

0 commit comments

Comments
 (0)