@mapcomponents/ra-geospatial 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,160 @@
1
+ import React, { useState, useEffect } from 'react';
2
+ import { useMap, MapLibreMap, MlGeoJsonLayer, MlFeatureEditor, MapComponentsProvider } from '@mapcomponents/react-maplibre';
3
+ import { useRecordContext, useInput } from 'react-admin';
4
+ import { parse, stringify } from 'wellknown';
5
+ import { centroid } from '@turf/turf';
6
+
7
+ /******************************************************************************
8
+ Copyright (c) Microsoft Corporation.
9
+
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
+ ***************************************************************************** */
21
+
22
+ function __rest(s, e) {
23
+ var t = {};
24
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
25
+ t[p] = s[p];
26
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
27
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
28
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
29
+ t[p[i]] = s[p[i]];
30
+ }
31
+ return t;
32
+ }
33
+
34
+ function GeospatialInputMap(props) {
35
+ const source = props === null || props === void 0 ? void 0 : props.source;
36
+ const record = useRecordContext();
37
+ const mapHook = useMap({ mapId: props === null || props === void 0 ? void 0 : props.mapId });
38
+ const [geojson, setGeojson] = useState();
39
+ const [oldGeoJson, setOldGeoJson] = useState();
40
+ const _a = useInput(props), _b = _a.field, { name, onChange } = _b; __rest(_b, ["name", "onChange"]);
41
+ useEffect(() => {
42
+ if (typeof record === "undefined" || !record[source])
43
+ return;
44
+ let _geoJson = {
45
+ type: "Feature",
46
+ properties: {},
47
+ geometry: parse(record[source]),
48
+ };
49
+ setGeojson(_geoJson);
50
+ setOldGeoJson(_geoJson);
51
+ }, [record, props.source]);
52
+ useEffect(() => {
53
+ var _a;
54
+ if (!mapHook.map)
55
+ return;
56
+ if (typeof record !== "undefined" && record[source]) {
57
+ const _center = centroid(parse(record[source]));
58
+ if ((_a = _center === null || _center === void 0 ? void 0 : _center.geometry) === null || _a === void 0 ? void 0 : _a.coordinates) {
59
+ mapHook.map.setCenter(_center.geometry.coordinates);
60
+ }
61
+ }
62
+ }, [mapHook.map]);
63
+ return (React.createElement(React.Fragment, null,
64
+ props.embeddedMap && (React.createElement(MapLibreMap, { options: {
65
+ zoom: 14.5,
66
+ style: "https://wms.wheregroup.com/tileserver/style/klokantech-basic.json",
67
+ center: [7.0851268, 50.73884],
68
+ }, style: { width: "100%", height: "400px" } })),
69
+ props.type === "point" && (React.createElement(React.Fragment, null,
70
+ oldGeoJson && (React.createElement(MlGeoJsonLayer, { mapId: props === null || props === void 0 ? void 0 : props.mapId, geojson: oldGeoJson, paint: {
71
+ "circle-radius": 8,
72
+ "circle-color": "#6f6f96",
73
+ "circle-stroke-color": "white",
74
+ "circle-stroke-width": 3,
75
+ "circle-opacity": 0.8,
76
+ }, type: "circle", insertBeforeLayer: "gl-draw-polygon-fill-inactive.cold" })),
77
+ React.createElement(MlFeatureEditor, { mapId: props === null || props === void 0 ? void 0 : props.mapId, geojson: geojson, mode: geojson ? "custom_select" : "draw_point", onChange: (_geojson) => {
78
+ if (typeof _geojson[0] !== "undefined") {
79
+ onChange(stringify(_geojson[0]));
80
+ }
81
+ } }))),
82
+ props.type === "polygon" && (React.createElement(React.Fragment, null,
83
+ oldGeoJson && (React.createElement(MlGeoJsonLayer, { mapId: props === null || props === void 0 ? void 0 : props.mapId, geojson: oldGeoJson, paint: {
84
+ "fill-color": "#6f6f96",
85
+ "fill-opacity": 0.6,
86
+ }, type: "fill", insertBeforeLayer: "gl-draw-polygon-fill-inactive.cold" })),
87
+ React.createElement(MlFeatureEditor, { mapId: props === null || props === void 0 ? void 0 : props.mapId, geojson: geojson, mode: geojson ? "custom_select" : "draw_polygon", onChange: (_geojson) => {
88
+ if (typeof _geojson[0] !== "undefined") {
89
+ onChange(stringify(_geojson[0]));
90
+ }
91
+ } }))),
92
+ props.type === "line" && (React.createElement(React.Fragment, null,
93
+ oldGeoJson && (React.createElement(MlGeoJsonLayer, { mapId: props === null || props === void 0 ? void 0 : props.mapId, geojson: oldGeoJson, paint: {
94
+ "line-width": 6,
95
+ "line-color": "#6f6f96",
96
+ "line-opacity": 0.6,
97
+ }, type: "line", insertBeforeLayer: "gl-draw-polygon-fill-inactive.cold" })),
98
+ React.createElement(MlFeatureEditor, { mapId: props === null || props === void 0 ? void 0 : props.mapId, geojson: geojson, mode: geojson ? "custom_select" : "draw_line_string", onChange: (_geojson) => {
99
+ if (typeof _geojson[0] !== "undefined") {
100
+ onChange(stringify(_geojson[0]));
101
+ }
102
+ } })))));
103
+ }
104
+ GeospatialInputMap.defaultProps = {
105
+ type: "point",
106
+ embeddedMap: true,
107
+ };
108
+
109
+ function GeospatialInput(props) {
110
+ return (React.createElement(React.Fragment, null, props.embeddedMap ? (React.createElement(MapComponentsProvider, null,
111
+ React.createElement(GeospatialInputMap, Object.assign({}, props)))) : (React.createElement(GeospatialInputMap, Object.assign({}, props)))));
112
+ }
113
+ GeospatialInput.defaultProps = {
114
+ embeddedMap: true,
115
+ };
116
+
117
+ function GeospatialShowMap(props) {
118
+ const source = props.source;
119
+ const record = useRecordContext();
120
+ const mapHook = useMap();
121
+ const [geojson, setGeojson] = useState();
122
+ useEffect(() => {
123
+ if (typeof record === "undefined")
124
+ return;
125
+ setGeojson({
126
+ type: "Feature",
127
+ properties: {},
128
+ geometry: parse(record[source]),
129
+ });
130
+ //console.log(record)
131
+ //onChange({target:{value:"POINT(7.083199846086359 50.73716918021759)"}});
132
+ }, [record]);
133
+ useEffect(() => {
134
+ var _a;
135
+ if (!mapHook.map)
136
+ return;
137
+ const _center = centroid(parse(record[source]));
138
+ if ((_a = _center === null || _center === void 0 ? void 0 : _center.geometry) === null || _a === void 0 ? void 0 : _a.coordinates) {
139
+ mapHook.map.setCenter(_center.geometry.coordinates);
140
+ }
141
+ }, [mapHook.map]);
142
+ return (React.createElement(React.Fragment, null,
143
+ props.embeddedMap && (React.createElement(MapLibreMap, { options: {
144
+ zoom: 14.5,
145
+ style: "https://wms.wheregroup.com/tileserver/style/klokantech-basic.json",
146
+ center: [0, 0],
147
+ }, style: { width: "100%", height: "400px" } })),
148
+ geojson && React.createElement(MlGeoJsonLayer, { geojson: geojson })));
149
+ }
150
+
151
+ function GeospatialShow(props) {
152
+ return (React.createElement(React.Fragment, null, props.embeddedMap ? (React.createElement(MapComponentsProvider, null,
153
+ React.createElement(GeospatialShowMap, Object.assign({}, props)))) : (React.createElement(GeospatialShowMap, Object.assign({}, props)))));
154
+ }
155
+ GeospatialShow.defaultProps = {
156
+ embeddedMap: true,
157
+ };
158
+
159
+ export { GeospatialInput as RaGeospatialInput, GeospatialShow as RaGeospatialShow };
160
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapcomponents/ra-geospatial",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "private": false,
6
6
  "main": "index.js",
