Skip to content

Commit 8b002fb

Browse files
committed
refactor: address gemini review on PR #52
- computePointSnap now accumulates all guides at the same best distance, mirroring computeSnap's behaviour: when two neighbour objects share an edge with the dragged endpoint, both guide lines render instead of only the first-encountered one. - BarcodeObject's text-group wrapper guards on .length > 0 instead of truthy. textNodes / textElements are arrays initialised to [] / undefined respectively; an empty array is truthy and would otherwise spawn an empty <Group> + ref callback every render.
1 parent df0ebcb commit 8b002fb

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

‎src/components/Canvas/BarcodeObject.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export function BarcodeObject({
446446
strokeWidth={isSelected ? 2 : 0}
447447
strokeScaleEnabled={false}
448448
/>
449-
{textNodes && <Group ref={excludeGroupFromBbox}>{textNodes}</Group>}
449+
{textNodes.length > 0 && <Group ref={excludeGroupFromBbox}>{textNodes}</Group>}
450450
</Group>
451451
);
452452
}
@@ -695,7 +695,9 @@ export function BarcodeObject({
695695
strokeWidth={isSelected ? 2 : 0}
696696
strokeScaleEnabled={false}
697697
/>
698-
{textElements && <Group ref={excludeGroupFromBbox}>{textElements}</Group>}
698+
{Array.isArray(textElements) && textElements.length > 0 && (
699+
<Group ref={excludeGroupFromBbox}>{textElements}</Group>
700+
)}
699701
</Group>
700702
);
701703
}

‎src/lib/snapGuides.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@ export function computePointSnap(
2323
): { value: number; guides: SnapGuide[] } => {
2424
let bestDelta = Infinity;
2525
let bestValue = drag;
26-
let bestGuide: SnapGuide | null = null;
26+
let bestGuides: SnapGuide[] = [];
2727
const consider = (target: number, perpFrom: number, perpTo: number) => {
2828
const d = Math.abs(target - drag);
29-
if (d > threshold || d >= bestDelta) return;
30-
bestDelta = d;
31-
bestValue = target;
29+
if (d > threshold || d > bestDelta) return;
3230
const guideOrientation: 'H' | 'V' = axis === 'x' ? 'V' : 'H';
33-
bestGuide = {
31+
const guide: SnapGuide = {
3432
orientation: guideOrientation,
3533
type: 'align',
3634
pos: target,
3735
from: Math.min(dragPerp, perpFrom) - GUIDE_PADDING_PX,
3836
to: Math.max(dragPerp, perpTo) + GUIDE_PADDING_PX,
3937
};
38+
if (d < bestDelta) {
39+
// Strictly closer — replace the best candidate.
40+
bestDelta = d;
41+
bestValue = target;
42+
bestGuides = [guide];
43+
} else if (target === bestValue) {
44+
// Same edge value at the same distance — accumulate so each
45+
// contributing object draws its own guide line.
46+
bestGuides.push(guide);
47+
}
4048
};
4149
for (const o of others) {
4250
const startEdge = axis === 'x' ? o.x : o.y;
@@ -54,7 +62,7 @@ export function computePointSnap(
5462
consider(startEdge, perpStart, perpEnd);
5563
consider(endEdge, perpStart, perpEnd);
5664
}
57-
return { value: bestValue, guides: bestGuide ? [bestGuide] : [] };
65+
return { value: bestValue, guides: bestGuides };
5866
};
5967

6068
const xRes = snapAxisPt(point.x, point.y, 'x');

0 commit comments

Comments
 (0)