@@ -18,47 +18,44 @@ import { generateClassName } from '../../utils/misc';
1818// import './import-plugins/code.scss';
1919
2020export interface DataSourceExportProps {
21- dataSourceList : DataSourceConfig [ ] ;
22- dataSourceTypes : DataSourceType [ ] ;
21+ dataSourceList : DataSourceConfig [ ] ;
22+ dataSourceTypes : DataSourceType [ ] ;
2323}
2424
2525export interface DataSourceExportState {
26- code : string ;
27- isCodeValid : boolean ;
26+ code : string ;
27+ isCodeValid : boolean ;
2828}
2929
30- export class DataSourceExport extends PureComponent <
31- DataSourceExportProps ,
32- DataSourceExportState
33- > {
34- static defaultProps = {
30+ export class DataSourceExport extends PureComponent < DataSourceExportProps , DataSourceExportState > {
31+ static defaultProps = {
3532 dataSourceList : [ ] ,
36- } ;
33+ } ;
3734
38- state = {
35+ state = {
3936 code : '' ,
4037 isCodeValid : true ,
41- } ;
38+ } ;
4239
43- submit = ( ) => {
40+ submit = ( ) => {
4441 return new Promise ( ( resolve , reject ) => {
4542 const { isCodeValid, code } = this . state ;
4643
4744 if ( isCodeValid ) reject ( new Error ( '格式有误' ) ) ;
4845 resolve ( { schema : code } ) ;
4946 } ) ;
50- } ;
47+ } ;
5148
52- private monacoRef : any ;
49+ private monacoRef : any ;
5350
54- constructor ( props : DataSourceExportProps ) {
51+ constructor ( props : DataSourceExportProps ) {
5552 super ( props ) ;
56- this . state . code = JSON . stringify ( this . deriveValue ( this . props . dataSourceList ) ) ;
53+ this . state . code = JSON . stringify ( this . deriveValue ( this . props . dataSourceList ) , null , 2 ) ;
5754 this . handleEditorDidMount = this . handleEditorDidMount . bind ( this ) ;
5855 this . handleEditorChange = this . handleEditorChange . bind ( this ) ;
59- }
56+ }
6057
61- deriveValue = ( value : any ) => {
58+ deriveValue = ( value : any ) => {
6259 const { dataSourceTypes } = this . props ;
6360
6461 if ( ! _isArray ( dataSourceTypes ) || dataSourceTypes . length === 0 ) return [ ] ;
@@ -74,36 +71,50 @@ deriveValue = (value: any) => {
7471 const ajv = new Ajv ( ) ;
7572
7673 return ( result as DataSourceConfig [ ] ) . filter ( ( dataSource ) => {
77- if ( ! dataSource . type ) return false ;
78- const dataSourceType = dataSourceTypes . find ( ( type ) => type . type === dataSource . type ) ;
79- if ( ! dataSourceType ) return false ;
80- return ajv . validate ( dataSourceType . schema , dataSource ) ;
74+ if ( ! dataSource . type ) return false ;
75+
76+ const dataSourceType = dataSourceTypes . find ( ( type ) => type . type === dataSource . type ) ;
77+
78+ if ( ! dataSourceType ) return false ;
79+
80+ // 向下兼容
81+ if ( dataSourceType . schema ) {
82+ // 校验失败的数据源,给予用户提示
83+ const validate = ajv . compile ( dataSourceType . schema )
84+ const valid = validate ( dataSource )
85+ if ( ! valid ) console . warn ( validate . errors )
86+ return valid
87+ } else {
88+ // 用户不传入 schema 校验规则,默认返回 true
89+ return true
90+ }
8191 } ) ;
82- } ;
92+ } ;
8393
84- handleCopy = ( ) => {
94+ handleCopy = ( ) => {
8595 Message . success ( '粘贴成功!' ) ;
86- } ;
96+ } ;
8797
88- handleEditorChange = ( newValue ) => {
98+ handleEditorChange = ( newValue ) => {
8999 if ( this . monacoRef ) {
90- if ( ! this . monacoRef . getModelMarkers ( ) . find ( ( marker : editor . IMarker ) => marker . owner === 'json' ) ) {
100+ if ( ! this . monacoRef . getModelMarkers ( ) . find ( ( marker : editor . IMarker ) => marker . owner === 'json' ) ) {
91101 this . setState ( { isCodeValid : true , code : newValue } ) ;
102+ }
92103 }
93- }
94- } ;
104+ } ;
95105
96- handleEditorDidMount = ( isFullscreen , editor , monaco ) => {
97- this . monacoRef = monaco ?. editor ;
98- } ;
106+ handleEditorDidMount = ( editor : MonacoEditor , monaco : MonacoEditor ) => {
107+ this . monacoRef = editor ?. editor ;
108+ } ;
99109
100- handleReset = ( ) => {
101- if ( this . monacoRef ) {
102- this . monacoRef . getModels ( ) ?. [ 0 ] ?. setValue ?.( code ) ;
103- }
104- } ;
110+ handleReset = ( ) => {
111+ const code = JSON . stringify ( this . deriveValue ( this . props . dataSourceList ) , null , 2 )
112+ if ( this . monacoRef ) {
113+ this . monacoRef . getModels ( ) ?. [ 0 ] ?. setValue ?.( code ) ;
114+ }
115+ } ;
105116
106- render ( ) {
117+ render ( ) {
107118 const { code, isCodeValid } = this . state ;
108119
109120 // @todo
@@ -134,5 +145,5 @@ render() {
134145 </ p >
135146 </ div >
136147 ) ;
137- }
148+ }
138149}
0 commit comments