Skip to content

Commit 0f41d86

Browse files
committed
refactor: extract shared commitHeightTransform helper for 1D barcodes
1 parent 1b27d67 commit 0f41d86

5 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎src/registry/barcode1d.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ObjectTypeDefinition, ObjectGroup, LabelObjectBase } from '../type
22
import { useT } from '../lib/useT';
33
import { inputCls, labelCls } from '../components/Properties/styles';
44
import { fieldPos } from './zplHelpers';
5+
import { commitHeightTransform } from './transformHelpers';
56

67
export interface Barcode1DProps {
78
content: string;
@@ -57,11 +58,7 @@ export function createBarcode1D(config: Barcode1DConfig): ObjectTypeDefinition<B
5758
defaultSize: { width: 300, height: 120 },
5859
heightLocked: config.heightLocked,
5960

60-
commitTransform: config.heightLocked
61-
? undefined
62-
: (obj, { sy, snap }) => ({
63-
height: Math.max(1, snap(Math.round(obj.props.height * sy))),
64-
}),
61+
commitTransform: config.heightLocked ? undefined : commitHeightTransform,
6562

6663
toZPL: (obj: LabelObjectBase & { props: Barcode1DProps }) => {
6764
const p = obj.props;

‎src/registry/code128.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ObjectTypeDefinition } from '../types/ObjectType';
22
import { useT } from '../lib/useT';
33
import { inputCls, labelCls } from '../components/Properties/styles';
44
import { fieldPos } from './zplHelpers';
5+
import { commitHeightTransform } from './transformHelpers';
56

67
export interface Code128Props {
78
content: string;
@@ -24,9 +25,7 @@ export const code128: ObjectTypeDefinition<Code128Props> = {
2425
},
2526
defaultSize: { width: 300, height: 120 },
2627

27-
commitTransform: (obj, { sy, snap }) => ({
28-
height: Math.max(1, snap(Math.round(obj.props.height * sy))),
29-
}),
28+
commitTransform: commitHeightTransform,
3029

3130
toZPL: (obj) => {
3231
const p = obj.props;

‎src/registry/code39.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ObjectTypeDefinition } from '../types/ObjectType';
22
import { useT } from '../lib/useT';
33
import { inputCls, labelCls } from '../components/Properties/styles';
44
import { fieldPos } from './zplHelpers';
5+
import { commitHeightTransform } from './transformHelpers';
56

67
export interface Code39Props {
78
content: string;
@@ -24,9 +25,7 @@ export const code39: ObjectTypeDefinition<Code39Props> = {
2425
},
2526
defaultSize: { width: 300, height: 120 },
2627

27-
commitTransform: (obj, { sy, snap }) => ({
28-
height: Math.max(1, snap(Math.round(obj.props.height * sy))),
29-
}),
28+
commitTransform: commitHeightTransform,
3029

3130
toZPL: (obj) => {
3231
const p = obj.props;

‎src/registry/ean13.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ObjectTypeDefinition } from '../types/ObjectType';
22
import { useT } from '../lib/useT';
33
import { inputCls, labelCls } from '../components/Properties/styles';
44
import { fieldPos } from './zplHelpers';
5+
import { commitHeightTransform } from './transformHelpers';
56

67
export interface Ean13Props {
78
content: string; // 12 digits — ZPL appends the check digit automatically
@@ -22,9 +23,7 @@ export const ean13: ObjectTypeDefinition<Ean13Props> = {
2223
},
2324
defaultSize: { width: 300, height: 120 },
2425

25-
commitTransform: (obj, { sy, snap }) => ({
26-
height: Math.max(1, snap(Math.round(obj.props.height * sy))),
27-
}),
26+
commitTransform: commitHeightTransform,
2827

2928
toZPL: (obj) => {
3029
const p = obj.props;

‎src/registry/transformHelpers.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ export function commitWidthHeightTransform<P extends WidthHeightProps>(
2222
} as Partial<P>;
2323
}
2424

25+
/** Shared commitTransform for 1D barcodes — only the bar height scales (width
26+
* is determined by content + module width, not by the resize anchor). */
27+
export function commitHeightTransform<P extends { height: number }>(
28+
obj: LabelObjectBase & { props: P },
29+
ctx: TransformContext,
30+
): Partial<P> {
31+
const { sy, snap } = ctx;
32+
return {
33+
height: Math.max(1, snap(Math.round(obj.props.height * sy))),
34+
} as Partial<P>;
35+
}
36+
2537
interface Stacked2DProps {
2638
rowHeight: number;
2739
moduleWidth: number;

0 commit comments

Comments
 (0)