Skip to content

Commit

Permalink
refactor:code and style refactor (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyforever authored Dec 18, 2019
1 parent aa903bc commit 24b3503
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 230 deletions.
4 changes: 2 additions & 2 deletions packages/react-schema-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"build": "tsc --declaration"
},
"devDependencies": {
"node-sass": "^4.13.0",
"typescript": "^3.5.2"
},
"peerDependencies": {
Expand All @@ -29,8 +30,7 @@
"react-dom": ">=16.8.0",
"react-eva": "^1.1.7"
},
"dependencies": {
},
"dependencies": {},
"publishConfig": {
"access": "public"
},
Expand Down
16 changes: 0 additions & 16 deletions packages/react-schema-editor/src/components/FieldEditor.css

This file was deleted.

15 changes: 5 additions & 10 deletions packages/react-schema-editor/src/components/FieldEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import {
getPropertyValue,
getExpressionValue,
getRuleMessage,
fieldTypeDisabled
fieldTypeDisabled,
getDefaultXProps,
getDefaultXRules
} from '../utils/fieldEditorHelpers'
import { InputTypes, ComponentPropsTypes } from '../utils/types'
import './FieldEditor.css'

const FormItem = Form.Item
const SelectOption = Select.Option
Expand Down Expand Up @@ -52,8 +53,6 @@ interface IFormItemGroupProps extends Partial<IFieldEditorProps> {
const FormItemGroup: React.FC<IFormItemGroupProps> = ({
title,
schema,
xProps,
xRules,
components,
propsKey,
onChange
Expand All @@ -62,8 +61,6 @@ const FormItemGroup: React.FC<IFormItemGroupProps> = ({
const inputTypeData = getInputTypeData()
const componentPropsData = getComponentPropsData({
schema,
xProps,
xRules,
components,
componentName,
propsKey
Expand Down Expand Up @@ -396,8 +393,6 @@ const FieldEditor: React.FC<IFieldEditorProps> = ({
fieldKey,
schema,
components,
xProps,
xRules,
onFieldKeyChange,
onChange
}) => {
Expand Down Expand Up @@ -499,7 +494,7 @@ const FieldEditor: React.FC<IFieldEditorProps> = ({
components={components}
propsKey={ComponentPropsTypes.X_PROPS}
schema={schema}
xProps={xProps}
xProps={getDefaultXProps()}
onChange={onChange}
/>
)}
Expand All @@ -510,7 +505,7 @@ const FieldEditor: React.FC<IFieldEditorProps> = ({
components={components}
propsKey={ComponentPropsTypes.X_RULES}
schema={schema}
xRules={xRules}
xRules={getDefaultXRules()}
onChange={onChange}
/>
)}
Expand Down
110 changes: 32 additions & 78 deletions packages/react-schema-editor/src/components/SchemaCode.tsx
Original file line number Diff line number Diff line change
@@ -1,95 +1,49 @@
import React, { useState } from 'react'
import {Drawer, Icon, Tooltip, notification } from 'antd';
import React from 'react'
import {Tooltip, Icon} from 'antd';
import MonacoEditor from 'react-monaco-editor'
import moment from 'moment';
import { ISchemaCodeProps } from '../utils/types'

// const options = {
// selectOnLineNumbers: true
// };

const styles = {
icon: {
fontSize: '24px',
color: '#fff',
marginBottom: '15px'
}
};
import {copySchema, downloadSchema} from '../utils/schemaTools'

export const SchemaCode: React.FC<ISchemaCodeProps> = ({
schema,
onChange
}) => {
const [showDrawer, setShowDrawer] = useState(false);

if (typeof schema === 'object') {
schema = JSON.stringify(schema, null, '\t')
}

const copySchema = () => {
const oEle = document.createElement('textarea');
oEle.value = schema;
document.body.appendChild(oEle);
oEle.select();
document.execCommand('Copy');
oEle.style.display = 'none';
notification
notification.open({
message: 'shcema复制成功',
duration: 2,
});
}

const downloadSchema = () => {
const aEle = document.createElement('a');
aEle.download = `${moment().format('YYYYMMDD-HHmmss')}schema.json`;
aEle.style.display = 'none';
var blob = new Blob([schema], {type:'application/json,charset=utf-8;'});
aEle.href = URL.createObjectURL(blob);
document.body.appendChild(aEle);
aEle.click();
document.body.removeChild(aEle);
}

const aside = (
<div style={{ width: '28px', padding: '5px 0', textAlign: 'center', background: '#1890ff' }}>
<Tooltip placement="left" title="预览、编辑协议">
<Icon type="codepen" onClick={() => { setShowDrawer(true) }} style={styles.icon} />
</Tooltip>
<Tooltip placement="left" title="复制协议">
<Icon type="copy" onClick={copySchema} style={styles.icon} />
</Tooltip>
<Tooltip placement="left" title="下载协议">
<Icon type="download" onClick={downloadSchema} style={styles.icon} />
</Tooltip>
</div>
);
const styles = {
icon: {
fontSize: '20px',
color: '#fff',
paddingRight: '10px'
}
};

const ToolBar = () => {
return (
<div style={{ height: '30px', lineHeight: '30px', padding: '3px 30px', textAlign: 'right', background: '#1890ff' }}>
<Tooltip placement="left" title="复制协议">
<Icon type="copy" onClick={() => {copySchema(schema)}} style={styles.icon} />
</Tooltip>
<Tooltip placement="left" title="下载协议">
<Icon type="download" onClick={()=> {downloadSchema(schema)}} style={styles.icon} />
</Tooltip>
</div>
)};

return (
<div style={{ height: '100%', }}>
{
!showDrawer ? aside : null
}

<Drawer
width="840"
placement="right"
visible={showDrawer}
closable={false}
bodyStyle={{ padding: 0, height: '100%' }}
onClose={() => setShowDrawer(false)} >
<div style={{ display: 'flex', flexDirection: 'row', height: '100%' }}>
<MonacoEditor
language="json"
theme="vs-dark"
onChange={(schema) => onChange(JSON.parse(schema))}
value={schema}
/>
{
showDrawer ? aside : null
}
</div>
</Drawer>
<div>
<ToolBar></ToolBar>
<MonacoEditor
height={500}
language="json"
theme="vs-dark"
onChange={(schema) => onChange(JSON.parse(schema))}
value={schema}
/>
</div>

)
}
82 changes: 13 additions & 69 deletions packages/react-schema-editor/src/components/SchemaTree.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
import React from 'react'
import { Tree, Row, Col, Icon } from 'antd'
import { Tree, Icon } from 'antd'
import { ISchemaTreeProps } from '../utils/types'
import * as fp from 'lodash/fp'
import _ from 'lodash'
import FieldEditor from './FieldEditor'

const xProps = {
colon: {},
extra: {},
hasFeedback: {},
help: {},
htmlFor: {},
label: {},
labelCol: {},
labelAlign: {},
required: {},
validateStatus: {},
wrapperCol: {}
}

const xRules = {
enum: {},
len: {},
max: {},
min: {},
pattern: {},
required: {},
transform: {},
type: {},
validator: {},
whitespace: {}
}

const TreeNode = Tree.TreeNode

export const SchemaTree: React.FC<ISchemaTreeProps> = ({
schema,
onChange,
components
onSelect
}) => {
const [selectedPath, setSelectedPath] = React.useState(null)

const handleSelect = React.useCallback((path: string[]) => {
setSelectedPath(path[0])
onSelect(path[0])
}, [])

const handleDrop = React.useCallback(
Expand Down Expand Up @@ -123,43 +93,17 @@ export const SchemaTree: React.FC<ISchemaTreeProps> = ({
[schema, onChange]
)

const selectedSchema =
selectedPath &&
(selectedPath === 'root' ? schema : fp.get(selectedPath, schema))
return (
<Row>
<Col span={8}>
<Tree
defaultExpandAll
showIcon
showLine
draggable
onSelect={handleSelect}
onDrop={handleDrop}
>
{TreeNodeBySchema({ schema, path: [] })}
</Tree>
</Col>
<Col span={16}>
{selectedSchema && (
<FieldEditor
xProps={xProps}
xRules={xRules}
components={components}
fieldKey="fieldC"
onFieldKeyChange={value => {
console.log('onFieldKeyChange====', value)
}}
schema={selectedSchema}
onChange={value => {
const newSchema = _.clone(schema)
_.set(newSchema, selectedPath, value)
onChange(newSchema)
}}
/>
)}
</Col>
</Row>
<Tree
defaultExpandAll
showIcon
showLine
draggable
onSelect={handleSelect}
onDrop={handleDrop}
>
{TreeNodeBySchema({ schema, path: [] })}
</Tree>
)
}

Expand Down
Loading

0 comments on commit 24b3503

Please sign in to comment.