Skip to content

Commit

Permalink
feat(core): add setSources to DragSource
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jun 7, 2021
1 parent 91d1d84 commit 5dc81b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions packages/core/src/models/DragSource.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { each, uid } from '@designable/shared'
import { TreeNode, ITreeNode } from './TreeNode'

export class DragSource {
tree: TreeNode
prefix: string
constructor() {
this.tree = new TreeNode({
componentName: 'SourceRoot',
})
this.prefix = uid()
}

setSources(sources: Record<string, ITreeNode[]>) {
each(sources, (data, group) => {
this.setSourcesByGroup(group, data)
})
}

setSourcesByGroup(group: string, sources: ITreeNode[]) {
Expand All @@ -16,7 +25,7 @@ export class DragSource {
} else {
const newParent = new TreeNode({
componentName: 'SourceGroup',
id: group,
id: `${this.prefix}_${group}`,
})
newParent.setNodeChildren(...nodes)
this.tree.appendNode(newParent)
Expand All @@ -31,15 +40,15 @@ export class DragSource {
} else {
const newParent = new TreeNode({
componentName: 'SourceGroup',
id: group,
id: `${this.prefix}_${group}`,
})
newParent.setNodeChildren(...nodes)
this.tree.appendNode(newParent)
}
}

getSourcesByGroup(group: string) {
const parent = this.tree.findById(group)
const parent = this.tree.findById(`${this.prefix}_${group}`)
return parent?.children
}

Expand All @@ -54,7 +63,7 @@ export class DragSource {
}

reduceSourcesByGroup<T>(
id: string,
group: string,
callback?: (
previousValue: T,
currentValue: TreeNode,
Expand All @@ -63,7 +72,7 @@ export class DragSource {
) => T,
init?: T
) {
const sources = this.getSourcesByGroup(id)
const sources = this.getSourcesByGroup(group)
return sources?.reduce?.(callback, init)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/widgets/DragSourceWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const DragSourceWidget: React.FC<IDragSourceWidgetProps> = observer(
<IconWidget infer="Expand" />
</div>
<div className={prefix + '-header-content'}>
<TextWidget>{props.title}</TextWidget>
<TextWidget>{props.title || `sources.${props.name}`}</TextWidget>
</div>
</div>
<div className={prefix + '-content'}>
Expand Down

0 comments on commit 5dc81b5

Please sign in to comment.