1
1
import { IDict } from '@jupytergis/schema' ;
2
2
import * as geojson from '@jupytergis/schema/src/schema/geojson.json' ;
3
+ import { showErrorMessage } from '@jupyterlab/apputils' ;
4
+ import { ISubmitEvent } from '@rjsf/core' ;
3
5
import { Ajv , ValidateFunction } from 'ajv' ;
4
6
5
7
import { loadFile } from '@/src/tools' ;
@@ -27,9 +29,12 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
27
29
if ( data ?. path !== '' ) {
28
30
this . removeFormEntry ( 'data' , data , schema , uiSchema ) ;
29
31
}
30
-
31
- super . processSchema ( data , schema , uiSchema ) ;
32
+ if ( this . props . formContext === 'create' ) {
33
+ ( ( schema . properties . path . description =
34
+ 'The local path to a GeoJSON file. (If no path/url is provided, an empty GeoJSON is created.)' ) ,
35
+ super . processSchema ( data , schema , uiSchema ) ) ;
32
36
}
37
+ }
33
38
34
39
/**
35
40
* Validate the path, to avoid invalid path or invalid GeoJSON.
@@ -40,7 +45,7 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
40
45
const extraErrors : IDict = this . state . extraErrors ;
41
46
42
47
let error = '' ;
43
- let valid = false ;
48
+ let valid = true ;
44
49
if ( path ) {
45
50
try {
46
51
const geoJSONData = await loadFile ( {
@@ -55,8 +60,6 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
55
60
} catch ( e ) {
56
61
error = `"${ path } " is not a valid GeoJSON file: ${ e } ` ;
57
62
}
58
- } else {
59
- error = 'Path is required' ;
60
63
}
61
64
62
65
if ( ! valid ) {
@@ -79,4 +82,18 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
79
82
this . props . formErrorSignal . emit ( ! valid ) ;
80
83
}
81
84
}
82
- }
85
+ protected onFormSubmit ( e : ISubmitEvent < any > ) {
86
+ if ( this . state . extraErrors ?. path ?. __errors ?. length >= 1 ) {
87
+ showErrorMessage ( 'Invalid file' , this . state . extraErrors . path . __errors [ 0 ] ) ;
88
+ return ;
89
+ }
90
+ if ( ! e . formData . path ) {
91
+ e . formData . data = {
92
+ type : 'FeatureCollection' ,
93
+ features : [ ] ,
94
+ } ;
95
+ super . onFormSubmit ( e ) ;
96
+
97
+ }
98
+ }
99
+ }
0 commit comments