index.tsx
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import React, { useCallback, useEffect, useState } from 'react';
import { Button } from '../../ui/button';
import { ArrowLeft, Save, Download } from 'lucide-react';
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from '../../ui/dialog';
import type { LabelTemplate, LabelElement } from '../../../types/labelTemplate';
import {
createDefaultTemplate,
createDefaultElement,
defaultValueSourceTypeForElement,
} from '../../../types/labelTemplate';
import type { LocationDto } from '../../../types/location';
import { getLocations } from '../../../services/locationService';
import { ElementsPanel } from './ElementsPanel';
import { LabelCanvas, LabelPreviewOnly } from './LabelCanvas';
import { PropertiesPanel } from './PropertiesPanel';
import { createLabelTemplate, updateLabelTemplate } from '../../../services/labelTemplateService';
import { toast } from 'sonner';
const MIN_SCALE = 0.5;
const MAX_SCALE = 2;
const SCALE_STEP = 0.25;
const DEFAULT_SCALE = 1.0;
interface LabelTemplateEditorProps {
/** null = 新建,string = 编辑该 id */
templateId: string | null;
initialTemplate: LabelTemplate | null;
onClose: () => void;
onSaved: () => void;
}
export function LabelTemplateEditor({
templateId,
initialTemplate,
onClose,
onSaved,
}: LabelTemplateEditorProps) {
const [template, setTemplate] = useState<LabelTemplate>(() => {
if (initialTemplate) return { ...initialTemplate };
return createDefaultTemplate(templateId ?? undefined);
});
const [selectedId, setSelectedId] = useState<string | null>(null);
const [scale, setScale] = useState(DEFAULT_SCALE);
const [previewOpen, setPreviewOpen] = useState(false);
const [locations, setLocations] = useState<LocationDto[]>([]);
useEffect(() => {
let cancelled = false;
(async () => {
try {
const res = await getLocations({ skipCount: 0, maxResultCount: 500 });
if (!cancelled) setLocations(res.items ?? []);
} catch {
if (!cancelled) setLocations([]);
}
})();
return () => {
cancelled = true;
};
}, []);
const selectedElement = template.elements.find((el) => el.id === selectedId) ?? null;
const addElement = useCallback((
type: Parameters<typeof createDefaultElement>[0],
configOverride?: Partial<Record<string, unknown>>
) => {
// 计算画布中心位置(像素)
const unitToPx = (value: number, unit: 'cm' | 'inch'): number => {
return unit === 'cm' ? value * 37.8 : value * 96;
};
const canvasWidthPx = unitToPx(template.width, template.unit);
const canvasHeightPx = unitToPx(template.height, template.unit);
// 创建默认元素以获取其尺寸
const tempEl = createDefaultElement(type, 0, 0);
// 对齐到网格
const GRID_SIZE = 8;
const snapToGrid = (value: number): number => {
return Math.round(value / GRID_SIZE) * GRID_SIZE;
};
// 计算元素中心对齐到画布中心的位置
let centerX = (canvasWidthPx - tempEl.width) / 2;
let centerY = (canvasHeightPx - tempEl.height) / 2;
// 检查是否与现有元素重叠,如果重叠则尝试偏移
const checkOverlap = (x: number, y: number, width: number, height: number): boolean => {
return template.elements.some((el) => {
const elRight = el.x + el.width;
const elBottom = el.y + el.height;
const newRight = x + width;
const newBottom = y + height;
return !(x >= elRight || newRight <= el.x || y >= elBottom || newBottom <= el.y);
});
};
// 如果中心位置重叠,尝试在周围寻找空位
if (checkOverlap(centerX, centerY, tempEl.width, tempEl.height)) {
const offset = GRID_SIZE * 2;
let found = false;
// 尝试右下方
for (let tryY = centerY; tryY < canvasHeightPx - tempEl.height && !found; tryY += offset) {
for (let tryX = centerX; tryX < canvasWidthPx - tempEl.width && !found; tryX += offset) {
if (!checkOverlap(tryX, tryY, tempEl.width, tempEl.height)) {
centerX = tryX;
centerY = tryY;
found = true;
break;
}
}
}
// 如果还没找到,尝试左上方
if (!found) {
for (let tryY = centerY; tryY >= 0 && !found; tryY -= offset) {
for (let tryX = centerX; tryX >= 0 && !found; tryX -= offset) {
if (!checkOverlap(tryX, tryY, tempEl.width, tempEl.height)) {
centerX = tryX;
centerY = tryY;
found = true;
break;
}
}
}
}
}
const el = createDefaultElement(type, Math.max(0, snapToGrid(centerX)), Math.max(0, snapToGrid(centerY)));
if (configOverride && Object.keys(configOverride).length > 0) {
el.config = { ...el.config, ...configOverride };
}
setTemplate((prev) => ({
...prev,
elements: [...prev.elements, el],
}));
setSelectedId(el.id);
}, [template.width, template.height, template.unit, template.elements]);
const updateElement = useCallback((id: string, patch: Partial<LabelElement>) => {
setTemplate((prev) => ({
...prev,
elements: prev.elements.map((el) =>
el.id === id ? { ...el, ...patch } : el
),
}));
}, []);
const deleteElement = useCallback((id: string) => {
setTemplate((prev) => ({
...prev,
elements: prev.elements.filter((el) => el.id !== id),
}));
setSelectedId(null);
}, []);
const handleTemplateChange = useCallback((patch: Partial<LabelTemplate>) => {
setTemplate((prev) => ({ ...prev, ...patch }));
}, []);
const handleSave = useCallback(async () => {
try {
const code = (template.id ?? "").trim();
if (!code) {
toast.error("Template code is required.", {
description: "Please enter a template code (e.g. TPL_TEST_001).",
});
return;
}
if (template.appliedLocation === "SPECIFIED" && !(template.appliedLocationIds?.length ?? 0)) {
toast.error("Locations required.", {
description: "When using specified locations, select at least one location.",
});
return;
}
// 转换 LabelTemplate 到 API 需要的格式(对齐 LabelTemplateCreateInputVo)
const apiInput = {
id: code,
name: template.name,
labelType: template.labelType,
unit: template.unit,
width: template.width,
height: template.height,
appliedLocation: template.appliedLocation,
showRuler: template.showRuler,
showGrid: template.showGrid ?? true,
state: true,
elements: template.elements.map((el, index) => ({
id: el.id,
type: el.type,
x: el.x,
y: el.y,
width: el.width,
height: el.height,
rotation: el.rotation,
border: el.border,
zIndex: el.zIndex ?? index + 1,
orderNum: el.orderNum ?? index + 1,
valueSourceType: el.valueSourceType ?? defaultValueSourceTypeForElement(el.type),
isRequiredInput: el.isRequiredInput ?? false,
config: el.config,
})),
appliedLocationIds:
template.appliedLocation === "ALL" ? [] : (template.appliedLocationIds ?? []),
};
if (templateId) {
// 编辑模式:使用 TemplateCode 作为 id
await updateLabelTemplate(code, apiInput);
toast.success("Template updated.", {
description: "The template has been updated successfully.",
});
} else {
// 新建模式
await createLabelTemplate(apiInput);
toast.success("Template created.", {
description: "The template has been created successfully.",
});
}
onSaved();
onClose();
} catch (e: any) {
toast.error("Failed to save template.", {
description: e?.message ? String(e.message) : "Please try again.",
});
}
}, [template, templateId, onSaved, onClose]);
const handleExport = useCallback(() => {
const blob = new Blob([JSON.stringify(template, null, 2)], {
type: 'application/json',
});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `label-template-${template.id}.json`;
a.click();
URL.revokeObjectURL(url);
}, [template]);
return (
<div className="flex flex-col h-full min-h-0">
{/* Toolbar */}
<div className="flex items-center gap-2 px-4 py-2 border-b border-gray-200 bg-white shrink-0">
<Button variant="outline" size="sm" onClick={onClose}>
<ArrowLeft className="w-4 h-4 mr-1" />
Back
</Button>
<span className="text-sm font-medium text-gray-700 truncate flex-1">
{template.name}
</span>
<Button size="sm" onClick={handleExport} variant="outline">
<Download className="w-4 h-4 mr-1" />
Export JSON
</Button>
<Button
size="sm"
className="bg-blue-600 hover:bg-blue-700 text-white"
onClick={handleSave}
>
<Save className="w-4 h-4 mr-1" />
Save
</Button>
</div>
{/* Three columns */}
<div className="flex flex-1 min-h-0">
<ElementsPanel onAddElement={addElement} />
<LabelCanvas
template={template}
selectedId={selectedId}
onSelect={setSelectedId}
onUpdateElement={updateElement}
onDeleteElement={deleteElement}
onTemplateChange={handleTemplateChange}
scale={scale}
onZoomIn={() => setScale((s) => Math.min(MAX_SCALE, s + SCALE_STEP))}
onZoomOut={() => setScale((s) => Math.max(MIN_SCALE, s - SCALE_STEP))}
onPreview={() => setPreviewOpen(true)}
/>
<Dialog open={previewOpen} onOpenChange={setPreviewOpen}>
<DialogContent className="max-w-[90vw] max-h-[90vh] overflow-auto">
<DialogHeader>
<DialogTitle>标签预览</DialogTitle>
</DialogHeader>
<LabelPreviewOnly template={template} maxWidth={500} />
</DialogContent>
</Dialog>
<PropertiesPanel
template={template}
selectedElement={selectedElement}
onTemplateChange={handleTemplateChange}
onElementChange={updateElement}
onDeleteElement={deleteElement}
locations={locations}
readOnlyTemplateCode={!!templateId}
/>
</div>
</div>
);
}