@octostar/map-component 0.2.10 → 0.2.12

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/dist/index.mjs CHANGED
@@ -5386,6 +5386,224 @@ function countFeaturesInBounds(featureGroups, bounds, options) {
5386
5386
  }
5387
5387
  return count;
5388
5388
  }
5389
+ const rafScheduler = {
5390
+ request: (cb) => requestAnimationFrame(() => cb()),
5391
+ cancel: (handle) => cancelAnimationFrame(handle)
5392
+ };
5393
+ function createRafCoalescer(run, scheduler = rafScheduler) {
5394
+ let reasons = /* @__PURE__ */ new Set();
5395
+ let handle = null;
5396
+ let running = false;
5397
+ const fire = () => {
5398
+ handle = null;
5399
+ const snapshot = reasons;
5400
+ reasons = /* @__PURE__ */ new Set();
5401
+ running = true;
5402
+ try {
5403
+ run(snapshot);
5404
+ } finally {
5405
+ running = false;
5406
+ }
5407
+ };
5408
+ return {
5409
+ schedule(reason) {
5410
+ reasons.add(reason);
5411
+ if (handle === null) {
5412
+ handle = scheduler.request(fire);
5413
+ }
5414
+ },
5415
+ isPending() {
5416
+ return handle !== null;
5417
+ },
5418
+ flush() {
5419
+ if (running || handle === null) return;
5420
+ scheduler.cancel(handle);
5421
+ fire();
5422
+ },
5423
+ cancel() {
5424
+ if (handle !== null) {
5425
+ scheduler.cancel(handle);
5426
+ handle = null;
5427
+ }
5428
+ reasons.clear();
5429
+ }
5430
+ };
5431
+ }
5432
+ const DEFAULT_LABEL_PRIORITY = 100;
5433
+ function createSelectionPaintCache() {
5434
+ return {
5435
+ presentIds: /* @__PURE__ */ new Set(),
5436
+ appliedSelectedIds: /* @__PURE__ */ new Set(),
5437
+ basePriorityById: /* @__PURE__ */ new Map(),
5438
+ updatable: true
5439
+ };
5440
+ }
5441
+ function recordPaintedFeature(cache, featureId, basePriority, isSelected) {
5442
+ if (cache.presentIds.has(featureId)) {
5443
+ cache.updatable = false;
5444
+ } else {
5445
+ cache.presentIds.add(featureId);
5446
+ }
5447
+ cache.basePriorityById.set(featureId, basePriority);
5448
+ if (isSelected) cache.appliedSelectedIds.add(featureId);
5449
+ }
5450
+ function applySelectionFastPath(cache, nextSelectedIds, source) {
5451
+ if (!cache || !cache.updatable || !source) return false;
5452
+ const diff = buildSelectionDiff(cache, nextSelectedIds);
5453
+ if (diff) {
5454
+ try {
5455
+ source.updateData(diff);
5456
+ } catch {
5457
+ return false;
5458
+ }
5459
+ }
5460
+ const applied = /* @__PURE__ */ new Set();
5461
+ nextSelectedIds.forEach((id) => {
5462
+ if (cache.presentIds.has(id)) applied.add(id);
5463
+ });
5464
+ cache.appliedSelectedIds = applied;
5465
+ return true;
5466
+ }
5467
+ function needsDrawSelectionUpdate(input) {
5468
+ return input.editableSelectionCount > 0 || input.drawSelectedCount > 0 || input.currentMode !== input.targetMode;
5469
+ }
5470
+ function buildSelectionDiff(cache, nextSelectedIds) {
5471
+ const update = [];
5472
+ cache.appliedSelectedIds.forEach((id) => {
5473
+ if (!nextSelectedIds.has(id) && cache.presentIds.has(id)) {
5474
+ update.push({
5475
+ id,
5476
+ addOrUpdateProperties: [
5477
+ { key: "isSelected", value: 0 },
5478
+ { key: "_labelPriority", value: cache.basePriorityById.get(id) ?? DEFAULT_LABEL_PRIORITY }
5479
+ ]
5480
+ });
5481
+ }
5482
+ });
5483
+ nextSelectedIds.forEach((id) => {
5484
+ if (!cache.appliedSelectedIds.has(id) && cache.presentIds.has(id)) {
5485
+ update.push({
5486
+ id,
5487
+ addOrUpdateProperties: [
5488
+ { key: "isSelected", value: 1 },
5489
+ { key: "_labelPriority", value: 0 }
5490
+ ]
5491
+ });
5492
+ }
5493
+ });
5494
+ return update.length > 0 ? { update } : null;
5495
+ }
5496
+ function deepClone(value) {
5497
+ try {
5498
+ return structuredClone(value);
5499
+ } catch {
5500
+ return JSON.parse(JSON.stringify(value));
5501
+ }
5502
+ }
5503
+ function cloneFeatures(features) {
5504
+ try {
5505
+ return structuredClone(features);
5506
+ } catch {
5507
+ return features.map((f) => deepClone(f));
5508
+ }
5509
+ }
5510
+ function numberEquals(a, b) {
5511
+ return a === b || !Number.isFinite(a) && !Number.isFinite(b);
5512
+ }
5513
+ function coordinatesEqual(a, b) {
5514
+ if (typeof a === "number" || typeof b === "number") {
5515
+ return typeof a === "number" && typeof b === "number" && numberEquals(a, b);
5516
+ }
5517
+ if (a.length !== b.length) return false;
5518
+ for (let i = 0; i < a.length; i++) {
5519
+ if (!coordinatesEqual(a[i], b[i])) return false;
5520
+ }
5521
+ return true;
5522
+ }
5523
+ function bboxEqual(a, b) {
5524
+ if (a === void 0 || b === void 0) return a === b;
5525
+ if (a.length !== b.length) return false;
5526
+ for (let i = 0; i < a.length; i++) {
5527
+ if (!numberEquals(a[i], b[i])) return false;
5528
+ }
5529
+ return true;
5530
+ }
5531
+ function geometryEquals(a, b) {
5532
+ if (a == null || b == null) return a === b;
5533
+ if (a.type !== b.type) return false;
5534
+ if (!bboxEqual(a.bbox, b.bbox)) return false;
5535
+ if (a.type === "GeometryCollection") {
5536
+ const bGeometries = b.geometries;
5537
+ if (a.geometries.length !== bGeometries.length) return false;
5538
+ for (let i = 0; i < a.geometries.length; i++) {
5539
+ if (!geometryEquals(a.geometries[i], bGeometries[i])) return false;
5540
+ }
5541
+ return true;
5542
+ }
5543
+ return coordinatesEqual(
5544
+ a.coordinates,
5545
+ b.coordinates
5546
+ );
5547
+ }
5548
+ function collectVisibleFeatureGroups(drawFeatures, readOnlyByLayer, layers) {
5549
+ const groups = [];
5550
+ if (drawFeatures) groups.push(drawFeatures);
5551
+ const layerById = new Map(layers.map((l) => [l.id, l]));
5552
+ readOnlyByLayer.forEach((features, layerId) => {
5553
+ var _a;
5554
+ if ((_a = layerById.get(layerId)) == null ? void 0 : _a.visible) groups.push(features);
5555
+ });
5556
+ return groups;
5557
+ }
5558
+ function extendBounds(bounds, coords) {
5559
+ if (!Array.isArray(coords) || coords.length === 0) return;
5560
+ if (typeof coords[0] === "number") {
5561
+ const lng = coords[0];
5562
+ const lat = coords[1];
5563
+ if (lng < bounds.minLng) bounds.minLng = lng;
5564
+ if (lng > bounds.maxLng) bounds.maxLng = lng;
5565
+ if (lat < bounds.minLat) bounds.minLat = lat;
5566
+ if (lat > bounds.maxLat) bounds.maxLat = lat;
5567
+ return;
5568
+ }
5569
+ for (const nested of coords) extendBounds(bounds, nested);
5570
+ }
5571
+ function computeBounds(coords) {
5572
+ const bounds = { minLng: Infinity, minLat: Infinity, maxLng: -Infinity, maxLat: -Infinity };
5573
+ extendBounds(bounds, coords);
5574
+ return bounds;
5575
+ }
5576
+ function boundsDisjoint(a, b) {
5577
+ return a.maxLng < b.minLng || a.minLng > b.maxLng || a.maxLat < b.minLat || a.minLat > b.maxLat;
5578
+ }
5579
+ function pointInBounds(lng, lat, b) {
5580
+ return lng >= b.minLng && lng <= b.maxLng && lat >= b.minLat && lat <= b.maxLat;
5581
+ }
5582
+ function hitTestSelection(featureGroups, selectionPolygon, options) {
5583
+ const selectionBounds = computeBounds(selectionPolygon.geometry.coordinates);
5584
+ const rectExactForPoints = (options == null ? void 0 : options.axisAlignedRectangle) === true;
5585
+ const selectedIds = [];
5586
+ for (const features of featureGroups) {
5587
+ for (const feature of features) {
5588
+ try {
5589
+ const geom = feature.geometry;
5590
+ let isInside = false;
5591
+ if ((geom == null ? void 0 : geom.type) === "Point") {
5592
+ const lng = geom.coordinates[0];
5593
+ const lat = geom.coordinates[1];
5594
+ isInside = typeof lng === "number" && typeof lat === "number" && pointInBounds(lng, lat, selectionBounds) && (rectExactForPoints || turf.booleanPointInPolygon(geom.coordinates, selectionPolygon));
5595
+ } else if ((geom == null ? void 0 : geom.type) === "LineString" || (geom == null ? void 0 : geom.type) === "Polygon" || (geom == null ? void 0 : geom.type) === "MultiPoint" || (geom == null ? void 0 : geom.type) === "MultiLineString" || (geom == null ? void 0 : geom.type) === "MultiPolygon") {
5596
+ isInside = !boundsDisjoint(computeBounds(geom.coordinates), selectionBounds) && turf.booleanIntersects(feature, selectionPolygon);
5597
+ }
5598
+ if (isInside && feature.id) {
5599
+ selectedIds.push(String(feature.id));
5600
+ }
5601
+ } catch {
5602
+ }
5603
+ }
5604
+ }
5605
+ return selectedIds;
5606
+ }
5389
5607
  const OTHER_BUCKET_KEY = "__other__";
