-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.tsx
146 lines (137 loc) · 4.62 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, { useState } from 'react'
import { Radio, Tabs } from 'antd'
import * as fp from 'lodash/fp'
import _ from 'lodash'
import { SchemaTree } from './components/SchemaTree'
import FieldEditor from './components/FieldEditor'
import { SchemaCode } from './components/SchemaCode'
import JsonDialog from './components/JsonDialog'
import { SchemaPreview } from './components/SchemaPreview'
import { ComponentTypes } from './utils/types'
import {
getDefaultComponentType,
getComponentsByComponentType
} from './utils/schemaHelpers'
import 'antd/dist/antd.css'
import './index.css'
export const SchemaEditor: React.FC<{
className?: string
schema: any
showAntdComponents: boolean
showFusionComponents: boolean
customComponents: []
onChange: (schema: any) => void
}> = ({
className,
schema,
showAntdComponents = true,
showFusionComponents = true,
customComponents = [],
onChange
}) => {
const [componentType, setComponentType] = useState(
getDefaultComponentType({ showAntdComponents, showFusionComponents })
)
const [selectedPath, setSelectedPath] = useState(null)
const selectedPaths = (selectedPath && selectedPath.split('.')) || []
const fieldKey =
selectedPaths.length > 0 && selectedPaths[selectedPaths.length - 1]
const handleTypeChange = e => {
setComponentType(e.target.value)
}
const handleTreeSelect = path => {
setSelectedPath(path)
}
const handleSchemaChange = (schema: string) => {
try {
onChange(JSON.parse(schema))
} catch (e) {
console.error(e);
}
}
const isRoot = selectedPath === 'root'
const selectedSchema =
selectedPath && (isRoot ? schema : fp.get(selectedPath, schema))
return (
<div className={`schema-editor ${className}`}>
<div className="schema-menus">
<JsonDialog onChange={handleSchemaChange} />
{(showAntdComponents || showFusionComponents) && (
<span className="select-component-type">
选择组件类型:
<Radio.Group
onChange={handleTypeChange}
defaultValue={componentType}
>
{showAntdComponents && (
<Radio value={ComponentTypes.ANTD}>Ant Design组件</Radio>
)}
{showFusionComponents && (
<Radio value={ComponentTypes.FUSION}>Fusion Design组件</Radio>
)}
</Radio.Group>
</span>
)}
</div>
<div className="schema-editor-main">
<div className="schema-tree">
<SchemaTree
schema={schema}
onChange={onChange}
onSelect={handleTreeSelect}
/>
</div>
<div className="schema-tabs">
<Tabs type="card">
<Tabs.TabPane tab="属性编辑" key="1">
{selectedSchema ? (
<FieldEditor
components={getComponentsByComponentType({
componentType,
customComponents
})}
isRoot={isRoot}
fieldKey={fieldKey}
onFieldKeyChange={value => {
const newSchema = _.cloneDeep(schema)
// 新增 key
const selectedPathPrev = selectedPaths
.slice(0, selectedPaths.length - 1)
.join('.')
const newSelectPath = selectedPathPrev + '.' + value
_.set(newSchema, newSelectPath, _.cloneDeep(selectedSchema))
// 移除旧 key
_.unset(newSchema, selectedPath)
onChange(newSchema)
setSelectedPath(newSelectPath)
}}
schema={selectedSchema}
onChange={value => {
if (isRoot) {
onChange(value)
} else {
const newSchema = _.cloneDeep(schema)
_.set(newSchema, selectedPath, value)
onChange(newSchema)
}
}}
/>
) : (
<div className="field-editor-holder">👈请先选择左侧树节点</div>
)}
</Tabs.TabPane>
<Tabs.TabPane tab="Schema源码" key="2">
<SchemaCode
schema={schema}
onChange={handleSchemaChange}
></SchemaCode>
</Tabs.TabPane>
<Tabs.TabPane tab="预览" key="3">
<SchemaPreview schema={schema}></SchemaPreview>
</Tabs.TabPane>
</Tabs>
</div>
</div>
</div>
)
}