@trackunit/react-map 0.2.250 → 0.2.251

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
@@ -6484,8 +6484,32 @@ const sqDistance = (a, b) => {
6484
6484
  return dx * dx + dy * dy;
6485
6485
  };
6486
6486
 
6487
- /** Screen "up" in math radians (+x right, y down): −π/2 points up on screen. */
6488
- const UP_ANGLE_RAD = -Math.PI / 2;
6487
+ /**
6488
+ * Preferred displacement directions, most-preferred first (screen angles; −π/2 is up, 0 is right,
6489
+ * +π/2 is down). Maplex-style cartographic ranking: upper-right, then the other corners, then the
6490
+ * sides, then top. Straight-down is deliberately omitted — a blocked zone (see {@link RANKED_WITH_BLOCKED_RAD}).
6491
+ * Tuned in the browser with design; the solver tries these in order and takes the first that fits.
6492
+ */
6493
+ const RANKED_ANGLES_RAD = [
6494
+ -Math.PI / 4, // upper-right
6495
+ Math.PI / 4, // lower-right
6496
+ (-3 * Math.PI) / 4, // upper-left
6497
+ (3 * Math.PI) / 4, // lower-left
6498
+ 0, // right
6499
+ Math.PI, // left
6500
+ -Math.PI / 2, // top
6501
+ ];
6502
+ /**
6503
+ * The blocked displacement zone: directions the primary ranked search never uses (straight-down reads
6504
+ * as the pill dangling off the bottom of its pin). The single source the ranked-with-blocked list and
6505
+ * the incumbency check both derive from, so a future second blocked direction is one edit, not three.
6506
+ */
6507
+ const BLOCKED_ANGLES_RAD = [Math.PI / 2]; // straight-down
6508
+ /**
6509
+ * The ranked directions plus the blocked zone appended last. Used only by the relaxed cluster-clear
6510
+ * fallback, where getting a guaranteed label off a cluster outranks the "never straight-down" rule.
6511
+ */
6512
+ const RANKED_WITH_BLOCKED_RAD = [...RANKED_ANGLES_RAD, ...BLOCKED_ANGLES_RAD];
6489
6513
  /** Anchor pin radius in px (the `xs` dot, 8px + 2px border): only feeds the tip's soft floor. */
6490
6514
  const ANCHOR_DOT_RADIUS_PX = 5;
6491
6515
  const collidesWithPlaced = (point, footprint, placed) => placed.some(other => overlaps(point, footprint, other.point, other.footprint));
