Skip to content

Commit

Permalink
fix(designable-antd): fix useDropTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jun 22, 2021
1 parent 4813754 commit 1830759
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions designable/antd/src/hooks/useDropTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AppendNodeEvent, TreeNode } from '@designable/core'
import { useDesigner } from '@designable/react'
import { matchComponent } from '../shared'
import { matchComponent, matchChildComponent } from '../shared'

export const useDropTemplate = (
name: string,
Expand All @@ -11,7 +11,15 @@ export const useDropTemplate = (
const { source, target } = event.data
if (Array.isArray(target)) return
if (!Array.isArray(source)) return
if (matchComponent(target, name) && target.children.length === 0) {
if (
matchComponent(
target,
(key) =>
key === name &&
source.every((child) => !matchChildComponent(child, name))
) &&
target.children.length === 0
) {
target.setNodeChildren(...getChildren(source))
return false
}
Expand Down
Empty file.
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions designable/antd/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ export const matchComponent = (
return componentName === name
}

export const matchChildComponent = (
node: TreeNode,
name: ComponentNameMatcher,
context?: any
) => {
if (name === '*') return true
const componentName = node?.props?.['x-component']
if (typeof name === 'function')
return name(componentName || '', node, context)
if (Array.isArray(name)) return name.includes(componentName)
return componentName.indexOf(`${name}.`) > -1
}

export const includesComponent = (
node: TreeNode,
names: ComponentNameMatcher[],
Expand Down

0 comments on commit 1830759

Please sign in to comment.