-
-
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(@uform/react-form-editor): add schema code editor, edit copy and…
… download (#469)
- Loading branch information
1 parent
478a10a
commit 143547b
Showing
4 changed files
with
95 additions
and
14 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
93 changes: 83 additions & 10 deletions
93
packages/react-schema-editor/src/components/SchemaCode.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 |
---|---|---|
@@ -1,24 +1,97 @@ | ||
import React from 'react' | ||
// import MonacoEditor from 'react-monaco-editor' | ||
import React, { useState } from 'react' | ||
import { Button, Drawer, Icon, Tooltip, notification } 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' | ||
} | ||
}; | ||
|
||
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> | ||
); | ||
|
||
return ( | ||
<div> | ||
{/* <MonacoEditor | ||
width="500" | ||
height="500" | ||
language="json" | ||
theme="vs-dark" | ||
value={schema} | ||
/> */} | ||
<div style={{ height: '100%', }}> | ||
{ | ||
!showDrawer ? aside : null | ||
} | ||
|
||
<Drawer | ||
width="840" | ||
placement="right" | ||
visible={showDrawer} | ||
closable={false} | ||
hasMask={false} | ||
bodyStyle={{ padding: 0, height: '100%' }} | ||
onClose={() => setShowDrawer(false)} > | ||
<div style={{ display: 'flex', flexDirection: 'row', height: '100%' }}> | ||
<MonacoEditor | ||
style={{ flexGrow: '1' }} | ||
language="json" | ||
theme="vs-dark" | ||
onChange={(schema) => onChange(JSON.parse(schema))} | ||
value={schema} | ||
/> | ||
{ | ||
showDrawer ? aside : null | ||
} | ||
</div> | ||
</Drawer> | ||
</div> | ||
) | ||
} |
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