@@ -3,14 +3,22 @@ import { MapComponentsProvider } from "@mapcomponents/react-maplibre";
3
3
  import GeometryInputMap, {
4
4
  GeospatialInputMapProps,
5
5
  } from "./GeospatialInputMap.js";
6
- import { InputProps } from "react-admin";
7
6
 
8
7
  function GeospatialInput(props: GeospatialInputMapProps) {
9
8
  return (
10
- <MapComponentsProvider>
11
- <GeometryInputMap {...props} />
12
- </MapComponentsProvider>
9
+ <>
10
+ {props.embeddedMap ? (
11
+ <MapComponentsProvider>
12
+ <GeometryInputMap {...props} />
13
+ </MapComponentsProvider>
14
+ ) : (
15
+ <GeometryInputMap {...props} />
16
+ )}
17
+ </>
13
18
  );
14
19
  }
20
+ GeospatialInput.defaultProps = {
21
+ embeddedMap: true,
22
+ };
15
23
 
16
24
  export default GeospatialInput;
@@ -20,12 +20,14 @@ import { Feature } from "@turf/helpers";
20
20
  export interface GeospatialInputMapProps extends InputProps<any> {
21
21
  MapLibreMapProps?: unknown;
22
22
  geometrytype?: "point" | "line" | "polygon";
23
+ embeddedMap?: boolean;
24
+ mapId?: string;
23
25
  }