5390
5608
  const OTHER_BUCKET_LABEL = "Other";
5391
5609
  const OTHER_BUCKET_COLOR = "#9ca3af";
@@ -6369,14 +6587,22 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6369
6587
  return false;
6370
6588
  }
6371
6589
  });
6372
- draw.current.changeMode(mode, { featureIds: visibleEditableIds });
6590
+ const needsDrawCall = needsDrawSelectionUpdate({
6591
+ editableSelectionCount: visibleEditableIds.length,
6592
+ drawSelectedCount: draw.current.getSelectedIds().length,
6593
+ currentMode: draw.current.getMode(),
6594
+ targetMode: mode
6595
+ });
6596
+ if (needsDrawCall) {
6597
+ draw.current.changeMode(mode, { featureIds: visibleEditableIds });
6598
+ }
6373
6599
  setSelectedFeatureIds(nextIds);
6374
6600
  selectedFeatureIdsRef.current = nextIds;
6375
6601
  if (options == null ? void 0 : options.focusProperties) setShowProperties(nextIds.length > 0);
6376
6602
  if (onFeatureSelect) {
6377
6603
  onFeatureSelect(nextIds.map((id) => getFeatureById(id)).filter(Boolean));
6378
6604
  }
6379
- syncRef.current();
6605
+ scheduleSync("selection");
6380
6606
  if (readOnlyIds.length > 0) {
6381
6607
  queueMicrotask(() => {
6382
6608
  isSelectingReadOnlyRef.current = false;
@@ -6399,9 +6625,26 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6399
6625
  setFeatureCount(drawFeatureCount + hiddenCount + readOnlyCount);
6400
6626
  }, []);
6401
6627
  const timelineVisibleIdsRef = useRef(null);
6402
- const timelineSyncRafRef = useRef(null);
6403
6628
  const syncRef = useRef(() => {
6404
6629
  });
6630
+ const trySelectionFastPathRef = useRef(null);
6631
+ const syncCoalescerRef = useRef(null);
6632
+ if (syncCoalescerRef.current === null) {
6633
+ syncCoalescerRef.current = createRafCoalescer((reasons) => {
6634
+ var _a;
6635
+ if (reasons.size === 1 && reasons.has("selection") && ((_a = trySelectionFastPathRef.current) == null ? void 0 : _a.call(trySelectionFastPathRef))) {
6636
+ return;
6637
+ }
6638
+ syncRef.current();
6639
+ });
6640
+ }
6641
+ const scheduleSync = useCallback((reason = "full") => {
6642
+ var _a;
6643
+ (_a = syncCoalescerRef.current) == null ? void 0 : _a.schedule(reason);
6644
+ }, []);
6645
+ useEffect(() => {
6646
+ scheduleSync();
6647
+ }, [layers, scheduleSync]);
6405
6648
  const hashStringToColor$1 = useCallback((str) => hashStringToColor(str), []);
6406
6649
  const selectedFeatureColors = useMemo(() => {
6407
6650
  if (selectedFeatureIds.length === 0) return /* @__PURE__ */ new Map();
@@ -6487,6 +6730,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6487
6730
  );
6488
6731
  }, []);
6489
6732
  const isSyncingRef = useRef(false);
6733
+ const selectionPaintCacheRef = useRef(null);
6490
6734
  const syncFeaturesToCustomLayers = useCallback(() => {
6491
6735
  if (!map.current) return;
6492
6736
  if (isSyncingRef.current) return;
@@ -6497,7 +6741,9 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6497
6741
  const drawFeatures = draw.current ? draw.current.getAll() : { type: "FeatureCollection", features: [] };
6498
6742
  const drawSelectedIds = draw.current ? draw.current.getSelectedIds() : [];
6499
6743
  const componentSelectedIds = selectedFeatureIdsRef.current;
6500
- const selectedIds = Array.from(/* @__PURE__ */ new Set([...drawSelectedIds, ...componentSelectedIds]));
6744
+ const selectedIdSet = /* @__PURE__ */ new Set();
6745
+ drawSelectedIds.forEach((id) => selectedIdSet.add(String(id)));
6746
+ componentSelectedIds.forEach((id) => selectedIdSet.add(String(id)));
6501
6747
  const visibleSet = timelineVisibleIdsRef.current;
6502
6748
  const currentLayers = layersRef.current;
6503
6749
  const isLayerVisible = (layer) => {
@@ -6602,6 +6848,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6602
6848
  }
6603
6849
  });
