@reearth/core 0.0.7-alpha.8 → 0.0.7-alpha.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reearth/core",
3
- "version": "0.0.7-alpha.8",
3
+ "version": "0.0.7-alpha.9",
4
4
  "author": "Re:Earth contributors <community@reearth.io>",
5
5
  "license": "Apache-2.0",
6
6
  "description": "A library that abstracts a map engine as one common API.",
@@ -202,7 +202,10 @@ export default function useHooks({
202
202
 
203
203
  const handleFeatureCreate = useCallback(
204
204
  (feature: SketchFeature) => {
205
- updateType(undefined);
205
+ if (sketchOptions.autoResetInteractionMode) {
206
+ updateType(undefined);
207
+ }
208
+
206
209
  if (from === "editor") {
207
210
  onSketchFeatureCreate?.(feature);
208
211
  return;
@@ -245,6 +248,7 @@ export default function useHooks({
245
248
  layersRef,
246
249
  from,
247
250
  sketchOptions.dataOnly,
251
+ sketchOptions.autoResetInteractionMode,
248
252
  pluginSketchLayerCreate,
249
253
  pluginSketchLayerFeatureAdd,
250
254
  onSketchFeatureCreate,
@@ -566,21 +570,17 @@ export default function useHooks({
566
570
  }
567
571
  }, [type, send, updateGeometryOptions]);
568
572
 
569
- useEffect(() => {
570
- if (type) {
571
- overrideInteractionMode?.("sketch");
572
- } else if (sketchOptions.autoResetInteractionMode) {
573
- overrideInteractionMode?.("default");
574
- }
573
+ const fromRef = useRef(from);
574
+ fromRef.current = from;
575
+ const overrideInteractionModeRef = useRef(overrideInteractionMode);
576
+ overrideInteractionModeRef.current = overrideInteractionMode;
577
+ const onSketchTypeChangeRef = useRef(onSketchTypeChange);
578
+ onSketchTypeChangeRef.current = onSketchTypeChange;
575
579
 
576
- onSketchTypeChange?.(type, from);
577
- }, [
578
- type,
579
- from,
580
- sketchOptions.autoResetInteractionMode,
581
- overrideInteractionMode,
582
- onSketchTypeChange,
583
- ]);
580
+ useEffect(() => {
581
+ overrideInteractionModeRef.current?.(type ? "sketch" : "default");
582
+ onSketchTypeChangeRef.current?.(type, fromRef.current);
583
+ }, [type]);
584
584
 
585
585
  // API
586
586
  const getType = useGet(type);
@@ -82,12 +82,24 @@ export default function useHooks(
82
82
  reason: LayerSelectionReason | undefined,
83
83
  info: SelectedFeatureInfo | undefined,
84
84
  ) => {
85
- if (selectedLayer.layerId === layerId && selectedLayer.featureId === featureId) return;
85
+ const isSketchLayer =
86
+ selectedLayer.layer?.layer.type === "simple" &&
87
+ selectedLayer.layer?.layer?.data?.isSketchLayer;
88
+ // Sketch layer feature has a fixed featureId, we need to exclude it from the skip condition
89
+ if (
90
+ selectedLayer.layerId === layerId &&
91
+ selectedLayer.featureId === featureId &&
92
+ !isSketchLayer
93
+ )
94
+ return;
86
95
 
87
96
  const computedLayer = await layer?.();
88
97
  const computedFeature =
89
98
  layerId && featureId
90
- ? mapRef.current?.engine.findComputedFeatureById?.(layerId, featureId) ?? info?.feature
99
+ ? (isSketchLayer
100
+ ? computedLayer?.features?.find(f => f.id === featureId)
101
+ : mapRef.current?.engine.findComputedFeatureById?.(layerId, featureId)) ??
102
+ info?.feature
91
103
  : undefined;
92
104
 
93
105
  selectFeature(
@@ -109,7 +109,11 @@ export function computeAtom(cache?: typeof globalDataFeaturesCache) {
109
109
  return true;
110
110
  }
111
111
 
112
- const curFeatures = processGeoJSON(currentLayer.data.value);
112
+ const curFeatures = processGeoJSON(
113
+ currentLayer.data.value,
114
+ undefined,
115
+ currentLayer.data.idProperty,
116
+ );
113
117
  if (curFeatures.length === prevFeatures?.length) {
114
118
  return !curFeatures.every((cur, i) => {
115
119
  const prev = prevFeatures[i];
@@ -10,62 +10,85 @@ export async function fetchGeoJSON(
10
10
  options?: FetchOptions,
11
11
  ): Promise<Feature[] | void> {
12
12
  const d = data.url ? await (await f(data.url, options)).json() : data.value;
13
- return processGeoJSON(d, range);
13
+ return processGeoJSON(d, range, data.idProperty);
14
14
  }
15
15
 
16
- export function processGeoJSON(geojson: GeoJSON, range?: DataRange): Feature[] {
16
+ export function processGeoJSON(
17
+ geojson: GeoJSON,
18
+ range?: DataRange,
19
+ idProperty?: string,
20
+ ): Feature[] {
17
21
  if (geojson.type === "FeatureCollection") {
18
- return geojson.features.flatMap(f => processGeoJSON(f, range));
22
+ return geojson.features.flatMap(f => processGeoJSON(f, range, idProperty));
19
23
  }
20
24
 
21
25
  if (geojson.type === "Feature") {
22
26
  const geo = geojson.geometry;
23
27
  if (geo.type === "GeometryCollection") {
24
28
  return geo.geometries.flatMap(geometry => {
25
- return processGeoJSON({
26
- ...geojson,
27
- geometry,
28
- });
29
+ return processGeoJSON(
30
+ {
31
+ ...geojson,
32
+ geometry,
33
+ },
34
+ undefined,
35
+ idProperty,
36
+ );
29
37
  });
30
38
  }
31
39
  if (geo.type === "MultiPoint") {
32
40
  return geo.coordinates.flatMap(coord => {
33
- return processGeoJSON({
34
- ...geojson,
35
- geometry: {
36
- type: "Point",
37
- coordinates: coord,
41
+ return processGeoJSON(
42
+ {
43
+ ...geojson,
44
+ geometry: {
45
+ type: "Point",
46
+ coordinates: coord,
47
+ },
38
48
  },
39
- });
49
+ undefined,
50
+ idProperty,
51
+ );
40
52
  });
41
53
  }
42
54
  if (geo.type === "MultiLineString") {
43
55
  return geo.coordinates.flatMap(coord => {
44
- return processGeoJSON({
45
- ...geojson,
46
- geometry: {
47
- type: "LineString",
48
- coordinates: coord,
56
+ return processGeoJSON(
57
+ {
58
+ ...geojson,
59
+ geometry: {
60
+ type: "LineString",
61
+ coordinates: coord,
62
+ },
49
63
  },
50
- });
64
+ undefined,
65
+ idProperty,
66
+ );
51
67
  });
52
68
  }
53
69
  if (geo.type === "MultiPolygon") {
54
70
  return geo.coordinates.flatMap(coord => {
55
- return processGeoJSON({
56
- ...geojson,
57
- geometry: {
58
- type: "Polygon",
59
- coordinates: coord,
71
+ return processGeoJSON(
72
+ {
73
+ ...geojson,
74
+ geometry: {
75
+ type: "Polygon",
76
+ coordinates: coord,
77
+ },
60
78
  },
61
- });
79
+ undefined,
80
+ idProperty,
81
+ );
62
82
  });
63
83
  }
64
84
 
65
85
  return [
66
86
  {
67
87
  type: "feature",
68
- id: generateRandomString(12),
88
+ id:
89
+ idProperty && geojson.properties && geojson.properties[idProperty]
90
+ ? geojson.properties[idProperty]
91
+ : generateRandomString(12),
69
92
  geometry:
70
93
  geo.type === "Point" || geo.type === "LineString" || geo.type === "Polygon"
71
94
  ? geo