@trackunit/react-map-adapter-google 0.0.200 → 0.0.201

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/index.cjs.js CHANGED
@@ -524,6 +524,32 @@ const collectFeatureIdsFromCluster = (cluster) => {
524
524
  /**
525
525
  * Convert a GeoJSON bbox [minLng, minLat, maxLng, maxLat] to Google Maps LatLngBoundsLiteral.
526
526
  */
527
+ /**
528
+ * Index a feature collection by `String(feature.id)`. Returns null when a
529
+ * feature has no id or two features share one: such a source has no per-feature
530
+ * identity to sync on and is rebuilt as a whole instead.
531
+ */
532
+ const indexFeaturesById = (features) => {
533
+ const byId = new Map();
534
+ for (const feature of features.features) {
535
+ if (feature.id === undefined)
536
+ return null;
537
+ const fid = String(feature.id);
538
+ if (byId.has(fid))
539
+ return null;
540
+ byId.set(fid, feature);
541
+ }
542
+ return byId;
543
+ };
544
+ /** The `ShapeType` a feature's own listeners report, from its geometry. */
545
+ const shapeTypeOfGeometry = (geometry) => {
546
+ const geometryType = geometry?.type;
547
+ if (geometryType === "LineString" || geometryType === "MultiLineString")
548
+ return "line";
549
+ if (geometryType === "Point" || geometryType === "MultiPoint")
550
+ return "point";
551
+ return "polygon";
552
+ };
527
553
  const bboxToGoogleBounds = (bbox) => ({
528
554
  west: bbox[0],
529
555
  south: bbox[1],
@@ -535,6 +561,8 @@ const CANVAS_VIEWPORT_PADDING = 50;
535
561
  * including hover-promoted fills (max fill z-index is HOVER_PROMOTION_Z_INDEX = 10_000
536
562
  * in useShapeFillTiling). A value of 1_000_000 keeps strokes visually complete. */
537
563
  const TILED_OUTLINE_Z_INDEX = 1000000;
564
+ // Thin strokes still need a grabbable hit target in stroke-only interaction mode.
565
+ const STROKE_HIT_MIN_WEIGHT = 3;
538
566
  // AdvancedMarkerElement zIndex for DOM-promoted adaptive markers. Must be high
539
567
  // enough to render above the canvas overlay (which has no explicit zIndex).
540
568
  const DOM_MARKER_Z_INDEX = 1000;
@@ -646,6 +674,17 @@ class GoogleMapsLayerPort {
646
674
  this.selectedShapeFeatureId = null;
647
675
  this.hoveredShapeHandleId = null;
648
676
  this.hoveredShapeFeatureId = null;
677
+ /**
678
+ * The shape feature under the pointer, from Google's own mouseover and
679
+ * mouseout. The snapshot hover above may instead come from `layers.hover()`
680
+ * (a list row), which no shape change may end on the consumer's behalf.
681
+ */
682
+ this.pointerShapeHover = null;
683
+ /**
684
+ * The `hover-end`s queued by `endShapeHover`, by handle id then feature id,
685
+ * so each feature has at most one queued and the reason it was queued for.
686
+ */
687
+ this.pendingShapeHoverEnds = new Map();
649
688
  this.theme = "light";
650
689
  // ---- Shape dblclick via DOM ----
651
690
  // Google Maps overlays suppress both the second click AND dblclick events
@@ -795,6 +834,8 @@ class GoogleMapsLayerPort {
795
834
  this.selectedShapeFeatureId = null;
796
835
  this.hoveredShapeHandleId = null;
797
836
  this.hoveredShapeFeatureId = null;
837
+ this.pointerShapeHover = null;
838
+ this.pendingShapeHoverEnds.clear();
798
839
  this.mapDblClickListenerCleanup?.();
799
840
  this.mapDblClickListenerCleanup = null;
800
841
  if (this.mapClickListener !== null) {
@@ -837,8 +878,14 @@ class GoogleMapsLayerPort {
837
878
  this.removeMarkerSourceInternal(id);
838
879
  }
839
880
  for (const id of [...this.shapeSources.keys()]) {
840
- if (!shapeIds.has(id))
841
- this.removeShapeSourceInternal(id);
881
+ if (shapeIds.has(id))
882
+ continue;
883
+ // A source that leaves the snapshot takes its pointer-hover record with it, so a
884
+ // source that later returns under the same id cannot inherit a stale one. A rebuild
885
+ // keeps the record (it goes through removeShapeSourceInternal directly).
886
+ if (this.pointerShapeHover?.handleId === id)
887
+ this.pointerShapeHover = null;
888
+ this.removeShapeSourceInternal(id);
842
889
  }
843
890
  for (const id of [...this.routeSources.keys()]) {
844
891
  if (!routeIds.has(id))
@@ -1032,127 +1079,239 @@ class GoogleMapsLayerPort {
1032
1079
  if (this.map === null)
1033
1080
  return;
1034
1081
  const existingShape = this.shapeSources.get(config.id);
1035
- if (existingShape !== undefined) {
1036
- const prev = existingShape.config;
1037
- if (prev.features === config.features &&
1038
- prev.style === config.style &&
1039
- prev.featureStyles === config.featureStyles &&
1040
- prev.featureFillGeometries === config.featureFillGeometries &&
1041
- prev.featureZIndexOverrides === config.featureZIndexOverrides &&
1042
- prev.interactive === config.interactive) {
1043
- return;
1082
+ if (existingShape !== undefined && this.isShapeConfigUnchanged(existingShape.config, config))
1083
+ return;
1084
+ // Only a hover the pointer started can be ended here; a `layers.hover()` from
1085
+ // a list row is the consumer's to keep.
1086
+ const pointerHover = this.pointerShapeHover;
1087
+ const hoveredFeatureId = pointerHover !== null && pointerHover.handleId === config.id ? pointerHover.featureId : null;
1088
+ // The features the hover started on are kept for the entity type, since the
1089
+ // sync may drop the feature from the new ones.
1090
+ const hoverableBefore = hoveredFeatureId !== null &&
1091
+ existingShape !== undefined &&
1092
+ this.isFeatureHoverable(existingShape, hoveredFeatureId)
1093
+ ? { featureId: hoveredFeatureId, features: existingShape.config.features }
1094
+ : null;
1095
+ // A changed features collection is synced per feature id, and changed
1096
+ // styles or per-feature overrides (Path B promotion, clipped fills, the
1097
+ // viewport-inside flip) are patched in place. Only an interactive-mode
1098
+ // change, or a source without usable feature ids, rebuilds the source,
1099
+ // which paints an empty frame in between the teardown and the re-add.
1100
+ const touchedFeatureIds = existingShape !== undefined ? this.patchShapeSourceInPlace(existingShape, config) : null;
1101
+ const tracked = touchedFeatureIds !== null && existingShape !== undefined
1102
+ ? existingShape
1103
+ : this.rebuildShapeSource(config, this.map);
1104
+ // Google only fires mouseout on a clickable shape, so a hovered feature that
1105
+ // just lost its last pointer surface would stay hovered for good.
1106
+ if (hoverableBefore !== null && !this.isFeatureHoverable(tracked, hoverableBefore.featureId)) {
1107
+ const { featureId, features } = hoverableBefore;
1108
+ this.endShapeHover(tracked, featureId, this.shapeTypeOfFeature(features, featureId), "lost-surface");
1109
+ }
1110
+ // After an in-place patch, re-apply hover and selection overrides on the
1111
+ // touched features only. After a rebuild of an existing source there is
1112
+ // nothing to re-apply: removing it reset the hovered and selected ids, and
1113
+ // applySnapshot re-applies them. A source added while those ids already
1114
+ // name it (a layer toggled off and on with its selection still in the
1115
+ // snapshot) is painted here, since applySnapshot sees unchanged ids.
1116
+ if (touchedFeatureIds === null) {
1117
+ const namedFeatureIds = new Set();
1118
+ if (this.selectedShapeHandleId === config.id && this.selectedShapeFeatureId !== null) {
1119
+ namedFeatureIds.add(this.selectedShapeFeatureId);
1120
+ }
1121
+ if (this.hoveredShapeHandleId === config.id && this.hoveredShapeFeatureId !== null) {
1122
+ namedFeatureIds.add(this.hoveredShapeFeatureId);
1123
+ }
1124
+ if (namedFeatureIds.size > 0)
1125
+ this.applyGoogleShapeInteraction(config.id, namedFeatureIds);
1126
+ return;
1127
+ }
1128
+ const selectedFeatureId = this.selectedShapeHandleId === config.id ? this.selectedShapeFeatureId : null;
1129
+ const hoveredNowFeatureId = this.hoveredShapeHandleId === config.id ? this.hoveredShapeFeatureId : null;
1130
+ if ((selectedFeatureId !== null && touchedFeatureIds.has(selectedFeatureId)) ||
1131
+ (hoveredNowFeatureId !== null && touchedFeatureIds.has(hoveredNowFeatureId))) {
1132
+ this.applyGoogleShapeInteraction(config.id, touchedFeatureIds);
1133
+ }
1134
+ }
1135
+ isShapeConfigUnchanged(prev, config) {
1136
+ return (prev.features === config.features &&
1137
+ prev.style === config.style &&
1138
+ prev.featureStyles === config.featureStyles &&
1139
+ prev.featureFillGeometries === config.featureFillGeometries &&
1140
+ prev.featureZIndexOverrides === config.featureZIndexOverrides &&
1141
+ prev.interactive === config.interactive);
1142
+ }
1143
+ /**
1144
+ * Patch a source in place. Returns the ids of the features whose shapes were
1145
+ * touched, or null when the change needs a rebuild: the interactive mode
1146
+ * changed, or a feature without an id (or with a duplicate id) has to be
1147
+ * restyled or re-synced, since only shapes mapped by id can be touched one
1148
+ * feature at a time.
1149
+ */
1150
+ patchShapeSourceInPlace(existingShape, config) {
1151
+ const prev = existingShape.config;
1152
+ if (prev.interactive !== config.interactive)
1153
+ return null;
1154
+ if (prev.features !== config.features)
1155
+ return this.syncShapeFeaturesById(existingShape, config);
1156
+ if (prev.style !== config.style && existingShape.featuresById === null)
1157
+ return null;
1158
+ existingShape.config = config;
1159
+ const touchedFeatureIds = prev.featureFillGeometries !== config.featureFillGeometries ||
1160
+ prev.featureZIndexOverrides !== config.featureZIndexOverrides
1161
+ ? this.syncShapeTilingOverrides(existingShape, prev, config)
1162
+ : new Set();
1163
+ if (prev.featureStyles !== config.featureStyles || prev.style !== config.style) {
1164
+ for (const fid of this.syncShapeFeatureStyles(existingShape, prev, config, touchedFeatureIds)) {
1165
+ touchedFeatureIds.add(fid);
1044
1166
  }
1045
- const coreUnchanged = prev.features === config.features &&
1046
- prev.style === config.style &&
1047
- prev.featureStyles === config.featureStyles &&
1048
- prev.interactive === config.interactive;
1049
- // Path B promotion: only clipped-fill / z-index overrides changed — patch
1050
- // affected features in place instead of rebuilding the entire source (~180 sites).
1051
- if (coreUnchanged &&
1052
- (prev.featureFillGeometries !== config.featureFillGeometries ||
1053
- prev.featureZIndexOverrides !== config.featureZIndexOverrides)) {
1054
- this.syncShapeTilingOverrides(existingShape, prev, config);
1055
- existingShape.config = config;
1056
- if ((this.selectedShapeHandleId === config.id && this.selectedShapeFeatureId !== null) ||
1057
- (this.hoveredShapeHandleId === config.id && this.hoveredShapeFeatureId !== null)) {
1058
- this.applyGoogleShapeInteraction(config.id);
1167
+ }
1168
+ return touchedFeatureIds;
1169
+ }
1170
+ /**
1171
+ * Sync a changed features collection one feature at a time, keyed on feature
1172
+ * id, so the shapes of every feature that is still there stay on the map: a
1173
+ * fleet map re-emits its whole site collection after each clusters query,
1174
+ * usually with the same sites in it, and tearing every polygon down for that
1175
+ * blanks the map for a frame. A feature whose geometry or clipped fill changed
1176
+ * is re-rendered, one whose resolved style or z-index changed is patched in
1177
+ * place, a new one is rendered and a gone one removed. The antimeridian merge
1178
+ * runs on the new collection first (the previous one is kept merged and
1179
+ * indexed on `tracked`), so a merged feature is compared with its merged
1180
+ * predecessor. Returns the ids of the features that are still there and were
1181
+ * repainted with their base style (re-rendered, added or restyled; a z-index
1182
+ * change alone repaints nothing), or null (nothing changed yet) when either
1183
+ * collection has a feature without an id or with a duplicate id: that source
1184
+ * is rebuilt as a whole.
1185
+ */
1186
+ syncShapeFeaturesById(tracked, config) {
1187
+ if (this.map === null)
1188
+ return null;
1189
+ const prev = tracked.config;
1190
+ const prevById = tracked.featuresById;
1191
+ const nextById = indexFeaturesById(reactMapAdapterShared.mergeAntimeridianFeatures(config.features));
1192
+ if (prevById === null || nextById === null)
1193
+ return null;
1194
+ const currentMap = this.map;
1195
+ tracked.config = config;
1196
+ tracked.featuresById = nextById;
1197
+ const pointerHover = this.pointerShapeHover;
1198
+ const touchedFeatureIds = new Set();
1199
+ for (const fid of prevById.keys()) {
1200
+ if (nextById.has(fid))
1201
+ continue;
1202
+ this.removeFeatureShapes(tracked, fid);
1203
+ this.removeFeaturePointMarkers(tracked, fid);
1204
+ }
1205
+ for (const [fid, feature] of nextById) {
1206
+ const prevFeature = prevById.get(fid);
1207
+ if (prevFeature === undefined) {
1208
+ this.renderShapeFeature(feature, config, tracked, currentMap);
1209
+ touchedFeatureIds.add(fid);
1210
+ continue;
1211
+ }
1212
+ // A collection rebuilt from the same data mostly carries the same
1213
+ // feature and geometry objects; the deep compare is for the rest.
1214
+ const geometryChanged = prevFeature !== feature &&
1215
+ prevFeature.geometry !== feature.geometry &&
1216
+ !esToolkit.isEqual(prevFeature.geometry, feature.geometry);
1217
+ const fillChanged = !esToolkit.isEqual(prev.featureFillGeometries?.get(fid), config.featureFillGeometries?.get(fid));
1218
+ if (geometryChanged || fillChanged) {
1219
+ // Google fires no mouseout for the shapes taken off the map, and the new
1220
+ // geometry may not cover the pointer: end a pointer hover on this feature
1221
+ // the way a lost surface does.
1222
+ if (pointerHover !== null && pointerHover.handleId === config.id && pointerHover.featureId === fid) {
1223
+ this.endShapeHover(tracked, fid, shapeTypeOfGeometry(prevFeature.geometry), "re-rendered");
1059
1224
  }
1060
- return;
1225
+ this.removeFeatureShapes(tracked, fid);
1226
+ this.removeFeaturePointMarkers(tracked, fid);
1227
+ this.renderShapeFeature(feature, config, tracked, currentMap);
1228
+ touchedFeatureIds.add(fid);
1229
+ continue;
1230
+ }
1231
+ const prevZ = prev.featureZIndexOverrides?.get(fid);
1232
+ const nextZ = config.featureZIndexOverrides?.get(fid);
1233
+ if (prevZ !== nextZ) {
1234
+ this.updateFeatureShapeZIndex(tracked, fid, nextZ);
1235
+ }
1236
+ const prevStyle = prev.featureStyles?.get(fid) ?? prev.style;
1237
+ const nextStyle = config.featureStyles?.get(fid) ?? config.style;
1238
+ if (!esToolkit.isEqual(prevStyle, nextStyle) && this.restyleFeatureShapes(tracked, config, fid, nextStyle)) {
1239
+ touchedFeatureIds.add(fid);
1061
1240
  }
1062
1241
  }
1063
- // Remove existing shapes for this source (update case)
1242
+ return touchedFeatureIds;
1243
+ }
1244
+ /** Tear the source down (update case) and build every shape from scratch. */
1245
+ rebuildShapeSource(config, currentMap) {
1064
1246
  this.removeShapeSourceInternal(config.id);
1247
+ const renderFeatures = reactMapAdapterShared.mergeAntimeridianFeatures(config.features);
1065
1248
  const tracked = {
1066
- shapes: [],
1067
- hitPolylines: [],
1068
- pointMarkers: [],
1069
- listeners: [],
1249
+ shapes: new Set(),
1250
+ hitPolylines: new Set(),
1251
+ pointMarkers: new Set(),
1070
1252
  shapeListeners: new WeakMap(),
1071
1253
  config,
1072
1254
  featureIdToShapes: new Map(),
1073
1255
  featureIdToPointMarkers: new Map(),
1256
+ featuresById: indexFeaturesById(renderFeatures),
1074
1257
  fillOnlyShapes: new Set(),
1075
1258
  };
1076
- const currentMap = this.map;
1077
- const isStrokeOnly = config.interactive === "stroke";
1078
- const renderFeatures = reactMapAdapterShared.mergeAntimeridianFeatures(config.features);
1079
1259
  for (const feature of renderFeatures.features) {
1080
- const geometry = feature.geometry;
1081
- if (geometry === null)
1082
- continue;
1083
- const { type } = geometry;
1084
- const fid = feature.id !== undefined ? String(feature.id) : undefined;
1085
- const featureStyle = (fid !== undefined ? config.featureStyles?.get(fid) : undefined) ?? config.style;
1086
- // ADR-0021 fill tiling: when a clipped fill geometry is present, draw the
1087
- // outline from the full geometry and the fill from the clipped geometry.
1088
- const fillOverride = (type === "Polygon" || type === "MultiPolygon") && fid !== undefined
1089
- ? config.featureFillGeometries?.get(fid)
1090
- : undefined;
1091
- if (fillOverride !== undefined && fid !== undefined) {
1092
- this.renderTiledPolygonFeature(geometry, fillOverride, featureStyle, fid, isStrokeOnly, config, tracked, currentMap);
1093
- continue;
1260
+ this.renderShapeFeature(feature, config, tracked, currentMap);
1261
+ }
1262
+ this.shapeSources.set(config.id, tracked);
1263
+ return tracked;
1264
+ }
1265
+ /** Render one feature's shapes and point markers onto `tracked`, the same way for a rebuild and an id-keyed sync. */
1266
+ renderShapeFeature(feature, config, tracked, currentMap) {
1267
+ const geometry = feature.geometry;
1268
+ if (geometry === null)
1269
+ return;
1270
+ const { type } = geometry;
1271
+ const fid = feature.id !== undefined ? String(feature.id) : undefined;
1272
+ const featureStyle = (fid !== undefined ? config.featureStyles?.get(fid) : undefined) ?? config.style;
1273
+ // ADR-0021 fill tiling: when a clipped fill geometry is present, draw the
1274
+ // outline from the full geometry and the fill from the clipped geometry.
1275
+ const fillOverride = (type === "Polygon" || type === "MultiPolygon") && fid !== undefined
1276
+ ? config.featureFillGeometries?.get(fid)
1277
+ : undefined;
1278
+ if (fillOverride !== undefined && fid !== undefined) {
1279
+ this.renderTiledPolygonFeature(geometry, fillOverride, featureStyle, fid, config, tracked, currentMap);
1280
+ return;
1281
+ }
1282
+ if (type === "Polygon") {
1283
+ const polygonZIndex = fid !== undefined ? config.featureZIndexOverrides?.get(fid) : undefined;
1284
+ this.renderPolygonPart(geometry.coordinates, featureStyle, fid, polygonZIndex, config, tracked, currentMap);
1285
+ }
1286
+ else if (type === "MultiPolygon") {
1287
+ const multiCoords = geometry.coordinates;
1288
+ if (!Array.isArray(multiCoords))
1289
+ return;
1290
+ const polygonZIndex = fid !== undefined ? config.featureZIndexOverrides?.get(fid) : undefined;
1291
+ for (const polygonCoords of multiCoords) {
1292
+ this.renderPolygonPart(polygonCoords, featureStyle, fid, polygonZIndex, config, tracked, currentMap);
1094
1293
  }
1095
- if (type === "Polygon") {
1096
- const paths = reactMapAdapterShared.extractPolygonPaths(geometry.coordinates);
1097
- if (paths === null)
1098
- continue;
1099
- const fillVisible = (featureStyle.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity) > 0;
1100
- const polygonZIndex = fid !== undefined ? config.featureZIndexOverrides?.get(fid) : undefined;
1101
- const polygon = this.createPolygon(paths, featureStyle, currentMap, !isStrokeOnly && fillVisible, polygonZIndex);
1102
- tracked.shapes.push(polygon);
1103
- if (fid !== undefined) {
1104
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, polygon);
1105
- }
1106
- if (config.interactive !== "none" && fid !== undefined) {
1107
- if (isStrokeOnly) {
1108
- const hitPls = this.createStrokeHitPolylines(paths, featureStyle.strokeWidth ?? this.shapeStyleDefaults.polygon.strokeWidth, currentMap);
1109
- for (const pl of hitPls) {
1110
- tracked.hitPolylines.push(pl);
1111
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, pl);
1112
- this.attachShapeInteractionListeners(pl, fid, "polygon", tracked, config.id);
1113
- }
1114
- }
1115
- else {
1116
- this.attachShapeInteractionListeners(polygon, fid, "polygon", tracked, config.id);
1117
- }
1118
- }
1294
+ }
1295
+ else if (type === "LineString") {
1296
+ const path = reactMapAdapterShared.extractLineCoordinates(feature);
1297
+ if (path === null)
1298
+ return;
1299
+ const polyline = this.createPolyline(path, featureStyle, currentMap);
1300
+ tracked.shapes.add(polyline);
1301
+ if (fid !== undefined) {
1302
+ this.pushToFeatureMap(tracked.featureIdToShapes, fid, polyline);
1119
1303
  }
1120
- else if (type === "MultiPolygon") {
1121
- const multiCoords = geometry.coordinates;
1122
- if (!Array.isArray(multiCoords))
1123
- continue;
1124
- for (const polygonCoords of multiCoords) {
1125
- const paths = reactMapAdapterShared.extractPolygonPaths(polygonCoords);
1126
- if (paths === null)
1127
- continue;
1128
- const fillVisible = (featureStyle.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity) > 0;
1129
- const polygonZIndex = fid !== undefined ? config.featureZIndexOverrides?.get(fid) : undefined;
1130
- const polygon = this.createPolygon(paths, featureStyle, currentMap, !isStrokeOnly && fillVisible, polygonZIndex);
1131
- tracked.shapes.push(polygon);
1132
- if (fid !== undefined) {
1133
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, polygon);
1134
- }
1135
- if (config.interactive !== "none" && fid !== undefined) {
1136
- if (isStrokeOnly) {
1137
- const hitPls = this.createStrokeHitPolylines(paths, featureStyle.strokeWidth ?? this.shapeStyleDefaults.polygon.strokeWidth, currentMap);
1138
- for (const pl of hitPls) {
1139
- tracked.hitPolylines.push(pl);
1140
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, pl);
1141
- this.attachShapeInteractionListeners(pl, fid, "polygon", tracked, config.id);
1142
- }
1143
- }
1144
- else {
1145
- this.attachShapeInteractionListeners(polygon, fid, "polygon", tracked, config.id);
1146
- }
1147
- }
1148
- }
1304
+ if (config.interactive !== "none" && fid !== undefined) {
1305
+ this.attachShapeInteractionListeners(polyline, fid, "line", tracked, config.id);
1149
1306
  }
1150
- else if (type === "LineString") {
1151
- const path = reactMapAdapterShared.extractLineCoordinates(feature);
1152
- if (path === null)
1307
+ }
1308
+ else if (type === "MultiLineString") {
1309
+ for (const lineCoords of geometry.coordinates) {
1310
+ const path = lineCoords.map(([lng, lat]) => ({ lng, lat }));
1311
+ if (path.length === 0)
1153
1312
  continue;
1154
1313
  const polyline = this.createPolyline(path, featureStyle, currentMap);
1155
- tracked.shapes.push(polyline);
1314
+ tracked.shapes.add(polyline);
1156
1315
  if (fid !== undefined) {
1157
1316
  this.pushToFeatureMap(tracked.featureIdToShapes, fid, polyline);
1158
1317
  }
@@ -1160,25 +1319,23 @@ class GoogleMapsLayerPort {
1160
1319
  this.attachShapeInteractionListeners(polyline, fid, "line", tracked, config.id);
1161
1320
  }
1162
1321
  }
1163
- else if (type === "MultiLineString") {
1164
- for (const lineCoords of geometry.coordinates) {
1165
- const path = lineCoords.map(([lng, lat]) => ({ lng, lat }));
1166
- if (path.length === 0)
1167
- continue;
1168
- const polyline = this.createPolyline(path, featureStyle, currentMap);
1169
- tracked.shapes.push(polyline);
1170
- if (fid !== undefined) {
1171
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, polyline);
1172
- }
1173
- if (config.interactive !== "none" && fid !== undefined) {
1174
- this.attachShapeInteractionListeners(polyline, fid, "line", tracked, config.id);
1175
- }
1176
- }
1322
+ }
1323
+ else if (type === "Point") {
1324
+ const [lng, lat] = geometry.coordinates;
1325
+ const marker = this.createShapePointMarker({ lng, lat }, featureStyle, currentMap, config.interactive !== "none");
1326
+ tracked.pointMarkers.add(marker);
1327
+ if (fid !== undefined) {
1328
+ this.pushToFeatureMap(tracked.featureIdToPointMarkers, fid, marker);
1329
+ }
1330
+ if (config.interactive !== "none" && fid !== undefined) {
1331
+ this.attachShapePointInteractionListeners(marker, fid, "point", config.id);
1177
1332
  }
1178
- else if (type === "Point") {
1179
- const [lng, lat] = geometry.coordinates;
1333
+ }
1334
+ else if (type === "MultiPoint") {
1335
+ for (const coords of geometry.coordinates) {
1336
+ const [lng, lat] = coords;
1180
1337
  const marker = this.createShapePointMarker({ lng, lat }, featureStyle, currentMap, config.interactive !== "none");
1181
- tracked.pointMarkers.push(marker);
1338
+ tracked.pointMarkers.add(marker);
1182
1339
  if (fid !== undefined) {
1183
1340
  this.pushToFeatureMap(tracked.featureIdToPointMarkers, fid, marker);
1184
1341
  }
@@ -1186,47 +1343,25 @@ class GoogleMapsLayerPort {
1186
1343
  this.attachShapePointInteractionListeners(marker, fid, "point", config.id);
1187
1344
  }
1188
1345
  }
1189
- else if (type === "MultiPoint") {
1190
- for (const coords of geometry.coordinates) {
1191
- const [lng, lat] = coords;
1192
- const marker = this.createShapePointMarker({ lng, lat }, featureStyle, currentMap, config.interactive !== "none");
1193
- tracked.pointMarkers.push(marker);
1194
- if (fid !== undefined) {
1195
- this.pushToFeatureMap(tracked.featureIdToPointMarkers, fid, marker);
1196
- }
1197
- if (config.interactive !== "none" && fid !== undefined) {
1198
- this.attachShapePointInteractionListeners(marker, fid, "point", config.id);
1199
- }
1200
- }
1201
- }
1202
- }
1203
- this.shapeSources.set(config.id, tracked);
1204
- // Re-apply interaction styles if this source is currently hovered or selected
1205
- if ((this.selectedShapeHandleId === config.id && this.selectedShapeFeatureId !== null) ||
1206
- (this.hoveredShapeHandleId === config.id && this.hoveredShapeFeatureId !== null)) {
1207
- this.applyGoogleShapeInteraction(config.id);
1208
1346
  }
1209
1347
  }
1210
- /** Deep equality for clipped fill overrides — map emissions create new Map refs every time. */
1211
- fillOverrideEqual(prev, next) {
1212
- if (prev === next)
1213
- return true;
1214
- if (prev === undefined || next === undefined)
1215
- return false;
1216
- return esToolkit.isEqual(prev, next);
1217
- }
1218
1348
  /**
1219
1349
  * Patch only features whose fill-override or z-index changed (ADR-0021 Path B).
1220
- * Returns the number of features that were updated or re-rendered.
1350
+ * Returns the ids of the features that were re-rendered, already carrying the
1351
+ * style from `config`.
1221
1352
  */
1222
1353
  syncShapeTilingOverrides(tracked, prevConfig, config) {
1354
+ const rebuiltFeatureIds = new Set();
1223
1355
  if (this.map === null)
1224
- return 0;
1356
+ return rebuiltFeatureIds;
1225
1357
  const currentMap = this.map;
1226
- const isStrokeOnly = config.interactive === "stroke";
1227
- const renderFeatures = reactMapAdapterShared.mergeAntimeridianFeatures(config.features);
1228
- let changedCount = 0;
1229
- for (const feature of renderFeatures.features) {
1358
+ const pointerHover = this.pointerShapeHover;
1359
+ // The merged collection is kept indexed on `tracked`; only a source without
1360
+ // usable ids (which has no per-feature overrides anyway) is merged again.
1361
+ const renderFeatures = tracked.featuresById !== null
1362
+ ? tracked.featuresById.values()
1363
+ : reactMapAdapterShared.mergeAntimeridianFeatures(config.features).features;
1364
+ for (const feature of renderFeatures) {
1230
1365
  if (feature.id === undefined)
1231
1366
  continue;
1232
1367
  const fid = String(feature.id);
@@ -1240,73 +1375,205 @@ class GoogleMapsLayerPort {
1240
1375
  const nextFill = config.featureFillGeometries?.get(fid);
1241
1376
  const prevZ = prevConfig.featureZIndexOverrides?.get(fid);
1242
1377
  const nextZ = config.featureZIndexOverrides?.get(fid);
1243
- const fillChanged = !this.fillOverrideEqual(prevFill, nextFill);
1378
+ const fillChanged = !esToolkit.isEqual(prevFill, nextFill);
1244
1379
  const zChanged = prevZ !== nextZ;
1245
1380
  if (!fillChanged && !zChanged)
1246
1381
  continue;
1247
- changedCount += 1;
1248
1382
  if (!fillChanged) {
1249
1383
  this.updateFeatureShapeZIndex(tracked, fid, nextZ);
1250
1384
  continue;
1251
1385
  }
1386
+ // Google fires no mouseout for the shapes taken off the map, and the new
1387
+ // clipped fill may not cover the pointer: end a pointer hover on this
1388
+ // feature the way a lost surface does.
1389
+ if (pointerHover !== null && pointerHover.handleId === config.id && pointerHover.featureId === fid) {
1390
+ this.endShapeHover(tracked, fid, "polygon", "re-rendered");
1391
+ }
1252
1392
  this.removeFeatureShapes(tracked, fid);
1393
+ rebuiltFeatureIds.add(fid);
1253
1394
  const featureStyle = config.featureStyles?.get(fid) ?? config.style;
1254
1395
  const fillOverride = config.featureFillGeometries?.get(fid);
1255
1396
  if (fillOverride !== undefined) {
1256
- this.renderTiledPolygonFeature(geometry, fillOverride, featureStyle, fid, isStrokeOnly, config, tracked, currentMap);
1397
+ this.renderTiledPolygonFeature(geometry, fillOverride, featureStyle, fid, config, tracked, currentMap);
1257
1398
  continue;
1258
1399
  }
1259
1400
  if (type === "Polygon") {
1260
- const paths = reactMapAdapterShared.extractPolygonPaths(geometry.coordinates);
1261
- if (paths === null)
1262
- continue;
1263
- const fillVisible = (featureStyle.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity) > 0;
1264
- const polygon = this.createPolygon(paths, featureStyle, currentMap, !isStrokeOnly && fillVisible, nextZ);
1265
- tracked.shapes.push(polygon);
1266
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, polygon);
1267
- if (config.interactive !== "none") {
1268
- if (isStrokeOnly) {
1269
- const hitPls = this.createStrokeHitPolylines(paths, featureStyle.strokeWidth ?? 1, currentMap);
1270
- for (const pl of hitPls) {
1271
- tracked.hitPolylines.push(pl);
1272
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, pl);
1273
- this.attachShapeInteractionListeners(pl, fid, "polygon", tracked, config.id);
1274
- }
1275
- }
1276
- else {
1277
- this.attachShapeInteractionListeners(polygon, fid, "polygon", tracked, config.id);
1278
- }
1279
- }
1401
+ this.renderPolygonPart(geometry.coordinates, featureStyle, fid, nextZ, config, tracked, currentMap);
1280
1402
  }
1281
1403
  else {
1282
1404
  const multiCoords = geometry.coordinates;
1283
1405
  if (!Array.isArray(multiCoords))
1284
1406
  continue;
1285
1407
  for (const polygonCoords of multiCoords) {
1286
- const paths = reactMapAdapterShared.extractPolygonPaths(polygonCoords);
1287
- if (paths === null)
1288
- continue;
1289
- const fillVisible = (featureStyle.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity) > 0;
1290
- const polygon = this.createPolygon(paths, featureStyle, currentMap, !isStrokeOnly && fillVisible, nextZ);
1291
- tracked.shapes.push(polygon);
1292
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, polygon);
1293
- if (config.interactive !== "none") {
1294
- if (isStrokeOnly) {
1295
- const hitPls = this.createStrokeHitPolylines(paths, featureStyle.strokeWidth ?? 1, currentMap);
1296
- for (const pl of hitPls) {
1297
- tracked.hitPolylines.push(pl);
1298
- this.pushToFeatureMap(tracked.featureIdToShapes, fid, pl);
1299
- this.attachShapeInteractionListeners(pl, fid, "polygon", tracked, config.id);
1300
- }
1301
- }
1302
- else {
1303
- this.attachShapeInteractionListeners(polygon, fid, "polygon", tracked, config.id);
1304
- }
1305
- }
1408
+ this.renderPolygonPart(polygonCoords, featureStyle, fid, nextZ, config, tracked, currentMap);
1306
1409
  }
1307
1410
  }
1308
1411
  }
1309
- return changedCount;
1412
+ return rebuiltFeatureIds;
1413
+ }
1414
+ /**
1415
+ * Restyle only the features whose resolved style (feature style or base
1416
+ * style) changed between two configs, on the shapes that already sit on the
1417
+ * map. Mirrors
1418
+ * what the create helpers derive from a style (clickability from fill
1419
+ * visibility, hit-polyline weight from stroke width) so a patched source is
1420
+ * indistinguishable from a rebuilt one. Features in `alreadyRebuiltFeatureIds`
1421
+ * were just re-rendered with the new style and are skipped. Returns the ids
1422
+ * of the features it restyled.
1423
+ */
1424
+ syncShapeFeatureStyles(tracked, prevConfig, config, alreadyRebuiltFeatureIds) {
1425
+ const prevStyles = prevConfig.featureStyles;
1426
+ const nextStyles = config.featureStyles;
1427
+ const restyledFeatureIds = new Set();
1428
+ const syncFeature = (fid) => {
1429
+ const prevStyle = prevStyles?.get(fid) ?? prevConfig.style;
1430
+ const nextStyle = nextStyles?.get(fid) ?? config.style;
1431
+ if (esToolkit.isEqual(prevStyle, nextStyle) || alreadyRebuiltFeatureIds.has(fid))
1432
+ return;
1433
+ if (this.restyleFeatureShapes(tracked, config, fid, nextStyle)) {
1434
+ restyledFeatureIds.add(fid);
1435
+ }
1436
+ };
1437
+ // A base style change can reach every feature without its own entry, so
1438
+ // walk every feature on the map. The caller has checked that each one has
1439
+ // an id, so all of them are mapped.
1440
+ if (prevConfig.style !== config.style) {
1441
+ for (const fid of tracked.featureIdToShapes.keys()) {
1442
+ syncFeature(fid);
1443
+ }
1444
+ for (const fid of tracked.featureIdToPointMarkers.keys()) {
1445
+ if (!tracked.featureIdToShapes.has(fid))
1446
+ syncFeature(fid);
1447
+ }
1448
+ return restyledFeatureIds;
1449
+ }
1450
+ // A fresh map for every site arrives on each viewport move, usually with no
1451
+ // entry changed: walk the larger map and probe the smaller instead of
1452
+ // building a union.
1453
+ const prevIsLarger = (prevStyles?.size ?? 0) >= (nextStyles?.size ?? 0);
1454
+ const larger = prevIsLarger ? prevStyles : nextStyles;
1455
+ const smaller = prevIsLarger ? nextStyles : prevStyles;
1456
+ for (const fid of larger?.keys() ?? []) {
1457
+ syncFeature(fid);
1458
+ }
1459
+ for (const fid of smaller?.keys() ?? []) {
1460
+ if (larger?.has(fid) !== true)
1461
+ syncFeature(fid);
1462
+ }
1463
+ return restyledFeatureIds;
1464
+ }
1465
+ /**
1466
+ * Apply `style` to every shape and point marker of one feature, in place.
1467
+ * Returns whether the feature had anything on the map to restyle.
1468
+ */
1469
+ restyleFeatureShapes(tracked, config, fid, style) {
1470
+ const shapes = tracked.featureIdToShapes.get(fid) ?? [];
1471
+ const pointMarkers = tracked.featureIdToPointMarkers.get(fid) ?? [];
1472
+ for (const shape of shapes) {
1473
+ if ("getPaths" in shape) {
1474
+ const isFillOnly = tracked.fillOnlyShapes.has(shape);
1475
+ const clickable = this.isPolygonClickable(style, config, isFillOnly);
1476
+ shape.setOptions(isFillOnly
1477
+ ? { ...this.polygonFillOptionsFromStyle(style), clickable }
1478
+ : { ...this.polygonOptionsFromStyle(style), clickable });
1479
+ if (config.interactive !== "none") {
1480
+ this.syncPolygonListeners(shape, clickable, fid, tracked, config.id);
1481
+ }
1482
+ continue;
1483
+ }
1484
+ if (tracked.hitPolylines.has(shape)) {
1485
+ shape.setOptions({ geodesic: style.geodesic ?? true, strokeWeight: this.strokeHitWeightFromStyle(style) });
1486
+ continue;
1487
+ }
1488
+ shape.setOptions(this.polylineOptionsFromStyle(style));
1489
+ }
1490
+ for (const marker of pointMarkers) {
1491
+ const el = marker.content;
1492
+ if (!(el instanceof HTMLElement))
1493
+ continue;
1494
+ this.applyShapePointStyle(el, style);
1495
+ }
1496
+ return shapes.length > 0 || pointMarkers.length > 0;
1497
+ }
1498
+ /**
1499
+ * Whether the port can still get mouse events for a feature. Google only
1500
+ * dispatches them on clickable shapes, and a polygon whose fill is hidden is
1501
+ * not clickable outside stroke mode, unless the feature keeps an interactive
1502
+ * polyline (stroke hit target, tiled outline, line) or a point marker. A
1503
+ * feature with nothing on the map, or on a non-interactive source, has no
1504
+ * listeners at all.
1505
+ */
1506
+ isFeatureHoverable(tracked, featureId) {
1507
+ const shapes = tracked.featureIdToShapes.get(featureId) ?? [];
1508
+ const pointMarkers = tracked.featureIdToPointMarkers.get(featureId) ?? [];
1509
+ if (shapes.length === 0 && pointMarkers.length === 0)
1510
+ return false;
1511
+ const { interactive, featureStyles, style } = tracked.config;
1512
+ if (interactive === "none")
1513
+ return false;
1514
+ if (interactive === "stroke")
1515
+ return true;
1516
+ if (this.isFillVisible(featureStyles?.get(featureId) ?? style))
1517
+ return true;
1518
+ if (shapes.some(shape => !("getPaths" in shape)))
1519
+ return true;
1520
+ return pointMarkers.length > 0;
1521
+ }
1522
+ /**
1523
+ * End a shape hover that Google can no longer end for us. The emit is deferred
1524
+ * because a consumer reacting to hover-end may synchronously push a new
1525
+ * snapshot while this sync is still running. When it runs, the hover-end goes
1526
+ * out if the latest snapshot still names this feature as hovered, or if the
1527
+ * snapshot names no hovered feature while the pointer record still names this
1528
+ * one (a consumer that never mirrors pointer hovers into the snapshot gets its
1529
+ * hover-end too). A hover the consumer moved elsewhere by then is left alone:
1530
+ * its hover-end handler clears whatever is hovered. The pointer record is
1531
+ * kept until then, and cleared only here or by Google's own mouseout. A
1532
+ * hover ended for a lost surface is kept after all when the surface is back
1533
+ * by the time the emit runs (a fill hidden and shown within one tick) and the
1534
+ * pointer still rests on the feature: Google can end that hover itself.
1535
+ */
1536
+ endShapeHover(tracked, featureId, shapeType, reason) {
1537
+ const handleId = tracked.config.id;
1538
+ const pendingForHandle = this.pendingShapeHoverEnds.get(handleId) ?? new Map();
1539
+ if (pendingForHandle.has(featureId))
1540
+ return;
1541
+ pendingForHandle.set(featureId, reason);
1542
+ this.pendingShapeHoverEnds.set(handleId, pendingForHandle);
1543
+ const entity = { type: "shape", id: featureId, shapeType, handleId };
1544
+ queueMicrotask(() => {
1545
+ // A disconnect in between dropped every queued end.
1546
+ if (this.pendingShapeHoverEnds.get(handleId) !== pendingForHandle)
1547
+ return;
1548
+ pendingForHandle.delete(featureId);
1549
+ if (pendingForHandle.size === 0)
1550
+ this.pendingShapeHoverEnds.delete(handleId);
1551
+ const pointerHover = this.pointerShapeHover;
1552
+ const pointerNamesFeature = pointerHover !== null && pointerHover.handleId === handleId && pointerHover.featureId === featureId;
1553
+ if (reason === "lost-surface" && pointerNamesFeature) {
1554
+ const current = this.shapeSources.get(handleId);
1555
+ if (current !== undefined && this.isFeatureHoverable(current, featureId))
1556
+ return;
1557
+ }
1558
+ if (pointerNamesFeature)
1559
+ this.pointerShapeHover = null;
1560
+ const snapshotNamesFeature = this.hoveredShapeHandleId === handleId && this.hoveredShapeFeatureId === featureId;
1561
+ const snapshotNamesNothing = this.hoveredShapeFeatureId === null;
1562
+ if (!snapshotNamesFeature && !(snapshotNamesNothing && pointerNamesFeature))
1563
+ return;
1564
+ this.emitEntityInteraction("hover-end", entity);
1565
+ });
1566
+ }
1567
+ clearPointerShapeHover(handleId, featureId) {
1568
+ const pointerHover = this.pointerShapeHover;
1569
+ if (pointerHover !== null && pointerHover.handleId === handleId && pointerHover.featureId === featureId) {
1570
+ this.pointerShapeHover = null;
1571
+ }
1572
+ }
1573
+ /** The `ShapeType` a feature's own listeners report, looked up by id in `features`. */
1574
+ shapeTypeOfFeature(features, featureId) {
1575
+ const feature = features.features.find(candidate => String(candidate.id) === featureId);
1576
+ return shapeTypeOfGeometry(feature?.geometry ?? null);
1310
1577
  }
1311
1578
  /** Remove all map shapes (and their listeners) for a single feature id. */
1312
1579
  removeFeatureShapes(tracked, featureId) {
@@ -1314,30 +1581,61 @@ class GoogleMapsLayerPort {
1314
1581
  if (shapes === undefined)
1315
1582
  return;
1316
1583
  for (const shape of shapes) {
1317
- const listeners = tracked.shapeListeners.get(shape) ?? [];
1318
- const listenersToRemove = new Set(listeners);
1319
- for (const listener of listeners) {
1320
- google.maps.event.removeListener(listener);
1321
- }
1322
- tracked.listeners = tracked.listeners.filter(listener => !listenersToRemove.has(listener));
1323
- tracked.shapeListeners.delete(shape);
1584
+ this.detachShapeInteractionListeners(shape, tracked);
1324
1585
  google.maps.event.clearInstanceListeners(shape);
1325
1586
  shape.setMap(null);
1326
1587
  if ("getPaths" in shape) {
1327
1588
  tracked.fillOnlyShapes.delete(shape);
1328
1589
  }
1329
- const shapeIndex = tracked.shapes.indexOf(shape);
1330
- if (shapeIndex >= 0) {
1331
- tracked.shapes.splice(shapeIndex, 1);
1332
- }
1333
- const hitIndex = tracked.hitPolylines.indexOf(shape);
1334
- if (hitIndex >= 0) {
1335
- tracked.hitPolylines.splice(hitIndex, 1);
1590
+ else {
1591
+ tracked.hitPolylines.delete(shape);
1336
1592
  }
1593
+ tracked.shapes.delete(shape);
1337
1594
  }
1338
1595
  tracked.featureIdToShapes.delete(featureId);
1339
1596
  }
1340
- /** Update z-index on fill-bearing polygons for one feature without a geometry rebuild. */
1597
+ /** Take a single feature's point markers off the map; their listeners live on the dropped elements. */
1598
+ removeFeaturePointMarkers(tracked, featureId) {
1599
+ const markers = tracked.featureIdToPointMarkers.get(featureId);
1600
+ if (markers === undefined)
1601
+ return;
1602
+ for (const marker of markers) {
1603
+ marker.map = null;
1604
+ tracked.pointMarkers.delete(marker);
1605
+ }
1606
+ tracked.featureIdToPointMarkers.delete(featureId);
1607
+ }
1608
+ /**
1609
+ * Listener rule for polygons, applied at creation and in place: a polygon of
1610
+ * an interactive source has interaction listeners if and only if it is
1611
+ * clickable. Google dispatches nothing on an unclickable polygon, so
1612
+ * listeners there would only be stale state.
1613
+ */
1614
+ syncPolygonListeners(polygon, clickable, fid, tracked, handleId) {
1615
+ if (!clickable) {
1616
+ this.detachShapeInteractionListeners(polygon, tracked);
1617
+ return;
1618
+ }
1619
+ if (!tracked.shapeListeners.has(polygon)) {
1620
+ this.attachShapeInteractionListeners(polygon, fid, "polygon", tracked, handleId);
1621
+ }
1622
+ }
1623
+ /** Undo `attachShapeInteractionListeners` for one shape; a no-op when it has none. */
1624
+ detachShapeInteractionListeners(shape, tracked) {
1625
+ const listeners = tracked.shapeListeners.get(shape);
1626
+ if (listeners === undefined)
1627
+ return;
1628
+ for (const listener of listeners) {
1629
+ google.maps.event.removeListener(listener);
1630
+ }
1631
+ tracked.shapeListeners.delete(shape);
1632
+ }
1633
+ /**
1634
+ * Update the z-index of one feature's fill-bearing polygons (full and
1635
+ * fill-only alike) without a geometry rebuild. Without an override the render
1636
+ * path passes no `zIndex`, so a dropped override (a hover promotion that
1637
+ * ended) restores that default by setting the option to undefined.
1638
+ */
1341
1639
  updateFeatureShapeZIndex(tracked, featureId, zIndex) {
1342
1640
  const shapes = tracked.featureIdToShapes.get(featureId);
1343
1641
  if (shapes === undefined)
@@ -1345,23 +1643,19 @@ class GoogleMapsLayerPort {
1345
1643
  for (const shape of shapes) {
1346
1644
  if (!("getPaths" in shape))
1347
1645
  continue;
1348
- const isFillOnly = tracked.fillOnlyShapes.has(shape);
1349
- if (isFillOnly || zIndex !== undefined) {
1350
- shape.setOptions(zIndex !== undefined ? { zIndex } : {});
1351
- }
1646
+ shape.setOptions({ zIndex });
1352
1647
  }
1353
1648
  }
1354
1649
  removeShapeSourceInternal(id) {
1355
1650
  const tracked = this.shapeSources.get(id);
1356
1651
  if (tracked === undefined)
1357
1652
  return;
1358
- for (const listener of tracked.listeners) {
1359
- google.maps.event.removeListener(listener);
1360
- }
1361
1653
  for (const shape of tracked.shapes) {
1654
+ this.detachShapeInteractionListeners(shape, tracked);
1362
1655
  shape.setMap(null);
1363
1656
  }
1364
1657
  for (const pl of tracked.hitPolylines) {
1658
+ this.detachShapeInteractionListeners(pl, tracked);
1365
1659
  pl.setMap(null);
1366
1660
  }
1367
1661
  for (const marker of tracked.pointMarkers) {
@@ -1382,26 +1676,37 @@ class GoogleMapsLayerPort {
1382
1676
  if (this.selectedShapeHandleId === handleId && this.selectedShapeFeatureId === featureId)
1383
1677
  return;
1384
1678
  const prevHandleId = this.selectedShapeHandleId;
1679
+ const prevFeatureId = this.selectedShapeFeatureId;
1385
1680
  this.selectedShapeHandleId = handleId;
1386
1681
  this.selectedShapeFeatureId = featureId;
1387
- if (prevHandleId !== null) {
1388
- this.applyGoogleShapeInteraction(prevHandleId);
1389
- }
1390
- if (handleId !== null && handleId !== prevHandleId) {
1391
- this.applyGoogleShapeInteraction(handleId);
1392
- }
1682
+ this.repaintInteractionTransition(prevHandleId, prevFeatureId, handleId, featureId);
1393
1683
  }
1394
1684
  applyShapeHoverState(handleId, featureId) {
1395
1685
  if (this.hoveredShapeHandleId === handleId && this.hoveredShapeFeatureId === featureId)
1396
1686
  return;
1397
1687
  const prevHandleId = this.hoveredShapeHandleId;
1688
+ const prevFeatureId = this.hoveredShapeFeatureId;
1398
1689
  this.hoveredShapeHandleId = handleId;
1399
1690
  this.hoveredShapeFeatureId = featureId;
1691
+ this.repaintInteractionTransition(prevHandleId, prevFeatureId, handleId, featureId);
1692
+ }
1693
+ /**
1694
+ * Repaint only the features a hover or selection transition touches: the one
1695
+ * that lost it and the one that got it, each on its own source. The rest of
1696
+ * a source keeps its paint. `setTheme` is the one full repaint.
1697
+ */
1698
+ repaintInteractionTransition(prevHandleId, prevFeatureId, handleId, featureId) {
1400
1699
  if (prevHandleId !== null) {
1401
- this.applyGoogleShapeInteraction(prevHandleId);
1700
+ const featureIds = new Set();
1701
+ if (prevFeatureId !== null)
1702
+ featureIds.add(prevFeatureId);
1703
+ if (handleId === prevHandleId && featureId !== null)
1704
+ featureIds.add(featureId);
1705
+ if (featureIds.size > 0)
1706
+ this.applyGoogleShapeInteraction(prevHandleId, featureIds);
1402
1707
  }
1403
- if (handleId !== null && handleId !== prevHandleId) {
1404
- this.applyGoogleShapeInteraction(handleId);
1708
+ if (handleId !== null && handleId !== prevHandleId && featureId !== null) {
1709
+ this.applyGoogleShapeInteraction(handleId, new Set([featureId]));
1405
1710
  }
1406
1711
  }
1407
1712
  // ---- Route sources ----
@@ -2457,8 +2762,9 @@ class GoogleMapsLayerPort {
2457
2762
  *
2458
2763
  * Called whenever hover, selection, or theme changes on the given handleId.
2459
2764
  * When neither hover nor selection targets a feature, it reverts to its base style.
2765
+ * `onlyFeatureIds` limits the repaint to those features (in-place restyles).
2460
2766
  */
2461
- applyGoogleShapeInteraction(handleId) {
2767
+ applyGoogleShapeInteraction(handleId, onlyFeatureIds) {
2462
2768
  const tracked = this.shapeSources.get(handleId);
2463
2769
  if (tracked === undefined)
2464
2770
  return;
@@ -2483,67 +2789,124 @@ class GoogleMapsLayerPort {
2483
2789
  return null;
2484
2790
  };
2485
2791
  for (const [featureId, shapes] of tracked.featureIdToShapes) {
2792
+ if (onlyFeatureIds !== undefined && !onlyFeatureIds.has(featureId))
2793
+ continue;
2486
2794
  const overrides = resolveForFeature(featureId);
2487
2795
  const base = featureStyles?.get(featureId) ?? style;
2796
+ const effective = this.styleWithOverrides(base, overrides);
2797
+ const polygonPaint = this.polygonOptionsFromStyle(effective);
2798
+ const polylinePaint = this.polylineOptionsFromStyle(effective);
2488
2799
  for (const shape of shapes) {
2489
2800
  if ("getPaths" in shape) {
2490
2801
  // Fill-only tiled polygons (ADR-0021) carry no stroke — their outline is a
2491
2802
  // separate polyline. Restyle only the fill so hover/select never paints a
2492
2803
  // stroke back onto them.
2493
2804
  if (tracked.fillOnlyShapes.has(shape)) {
2494
- shape.setOptions({
2495
- fillColor: overrides?.fill ?? base.fill ?? "rgba(0,0,0,0.1)",
2496
- fillOpacity: overrides?.fillOpacity ?? base.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity,
2497
- });
2805
+ shape.setOptions({ fillColor: polygonPaint.fillColor, fillOpacity: polygonPaint.fillOpacity });
2498
2806
  continue;
2499
2807
  }
2500
2808
  shape.setOptions({
2501
- fillColor: overrides?.fill ?? base.fill ?? "rgba(0,0,0,0.1)",
2502
- fillOpacity: overrides?.fillOpacity ?? base.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity,
2503
- strokeColor: overrides?.stroke ?? base.stroke ?? "#000000",
2504
- strokeWeight: overrides?.strokeWidth ?? base.strokeWidth ?? this.shapeStyleDefaults.polygon.strokeWidth,
2505
- strokeOpacity: overrides?.strokeOpacity ?? base.strokeOpacity ?? this.shapeStyleDefaults.polygon.strokeOpacity,
2809
+ fillColor: polygonPaint.fillColor,
2810
+ fillOpacity: polygonPaint.fillOpacity,
2811
+ strokeColor: polygonPaint.strokeColor,
2812
+ strokeWeight: polygonPaint.strokeWeight,
2813
+ strokeOpacity: polygonPaint.strokeOpacity,
2506
2814
  });
2507
2815
  }
2816
+ else if (tracked.hitPolylines.has(shape)) {
2817
+ // Invisible stroke hit targets: only the hit width follows the override.
2818
+ shape.setOptions({ strokeWeight: this.strokeHitWeightFromStyle(effective) });
2819
+ }
2508
2820
  else {
2509
2821
  shape.setOptions({
2510
- strokeColor: overrides?.stroke ?? base.stroke ?? "#000000",
2511
- strokeWeight: overrides?.strokeWidth ?? base.strokeWidth ?? this.shapeStyleDefaults.line.strokeWidth,
2512
- strokeOpacity: overrides?.strokeOpacity ?? base.strokeOpacity ?? this.shapeStyleDefaults.line.strokeOpacity,
2822
+ strokeColor: polylinePaint.strokeColor,
2823
+ strokeWeight: polylinePaint.strokeWeight,
2824
+ strokeOpacity: polylinePaint.strokeOpacity,
2513
2825
  });
2514
2826
  }
2515
2827
  }
2516
2828
  }
2517
2829
  for (const [featureId, pointMarkers] of tracked.featureIdToPointMarkers) {
2518
- const overrides = resolveForFeature(featureId);
2519
- const base = featureStyles?.get(featureId) ?? style;
2830
+ if (onlyFeatureIds !== undefined && !onlyFeatureIds.has(featureId))
2831
+ continue;
2832
+ const effective = this.styleWithOverrides(featureStyles?.get(featureId) ?? style, resolveForFeature(featureId));
2520
2833
  for (const marker of pointMarkers) {
2521
2834
  const el = marker.content;
2522
2835
  if (!(el instanceof HTMLElement))
2523
2836
  continue;
2524
- const fillOpacity = overrides?.fillOpacity ?? base.fillOpacity ?? this.shapeStyleDefaults.point.fillOpacity;
2525
- const strokeOpacity = overrides?.strokeOpacity ?? base.strokeOpacity ?? this.shapeStyleDefaults.point.strokeOpacity;
2526
- el.style.backgroundColor = reactMapColorUtils.colorWithOpacity(overrides?.fill ?? base.fill ?? "rgba(0,0,0,0.1)", fillOpacity);
2527
- el.style.borderColor = reactMapColorUtils.colorWithOpacity(overrides?.stroke ?? base.stroke ?? "#000", strokeOpacity);
2528
- el.style.borderWidth = `${overrides?.strokeWidth ?? base.strokeWidth ?? this.shapeStyleDefaults.point.strokeWidth}px`;
2837
+ this.applyShapePointStyle(el, effective);
2529
2838
  }
2530
2839
  }
2531
2840
  }
2841
+ /** `base` with the interaction overrides that are set layered on top of it. */
2842
+ styleWithOverrides(base, overrides) {
2843
+ if (overrides === null)
2844
+ return base;
2845
+ return {
2846
+ ...base,
2847
+ fill: overrides.fill ?? base.fill,
2848
+ fillOpacity: overrides.fillOpacity ?? base.fillOpacity,
2849
+ stroke: overrides.stroke ?? base.stroke,
2850
+ strokeOpacity: overrides.strokeOpacity ?? base.strokeOpacity,
2851
+ strokeWidth: overrides.strokeWidth ?? base.strokeWidth,
2852
+ };
2853
+ }
2532
2854
  // ============================================================================
2533
2855
  // Private: Shape creation
2534
2856
  // ============================================================================
2535
- createPolygon(paths, style, map, clickable = true, zIndex) {
2536
- return new google.maps.Polygon({
2537
- paths,
2538
- map,
2857
+ isFillVisible(style) {
2858
+ return (style.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity) > 0;
2859
+ }
2860
+ /**
2861
+ * Whether a polygon is a hit target, at creation and in place: never in stroke
2862
+ * mode (the hit polylines take over) and never with a hidden fill. A fill-only
2863
+ * tiling polygon additionally needs an interactive source; a full polygon stays
2864
+ * clickable on a non-interactive source, as it always has.
2865
+ */
2866
+ isPolygonClickable(style, config, isFillOnly) {
2867
+ if (config.interactive === "stroke" || !this.isFillVisible(style))
2868
+ return false;
2869
+ return !isFillOnly || config.interactive !== "none";
2870
+ }
2871
+ /** Fill paint shared by full and fill-only polygons. */
2872
+ polygonFillOptionsFromStyle(style) {
2873
+ return {
2874
+ fillColor: style.fill ?? "rgba(0,0,0,0.1)",
2875
+ fillOpacity: style.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity,
2876
+ };
2877
+ }
2878
+ /** The one place polygon paint and its defaults are derived from a `ShapeStyle`. */
2879
+ polygonOptionsFromStyle(style) {
2880
+ return {
2539
2881
  // Great-circle edges natively (no densification needed); honours the shape
2540
2882
  // style flag, defaulting to true per the ShapeStyle.geodesic contract.
2541
2883
  geodesic: style.geodesic ?? true,
2542
- fillColor: style.fill ?? "rgba(0,0,0,0.1)",
2543
- fillOpacity: style.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity,
2884
+ ...this.polygonFillOptionsFromStyle(style),
2544
2885
  strokeColor: style.stroke ?? "#000000",
2545
2886
  strokeWeight: style.strokeWidth ?? this.shapeStyleDefaults.polygon.strokeWidth,
2546
2887
  strokeOpacity: style.strokeOpacity ?? this.shapeStyleDefaults.polygon.strokeOpacity,
2888
+ };
2889
+ }
2890
+ /** The one place polyline paint and its defaults are derived from a `ShapeStyle`. */
2891
+ polylineOptionsFromStyle(style) {
2892
+ return {
2893
+ // Match the polygon: native great-circle stroke so a tiled outline curves
2894
+ // the same way as its densified fill instead of drawing a straight chord.
2895
+ geodesic: style.geodesic ?? true,
2896
+ strokeColor: style.stroke ?? "#000000",
2897
+ strokeWeight: style.strokeWidth ?? this.shapeStyleDefaults.line.strokeWidth,
2898
+ strokeOpacity: style.strokeOpacity ?? this.shapeStyleDefaults.line.strokeOpacity,
2899
+ };
2900
+ }
2901
+ /** Width of the invisible stroke hit target; thin strokes still need a grabbable one. */
2902
+ strokeHitWeightFromStyle(style) {
2903
+ return Math.max(style.strokeWidth ?? this.shapeStyleDefaults.polygon.strokeWidth, STROKE_HIT_MIN_WEIGHT);
2904
+ }
2905
+ createPolygon(paths, style, map, clickable = true, zIndex) {
2906
+ return new google.maps.Polygon({
2907
+ paths,
2908
+ map,
2909
+ ...this.polygonOptionsFromStyle(style),
2547
2910
  clickable,
2548
2911
  ...(zIndex !== undefined ? { zIndex } : {}),
2549
2912
  });
@@ -2552,12 +2915,7 @@ class GoogleMapsLayerPort {
2552
2915
  return new google.maps.Polyline({
2553
2916
  path,
2554
2917
  map,
2555
- // Match the polygon: native great-circle stroke so a tiled outline curves
2556
- // the same way as its densified fill instead of drawing a straight chord.
2557
- geodesic: style.geodesic ?? true,
2558
- strokeColor: style.stroke ?? "#000000",
2559
- strokeWeight: style.strokeWidth ?? this.shapeStyleDefaults.line.strokeWidth,
2560
- strokeOpacity: style.strokeOpacity ?? this.shapeStyleDefaults.line.strokeOpacity,
2918
+ ...this.polylineOptionsFromStyle(style),
2561
2919
  clickable: true,
2562
2920
  ...(zIndex !== undefined ? { zIndex } : {}),
2563
2921
  });
@@ -2576,14 +2934,41 @@ class GoogleMapsLayerPort {
2576
2934
  // while the clip cuts must stay as straight Web-Mercator segments so they
2577
2935
  // land exactly on the covering shape's rendered edge. geodesic:true would
2578
2936
  // re-curve those straight cuts and break the tiling seam.
2579
- fillColor: style.fill ?? "rgba(0,0,0,0.1)",
2580
- fillOpacity: style.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity,
2937
+ ...this.polygonFillOptionsFromStyle(style),
2581
2938
  strokeWeight: 0,
2582
2939
  strokeOpacity: 0,
2583
2940
  clickable,
2584
2941
  ...(zIndex !== undefined ? { zIndex } : {}),
2585
2942
  });
2586
2943
  }
2944
+ /**
2945
+ * One polygon ring set (a Polygon, or one part of a MultiPolygon) with its
2946
+ * hit targets: the polygon itself outside stroke mode, invisible hit
2947
+ * polylines in stroke mode.
2948
+ */
2949
+ renderPolygonPart(coordinates, featureStyle, fid, zIndex, config, tracked, map) {
2950
+ const paths = reactMapAdapterShared.extractPolygonPaths(coordinates);
2951
+ if (paths === null)
2952
+ return;
2953
+ const isStrokeOnly = config.interactive === "stroke";
2954
+ const clickable = this.isPolygonClickable(featureStyle, config, false);
2955
+ const polygon = this.createPolygon(paths, featureStyle, map, clickable, zIndex);
2956
+ tracked.shapes.add(polygon);
2957
+ if (fid === undefined)
2958
+ return;
2959
+ this.pushToFeatureMap(tracked.featureIdToShapes, fid, polygon);
2960
+ if (config.interactive === "none")
2961
+ return;
2962
+ if (isStrokeOnly) {
2963
+ for (const pl of this.createStrokeHitPolylines(paths, featureStyle, map)) {
2964
+ tracked.hitPolylines.add(pl);
2965
+ this.pushToFeatureMap(tracked.featureIdToShapes, fid, pl);
2966
+ this.attachShapeInteractionListeners(pl, fid, "polygon", tracked, config.id);
2967
+ }
2968
+ return;
2969
+ }
2970
+ this.syncPolygonListeners(polygon, clickable, fid, tracked, config.id);
2971
+ }
2587
2972
  /**
2588
2973
  * Render a polygonal feature whose fill is tiled (ADR-0021): the visible
2589
2974
  * outline is drawn from the feature's full geometry as polylines (so it stays
@@ -2591,7 +2976,7 @@ class GoogleMapsLayerPort {
2591
2976
  * clipped geometry as fill-only polygons. A fully covered loser yields no fill
2592
2977
  * polygon — just its outline.
2593
2978
  */
2594
- renderTiledPolygonFeature(fullGeometry, fillGeometry, style, fid, isStrokeOnly, config, tracked, map) {
2979
+ renderTiledPolygonFeature(fullGeometry, fillGeometry, style, fid, config, tracked, map) {
2595
2980
  const polygonParts = (geometry) => {
2596
2981
  if (geometry.type === "Polygon")
2597
2982
  return [geometry.coordinates];
@@ -2611,7 +2996,7 @@ class GoogleMapsLayerPort {
2611
2996
  if (first === undefined)
2612
2997
  continue;
2613
2998
  const outline = this.createPolyline([...ring, first], style, map, TILED_OUTLINE_Z_INDEX);
2614
- tracked.shapes.push(outline);
2999
+ tracked.shapes.add(outline);
2615
3000
  this.pushToFeatureMap(tracked.featureIdToShapes, fid, outline);
2616
3001
  if (config.interactive !== "none") {
2617
3002
  this.attachShapeInteractionListeners(outline, fid, "polygon", tracked, config.id);
@@ -2619,19 +3004,18 @@ class GoogleMapsLayerPort {
2619
3004
  }
2620
3005
  }
2621
3006
  // Fill-only polygons from the clipped geometry.
2622
- const fillVisible = (style.fillOpacity ?? this.shapeStyleDefaults.polygon.fillOpacity) > 0;
2623
- const fillClickable = !isStrokeOnly && fillVisible && config.interactive !== "none";
3007
+ const fillClickable = this.isPolygonClickable(style, config, true);
2624
3008
  const fillZIndex = config.featureZIndexOverrides?.get(fid);
2625
3009
  for (const partCoords of polygonParts(fillGeometry)) {
2626
3010
  const paths = reactMapAdapterShared.extractPolygonPaths(partCoords);
2627
3011
  if (paths === null)
2628
3012
  continue;
2629
3013
  const fillPolygon = this.createFillOnlyPolygon(paths, style, map, fillClickable, fillZIndex);
2630
- tracked.shapes.push(fillPolygon);
3014
+ tracked.shapes.add(fillPolygon);
2631
3015
  tracked.fillOnlyShapes.add(fillPolygon);
2632
3016
  this.pushToFeatureMap(tracked.featureIdToShapes, fid, fillPolygon);
2633
- if (fillClickable) {
2634
- this.attachShapeInteractionListeners(fillPolygon, fid, "polygon", tracked, config.id);
3017
+ if (config.interactive !== "none") {
3018
+ this.syncPolygonListeners(fillPolygon, fillClickable, fid, tracked, config.id);
2635
3019
  }
2636
3020
  }
2637
3021
  }
@@ -2644,8 +3028,9 @@ class GoogleMapsLayerPort {
2644
3028
  * `geodesic` must match the parent polygon so hit areas align with the
2645
3029
  * visible stroke.
2646
3030
  */
2647
- createStrokeHitPolylines(paths, strokeWeight, map, geodesic = true) {
2648
- const hitWeight = Math.max(strokeWeight, 3);
3031
+ createStrokeHitPolylines(paths, style, map) {
3032
+ const hitWeight = this.strokeHitWeightFromStyle(style);
3033
+ const geodesic = style.geodesic ?? true;
2649
3034
  const polylines = [];
2650
3035
  for (const ring of paths) {
2651
3036
  if (ring.length < 2)
@@ -2666,26 +3051,30 @@ class GoogleMapsLayerPort {
2666
3051
  return polylines;
2667
3052
  }
2668
3053
  createShapePointMarker(position, style, map, interactive) {
2669
- const diameter = (style.pointRadius ?? this.shapeStyleDefaults.point.pointRadius) * 2;
2670
3054
  const el = document.createElement("div");
2671
3055
  el.style.boxSizing = "content-box";
2672
3056
  // A non-interactive source (drawing in progress) must let clicks through to the map.
2673
3057
  el.style.pointerEvents = interactive ? "auto" : "none";
2674
- el.style.width = `${diameter}px`;
2675
- el.style.height = `${diameter}px`;
2676
3058
  el.style.borderRadius = "50%";
2677
- const fillOpacity = style.fillOpacity ?? this.shapeStyleDefaults.point.fillOpacity;
2678
- const strokeOpacity = style.strokeOpacity ?? this.shapeStyleDefaults.point.strokeOpacity;
2679
- el.style.backgroundColor = reactMapColorUtils.colorWithOpacity(style.fill ?? "rgba(0,0,0,0.1)", fillOpacity);
2680
- el.style.borderWidth = `${style.strokeWidth ?? this.shapeStyleDefaults.point.strokeWidth}px`;
2681
3059
  el.style.borderStyle = "solid";
2682
- el.style.borderColor = reactMapColorUtils.colorWithOpacity(style.stroke ?? "#000", strokeOpacity);
2683
3060
  el.style.transform = reactMapAdapterShared.anchorFromBottomCenter("center");
3061
+ this.applyShapePointStyle(el, style);
2684
3062
  const marker = new google.maps.marker.AdvancedMarkerElement({ map, position, content: el });
2685
3063
  if (!interactive)
2686
3064
  reactMapAdapterShared.setHitTargetInert(marker.element, true);
2687
3065
  return marker;
2688
3066
  }
3067
+ /** The style-derived part of a point marker's element; shared by creation and in-place restyling. */
3068
+ applyShapePointStyle(el, style) {
3069
+ const diameter = (style.pointRadius ?? this.shapeStyleDefaults.point.pointRadius) * 2;
3070
+ const fillOpacity = style.fillOpacity ?? this.shapeStyleDefaults.point.fillOpacity;
3071
+ const strokeOpacity = style.strokeOpacity ?? this.shapeStyleDefaults.point.strokeOpacity;
3072
+ el.style.width = `${diameter}px`;
3073
+ el.style.height = `${diameter}px`;
3074
+ el.style.backgroundColor = reactMapColorUtils.colorWithOpacity(style.fill ?? "rgba(0,0,0,0.1)", fillOpacity);
3075
+ el.style.borderWidth = `${style.strokeWidth ?? this.shapeStyleDefaults.point.strokeWidth}px`;
3076
+ el.style.borderColor = reactMapColorUtils.colorWithOpacity(style.stroke ?? "#000", strokeOpacity);
3077
+ }
2689
3078
  // ============================================================================
2690
3079
  // Private: Route creation
2691
3080
  // ============================================================================
@@ -2818,6 +3207,7 @@ class GoogleMapsLayerPort {
2818
3207
  if (domEvent instanceof MouseEvent && this.isSymbolMarkerAtClientPosition(domEvent.clientX, domEvent.clientY)) {
2819
3208
  return;
2820
3209
  }
3210
+ this.pointerShapeHover = { handleId, featureId };
2821
3211
  this.emitEntityInteraction("hover-start", entity);
2822
3212
  }),
2823
3213
  shape.addListener("mousemove", (e) => {
@@ -2827,10 +3217,10 @@ class GoogleMapsLayerPort {
2827
3217
  this.pointerMoveReporter?.(latLngToPosition(latLng));
2828
3218
  }),
2829
3219
  shape.addListener("mouseout", () => {
3220
+ this.clearPointerShapeHover(handleId, featureId);
2830
3221
  this.emitEntityInteraction("hover-end", entity);
2831
3222
  }),
2832
3223
  ];
2833
- tracked.listeners.push(...listeners);
2834
3224
  tracked.shapeListeners.set(shape, listeners);
2835
3225
  }
2836
3226
  attachShapePointInteractionListeners(marker, featureId, shapeType, handleId) {
@@ -2853,9 +3243,11 @@ class GoogleMapsLayerPort {
2853
3243
  this.emitEntityInteraction("dblclick", entity);
2854
3244
  });
2855
3245
  content.addEventListener("mouseenter", () => {
3246
+ this.pointerShapeHover = { handleId, featureId };
2856
3247
  this.emitEntityInteraction("hover-start", entity);
2857
3248
  });
2858
3249
  content.addEventListener("mouseleave", () => {
3250
+ this.clearPointerShapeHover(handleId, featureId);
2859
3251
  this.emitEntityInteraction("hover-end", entity);
2860
3252
  });
2861
3253
  }