6604
6850
  });
6851
+ const paintCache = createSelectionPaintCache();
6605
6852
  const sourceData = {
6606
6853
  type: "FeatureCollection",
6607
6854
  features: allFeatures.map((f) => {
@@ -6679,7 +6926,6 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6679
6926
  const isPoint = f.geometry.type === "Point";
6680
6927
  const hideByHeatmap = isPoint && ((_i = layer == null ? void 0 : layer.heatmapConfig) == null ? void 0 : _i.enabled) === true;
6681
6928
  const hideByCluster = isPoint && ((_j = layer == null ? void 0 : layer.clusterConfig) == null ? void 0 : _j.enabled) === true && !((_k = layer == null ? void 0 : layer.clusterConfig) == null ? void 0 : _k.showPointDots);
6682
- const isSelected = selectedIds.includes(featureId);
6683
6929
  const hideByTrace = false;
6684
6930
  const isLine = f.geometry.type === "LineString" || f.geometry.type === "MultiLineString";
6685
6931
  const hideFlatFor3DOnly = isLine && (layer == null ? void 0 : layer.hasAltitudeData) && (layer == null ? void 0 : layer.altitudeDisplayMode) === "3d";
@@ -6697,14 +6943,12 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6697
6943
  }
6698
6944
  });
6699
6945
  }
6700
- const isFeatureSelected = (enableSelectionRef.current || enableDrawingRef.current) && selectedIds.includes(featureId) ? 1 : 0;
6701
- let labelPriority = 100;
6702
- if (isFeatureSelected) {
6703
- labelPriority = 0;
6704
- } else if (traceLabel) {
6705
- labelPriority = 5;
6946
+ const isFeatureSelected = (enableSelectionRef.current || enableDrawingRef.current) && selectedIdSet.has(featureId) ? 1 : 0;
6947
+ let basePriority = DEFAULT_LABEL_PRIORITY;
6948
+ if (traceLabel) {
6949
+ basePriority = 5;
6706
6950
  } else if (((_l = f.properties) == null ? void 0 : _l.kind) === "text") {
6707
- labelPriority = ((_m = f.properties) == null ? void 0 : _m.fontWeight) === "bold" ? 8 : 10;
6951
+ basePriority = ((_m = f.properties) == null ? void 0 : _m.fontWeight) === "bold" ? 8 : 10;
6708
6952
  } else {
6709
6953
  const idStr = String(featureId);
6710
6954
  let h = 0;
@@ -6712,8 +6956,10 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6712
6956
  h = (h << 5) - h + idStr.charCodeAt(i);
6713
6957
  h |= 0;
6714
6958
  }
6715
- labelPriority = 100 + Math.abs(h) % 1e3;
6959
+ basePriority = DEFAULT_LABEL_PRIORITY + Math.abs(h) % 1e3;
6716
6960
  }
6961
+ const labelPriority = isFeatureSelected ? 0 : basePriority;
6962
+ recordPaintedFeature(paintCache, featureId, basePriority, isFeatureSelected === 1);
6717
6963
  return {
6718
6964
  type: "Feature",
6719
6965
  id: featureId,
@@ -6746,6 +6992,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
6746
6992
  })
6747
6993
  };
6748
6994
  source.setData(sourceData);
6995
+ selectionPaintCacheRef.current = paintCache;
6749
6996
  const traceSource = map.current.getSource("trace-features");
6750
6997
  if (traceSource) {
6751
6998
  const traceSegments = [];
@@ -7264,13 +7511,13 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
7264
7511
  layerTemporalCacheRef.current.clear();
7265
7512
  updateFeatureCount();
7266
7513
  scheduleTimelineUpdate();
7267
- syncFeaturesToCustomLayers();
7514
+ scheduleSync();
7268
7515
  setFeatureUpdateCounter((c) => c + 1);
7269
7516
  queueMicrotask(() => {
7270
7517
  isSelectingReadOnlyRef.current = false;
7271
7518
  });
7272
7519
  },
7273
- [cleanupOrphanLayerSources, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers]
7520
+ [cleanupOrphanLayerSources, updateFeatureCount, scheduleTimelineUpdate, scheduleSync]
7274
7521
  );
7275
7522
  const buildDiffCommand = useCallback(
7276
7523
  (label, coalesceKey, before, after, beforeLayers, afterLayers, beforeSelectedIds, afterSelectedIds) => {
@@ -8382,7 +8629,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8382
8629
  saveStateForUndo("draw feature", {
8383
8630
  excludeDrawIds: new Set(newFeatures.map((f) => String(f.id)))
8384
8631
  });
8385
- syncRef.current();
8632
+ scheduleSync();
8386
8633
  setTimeout(() => {
8387
8634
  setActiveMode("simple_select");
8388
8635
  if (drawInstance.getMode() !== "simple_select") {
@@ -8394,7 +8641,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8394
8641
  });
8395
8642
  });
8396
8643
  mapInstance.on("draw.render", () => {
8397
- syncRef.current();
8644
+ scheduleSync();
8398
8645
  });
8399
8646
  mapInstance.on("draw.update", (e) => {
8400
8647
  const activeLayerId = activeEditingLayerIdRef.current;
@@ -8424,9 +8671,9 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8424
8671
  }
8425
8672
  } else {
8426
8673
  const previousGeometry = originalGeometriesRef.current.get(fid);
8427
- const geometryChanged = !previousGeometry || JSON.stringify(previousGeometry) !== JSON.stringify(feature.geometry);
8674
+ const geometryChanged = !previousGeometry || !geometryEquals(previousGeometry, feature.geometry);
8428
8675
  if (geometryChanged) {
8429
- originalGeometriesRef.current.set(fid, JSON.parse(JSON.stringify(feature.geometry)));
8676
+ originalGeometriesRef.current.set(fid, deepClone(feature.geometry));
8430
8677
  if (!externalPersistence) {
8431
8678
  drawInstance.setFeatureProperty(fid, "_unsaved", true);
8432
8679
  }
@@ -8435,7 +8682,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8435
8682
  }
8436
8683
  }
8437
8684
  });
8438
- syncRef.current();
8685
+ scheduleSync();
8439
8686
  if (e.action === "change_coordinates" && !hasRevertedFeatures) {
8440
8687
  scheduleTimelineUpdate();
8441
8688
  recordMoveFromPreDrag();
@@ -8455,7 +8702,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8455
8702
  updateFeatureCount();
8456
8703
  scheduleTimelineUpdate();
8457
8704
  saveStateForUndo("delete feature", { addDrawFeatures: e.features });
8458
- syncRef.current();
8705
+ scheduleSync();
8459
8706
  onFeatureDelete == null ? void 0 : onFeatureDelete(deletedIds);
8460
8707
  });