@@ -6529,35 +6553,26 @@ const lineCrossesPlaced = (anchor, tip, placed) => placed.some(({ point, footpri
6529
6553
  return false;
6530
6554
  return segmentIntersectsBox(anchor, tip, point[0] - halfW, point[1] - halfH, point[0] + halfW, point[1] + halfH);
6531
6555
  });
6532
- /** Squared distance from `point` to the nearest placed label centre (Infinity when none). */
6533
- const clearanceSq = (point, placed) => {
6534
- let min = Infinity;
6535
- for (const other of placed) {
6536
- const d = sqDistance(point, other.point);
6537
- if (d < min)
6538
- min = d;
6539
- }
6540
- return min;
6541
- };
6542
6556
  /**
6543
6557
  * Finds a stick offset for a guaranteed (Tier-A) label whose anchor is taken.
6544
6558
  *
6545
- * Searches a candidate ring — `anglesPerRing` evenly-spaced angles, at increasing radii from
6546
- * `baseDistancePx` outward (`radiusSteps` rings). At the shortest ring that has room it aims the
6547
- * stick into the **most open direction** — the footprint-free angle whose displaced label is
6548
- * farthest from every other label — preferring angles whose leader line does not cross a label, and
6549
- * breaking ties straight-up-first for determinism. Candidates whose pill would fall outside `viewBox`
6550
- * are rejected so a guaranteed label is not pushed off-canvas. If a ring offers only line-crossing
6551
- * angles it keeps looking outward for a clean one, else falls back to the most-open crossing angle of
6552
- * the innermost ring that had room; if every candidate is rejected it returns `undefined` (the caller
6553
- * then keeps the label at its anchor — never dropped). `previousOffset` is reused only while it stays
6554
- * footprint-free, on-canvas and line-clear, so a clean stick keeps its direction between settles.
6559
+ * Tries a **ranked preferred order** of directions ({@link RANKED_ANGLES_RAD}, Maplex-style:
6560
+ * upper-right first, straight-down blocked) at increasing radii from `baseDistancePx` outward
6561
+ * (`radiusSteps` rings). At the shortest ring that has room it takes the **highest-ranked** angle that
6562
+ * is footprint-free and on-canvas; among those it prefers one whose leader line does not cross a label,
6563
+ * but a crossing is allowed rather than blocking (connectors render below the pill band, so a crossing
6564
+ * line passes behind the labels it crosses). The radius ramp is a fallback used only when a ring has no
6565
+ * free angle. If every candidate is rejected it returns `undefined` (the caller keeps the label at its
6566
+ * anchor — never dropped). `previousOffset` is reused while it stays footprint-free and on-canvas
6567
+ * (regardless of crossing), so a settled stick keeps its direction between settles and never swings for
6568
+ * a now-free higher-ranked angle — except a remembered blocked-zone (straight-down) offset, which is
6569
+ * reused only while `clusterClearFallback` is active, so it does not persist once its cluster is gone.
6555
6570
  *
6556
6571
  * Geometry matches the renderer: the collision/reserved point is the **rendered pill centre**
6557
6572
  * (`resolveTentativeTip` + `computePillCSSPlacement`), not the raw polar tip; the line is tested to
6558
6573
  * that tip.
6559
6574
  */
6560
- const findStickOffset = ({ anchor, placed, footprint, pillWidthPx, pillHeightPx, degreesPerPixel, viewBox, baseDistancePx, radiusStepPx, radiusSteps, anglesPerRing, previousOffset, clusterClearFallback, }) => {
6575
+ const findStickOffset = ({ anchor, placed, footprint, pillWidthPx, pillHeightPx, degreesPerPixel, viewBox, baseDistancePx, radiusStepPx, radiusSteps, previousOffset, clusterClearFallback, }) => {
6561
6576
  // Geometry uses the physical (as-painted) pill size; the view-box clamp insets by its half-extents,
6562
6577
  // so a pill that visually fits is not rejected. The padded `footprint` is used only for keep-out
6563
6578
  // (`collidesWithPlaced`), not here — that separation is the point of SAGA-855.
@@ -6593,81 +6608,76 @@ const findStickOffset = ({ anchor, placed, footprint, pillWidthPx, pillHeightPx,
6593
6608
  return undefined;
6594
6609
  return { pillCentre, lineClear: !lineCrossesPlaced(anchor, tip, placed) };
6595
6610
  };
6596
- // Reuse the remembered offset only while it stays clean, so a settled stick keeps its direction.
6611
+ // Reuse the remembered offset while it stays footprint-free and on-canvas, so a settled stick keeps
6612
+ // its direction (crossing is no longer required — connectors render below the pill band). A blocked-zone
6613
+ // angle (straight-down) only ever comes from the cluster-clear fallback, so reuse it only while that
6614
+ // fallback is still in play; otherwise a stale straight-down would persist through incumbency after the
6615
+ // cluster moved away, a direction the primary ranked search would never choose (SAGA-883 review).
6597
6616
  if (previousOffset !== undefined) {
6598
- const prev = evaluate(previousOffset.angleRad, previousOffset.distancePx);
6599
- if (prev !== undefined && prev.lineClear) {
6600
- return {
6601
- positioning: { type: "polar", angleRad: previousOffset.angleRad, distance: previousOffset.distancePx },
6602
- pillCentre: prev.pillCentre,
6603
- };
6617
+ const isBlockedAngle = BLOCKED_ANGLES_RAD.includes(previousOffset.angleRad);
6618
+ if (!isBlockedAngle || clusterClearFallback === true) {
6619
+ const prev = evaluate(previousOffset.angleRad, previousOffset.distancePx);
6620
+ if (prev !== undefined) {
6621
+ return {
6622
+ positioning: { type: "polar", angleRad: previousOffset.angleRad, distance: previousOffset.distancePx },
6623
+ pillCentre: prev.pillCentre,
6624
+ };
6625
+ }
6604
6626
  }
6605
6627
  }
6606
- const angleStep = (2 * Math.PI) / anglesPerRing;
6607
- // A candidate only beats the current best when meaningfully more open; near-ties (including the
6608
- // float noise of a radially symmetric ring) keep the earlier, up-first angle — deterministic.
6609
- const beats = (clearance, best) => best === undefined || clearance > best.clearance * (1 + 1e-6);
6610
- let fallback;
6628
+ // Ranked preferred order, inner ring first. At each ring take the highest-ranked free angle, preferring
6629
+ // a non-crossing one but accepting a crossing one rather than skipping to an outer ring.
6611
6630
  for (let ring = 0; ring < radiusSteps; ring++) {
6612
6631
  const distance = baseDistancePx + ring * radiusStepPx;
6613
- let bestClear;
6614
- let bestCrossing;
6615
- for (let a = 0; a < anglesPerRing; a++) {
6616
- const angleRad = UP_ANGLE_RAD + a * angleStep;
6632
+ let bestPreferred; // highest-ranked, non-crossing
6633
+ let bestAny; // highest-ranked, crossing allowed
6634
+ for (const angleRad of RANKED_ANGLES_RAD) {
6617
6635
  const evaluated = evaluate(angleRad, distance);
6618
6636
  if (evaluated === undefined)
6619
6637
  continue;
6620
- const clearance = clearanceSq(evaluated.pillCentre, placed);
6638
+ if (bestAny === undefined)
6639
+ bestAny = { angleRad, pillCentre: evaluated.pillCentre };
6621
6640
  if (evaluated.lineClear) {
6622
- if (beats(clearance, bestClear))
6623
- bestClear = { angleRad, pillCentre: evaluated.pillCentre, clearance };
6624
- }
6625
- else if (beats(clearance, bestCrossing)) {
6626
- bestCrossing = { angleRad, pillCentre: evaluated.pillCentre, clearance };
6641
+ bestPreferred = { angleRad, pillCentre: evaluated.pillCentre };
6642
+ break; // highest-ranked non-crossing found — nothing lower-ranked can beat it
6627
6643
  }
6628
6644
  }
6629
- if (bestClear !== undefined) {
6630
- return {
6631
- positioning: { type: "polar", angleRad: bestClear.angleRad, distance },
6632
- pillCentre: bestClear.pillCentre,
6633
- };
6634
- }
6635
- if (fallback === undefined && bestCrossing !== undefined) {
6636
- fallback = {
6637
- positioning: { type: "polar", angleRad: bestCrossing.angleRad, distance },
6638
- pillCentre: bestCrossing.pillCentre,
6639
- };
6645
+ const chosen = bestPreferred ?? bestAny;
6646
+ if (chosen !== undefined) {
6647
+ return { positioning: { type: "polar", angleRad: chosen.angleRad, distance }, pillCentre: chosen.pillCentre };
6640
6648
  }
6641
6649
  }
6642
- if (fallback !== undefined)
6643
- return fallback;
6644
6650
  if (clusterClearFallback !== true)
6645
6651
  return undefined;
6646
- let bestOnCanvas;
6652
+ // Relaxed fallback: no offset clears every label, so rather than drop a guaranteed label back onto
6653
+ // its anchor (which may sit on a cluster), take the highest-ranked offset that clears all CLUSTERS,
6654
+ // even if it overlaps other labels. "Never cover a cluster" wins over "never overlap a label" here;
6655
+ // only if even that is impossible does the caller keep the anchor. Blocked zones (straight-down) are
6656
+ // allowed here, and line-crossing is ignored (connectors render below the pill band).
6657
+ //
6658
+ // An on-canvas offset is preferred (inner ring, ranked first). If none exists — e.g. an asset wedged
6659
+ // between an edge cluster and the map edge, where inward offsets hit the cluster and outward ones fall
6660
+ // off-canvas — an edge-clipping offset is the last resort: clipping the viewport beats covering a cluster.
6647
6661
  let bestOffCanvas;
6648
6662
  for (let ring = 0; ring < radiusSteps; ring++) {
6649
6663
  const distance = baseDistancePx + ring * radiusStepPx;
6650
- for (let a = 0; a < anglesPerRing; a++) {
6651
- const angleRad = UP_ANGLE_RAD + a * angleStep;
6664
+ for (const angleRad of RANKED_WITH_BLOCKED_RAD) {
6652
6665
  const { pillCentre } = geometryFor(angleRad, distance);
6653
6666
  if (collidesWithClusters(pillCentre, footprint, placed))
6654
6667
  continue;
6655
- const clearance = clearanceSq(pillCentre, placed);
6656
- if (outsideViewBox(pillCentre)) {
6657
- if (beats(clearance, bestOffCanvas))
6658
- bestOffCanvas = { angleRad, distance, pillCentre, clearance };
6659
- }
6660
- else if (beats(clearance, bestOnCanvas)) {
6661
- bestOnCanvas = { angleRad, distance, pillCentre, clearance };
6668
+ if (!outsideViewBox(pillCentre)) {
6669
+ // Highest-ranked cluster-clear offset that stays on-canvas, at the innermost ring that has one.
6670
+ return { positioning: { type: "polar", angleRad, distance }, pillCentre };
6662
6671
  }
6672
+ if (bestOffCanvas === undefined)
6673
+ bestOffCanvas = { angleRad, distance, pillCentre };
6663
6674
  }
6664
6675
  }
6665
- const chosen = bestOnCanvas ?? bestOffCanvas;
6666
- return chosen === undefined
6676
+ return bestOffCanvas === undefined
6667
6677
  ? undefined
6668
6678
  : {
6669
- positioning: { type: "polar", angleRad: chosen.angleRad, distance: chosen.distance },
6670
- pillCentre: chosen.pillCentre,
6679
+ positioning: { type: "polar", angleRad: bestOffCanvas.angleRad, distance: bestOffCanvas.distance },
6680
+ pillCentre: bestOffCanvas.pillCentre,
6671
6681
  };
6672
6682
  };
6673
6683
 
@@ -6676,7 +6686,6 @@ const EMPTY_IDS = new Set();
6676
6686
  const STICK_BASE_DISTANCE_PX = MARKER_TUNING.stick.defaultPositioning.distance;
6677
6687
  const STICK_RADIUS_STEP_PX = 24;
6678
6688
  const STICK_RADIUS_STEPS = 3;
6679
- const STICK_ANGLES_PER_RING = 8;
6680
6689
  const byId = (a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
6681
6690
  const isWithinBounds = (position, bounds) => {
6682
6691
  const [lng, lat] = position;
@@ -6870,7 +6879,6 @@ const buildLabelPlacement = ({ items, getId, getPosition, focus, bounds, footpri
6870
6879
  baseDistancePx: STICK_BASE_DISTANCE_PX,
6871
6880
  radiusStepPx: STICK_RADIUS_STEP_PX,
6872
6881
  radiusSteps: STICK_RADIUS_STEPS,
6873
- anglesPerRing: STICK_ANGLES_PER_RING,
6874
6882
  previousOffset: previousStickOffsets?.get(candidate.id),
6875
6883
  clusterClearFallback: anchorOnCluster,
6876
6884
  });
package/index.esm.js CHANGED
@@ -6483,8 +6483,32 @@ const sqDistance = (a, b) => {
6483
6483
  return dx * dx + dy * dy;
6484
6484
  };
6485
6485
 
6486
- /** Screen "up" in math radians (+x right, y down): −π/2 points up on screen. */
6487
- const UP_ANGLE_RAD = -Math.PI / 2;
6486
+ /**
6487
+ * Preferred displacement directions, most-preferred first (screen angles; −π/2 is up, 0 is right,
6488
+ * +π/2 is down). Maplex-style cartographic ranking: upper-right, then the other corners, then the
6489
+ * sides, then top. Straight-down is deliberately omitted — a blocked zone (see {@link RANKED_WITH_BLOCKED_RAD}).
6490
+ * Tuned in the browser with design; the solver tries these in order and takes the first that fits.
6491
+ */
6492
+ const RANKED_ANGLES_RAD = [
6493
+ -Math.PI / 4, // upper-right
6494
+ Math.PI / 4, // lower-right
6495
+ (-3 * Math.PI) / 4, // upper-left
6496
+ (3 * Math.PI) / 4, // lower-left
6497
+ 0, // right
6498
+ Math.PI, // left
6499
+ -Math.PI / 2, // top
6500
+ ];
6501
+ /**
6502
+ * The blocked displacement zone: directions the primary ranked search never uses (straight-down reads
6503
+ * as the pill dangling off the bottom of its pin). The single source the ranked-with-blocked list and
6504
+ * the incumbency check both derive from, so a future second blocked direction is one edit, not three.
6505
+ */
6506
+ const BLOCKED_ANGLES_RAD = [Math.PI / 2]; // straight-down
6507
+ /**
6508
+ * The ranked directions plus the blocked zone appended last. Used only by the relaxed cluster-clear
6509
+ * fallback, where getting a guaranteed label off a cluster outranks the "never straight-down" rule.
6510
+ */
6511
+ const RANKED_WITH_BLOCKED_RAD = [...RANKED_ANGLES_RAD, ...BLOCKED_ANGLES_RAD];
6488
6512
  /** Anchor pin radius in px (the `xs` dot, 8px + 2px border): only feeds the tip's soft floor. */
6489
6513
  const ANCHOR_DOT_RADIUS_PX = 5;
6490
6514
  const collidesWithPlaced = (point, footprint, placed) => placed.some(other => overlaps(point, footprint, other.point, other.footprint));
@@ -6528,35 +6552,26 @@ const lineCrossesPlaced = (anchor, tip, placed) => placed.some(({ point, footpri
6528
6552
  return false;
6529
6553
  return segmentIntersectsBox(anchor, tip, point[0] - halfW, point[1] - halfH, point[0] + halfW, point[1] + halfH);
6530
6554
  });
6531
- /** Squared distance from `point` to the nearest placed label centre (Infinity when none). */
6532
- const clearanceSq = (point, placed) => {
6533
- let min = Infinity;
6534
- for (const other of placed) {
6535
- const d = sqDistance(point, other.point);
6536
- if (d < min)
6537
- min = d;
6538
- }
6539
- return min;
6540
- };
6541
6555
  /**
6542
6556
  * Finds a stick offset for a guaranteed (Tier-A) label whose anchor is taken.
6543
6557
  *
6544
- * Searches a candidate ring — `anglesPerRing` evenly-spaced angles, at increasing radii from
6545
- * `baseDistancePx` outward (`radiusSteps` rings). At the shortest ring that has room it aims the
6546
- * stick into the **most open direction** — the footprint-free angle whose displaced label is
6547
- * farthest from every other label — preferring angles whose leader line does not cross a label, and
6548
- * breaking ties straight-up-first for determinism. Candidates whose pill would fall outside `viewBox`
6549
- * are rejected so a guaranteed label is not pushed off-canvas. If a ring offers only line-crossing
6550
- * angles it keeps looking outward for a clean one, else falls back to the most-open crossing angle of
6551
- * the innermost ring that had room; if every candidate is rejected it returns `undefined` (the caller
6552
- * then keeps the label at its anchor — never dropped). `previousOffset` is reused only while it stays
6553
- * footprint-free, on-canvas and line-clear, so a clean stick keeps its direction between settles.
6558
+ * Tries a **ranked preferred order** of directions ({@link RANKED_ANGLES_RAD}, Maplex-style:
6559
+ * upper-right first, straight-down blocked) at increasing radii from `baseDistancePx` outward
6560
+ * (`radiusSteps` rings). At the shortest ring that has room it takes the **highest-ranked** angle that
6561
+ * is footprint-free and on-canvas; among those it prefers one whose leader line does not cross a label,
6562
+ * but a crossing is allowed rather than blocking (connectors render below the pill band, so a crossing
6563
+ * line passes behind the labels it crosses). The radius ramp is a fallback used only when a ring has no
6564
+ * free angle. If every candidate is rejected it returns `undefined` (the caller keeps the label at its
6565
+ * anchor — never dropped). `previousOffset` is reused while it stays footprint-free and on-canvas
6566
+ * (regardless of crossing), so a settled stick keeps its direction between settles and never swings for
6567
+ * a now-free higher-ranked angle — except a remembered blocked-zone (straight-down) offset, which is
6568
+ * reused only while `clusterClearFallback` is active, so it does not persist once its cluster is gone.
6554
6569
  *
6555
6570
  * Geometry matches the renderer: the collision/reserved point is the **rendered pill centre**
6556
6571
  * (`resolveTentativeTip` + `computePillCSSPlacement`), not the raw polar tip; the line is tested to
6557
6572
  * that tip.
6558
6573
  */
6559
- const findStickOffset = ({ anchor, placed, footprint, pillWidthPx, pillHeightPx, degreesPerPixel, viewBox, baseDistancePx, radiusStepPx, radiusSteps, anglesPerRing, previousOffset, clusterClearFallback, }) => {
6574
+ const findStickOffset = ({ anchor, placed, footprint, pillWidthPx, pillHeightPx, degreesPerPixel, viewBox, baseDistancePx, radiusStepPx, radiusSteps, previousOffset, clusterClearFallback, }) => {
6560
6575
  // Geometry uses the physical (as-painted) pill size; the view-box clamp insets by its half-extents,
6561
6576
  // so a pill that visually fits is not rejected. The padded `footprint` is used only for keep-out
6562
6577
  // (`collidesWithPlaced`), not here — that separation is the point of SAGA-855.
@@ -6592,81 +6607,76 @@ const findStickOffset = ({ anchor, placed, footprint, pillWidthPx, pillHeightPx,
6592
6607
  return undefined;
6593
6608
  return { pillCentre, lineClear: !lineCrossesPlaced(anchor, tip, placed) };
6594
6609
  };
6595
- // Reuse the remembered offset only while it stays clean, so a settled stick keeps its direction.
6610
+ // Reuse the remembered offset while it stays footprint-free and on-canvas, so a settled stick keeps
6611
+ // its direction (crossing is no longer required — connectors render below the pill band). A blocked-zone
6612
+ // angle (straight-down) only ever comes from the cluster-clear fallback, so reuse it only while that
6613
+ // fallback is still in play; otherwise a stale straight-down would persist through incumbency after the
6614
+ // cluster moved away, a direction the primary ranked search would never choose (SAGA-883 review).
6596
6615
  if (previousOffset !== undefined) {
6597
- const prev = evaluate(previousOffset.angleRad, previousOffset.distancePx);
6598
- if (prev !== undefined && prev.lineClear) {
6599
- return {
6600
- positioning: { type: "polar", angleRad: previousOffset.angleRad, distance: previousOffset.distancePx },
6601
- pillCentre: prev.pillCentre,
6602
- };
6616
+ const isBlockedAngle = BLOCKED_ANGLES_RAD.includes(previousOffset.angleRad);
6617
+ if (!isBlockedAngle || clusterClearFallback === true) {
6618
+ const prev = evaluate(previousOffset.angleRad, previousOffset.distancePx);
6619
+ if (prev !== undefined) {
6620
+ return {
6621
+ positioning: { type: "polar", angleRad: previousOffset.angleRad, distance: previousOffset.distancePx },
6622
+ pillCentre: prev.pillCentre,
6623
+ };
6624
+ }
6603
6625
  }
6604
6626
  }
6605
- const angleStep = (2 * Math.PI) / anglesPerRing;
6606
- // A candidate only beats the current best when meaningfully more open; near-ties (including the
6607
- // float noise of a radially symmetric ring) keep the earlier, up-first angle — deterministic.
6608
- const beats = (clearance, best) => best === undefined || clearance > best.clearance * (1 + 1e-6);
6609
- let fallback;
6627
+ // Ranked preferred order, inner ring first. At each ring take the highest-ranked free angle, preferring
6628
+ // a non-crossing one but accepting a crossing one rather than skipping to an outer ring.
6610
6629
  for (let ring = 0; ring < radiusSteps; ring++) {
6611
6630
  const distance = baseDistancePx + ring * radiusStepPx;
6612
- let bestClear;
6613
- let bestCrossing;
6614
- for (let a = 0; a < anglesPerRing; a++) {
6615
- const angleRad = UP_ANGLE_RAD + a * angleStep;
6631
+ let bestPreferred; // highest-ranked, non-crossing
6632
+ let bestAny; // highest-ranked, crossing allowed
6633
+ for (const angleRad of RANKED_ANGLES_RAD) {
6616
6634
  const evaluated = evaluate(angleRad, distance);
6617
6635
  if (evaluated === undefined)
6618
6636
  continue;
6619
- const clearance = clearanceSq(evaluated.pillCentre, placed);
6637
+ if (bestAny === undefined)
6638
+ bestAny = { angleRad, pillCentre: evaluated.pillCentre };
6620
6639
  if (evaluated.lineClear) {
6621
- if (beats(clearance, bestClear))
6622
- bestClear = { angleRad, pillCentre: evaluated.pillCentre, clearance };
6623
- }
6624
- else if (beats(clearance, bestCrossing)) {
6625
- bestCrossing = { angleRad, pillCentre: evaluated.pillCentre, clearance };
6640
+ bestPreferred = { angleRad, pillCentre: evaluated.pillCentre };
6641
+ break; // highest-ranked non-crossing found — nothing lower-ranked can beat it
6626
6642
  }
6627
6643
  }
6628
- if (bestClear !== undefined) {
6629
- return {
6630
- positioning: { type: "polar", angleRad: bestClear.angleRad, distance },
6631
- pillCentre: bestClear.pillCentre,
6632
- };
6633
- }
6634
- if (fallback === undefined && bestCrossing !== undefined) {
6635
- fallback = {
6636
- positioning: { type: "polar", angleRad: bestCrossing.angleRad, distance },
6637
- pillCentre: bestCrossing.pillCentre,
6638
- };
6644
+ const chosen = bestPreferred ?? bestAny;
6645
+ if (chosen !== undefined) {
6646
+ return { positioning: { type: "polar", angleRad: chosen.angleRad, distance }, pillCentre: chosen.pillCentre };
6639
6647
  }
6640
6648
  }
6641
- if (fallback !== undefined)
6642
- return fallback;
6643
6649
  if (clusterClearFallback !== true)
6644
6650
  return undefined;
6645
- let bestOnCanvas;
6651
+ // Relaxed fallback: no offset clears every label, so rather than drop a guaranteed label back onto
6652
+ // its anchor (which may sit on a cluster), take the highest-ranked offset that clears all CLUSTERS,
6653
+ // even if it overlaps other labels. "Never cover a cluster" wins over "never overlap a label" here;
6654
+ // only if even that is impossible does the caller keep the anchor. Blocked zones (straight-down) are
6655
+ // allowed here, and line-crossing is ignored (connectors render below the pill band).
6656
+ //
6657
+ // An on-canvas offset is preferred (inner ring, ranked first). If none exists — e.g. an asset wedged
6658
+ // between an edge cluster and the map edge, where inward offsets hit the cluster and outward ones fall
6659
+ // off-canvas — an edge-clipping offset is the last resort: clipping the viewport beats covering a cluster.
6646
6660
  let bestOffCanvas;
6647
6661
  for (let ring = 0; ring < radiusSteps; ring++) {
6648
6662
  const distance = baseDistancePx + ring * radiusStepPx;
6649
- for (let a = 0; a < anglesPerRing; a++) {
6650
- const angleRad = UP_ANGLE_RAD + a * angleStep;
6663
+ for (const angleRad of RANKED_WITH_BLOCKED_RAD) {
6651
6664
  const { pillCentre } = geometryFor(angleRad, distance);
6652
6665
  if (collidesWithClusters(pillCentre, footprint, placed))
6653
6666
  continue;
6654
- const clearance = clearanceSq(pillCentre, placed);
6655
- if (outsideViewBox(pillCentre)) {
6656
- if (beats(clearance, bestOffCanvas))
6657
- bestOffCanvas = { angleRad, distance, pillCentre, clearance };
6658
- }
6659
- else if (beats(clearance, bestOnCanvas)) {
6660
- bestOnCanvas = { angleRad, distance, pillCentre, clearance };
6667
+ if (!outsideViewBox(pillCentre)) {
6668
+ // Highest-ranked cluster-clear offset that stays on-canvas, at the innermost ring that has one.
6669
+ return { positioning: { type: "polar", angleRad, distance }, pillCentre };
6661
6670
  }
6671
+ if (bestOffCanvas === undefined)
6672
+ bestOffCanvas = { angleRad, distance, pillCentre };
6662
6673
  }
6663
6674
  }
6664
- const chosen = bestOnCanvas ?? bestOffCanvas;
6665
- return chosen === undefined
6675
+ return bestOffCanvas === undefined
6666
6676
  ? undefined
6667
6677
  : {
6668
- positioning: { type: "polar", angleRad: chosen.angleRad, distance: chosen.distance },
6669
- pillCentre: chosen.pillCentre,
6678
+ positioning: { type: "polar", angleRad: bestOffCanvas.angleRad, distance: bestOffCanvas.distance },
6679
+ pillCentre: bestOffCanvas.pillCentre,
6670
6680
  };
6671
6681
  };
6672
6682
 
@@ -6675,7 +6685,6 @@ const EMPTY_IDS = new Set();
6675
6685
  const STICK_BASE_DISTANCE_PX = MARKER_TUNING.stick.defaultPositioning.distance;
6676
6686
  const STICK_RADIUS_STEP_PX = 24;
6677
6687
  const STICK_RADIUS_STEPS = 3;
6678
- const STICK_ANGLES_PER_RING = 8;
6679
6688
  const byId = (a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
6680
6689
  const isWithinBounds = (position, bounds) => {
6681
6690
  const [lng, lat] = position;
@@ -6869,7 +6878,6 @@ const buildLabelPlacement = ({ items, getId, getPosition, focus, bounds, footpri
6869
6878
  baseDistancePx: STICK_BASE_DISTANCE_PX,
6870
6879
  radiusStepPx: STICK_RADIUS_STEP_PX,
6871
6880
  radiusSteps: STICK_RADIUS_STEPS,
6872
- anglesPerRing: STICK_ANGLES_PER_RING,
6873
6881
  previousOffset: previousStickOffsets?.get(candidate.id),
6874
6882
  clusterClearFallback: anchorOnCluster,
6875
6883
  });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entry.js","sourceRoot":"","sources":["../../../../../libs/react/map/migrations/entry.ts"],"names":[],"mappings":"","sourcesContent":["export {};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-map",
3
- "version": "0.2.250",
3
+ "version": "0.2.251",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -43,8 +43,6 @@ export type StickSolverParams = Readonly<{
43
43
  radiusStepPx: number;
44
44
  /** Number of rings tried outward from the base distance. */
45
45
  radiusSteps: number;
46
- /** Candidate angles per ring (evenly spaced, starting straight up). */
47
- anglesPerRing: number;
48
46
  /** Previously-chosen offset for this asset — tried first so a stable label does not swing. */
49
47
  previousOffset?: StickOffset;
50
48
  /**
@@ -55,23 +53,42 @@ export type StickSolverParams = Readonly<{
55
53
  */
56
54
  clusterClearFallback?: boolean;
57
55
  }>;
56
+ /**
57
+ * Preferred displacement directions, most-preferred first (screen angles; −π/2 is up, 0 is right,
58
+ * +π/2 is down). Maplex-style cartographic ranking: upper-right, then the other corners, then the
59
+ * sides, then top. Straight-down is deliberately omitted — a blocked zone (see {@link RANKED_WITH_BLOCKED_RAD}).
60
+ * Tuned in the browser with design; the solver tries these in order and takes the first that fits.
61
+ */
62
+ export declare const RANKED_ANGLES_RAD: ReadonlyArray<number>;
63
+ /**
64
+ * The blocked displacement zone: directions the primary ranked search never uses (straight-down reads
65
+ * as the pill dangling off the bottom of its pin). The single source the ranked-with-blocked list and
66
+ * the incumbency check both derive from, so a future second blocked direction is one edit, not three.
67
+ */
68
+ export declare const BLOCKED_ANGLES_RAD: ReadonlyArray<number>;
69
+ /**
70
+ * The ranked directions plus the blocked zone appended last. Used only by the relaxed cluster-clear
71
+ * fallback, where getting a guaranteed label off a cluster outranks the "never straight-down" rule.
72
+ */
73
+ export declare const RANKED_WITH_BLOCKED_RAD: ReadonlyArray<number>;
58
74
  /**
59
75
  * Finds a stick offset for a guaranteed (Tier-A) label whose anchor is taken.
60
76
  *
61
- * Searches a candidate ring — `anglesPerRing` evenly-spaced angles, at increasing radii from
62
- * `baseDistancePx` outward (`radiusSteps` rings). At the shortest ring that has room it aims the
63
- * stick into the **most open direction** — the footprint-free angle whose displaced label is
64
- * farthest from every other label — preferring angles whose leader line does not cross a label, and
65
- * breaking ties straight-up-first for determinism. Candidates whose pill would fall outside `viewBox`
66
- * are rejected so a guaranteed label is not pushed off-canvas. If a ring offers only line-crossing
67
- * angles it keeps looking outward for a clean one, else falls back to the most-open crossing angle of
68
- * the innermost ring that had room; if every candidate is rejected it returns `undefined` (the caller
69
- * then keeps the label at its anchor — never dropped). `previousOffset` is reused only while it stays
70
- * footprint-free, on-canvas and line-clear, so a clean stick keeps its direction between settles.
77
+ * Tries a **ranked preferred order** of directions ({@link RANKED_ANGLES_RAD}, Maplex-style:
78
+ * upper-right first, straight-down blocked) at increasing radii from `baseDistancePx` outward
79
+ * (`radiusSteps` rings). At the shortest ring that has room it takes the **highest-ranked** angle that
80
+ * is footprint-free and on-canvas; among those it prefers one whose leader line does not cross a label,
81
+ * but a crossing is allowed rather than blocking (connectors render below the pill band, so a crossing
82
+ * line passes behind the labels it crosses). The radius ramp is a fallback used only when a ring has no
83
+ * free angle. If every candidate is rejected it returns `undefined` (the caller keeps the label at its
84
+ * anchor — never dropped). `previousOffset` is reused while it stays footprint-free and on-canvas
85
+ * (regardless of crossing), so a settled stick keeps its direction between settles and never swings for
86
+ * a now-free higher-ranked angle — except a remembered blocked-zone (straight-down) offset, which is
87
+ * reused only while `clusterClearFallback` is active, so it does not persist once its cluster is gone.
71
88
  *
72
89
  * Geometry matches the renderer: the collision/reserved point is the **rendered pill centre**
73
90
  * (`resolveTentativeTip` + `computePillCSSPlacement`), not the raw polar tip; the line is tested to
74
91
  * that tip.
75
92
  */
76
- export declare const findStickOffset: ({ anchor, placed, footprint, pillWidthPx, pillHeightPx, degreesPerPixel, viewBox, baseDistancePx, radiusStepPx, radiusSteps, anglesPerRing, previousOffset, clusterClearFallback, }: StickSolverParams) => StickSolution | undefined;
93
+ export declare const findStickOffset: ({ anchor, placed, footprint, pillWidthPx, pillHeightPx, degreesPerPixel, viewBox, baseDistancePx, radiusStepPx, radiusSteps, previousOffset, clusterClearFallback, }: StickSolverParams) => StickSolution | undefined;
77
94
  export {};