Skip to content

Commit ebcf4b8

Browse files
Create an empty vector layer with empty embedded data when no path is provided.
1 parent aa34884 commit ebcf4b8

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

packages/base/src/formbuilder/objectform/source/geojsonsource.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { IDict } from '@jupytergis/schema';
22
import * as geojson from '@jupytergis/schema/src/schema/geojson.json';
3+
import { showErrorMessage } from '@jupyterlab/apputils';
4+
import { ISubmitEvent } from '@rjsf/core';
35
import { Ajv, ValidateFunction } from 'ajv';
46

57
import { loadFile } from '@/src/tools';
@@ -27,9 +29,12 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
2729
if (data?.path !== '') {
2830
this.removeFormEntry('data', data, schema, uiSchema);
2931
}
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));
3236
}
37+
}
3338

3439
/**
3540
* Validate the path, to avoid invalid path or invalid GeoJSON.
@@ -40,7 +45,7 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
4045
const extraErrors: IDict = this.state.extraErrors;
4146

4247
let error = '';
43-
let valid = false;
48+
let valid = true;
4449
if (path) {
4550
try {
4651
const geoJSONData = await loadFile({
@@ -55,8 +60,6 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
5560
} catch (e) {
5661
error = `"${path}" is not a valid GeoJSON file: ${e}`;
5762
}
58-
} else {
59-
error = 'Path is required';
6063
}
6164

6265
if (!valid) {
@@ -79,4 +82,18 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
7982
this.props.formErrorSignal.emit(!valid);
8083
}
8184
}
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+
}

packages/base/src/tools.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ export const loadFile = async (fileInfo: {
498498
model: IJupyterGISModel;
499499
}) => {
500500
const { filepath, type, model } = fileInfo;
501+
if (!filepath) {
502+
return;
503+
}
501504

502505
if (filepath.startsWith('http://') || filepath.startsWith('https://')) {
503506
switch (type) {

0 commit comments

Comments
 (0)