8461
8708
  mapInstance.on("draw.selectionchange", (e) => {
@@ -8471,7 +8718,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8471
8718
  e.features.forEach((feature) => {
8472
8719
  const fid = feature.id;
8473
8720
  if (!originalGeometriesRef.current.has(fid)) {
8474
- originalGeometriesRef.current.set(fid, JSON.parse(JSON.stringify(feature.geometry)));
8721
+ originalGeometriesRef.current.set(fid, deepClone(feature.geometry));
8475
8722
  }
8476
8723
  });
8477
8724
  }
@@ -8488,11 +8735,11 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8488
8735
  if (enableProperties && (autoOpenProperties || !wasJustCreated)) {
8489
8736
  setShowProperties(ids.length > 0);
8490
8737
  }
8491
- syncRef.current();
8738
+ scheduleSync("selection");
8492
8739
  onFeatureSelect == null ? void 0 : onFeatureSelect(e.features);
8493
8740
  });
8494
8741
  mapInstance.on("draw.modechange", () => {
8495
- syncRef.current();
8742
+ scheduleSync();
8496
8743
  });
8497
8744
  mapInstance.on("click", (e) => {
8498
8745
  var _a;
@@ -8540,7 +8787,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8540
8787
  setSelectedFeatureIds(nextIds);
8541
8788
  selectedFeatureIdsRef.current = nextIds;
8542
8789
  if (enableProperties) setShowProperties(nextIds.length > 0);
8543
- syncRef.current();
8790
+ scheduleSync("selection");
8544
8791
  const selectedFeatures2 = [];
8545
8792
  for (const id of nextIds) {
8546
8793
  readOnlyFeatures.current.forEach((layerFeatures) => {
@@ -8573,7 +8820,9 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8573
8820
  setMapError(error instanceof Error ? error.message : "Failed to initialize map");
8574
8821
  }
8575
8822
  return () => {
8823
+ var _a;
8576
8824
  if (timelineUpdateTimerRef.current) clearTimeout(timelineUpdateTimerRef.current);
8825
+ (_a = syncCoalescerRef.current) == null ? void 0 : _a.cancel();
8577
8826
  if (map.current) {
8578
8827
  map.current.remove();
8579
8828
  map.current = null;
@@ -8696,10 +8945,11 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8696
8945
  updateFeatureCount();
8697
8946
  scheduleTimelineUpdate();
8698
8947
  }
8699
- requestAnimationFrame(() => syncRef.current());
8948
+ scheduleSync();
8700
8949
  return true;
8701
8950
  },
8702
8951
  captureScreenshot: async (options) => {
8952
+ var _a;
8703
8953
  const m = map.current;
8704
8954
  if (!m || !isMapLoadedRef.current) {
8705
8955
  toast({
@@ -8709,6 +8959,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8709
8959
  });
8710
8960
  throw new Error("Map is not ready");
8711
8961
  }
8962
+ (_a = syncCoalescerRef.current) == null ? void 0 : _a.flush();
8712
8963
  const SCREENSHOT_RENDER_TIMEOUT_MS = 1e4;
8713
8964
  try {
8714
8965
  await new Promise((resolve, reject) => {
@@ -8851,7 +9102,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8851
9102
  setLayers(updatedLayers);
8852
9103
  updateFeatureCount();
8853
9104
  scheduleTimelineUpdate();
8854
- syncFeaturesToCustomLayers();
9105
+ scheduleSync();
8855
9106
  if (!(options == null ? void 0 : options.skipFit) && geojson && geojson.features.length > 0 && map.current) {
8856
9107
  try {
8857
9108
  const bbox = turf.bbox(geojson);
@@ -8886,9 +9137,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8886
9137
  setLayers(updatedLayers);
8887
9138
  updateFeatureCount();
8888
9139
  scheduleTimelineUpdate();
8889
- requestAnimationFrame(() => {
8890
- syncRef.current();
8891
- });
9140
+ scheduleSync();
8892
9141
  return true;
8893
9142
  },
8894
9143
  setLayerVisibility: (layerId, visible) => {
@@ -8902,9 +9151,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8902
9151
  setLayers(nextLayers2);
8903
9152
  updateFeatureCount();
8904
9153
  scheduleTimelineUpdate();
8905
- requestAnimationFrame(() => {
8906
- syncRef.current();
8907
- });
9154
+ scheduleSync();
8908
9155
  return;
8909
9156
  }
8910
9157
  if (!draw.current) return;
@@ -8942,9 +9189,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8942
9189
  setLayers(nextLayers);
8943
9190
  updateFeatureCount();
8944
9191
  scheduleTimelineUpdate();
8945
- requestAnimationFrame(() => {
8946
- syncRef.current();
8947
- });
9192
+ scheduleSync();
8948
9193
  },
8949
9194
  addFeatures: (layerId, features, options) => {
8950
9195
  var _a;
@@ -8985,9 +9230,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
8985
9230
  if (layerHasTemporal) {
8986
9231
  scheduleTimelineUpdate();
8987
9232
  }
8988
- requestAnimationFrame(() => {
8989
- syncRef.current();
8990
- });
9233
+ scheduleSync();
8991
9234
  return addedIds2;
8992
9235
  }
8993
9236
  if (!draw.current) return [];
@@ -9017,7 +9260,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9017
9260
  setLayers(updatedLayers);
9018
9261
  updateFeatureCount();
9019
9262
  scheduleTimelineUpdate();
9020
- syncFeaturesToCustomLayers();
9263
+ scheduleSync();
9021
9264
  return addedIds;
9022
9265
  },
9023
9266
  removeFeatures: (featureIds, options) => {
@@ -9053,7 +9296,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9053
9296
  });
9054
9297
  updateFeatureCount();
9055
9298
  scheduleTimelineUpdate();
9056
- requestAnimationFrame(() => syncRef.current());
9299
+ scheduleSync();
9057
9300
  onFeatureDelete == null ? void 0 : onFeatureDelete(featureIds);
9058
9301
  },
9059
9302
  selectFeatures: (featureIds) => {
@@ -9129,7 +9372,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9129
9372
  ));
9130
9373
  updateFeatureCount();
9131
9374
  scheduleTimelineUpdate();
9132
- syncFeaturesToCustomLayers();
9375
+ scheduleSync();
9133
9376
  return addedIds;
9134
9377
  },
9135
9378
  getActiveEditableLayerId: () => {
@@ -9243,9 +9486,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9243
9486
  layersRef.current = updatedLayers;
9244
9487
  setLayers(updatedLayers);
9245
9488
  updateFeatureCount();
9246
- requestAnimationFrame(() => {
9247
- syncRef.current();
9248
- });
9489
+ scheduleSync();
9249
9490
  return true;
9250
9491
  },
9251
9492
  setLayerData: (name, geojson, options) => {
@@ -9319,9 +9560,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9319
9560
  return result;
9320
9561
  });
9321
9562
  updateFeatureCount();
9322
- requestAnimationFrame(() => {
9323
- syncRef.current();
9324
- });
9563
+ scheduleSync();
9325
9564
  if ((options == null ? void 0 : options.fitPadding) != null && geojson.features.length > 0 && map.current) {
9326
9565
  try {
9327
9566
  const bbox = turf.bbox(geojson);
@@ -9375,21 +9614,18 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9375
9614
  const features = [];
9376
9615
  const readOnly = readOnlyFeatures.current.get(layerId);
9377
9616
  if (readOnly) {
9378
- return readOnly.map((f) => JSON.parse(JSON.stringify(f)));
9617
+ return cloneFeatures(readOnly);
9379
9618
  }
9380
9619
  if (layer.visible) {
9381
9620
  layer.featureIds.forEach((id) => {
9382
9621
  var _a;
9383
9622
  const f = (_a = draw.current) == null ? void 0 : _a.get(id);
9384
- if (f) {
9385
- features.push(JSON.parse(JSON.stringify(f)));
9386
- }
9623
+ if (f) features.push(f);
9387
9624
  });
9388
9625
  } else {
9389
- const hidden = hiddenFeatures.current.get(layerId) || [];
9390
- return hidden.map((f) => JSON.parse(JSON.stringify(f)));
9626
+ return cloneFeatures(hiddenFeatures.current.get(layerId) || []);
9391
9627
  }
9392
- return features;
9628
+ return cloneFeatures(features);
9393
9629
  },
9394
9630
  getTimelineRange: () => {
9395
9631
  const ts = timelineStoreApi.getState();
@@ -9428,7 +9664,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9428
9664
  var _a;
9429
9665
  (_a = restoreEnrichedGeoJSONRef.current) == null ? void 0 : _a.call(restoreEnrichedGeoJSONRef, enriched);
9430
9666
  }
9431
- }), [layers, isEditMode, isMapLoaded, getSelectedFeatures, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, onFeatureDelete, onTimelineChange, activeEditingLayerId, currentMapStyle, activeMode, saveStateForUndo, history]);
9667
+ }), [layers, isEditMode, isMapLoaded, getSelectedFeatures, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, onFeatureDelete, onTimelineChange, activeEditingLayerId, currentMapStyle, activeMode, saveStateForUndo, history]);
9432
9668
  const handleModeChange = useCallback((mode) => {
9433
9669
  if (!draw.current || !isMapLoaded) return;
9434
9670
  if (!hasVisibleLayers && mode !== "simple_select" && mode !== "view_select") {
@@ -9505,12 +9741,12 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9505
9741
  setShowProperties(false);
9506
9742
  updateFeatureCount();
9507
9743
  scheduleTimelineUpdate();
9508
- syncFeaturesToCustomLayers();
9744
+ scheduleSync();
9509
9745
  toast({
9510
9746
  title: "Layer cleared",
9511
9747
  description: `Removed all features from "${activeLayer.name}"`
9512
9748
  });
9513
- }, [activeEditingLayerId, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, toast]);
9749
+ }, [activeEditingLayerId, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, toast]);
9514
9750
  const handleDeleteSelected = useCallback(() => {
9515
9751
  if (selectedFeatureIds.length === 0) return;
9516
9752
  const idSet = new Set(selectedFeatureIds);
@@ -9561,8 +9797,8 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9561
9797
  setShowProperties(false);
9562
9798
  updateFeatureCount();
9563
9799
  scheduleTimelineUpdate();
9564
- syncFeaturesToCustomLayers();
9565
- }, [selectedFeatureIds, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, onFeatureDelete]);
9800
+ scheduleSync();
9801
+ }, [selectedFeatureIds, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, onFeatureDelete]);
9566
9802
  const handleToggleEditMode = useCallback(() => {
9567
9803
  setIsEditMode((prev) => {
9568
9804
  const newMode = !prev;
@@ -9698,9 +9934,9 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9698
9934
  updateFeatureCount();
9699
9935
  scheduleTimelineUpdate();
9700
9936
  saveStateForUndo();
9701
- syncFeaturesToCustomLayers();
9937
+ scheduleSync();
9702
9938
  onFeatureCreate == null ? void 0 : onFeatureCreate(feature);
9703
- }, [isMapLoaded, externalPersistence, enableProperties, autoOpenProperties, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, onFeatureCreate]);
9939
+ }, [isMapLoaded, externalPersistence, enableProperties, autoOpenProperties, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, onFeatureCreate]);
9704
9940
  const handleFeatureContextMenu = useCallback((e) => {
9705
9941
  var _a, _b;
9706
9942
  contextMenuFiredRef.current = true;
@@ -9885,7 +10121,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9885
10121
  if (properties.kind === "text" || ((_c = drawFeature.properties) == null ? void 0 : _c.kind) === "text") {
9886
10122
  draw.current.setFeatureProperty(featureId, "text", void 0);
9887
10123
  }
9888
- syncFeaturesToCustomLayers();
10124
+ scheduleSync();
9889
10125
  scheduleTimelineUpdate();
9890
10126
  setFeatureUpdateCounter((c) => c + 1);
9891
10127
  const updated = draw.current.get(featureId);
@@ -9920,13 +10156,13 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9920
10156
  }
9921
10157
  });
