-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(designable-antd): support markup schema view
- Loading branch information
Showing
6 changed files
with
224 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
designable/antd/playground/widgets/MarkupSchemaWidget.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import React from 'react' | ||
import { TreeNode } from '@designable/core' | ||
import { MonacoInput } from '@designable/react-settings-form' | ||
import { isEmpty, isPlainObj } from '@formily/shared' | ||
|
||
export interface IMarkupSchemaWidgetProps { | ||
tree: TreeNode | ||
} | ||
|
||
const transformToMarkupSchemaCode = (tree: TreeNode) => { | ||
const printAttribute = (node: TreeNode) => { | ||
if (!node) return '' | ||
return `${Object.keys(node.props || {}) | ||
.map((key) => { | ||
if ( | ||
key === '_designableId' || | ||
key === '_isJSONSchemaObject' || | ||
key === 'version' | ||
) | ||
return '' | ||
const value = node.props[key] | ||
if (isPlainObj(value) && isEmpty(value)) return '' | ||
if (typeof value === 'string') return `${key}="${value}"` | ||
return `${key}={${JSON.stringify(value)}}` | ||
}) | ||
.join(' ')}` | ||
} | ||
const printChildren = (node: TreeNode) => { | ||
if (!node) return '' | ||
return node.children | ||
.map((child) => { | ||
return printNode(child) | ||
}) | ||
.join('') | ||
} | ||
const printNode = (node: TreeNode) => { | ||
if (!node) return '' | ||
return `<SchemaField.Markup ${printAttribute(node)} ${ | ||
node.children.length | ||
? `>${printChildren(node)}</SchemaField.Markup>` | ||
: '/>' | ||
}` | ||
} | ||
const root = tree.find((child) => { | ||
return child.componentName === 'Root' | ||
}) | ||
return `import React, { useMemo } from 'react' | ||
import { createForm } from '@formily/core' | ||
import { createSchemaField } from '@formily/react' | ||
import { | ||
Form, | ||
FormItem, | ||
DatePicker, | ||
Checkbox, | ||
Cascader, | ||
Editable, | ||
Input, | ||
NumberPicker, | ||
Switch, | ||
Password, | ||
PreviewText, | ||
Radio, | ||
Reset, | ||
Select, | ||
Space, | ||
Submit, | ||
TimePicker, | ||
Transfer, | ||
TreeSelect, | ||
Upload, | ||
FormGrid, | ||
FormLayout, | ||
FormTab, | ||
FormCollapse, | ||
ArrayTable, | ||
ArrayCards, | ||
} from '@formily/antd' | ||
import { Card, Slider, Rate } from 'antd' | ||
const SchemaField = createSchemaField({ | ||
components: { | ||
Space, | ||
FormGrid, | ||
FormLayout, | ||
FormTab, | ||
FormCollapse, | ||
ArrayTable, | ||
ArrayCards, | ||
FormItem, | ||
DatePicker, | ||
Checkbox, | ||
Cascader, | ||
Editable, | ||
Input, | ||
NumberPicker, | ||
Switch, | ||
Password, | ||
PreviewText, | ||
Radio, | ||
Reset, | ||
Select, | ||
Submit, | ||
TimePicker, | ||
Transfer, | ||
TreeSelect, | ||
Upload, | ||
Card, | ||
Slider, | ||
Rate, | ||
}, | ||
}) | ||
export default ()=>{ | ||
const form = useMemo(()=>createForm(),[]) | ||
return <Form form={form} ${printAttribute(root)}> | ||
<SchemaField> | ||
${printChildren(root)} | ||
</SchemaField> | ||
</Form> | ||
} | ||
` | ||
} | ||
|
||
export const MarkupSchemaWidget: React.FC<IMarkupSchemaWidgetProps> = ( | ||
props | ||
) => { | ||
return ( | ||
<MonacoInput | ||
{...props} | ||
options={{ readOnly: true }} | ||
value={transformToMarkupSchemaCode(props.tree)} | ||
language="typescript" | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.