Skip to content

Commit d3c38d0

Browse files
committed
refactor(canvas): tighten showHriOverlay readability per second review
Three cosmetic cleanups in the merged showHriOverlay branch: - Hoist `isEanUpc` once; derive `isUprightEanUpc` and `useUprightTransform` from it. Eliminates three repeated `EAN_UPC_TYPES.has(obj.type)` call sites. - Collapse four parallel `clipX/Y/Width/Height` ternaries into a single `clipProps` object spread, with extents read directly from the EAN/UPC overlay return rather than via pre-if-defaulted locals. - Drop the unused `rotatedBboxDims` export from `rotatedGroupTransform.ts` (only its own test referenced it). No behaviour change.
1 parent 66ae573 commit d3c38d0

3 files changed

Lines changed: 25 additions & 41 deletions

File tree

‎src/components/Canvas/BarcodeObject.tsx‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,10 @@ export function BarcodeObject({
216216
// bbox computation. Upright Other 1D additionally counter-
217217
// scales the HRI text during a resize drag so it stays at
218218
// constant visual size while the bars stretch.
219+
const isEanUpc = EAN_UPC_TYPES.has(obj.type);
219220
const showHriOverlay =
220221
printInterpEnabled && BARCODE_1D_TYPES.has(obj.type);
221-
const isUprightEanUpc = isUpright && EAN_UPC_TYPES.has(obj.type);
222+
const isUprightEanUpc = isUpright && isEanUpc;
222223

223224
if (showHriOverlay) {
224225
const ub = dim.upright;
@@ -228,13 +229,16 @@ export function BarcodeObject({
228229
const innerTr = rotatedGroupTransform(rotation, ub.w, ub.h);
229230

230231
// Build text overlay content in upright bbox-relative coords.
232+
// Upright EAN/UPC also needs clip extents to keep the floated
233+
// sys/trail digits visible past the bar bbox; the helper returns
234+
// them alongside the digit nodes, gated by isUprightEanUpc at
235+
// the parent Group below.
231236
let overlayContent: React.ReactNode;
232-
let clipLeft = 0;
233-
let clipRight = 0;
234-
if (EAN_UPC_TYPES.has(obj.type)) {
237+
let eanOverlay: ReturnType<typeof buildEanUpcDigitOverlay> | null = null;
238+
if (isEanUpc) {
235239
const bwipSc = get1DBwipScale(moduleWidth, scale, dpmm);
236240
const layout = getEanUpcLayout(obj.type as EanUpcType, ub.w, barcodeCanvas.width, bwipSc);
237-
const overlay = buildEanUpcDigitOverlay({
241+
eanOverlay = buildEanUpcDigitOverlay({
238242
type: obj.type as EanUpcType,
239243
displayText,
240244
layout,
@@ -246,9 +250,7 @@ export function BarcodeObject({
246250
// EAN/UPC have barTopPx === 0 (no text zone above), so the
247251
// helper's bar-relative textY equals the bbox-relative one
248252
// here — render directly without an offset wrapper.
249-
overlayContent = overlay.nodes;
250-
clipLeft = overlay.clipLeft;
251-
clipRight = overlay.clipRight;
253+
overlayContent = eanOverlay.nodes;
252254
} else {
253255
// Other 1D: single centered text. textRef is wired only for the
254256
// upright case so handleTransform/End can counter-scale it; for
@@ -275,7 +277,7 @@ export function BarcodeObject({
275277
// returns the position in inner-Group local coords (bbox-relative
276278
// upright). For above-bars (logmars): barTopPx - (font+gap)/sy.
277279
// For below: barTopPx + barH + gap/sy.
278-
const useUprightTransform = isUpright && !EAN_UPC_TYPES.has(obj.type);
280+
const useUprightTransform = isUpright && !isEanUpc;
279281
const textLocalY = (sy: number) =>
280282
isTextAbove
281283
? ub.barTopPx - (textFontSize + aboveGapPx) / sy
@@ -300,16 +302,25 @@ export function BarcodeObject({
300302
txt.y(textLocalY(1));
301303
};
302304

305+
// Clip-expansion is upright-EAN/UPC only; absent on other paths
306+
// so Konva computes a natural bbox. Spread-or-empty keeps the
307+
// JSX free of four parallel ternaries.
308+
const clipProps = isUprightEanUpc && eanOverlay
309+
? {
310+
clipX: -eanOverlay.clipLeft,
311+
clipY: 0,
312+
clipWidth: Math.max(ub.w, 1) + eanOverlay.clipLeft + eanOverlay.clipRight,
313+
clipHeight: Math.max(ub.h, 1) + textFontSize + textGap,
314+
}
315+
: {};
316+
303317
return (
304318
<Group
305319
ref={useUprightTransform ? groupRef : undefined}
306320
id={obj.id}
307321
x={x}
308322
y={y}
309-
clipX={isUprightEanUpc ? -clipLeft : undefined}
310-
clipY={isUprightEanUpc ? 0 : undefined}
311-
clipWidth={isUprightEanUpc ? Math.max(ub.w, 1) + clipLeft + clipRight : undefined}
312-
clipHeight={isUprightEanUpc ? Math.max(ub.h, 1) + textFontSize + textGap : undefined}
323+
{...clipProps}
313324
draggable={!obj.locked}
314325
{...selectionHandlers(onSelect)}
315326
onDragMove={(e) => e.target.position(snapPos(e.target.x(), e.target.y()))}

‎src/components/Canvas/rotatedGroupTransform.test.ts‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import {
3-
rotatedGroupTransform,
4-
rotatedBboxDims,
5-
} from "./rotatedGroupTransform";
2+
import { rotatedGroupTransform } from "./rotatedGroupTransform";
63

74
describe("rotatedGroupTransform", () => {
85
it("returns identity for N", () => {
@@ -29,14 +26,3 @@ describe("rotatedGroupTransform", () => {
2926
});
3027
});
3128

32-
describe("rotatedBboxDims", () => {
33-
it("keeps axes for N and I", () => {
34-
expect(rotatedBboxDims("N", 100, 40)).toEqual({ width: 100, height: 40 });
35-
expect(rotatedBboxDims("I", 100, 40)).toEqual({ width: 100, height: 40 });
36-
});
37-
38-
it("swaps axes for R and B", () => {
39-
expect(rotatedBboxDims("R", 100, 40)).toEqual({ width: 40, height: 100 });
40-
expect(rotatedBboxDims("B", 100, 40)).toEqual({ width: 40, height: 100 });
41-
});
42-
});

‎src/components/Canvas/rotatedGroupTransform.ts‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,3 @@ export function rotatedGroupTransform(
4848
}
4949
}
5050

51-
/** Outer-bbox dimensions for a given upright (W × H) content under
52-
* rotation. R / B swap axes; N / I keep them. Useful when the caller
53-
* needs the rotated footprint (e.g. for an invisible bbox rect). */
54-
export function rotatedBboxDims(
55-
rotation: ZplRotation,
56-
uprightW: number,
57-
uprightH: number,
58-
): { width: number; height: number } {
59-
if (rotation === "R" || rotation === "B") {
60-
return { width: uprightH, height: uprightW };
61-
}
62-
return { width: uprightW, height: uprightH };
63-
}

0 commit comments

Comments
 (0)