9922
10158
  if (updatedFeature) {
9923
- syncFeaturesToCustomLayers();
10159
+ scheduleSync();
9924
10160
  scheduleTimelineUpdate();
9925
10161
  setFeatureUpdateCounter((c) => c + 1);
9926
10162
  onFeatureUpdate == null ? void 0 : onFeatureUpdate(updatedFeature);
9927
10163
  }
9928
10164
  }
9929
- }, [saveStateForUndo, scheduleTimelineUpdate, onFeatureUpdate, syncFeaturesToCustomLayers]);
10165
+ }, [saveStateForUndo, scheduleTimelineUpdate, onFeatureUpdate, scheduleSync]);
9930
10166
  const handleUpdateGeometry = useCallback((featureId, geometry) => {
9931
10167
  if (!draw.current || !map.current) return;
9932
10168
  const feature = draw.current.get(featureId);
@@ -9948,13 +10184,13 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9948
10184
  draw.current.setFeatureProperty(featureId, "_unsaved", true);
9949
10185
  }
9950
10186
  draw.current.changeMode("simple_select", { featureIds: [featureId] });
9951
- syncFeaturesToCustomLayers();
10187
+ scheduleSync();
9952
10188
  setFeatureUpdateCounter((c) => c + 1);
9953
10189
  const updated = draw.current.get(featureId);
9954
10190
  if (updated) {
9955
10191
  onFeatureUpdate == null ? void 0 : onFeatureUpdate(updated);
9956
10192
  }
9957
- }, [saveStateForUndo, onFeatureUpdate, syncFeaturesToCustomLayers]);
10193
+ }, [saveStateForUndo, onFeatureUpdate, scheduleSync]);
9958
10194
  const handleFeatureSaved = useCallback((oldId, newId, metadata) => {
9959
10195
  if (!draw.current) return;
9960
10196
  const feature = draw.current.get(oldId);
@@ -9999,7 +10235,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
9999
10235
  setSelectedFeatureIds((prev) => prev.map((fid) => fid === oldId ? featureId : fid));
10000
10236
  draw.current.changeMode("simple_select", { featureIds: [featureId] });
10001
10237
  }
10002
- syncFeaturesToCustomLayers();
10238
+ scheduleSync();
10003
10239
  updateFeatureCount();
10004
10240
  setFeatureUpdateCounter((c) => c + 1);
10005
10241
  const savedFeatureId = featureId;
@@ -10010,7 +10246,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10010
10246
  }
10011
10247
  setFeatureUpdateCounter((c) => c + 1);
10012
10248
  });
