Skip to content

Commit

Permalink
feat(designable-antd): add DesignableArrayCards
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jun 16, 2021
1 parent c992f02 commit 8a7a788
Show file tree
Hide file tree
Showing 18 changed files with 770 additions and 359 deletions.
9 changes: 5 additions & 4 deletions designable/antd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"start": "webpack-dev-server --config playground/webpack.dev.ts"
},
"devDependencies": {
"@designable/react-settings-form": "^0.1.48",
"@designable/react-settings-form": "^0.2.0",
"file-loader": "^5.0.2",
"fs-extra": "^8.1.0",
"html-webpack-plugin": "^3.2.0",
Expand All @@ -42,7 +42,8 @@
"webpack": "^4.41.5",
"webpack-bundle-analyzer": "^3.9.0",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.1"
"webpack-dev-server": "^3.10.1",
"autoprefixer": "^9.0"
},
"peerDependencies": {
"@types/react": ">=16.8.0 || >=17.0.0",
Expand All @@ -53,8 +54,8 @@
"react-is": ">=16.8.0 || >=17.0.0"
},
"dependencies": {
"@designable/core": "^0.1.48",
"@designable/react": "^0.1.48",
"@designable/core": "^0.2.0",
"@designable/react": "^0.2.0",
"@formily/antd": "2.0.0-beta.65",
"@formily/core": "2.0.0-beta.65",
"@formily/react": "2.0.0-beta.65",
Expand Down
6 changes: 6 additions & 0 deletions designable/antd/playground/template.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<title>
Designable Playground
</title>
<style>
html,body{
user-select: none;
-webkit-user-select: none;
}
</style>
</head>
<body>
<div id="root">
Expand Down
7 changes: 7 additions & 0 deletions designable/antd/playground/webpack.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs-extra'
import { GlobSync } from 'glob'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import autoprefixer from 'autoprefixer'
//import { getThemeVariables } from 'antd/dist/theme'

const getWorkspaceAlias = () => {
Expand Down Expand Up @@ -70,6 +71,12 @@ export default {
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader' },
{
loader: 'postcss-loader',
options: {
plugins: () => autoprefixer(),
},
},
{
loader: 'less-loader',
options: {
Expand Down
242 changes: 242 additions & 0 deletions designable/antd/src/components/DesignableArrayCards/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
import React, { Fragment } from 'react'
import { Card, CardProps } from 'antd'
import { Droppable } from '../Droppable'
import { TreeNode } from '@designable/core'
import { useTreeNode, TreeNodeWidget, useNodeIdProps } from '@designable/react'
import { ArrayBase } from '@formily/antd'
import { observer } from '@formily/react'
import { LoadTemplate } from '../LoadTemplate'
import cls from 'classnames'
import { useDropTemplate } from '../../hooks'
import {
hasNodeByComponentPath,
queryNodesByComponentPath,
createEnsureTypeItemsNode,
findNodeByComponentPath,
createNodeId,
} from '../../shared'

const ensureVoidItemsNode = createEnsureTypeItemsNode('void')

const isArrayCardsOperation = (name: string) =>
name === 'ArrayCards.Remove' ||
name === 'ArrayCards.MoveDown' ||
name === 'ArrayCards.MoveUp'

export const DesignableArrayCards: React.FC<CardProps> = observer((props) => {
const node = useTreeNode()
const nodeId = useNodeIdProps()
const designer = useDropTemplate('ArrayCards', (source) => {
const indexNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
'x-component': 'ArrayCards.Index',
},
})
const additionNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
title: 'Addition',
'x-component': 'ArrayCards.Addition',
},
})
const removeNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
title: 'Addition',
'x-component': 'ArrayCards.Remove',
},
})
const moveDownNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
title: 'Addition',
'x-component': 'ArrayCards.MoveDown',
},
})
const moveUpNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
title: 'Addition',
'x-component': 'ArrayCards.MoveUp',
},
})

const voidNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
},
children: [indexNode, ...source, removeNode, moveDownNode, moveUpNode],
})
return [voidNode, additionNode]
})
const renderCard = () => {
if (node.children.length === 0) return <Droppable />
const additions = queryNodesByComponentPath(node, [
'ArrayCards',
'ArrayCards.Addition',
])
const indexes = queryNodesByComponentPath(node, [
'ArrayCards',
'*',
'ArrayCards.Index',
])
const operations = queryNodesByComponentPath(node, [
'ArrayCards',
'*',
isArrayCardsOperation,
])
const children = queryNodesByComponentPath(node, [
'ArrayCards',
'*',
(name) => name.indexOf('ArrayCards.') === -1,
])
return (
<ArrayBase disabled>
<ArrayBase.Item index={0}>
<Card
{...props}
title={
<span>
{indexes.map((node, key) => (
<TreeNodeWidget key={key} node={node} />
))}
{props.title || 'Title'}
</span>
}
className={cls('ant-formily-array-cards-item', props.className)}
extra={
<Fragment>
{operations.map((node) => (
<TreeNodeWidget key={node.id} node={node} />
))}
{props.extra}
</Fragment>
}
>
<div {...createNodeId(designer, ensureVoidItemsNode(node).id)}>
{children.length ? (
children.map((node) => (
<TreeNodeWidget key={node.id} node={node} />
))
) : (
<Droppable />
)}
</div>
</Card>
</ArrayBase.Item>
{additions.map((node) => (
<TreeNodeWidget key={node.id} node={node} />
))}
</ArrayBase>
)
}

return (
<div {...nodeId}>
{renderCard()}
<LoadTemplate
actions={[
{
title: 'addIndex',
onClick: () => {
if (
hasNodeByComponentPath(node, [
'ArrayCards',
'*',
'ArrayCards.Index',
])
)
return
const indexNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
'x-component': 'ArrayCards.Index',
},
})
ensureVoidItemsNode(node).appendNode(indexNode)
},
},

{
title: 'addOperation',
onClick: () => {
const oldAdditionNode = findNodeByComponentPath(node, [
'ArrayCards',
'ArrayCards.Addition',
])
if (!oldAdditionNode) {
const additionNode = new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
title: 'Addition',
'x-component': 'ArrayCards.Addition',
},
})
ensureVoidItemsNode(node).insertAfter(additionNode)
}
const oldRemoveNode = findNodeByComponentPath(node, [
'ArrayCards',
'*',
'ArrayCards.Remove',
])
const oldMoveDownNode = findNodeByComponentPath(node, [
'ArrayCards',
'*',
'ArrayCards.MoveDown',
])
const oldMoveUpNode = findNodeByComponentPath(node, [
'ArrayCards',
'*',
'ArrayCards.MoveUp',
])
if (!oldRemoveNode) {
ensureVoidItemsNode(node).appendNode(
new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
'x-component': 'ArrayCards.Remove',
},
})
)
}
if (!oldMoveDownNode) {
ensureVoidItemsNode(node).appendNode(
new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
'x-component': 'ArrayCards.MoveDown',
},
})
)
}
if (!oldMoveUpNode) {
ensureVoidItemsNode(node).appendNode(
new TreeNode({
componentName: 'DesignableField',
props: {
type: 'void',
'x-component': 'ArrayCards.MoveUp',
},
})
)
}
},
},
]}
/>
</div>
)
})

ArrayBase.mixin(DesignableArrayCards)
Loading

0 comments on commit 8a7a788

Please sign in to comment.