24
26
 
25
27
  function GeospatialInputMap(props: GeospatialInputMapProps) {
26
28
  const source = props?.source;
27
29
  const record = useRecordContext();
28
- const mapHook = useMap();
30
+ const mapHook = useMap({mapId:props?.mapId});
29
31
 
30
32
  const [geojson, setGeojson] = useState<typeof feature>();
31
33
  const [oldGeoJson, setOldGeoJson] = useState<typeof feature>();
@@ -65,20 +67,23 @@ function GeospatialInputMap(props: GeospatialInputMapProps) {
65
67
 
66
68
  return (
67
69
  <>
68
- <MapLibreMap
69
- options={{
70
- zoom: 14.5,
71
- style:
72
- "https://wms.wheregroup.com/tileserver/style/klokantech-basic.json",
73
- center: [7.0851268, 50.73884],
74
- }}
75
- style={{ width: "100%", height: "400px" }}
76
- />
70
+ {props.embeddedMap && (
71
+ <MapLibreMap
72
+ options={{
73
+ zoom: 14.5,
74
+ style:
75
+ "https://wms.wheregroup.com/tileserver/style/klokantech-basic.json",
76
+ center: [7.0851268, 50.73884],
77
+ }}
78
+ style={{ width: "100%", height: "400px" }}
79
+ />
80
+ )}
77
81
 
78
82
  {props.type === "point" && (
79
83
  <>
80
84
  {oldGeoJson && (
81
85
  <MlGeoJsonLayer
86
+ mapId={props?.mapId}
82
87
  geojson={oldGeoJson as typeof feature}
83
88
  paint={{
84
89
  "circle-radius": 8,
@@ -88,10 +93,12 @@ function GeospatialInputMap(props: GeospatialInputMapProps) {
88
93
  "circle-opacity": 0.8,
89
94
  }}
90
95
  type="circle"
96
+ insertBeforeLayer="gl-draw-polygon-fill-inactive.cold"
91
97
  />
92
98
  )}
93
99
 
94
100
  <MlFeatureEditor
101
+ mapId={props?.mapId}
95
102
  geojson={geojson as typeof feature}
96
103
  mode={geojson ? "custom_select" : "draw_point"}
97
104
  onChange={(_geojson) => {
@@ -106,16 +113,19 @@ function GeospatialInputMap(props: GeospatialInputMapProps) {
106
113
  <>
107
114
  {oldGeoJson && (
108
115
  <MlGeoJsonLayer
116
+ mapId={props?.mapId}
109
117
  geojson={oldGeoJson as typeof feature}
110
118
  paint={{
111
119
  "fill-color": "#6f6f96",
112
120
  "fill-opacity": 0.6,
113
121
  }}
114
122
  type="fill"
123
+ insertBeforeLayer="gl-draw-polygon-fill-inactive.cold"
115
124
  />
116
125
  )}
117
126
 
118
127
  <MlFeatureEditor
128
+ mapId={props?.mapId}
119
129
  geojson={geojson as typeof feature}
120
130
  mode={geojson ? "custom_select" : "draw_polygon"}
121
131
  onChange={(_geojson) => {
@@ -130,6 +140,7 @@ function GeospatialInputMap(props: GeospatialInputMapProps) {
130
140
  <>
131
141
  {oldGeoJson && (
132
142
  <MlGeoJsonLayer
143
+ mapId={props?.mapId}
133
144
  geojson={oldGeoJson as typeof feature}
134
145
  paint={{
135
146
  "line-width": 6,
@@ -137,10 +148,12 @@ function GeospatialInputMap(props: GeospatialInputMapProps) {
137
148
  "line-opacity": 0.6,
138
149
  }}
139
150
  type="line"
151
+ insertBeforeLayer="gl-draw-polygon-fill-inactive.cold"
140
152
  />
141
153
  )}
142
154
 
143
155
  <MlFeatureEditor
156
+ mapId={props?.mapId}
144
157
  geojson={geojson as typeof feature}
145
158
  mode={geojson ? "custom_select" : "draw_line_string"}
146
159
  onChange={(_geojson) => {
@@ -157,6 +170,7 @@ function GeospatialInputMap(props: GeospatialInputMapProps) {
157
170
 
158
171
  GeospatialInputMap.defaultProps = {
159
172
  type: "point",
173
+ embeddedMap: true,
160
174
  };
161
175
 
162
176
  export default GeospatialInputMap;
@@ -0,0 +1,24 @@
1
+ import React from "react";
2
+ import { MapComponentsProvider } from "@mapcomponents/react-maplibre";
3
+ import GeometryShowMap, {
4
+ GeospatialShowMapProps,
5
+ } from "./GeospatialShowMap.js";
6
+
7
+ function GeospatialShow(props: GeospatialShowMapProps) {
8
+ return (
9
+ <>
10
+ {props.embeddedMap ? (
11
+ <MapComponentsProvider>
12
+ <GeometryShowMap {...props} />
13
+ </MapComponentsProvider>
14
+ ) : (
15
+ <GeometryShowMap {...props} />
16
+ )}
17
+ </>
18
+ );
19
+ }
20
+ GeospatialShow.defaultProps = {
21
+ embeddedMap: true,
22
+ };
23
+
24
+ export default GeospatialShow;
@@ -10,7 +10,12 @@ import { LngLatLike } from "maplibre-gl";
10
10
  import { feature, centroid } from "@turf/turf";
11
11
  import { Feature } from "@turf/helpers";
12
12
 
13
- function GeometryShowMap(props: InputProps<any>) {
13
+ export interface GeospatialShowMapProps extends InputProps<any> {
14
+ embeddedMap?: boolean;
15
+ mapId?: string;
16
+ }
17
+
18
+ function GeospatialShowMap(props: GeospatialShowMapProps) {
14
19
  const source = props.source;
15
20
  const record = useRecordContext();
16
21
  const mapHook = useMap();
@@ -40,19 +45,21 @@ function GeometryShowMap(props: InputProps<any>) {
40
45
 
41
46
  return (
42
47
  <>
43
- <MapLibreMap
44
- options={{
45
- zoom: 14.5,
46
- style:
47
- "https://wms.wheregroup.com/tileserver/style/klokantech-basic.json",
48
- center: [0, 0],
49
- }}
50
- style={{ width: "100%", height: "400px" }}
51
- />
48
+ {props.embeddedMap && (
49
+ <MapLibreMap
50
+ options={{
51
+ zoom: 14.5,
52
+ style:
53
+ "https://wms.wheregroup.com/tileserver/style/klokantech-basic.json",
54
+ center: [0, 0],
55
+ }}
56
+ style={{ width: "100%", height: "400px" }}
57
+ />
58
+ )}
52
59
 
53
60
  {geojson && <MlGeoJsonLayer geojson={geojson}></MlGeoJsonLayer>}
54
61
  </>
55
62
  );
56
63
  }
57
64
 
58
- export default GeometryShowMap;
65
+ export default GeospatialShowMap;
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export {default as RaGeospatialInput} from "./components/GeospatialInput.js";
2
- export {default as RaGeospatialShow} from "./components/GeometryShow.js";
2
+ export {default as RaGeospatialShow} from "./components/GeospatialShow.js";
@@ -1,14 +0,0 @@
1
- import React from "react"
2
- import {MapComponentsProvider} from "@mapcomponents/react-maplibre";
3
- import GeometryShowMap from "./GeometryShowMap.js";
4
- import { InputProps } from "react-admin";
5
-
6
- function GeometryShow(props:InputProps<any>) {
7
- return (
8
- <MapComponentsProvider>
9
- <GeometryShowMap {...props} />
10
- </MapComponentsProvider>
11
- )
12
- }
13
-
14
- export default GeometryShow