10013
- }, [syncFeaturesToCustomLayers, updateFeatureCount]);
10249
+ }, [scheduleSync, updateFeatureCount]);
10014
10250
  const handleAddLayer = useCallback((name) => {
10015
10251
  const newLayer = {
10016
10252
  id: `layer-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
@@ -10095,9 +10331,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10095
10331
  updateFeatureCount();
10096
10332
  scheduleTimelineUpdate();
10097
10333
  if (layer.readOnly) {
10098
- requestAnimationFrame(() => {
10099
- syncRef.current();
10100
- });
10334
+ scheduleSync();
10101
10335
  }
10102
10336
  }, [layers, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate]);
10103
10337
  const handleMoveFeatureToLayer = useCallback((featureId, targetLayerId) => {
@@ -10155,10 +10389,10 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10155
10389
  }
10156
10390
  return remaining.length !== l.featureIds.length ? { ...l, featureIds: remaining } : l;
10157
10391
  }));
10158
- syncFeaturesToCustomLayers();
10392
+ scheduleSync();
10159
10393
  updateFeatureCount();
10160
10394
  }
10161
- }, [saveStateForUndo, findFeatureById, updateFeatureCount, syncFeaturesToCustomLayers]);
10395
+ }, [saveStateForUndo, findFeatureById, updateFeatureCount, scheduleSync]);
10162
10396
  const handleCopyFeaturesToLayer = useCallback((featureIds, targetLayerId) => {
10163
10397
  if (!draw.current) return;
10164
10398
  saveStateForUndo();
@@ -10215,10 +10449,10 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10215
10449
  }),
10216
10450
  { id: newLayerId, name: layerName, visible: true, featureIds: movedIds }
10217
10451
  ]);
10218
- syncFeaturesToCustomLayers();
10452
+ scheduleSync();
10219
10453
  updateFeatureCount();
10220
10454
  }
10221
- }, [saveStateForUndo, findFeatureById, updateFeatureCount, syncFeaturesToCustomLayers]);
10455
+ }, [saveStateForUndo, findFeatureById, updateFeatureCount, scheduleSync]);
10222
10456
  const handleCreateLayerAndCopyFeatures = useCallback((featureIds, layerName) => {
10223
10457
  if (!draw.current) return;
10224
10458
  saveStateForUndo();
@@ -10512,9 +10746,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10512
10746
  const mapInstance = map.current;
10513
10747
  const handleMove = () => {
10514
10748
  if (!hasArrowFeaturesRef.current) return;
10515
- if (syncRef.current) {
10516
- syncRef.current();
10517
- }
10749
+ scheduleSync();
10518
10750
  };
10519
10751
  mapInstance.on("move", handleMove);
10520
10752
  return () => {
@@ -10832,6 +11064,27 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10832
11064
  useEffect(() => {
10833
11065
  enableDrawingRef.current = enableDrawing;
10834
11066
  }, [enableDrawing]);
11067
+ const trySelectionFastPath = useCallback(() => {
11068
+ var _a;
11069
+ const m = map.current;
11070
+ if (!m) return true;
11071
+ const source = m.getSource("custom-features");
11072
+ const gate = enableSelectionRef.current || enableDrawingRef.current;
11073
+ const next = /* @__PURE__ */ new Set();
11074
+ if (gate) {
11075
+ (((_a = draw.current) == null ? void 0 : _a.getSelectedIds()) ?? []).forEach((id) => next.add(String(id)));
11076
+ selectedFeatureIdsRef.current.forEach((id) => next.add(String(id)));
11077
+ }
11078
+ return applySelectionFastPath(selectionPaintCacheRef.current, next, source);
11079
+ }, []);
11080
+ trySelectionFastPathRef.current = trySelectionFastPath;
11081
+ const collectSelectionFeatureGroups = useCallback(() => {
11082
+ return collectVisibleFeatureGroups(
11083
+ draw.current ? draw.current.getAll().features : null,
11084
+ readOnlyFeatures.current,
11085
+ layersRef.current
11086
+ );
11087
+ }, []);
10835
11088
  useEffect(() => {
10836
11089
  if (!map.current || !isMapLoaded) {
10837
11090
  return;
@@ -10922,29 +11175,6 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10922
11175
  [minLng, maxLat]
10923
11176
  // close polygon
10924
11177
  ]]);
10925
- const drawFeatures = draw.current ? draw.current.getAll().features : [];
10926
- const readOnlyFeaturesList = [];
10927
- readOnlyFeatures.current.forEach((features, layerId) => {
10928
- const layer = layersRef.current.find((l) => l.id === layerId);
10929
- if (layer == null ? void 0 : layer.visible) pushAll(readOnlyFeaturesList, features);
10930
- });
10931
- const allFeatures = [...drawFeatures, ...readOnlyFeaturesList];
10932
- const selectedIds = [];
10933
- allFeatures.forEach((feature) => {
10934
- try {
10935
- const geom = feature.geometry;
10936
- let isInside = false;
10937
- if (geom.type === "Point") {
10938
- isInside = turf.booleanPointInPolygon(geom.coordinates, selectionPolygon);
10939
- } else if (geom.type === "LineString" || geom.type === "Polygon" || geom.type === "MultiPoint" || geom.type === "MultiLineString" || geom.type === "MultiPolygon") {
10940
- isInside = turf.booleanIntersects(feature, selectionPolygon);
10941
- }
10942
- if (isInside && feature.id) {
10943
- selectedIds.push(String(feature.id));
10944
- }
10945
- } catch (err) {
10946
- }
10947
- });
10948
11178
  const isFilterAction = filterActiveRef.current && onSelectionComplete;
10949
11179
  if (isFilterAction) {
10950
11180
  onSelectionComplete(selectionPolygon.geometry);
@@ -10958,6 +11188,9 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10958
11188
  ]);
10959
11189
  }
10960
11190
  } else {
11191
+ const selectedIds = hitTestSelection(collectSelectionFeatureGroups(), selectionPolygon, {
11192
+ axisAlignedRectangle: true
11193
+ });
10961
11194
  const finalIds = e.shiftKey ? Array.from(/* @__PURE__ */ new Set([...selectedFeatureIdsRef.current, ...selectedIds])) : selectedIds;
10962
11195
  applySelectionState(finalIds, { focusProperties: true });
10963
11196
  }
@@ -10973,29 +11206,6 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
10973
11206
  const radiusKm = turf.distance(center, edge, { units: "kilometers" });
10974
11207
  if (radiusKm > 0) {
10975
11208
  const selectionPolygon = turf.circle([centerLngLat.lng, centerLngLat.lat], radiusKm, { steps: 64, units: "kilometers" });
10976
- const drawFeatures = draw.current ? draw.current.getAll().features : [];
10977
- const readOnlyFeaturesList = [];
10978
- readOnlyFeatures.current.forEach((features, layerId) => {
10979
- const layer = layersRef.current.find((l) => l.id === layerId);
10980
- if (layer == null ? void 0 : layer.visible) pushAll(readOnlyFeaturesList, features);
10981
- });
10982
- const allFeatures = [...drawFeatures, ...readOnlyFeaturesList];
10983
- const selectedIds = [];
10984
- allFeatures.forEach((feature) => {
10985
- try {
10986
- const geom = feature.geometry;
10987
- let isInside = false;
10988
- if (geom.type === "Point") {
10989
- isInside = turf.booleanPointInPolygon(geom.coordinates, selectionPolygon);
10990
- } else if (geom.type === "LineString" || geom.type === "Polygon" || geom.type === "MultiPoint" || geom.type === "MultiLineString" || geom.type === "MultiPolygon") {
10991
- isInside = turf.booleanIntersects(feature, selectionPolygon);
10992
- }
10993
- if (isInside && feature.id) {
10994
- selectedIds.push(String(feature.id));
10995
- }
10996
- } catch (err) {
10997
- }
10998
- });
10999
11209
  const isFilterAction = filterActiveRef.current && onSelectionComplete;
11000
11210
  if (isFilterAction) {
11001
11211
  onSelectionComplete(selectionPolygon.geometry);
@@ -11003,6 +11213,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11003
11213
  setPersistedSelectionPolygon(selectionPolygon.geometry.coordinates[0]);
11004
11214
  }
11005
11215
  } else {
11216
+ const selectedIds = hitTestSelection(collectSelectionFeatureGroups(), selectionPolygon);
11006
11217
  const finalIds = e.shiftKey ? Array.from(/* @__PURE__ */ new Set([...selectedFeatureIdsRef.current, ...selectedIds])) : selectedIds;
11007
11218
  applySelectionState(finalIds, { focusProperties: true });
11008
11219
  }
@@ -11029,29 +11240,6 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11029
11240
  }
11030
11241
  const closedPoints = [...currentPoints, currentPoints[0]];
11031
11242
  const selectionPolygon = turf.polygon([closedPoints]);
11032
- const drawFeatures = draw.current ? draw.current.getAll().features : [];
11033
- const readOnlyFeaturesList = [];
11034
- readOnlyFeatures.current.forEach((features, layerId) => {
11035
- const layer = layersRef.current.find((l) => l.id === layerId);
11036
- if (layer == null ? void 0 : layer.visible) pushAll(readOnlyFeaturesList, features);
11037
- });
11038
- const allFeatures = [...drawFeatures, ...readOnlyFeaturesList];
11039
- const selectedIds = [];
11040
- allFeatures.forEach((feature) => {
11041
- try {
11042
- const geom = feature.geometry;
11043
- let isInside = false;
11044
- if (geom.type === "Point") {
11045
- isInside = turf.booleanPointInPolygon(geom.coordinates, selectionPolygon);
11046
- } else if (geom.type === "LineString" || geom.type === "Polygon" || geom.type === "MultiPoint" || geom.type === "MultiLineString" || geom.type === "MultiPolygon") {
11047
- isInside = turf.booleanIntersects(feature, selectionPolygon);
11048
- }
11049
- if (isInside && feature.id) {
11050
- selectedIds.push(String(feature.id));
11051
- }
11052
- } catch (err) {
11053
- }
11054
- });
11055
11243
  const isFilterAction = filterActiveRef.current && onSelectionComplete;
11056
11244
  if (isFilterAction) {
11057
11245
  onSelectionComplete(selectionPolygon.geometry);
@@ -11059,6 +11247,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11059
11247
  setPersistedSelectionPolygon(closedPoints);
11060
11248
  }
11061
11249
  } else {
11250
+ const selectedIds = hitTestSelection(collectSelectionFeatureGroups(), selectionPolygon);
11062
11251
  const finalIds = e.shiftKey ? Array.from(/* @__PURE__ */ new Set([...selectedFeatureIdsRef.current, ...selectedIds])) : selectedIds;
11063
11252
  applySelectionState(finalIds, { focusProperties: true });
11064
11253
  }
@@ -11108,7 +11297,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11108
11297
  window.removeEventListener("keydown", handleKeyDown);
11109
11298
  canvas.removeEventListener("contextmenu", handleContextMenu);
11110
11299
  };
11111
- }, [selectionMode, isMapLoaded, onFeatureSelect, onSelectionComplete, persistSelection, isSelectionToolActive]);
11300
+ }, [selectionMode, isMapLoaded, onFeatureSelect, onSelectionComplete, persistSelection, isSelectionToolActive, collectSelectionFeatureGroups]);
11112
11301
  const shiftSelectionRef = useRef({ isActive: false, startPoint: null, completedAt: 0 });
11113
11302
  useEffect(() => {
11114
11303
  if (!map.current || !isMapLoaded || !draw.current) return;
@@ -11168,28 +11357,8 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11168
11357
  [minLng, minLat],
11169
11358
  [minLng, maxLat]
11170
11359
  ]]);
11171
- const drawFeatures = draw.current ? draw.current.getAll().features : [];
11172
- const readOnlyFeaturesList = [];
11173
- readOnlyFeatures.current.forEach((features, layerId) => {
11174
- const layer = layersRef.current.find((l) => l.id === layerId);
11175
- if (layer == null ? void 0 : layer.visible) pushAll(readOnlyFeaturesList, features);
11176
- });
11177
- const allFeatures = [...drawFeatures, ...readOnlyFeaturesList];
11178
- const selectedIds = [];
11179
- allFeatures.forEach((feature) => {
11180
- try {
11181
- const geom = feature.geometry;
11182
- let isInside = false;
11183
- if (geom.type === "Point") {
11184
- isInside = turf.booleanPointInPolygon(geom.coordinates, selectionPolygon);
11185
- } else if (geom.type === "LineString" || geom.type === "Polygon" || geom.type === "MultiPoint" || geom.type === "MultiLineString" || geom.type === "MultiPolygon") {
11186
- isInside = turf.booleanIntersects(feature, selectionPolygon);
11187
- }
11188
- if (isInside && feature.id) {
11189
- selectedIds.push(String(feature.id));
11190
- }
11191
- } catch {
11192
- }
11360
+ const selectedIds = hitTestSelection(collectSelectionFeatureGroups(), selectionPolygon, {
11361
+ axisAlignedRectangle: true
11193
11362
  });
11194
11363
  const mergedIds = Array.from(/* @__PURE__ */ new Set([...selectedFeatureIdsRef.current, ...selectedIds]));
11195
11364
  shiftSelectionRef.current = { isActive: false, startPoint: null, completedAt: Date.now() };
@@ -11209,7 +11378,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11209
11378
  map.current.dragPan.enable();
11210
11379
  }
11211
11380
  };
11212
- }, [isMapLoaded, isSelectionToolActive, applySelectionState]);
11381
+ }, [isMapLoaded, isSelectionToolActive, applySelectionState, collectSelectionFeatureGroups]);
11213
11382
  useEffect(() => {
11214
11383
  if (!map.current || !isMapLoaded) return;
11215
11384
  const mapInstance = map.current;
@@ -11343,7 +11512,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11343
11512
  (layer) => layer.id === targetLayer.id ? { ...layer, featureIds: [...layer.featureIds, featureId] } : layer
11344
11513
  );
11345
11514
  });
11346
- syncFeaturesToCustomLayers();
11515
+ scheduleSync();
11347
11516
  const newCount = drawInstance.getAll().features.length;
11348
11517
  setFeatureCount(newCount);
11349
11518
  drawInstance.changeMode("simple_select", { featureIds: [featureId] });
@@ -11465,7 +11634,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11465
11634
  (layer) => layer.id === targetLayer.id ? { ...layer, featureIds: [...layer.featureIds, featureId] } : layer
11466
11635
  );
11467
11636
  });
11468
- syncFeaturesToCustomLayers();
11637
+ scheduleSync();
11469
11638
  const newCount = drawInstance.getAll().features.length;
11470
11639
  setFeatureCount(newCount);
11471
11640
  justCreatedRectangleRef.current = true;
@@ -11821,7 +11990,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11821
11990
  }
11822
11991
  updateFeatureCount();
11823
11992
  scheduleTimelineUpdate();
11824
- syncFeaturesToCustomLayers();
11993
+ scheduleSync();
11825
11994
  }
11826
11995
  } catch (error) {
11827
11996
  toast({
@@ -11830,7 +11999,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11830
11999
  variant: "destructive"
11831
12000
  });
11832
12001
  }
11833
- }, [onSearchEngineRequest, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, toast]);
12002
+ }, [onSearchEngineRequest, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, toast]);
11834
12003
  const restoreEnrichedGeoJSON = useCallback((enriched) => {
11835
12004
  var _a;
11836
12005
  const mapState = enriched["octostar:map"];
@@ -11900,7 +12069,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11900
12069
  setLayers(newLayers);
11901
12070
  updateFeatureCount();
11902
12071
  scheduleTimelineUpdate();
11903
- syncFeaturesToCustomLayers();
12072
+ scheduleSync();
11904
12073
  if (mapState.viewport && map.current) {
11905
12074
  map.current.flyTo({
11906
12075
  center: mapState.viewport.center,
@@ -11917,7 +12086,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11917
12086
  title: "Map restored",
11918
12087
  description: `Restored ${newLayers.length} layers with ${totalFeatures} features`
11919
12088
  });
11920
- }, [saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, onMapStateLoaded, toast]);
12089
+ }, [saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, onMapStateLoaded, toast]);
11921
12090
  restoreEnrichedGeoJSONRef.current = restoreEnrichedGeoJSON;
11922
12091
  const handleWorkspaceLoad = useCallback(async () => {
11923
12092
  if (!onWorkspaceLoad) return;
@@ -11955,7 +12124,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11955
12124
  setLayers((prev) => [...prev, newLayer]);
11956
12125
  updateFeatureCount();
11957
12126
  scheduleTimelineUpdate();
11958
- syncFeaturesToCustomLayers();
12127
+ scheduleSync();
11959
12128
  toast({
11960
12129
  title: "Features loaded",
11961
12130
  description: `Created layer with ${featuresWithIds.length} features`
@@ -11967,7 +12136,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
11967
12136
  variant: "destructive"
11968
12137
  });
11969
12138
  }
11970
- }, [onWorkspaceLoad, saveStateForUndo, restoreEnrichedGeoJSON, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, toast]);
12139
+ }, [onWorkspaceLoad, saveStateForUndo, restoreEnrichedGeoJSON, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, toast]);
11971
12140
  const handleWorkspaceSave = useCallback(async () => {
11972
12141
  if (!onWorkspaceSave) return;
11973
12142
  const geojson = buildEnrichedGeoJSON();
@@ -12131,7 +12300,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
12131
12300
  setLayers((prev) => [...prev, newLayer]);
12132
12301
  updateFeatureCount();
12133
12302
  scheduleTimelineUpdate();
12134
- syncFeaturesToCustomLayers();
12303
+ scheduleSync();
12135
12304
  if (map.current && geojson.features.length > 0) {
12136
12305
  try {
12137
12306
  const bbox = turf.bbox(geojson);
@@ -12202,7 +12371,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
12202
12371
  }
12203
12372
  updateFeatureCount();
12204
12373
  scheduleTimelineUpdate();
12205
- syncFeaturesToCustomLayers();
12374
+ scheduleSync();
12206
12375
  const reprojectMsg = reprojected ? ` (reprojected from ${sourceCRS})` : "";
12207
12376
  toast({
12208
12377
  title: "GeoJSON loaded",
@@ -12218,7 +12387,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
12218
12387
  };
12219
12388
  reader.readAsText(file);
12220
12389
  e.target.value = "";
12221
- }, [layers, activeEditingLayerId, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, restoreEnrichedGeoJSON, toast, interceptGeoJSONFileLoad]);
12390
+ }, [layers, activeEditingLayerId, saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, restoreEnrichedGeoJSON, toast, interceptGeoJSONFileLoad]);
12222
12391
  const handleDropGeoJSON = useCallback((name, data) => {
12223
12392
  var _a;
12224
12393
  if (!map.current) return;
@@ -12278,7 +12447,7 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
12278
12447
  setLayers(nextLayers);
12279
12448
  updateFeatureCount();
12280
12449
  scheduleTimelineUpdate();
12281
- syncFeaturesToCustomLayers();
12450
+ scheduleSync();
12282
12451
  if (reprojectedData.features.length > 0) {
12283
12452
  try {
12284
12453
  const bbox = turf.bbox(reprojectedData);
@@ -12296,17 +12465,12 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
12296
12465
  title: "Layer added",
12297
12466
  description: `Created "${newLayer.name}" with ${addedIds.length} features${reprojectMsg} (high-performance)`
12298
12467
  });
12299
- }, [saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, syncFeaturesToCustomLayers, restoreEnrichedGeoJSON, toast, interceptGeoJSONFileLoad]);
12468
+ }, [saveStateForUndo, updateFeatureCount, scheduleTimelineUpdate, scheduleSync, restoreEnrichedGeoJSON, toast, interceptGeoJSONFileLoad]);
12300
12469
  const handleTimelineFilterChange = useCallback((visibleIds, hiddenIds, untimedIds) => {
12301
12470
  if (!draw.current || !map.current) return;
12302
12471
  timelineVisibleIdsRef.current = /* @__PURE__ */ new Set([...visibleIds, ...untimedIds]);
12303
- if (timelineSyncRafRef.current == null) {
12304
- timelineSyncRafRef.current = requestAnimationFrame(() => {
12305
- timelineSyncRafRef.current = null;
12306
- syncFeaturesToCustomLayers();
12307
- });
12308
- }
12309
- }, [syncFeaturesToCustomLayers]);
12472
+ scheduleSync();
12473
+ }, [scheduleSync]);
12310
12474
  const prevTimelineEnabledRef = useRef(timelineStore.isEnabled);
12311
12475
  useEffect(() => {
12312
12476
  const wasEnabled = prevTimelineEnabledRef.current;
@@ -12314,11 +12478,11 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
12314
12478
  prevTimelineEnabledRef.current = isNowEnabled;
12315
12479
  if (!isNowEnabled || !timelineStore.hasTemporalData) {
12316
12480
  timelineVisibleIdsRef.current = null;
12317
- syncFeaturesToCustomLayers();
12481
+ scheduleSync();
12318
12482
  } else if (!wasEnabled && isNowEnabled) {
12319
12483
  updateTimelineStore();
12320
12484
  }
12321
- }, [timelineStore.isEnabled, timelineStore.hasTemporalData, syncFeaturesToCustomLayers, updateTimelineStore, scheduleTimelineUpdate]);
12485
+ }, [timelineStore.isEnabled, timelineStore.hasTemporalData, scheduleSync, updateTimelineStore, scheduleTimelineUpdate]);
12322
12486
  useEffect(() => {
12323
12487
  if (!map.current || !draw.current || !isMapLoaded) return;
12324
12488
  const mapInstance = map.current;