LabelSheetLayoutEditor.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { useId } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import type { LabelPageSize, LabelSheetLayout } from '../api/client';
  4. import {
  5. MAX_COLS,
  6. MAX_ROWS,
  7. PAGE_SIZES_MM,
  8. formatMm,
  9. parseDraft,
  10. type LayoutDraft,
  11. type LayoutNumberField,
  12. type LayoutProblem,
  13. } from '../utils/labelSheetLayout';
  14. // The form for a custom label sheet, and a to-scale drawing of it (#3296).
  15. const NUMBER_INPUTS: { field: LayoutNumberField; i18nKey: string; fallback: string; unit: boolean }[] = [
  16. { field: 'label_w_mm', i18nKey: 'labelWidth', fallback: 'Label width', unit: true },
  17. { field: 'label_h_mm', i18nKey: 'labelHeight', fallback: 'Label height', unit: true },
  18. { field: 'cols', i18nKey: 'columns', fallback: 'Columns', unit: false },
  19. { field: 'rows', i18nKey: 'rows', fallback: 'Rows', unit: false },
  20. { field: 'top_mm', i18nKey: 'topMargin', fallback: 'Top margin', unit: true },
  21. { field: 'left_mm', i18nKey: 'leftMargin', fallback: 'Left margin', unit: true },
  22. { field: 'col_gap_mm', i18nKey: 'columnGap', fallback: 'Gap between columns', unit: true },
  23. { field: 'row_gap_mm', i18nKey: 'rowGap', fallback: 'Gap between rows', unit: true },
  24. ];
  25. /** Every label of ``layout`` drawn on its page. Labels that run off the page
  26. * are red; the positions a starting position leaves blank are faded. */
  27. export function LabelSheetDiagram({
  28. layout,
  29. startingPosition = 1,
  30. }: {
  31. layout: LabelSheetLayout;
  32. startingPosition?: number;
  33. }) {
  34. const { t } = useTranslation();
  35. const [pageW, pageH] = PAGE_SIZES_MM[layout.page];
  36. const cells = [];
  37. for (let row = 0; row < layout.rows; row++) {
  38. for (let col = 0; col < layout.cols; col++) {
  39. const x = layout.left_mm + col * (layout.label_w_mm + layout.col_gap_mm);
  40. const y = layout.top_mm + row * (layout.label_h_mm + layout.row_gap_mm);
  41. const offPage = x + layout.label_w_mm > pageW + 0.01 || y + layout.label_h_mm > pageH + 0.01;
  42. const skipped = row * layout.cols + col < startingPosition - 1;
  43. cells.push(
  44. <rect
  45. key={`${row}-${col}`}
  46. data-testid={offPage ? 'label-sheet-cell-off-page' : 'label-sheet-cell'}
  47. x={x}
  48. y={y}
  49. width={layout.label_w_mm}
  50. height={layout.label_h_mm}
  51. rx={1}
  52. className={
  53. offPage
  54. ? 'fill-red-400/40 stroke-red-500'
  55. : skipped
  56. ? 'fill-transparent stroke-gray-300'
  57. : 'fill-bambu-green/25 stroke-bambu-green'
  58. }
  59. strokeWidth={0.6}
  60. />,
  61. );
  62. }
  63. }
  64. return (
  65. <svg
  66. data-testid="label-sheet-diagram"
  67. role="img"
  68. aria-label={t('inventory.labels.custom.diagram', 'Drawing of the sheet with every label in place')}
  69. // Room around the page so a grid that runs off it still shows.
  70. viewBox={`-4 -4 ${pageW + 8} ${pageH + 8}`}
  71. className="h-48 w-auto mx-auto overflow-visible"
  72. >
  73. <rect x={0} y={0} width={pageW} height={pageH} className="fill-white stroke-gray-400" strokeWidth={0.6} />
  74. {cells}
  75. </svg>
  76. );
  77. }
  78. export function LabelSheetLayoutEditor({
  79. draft,
  80. onChange,
  81. problem,
  82. startingPosition,
  83. }: {
  84. draft: LayoutDraft;
  85. onChange: (draft: LayoutDraft) => void;
  86. problem: LayoutProblem | null;
  87. startingPosition?: number;
  88. }) {
  89. const { t, i18n } = useTranslation();
  90. const hintId = useId();
  91. const problemId = useId();
  92. const layout = parseDraft(draft);
  93. // Lengths and counts in the reader's own number format (70,5 mm in German).
  94. const problemParams = problem?.params
  95. ? Object.fromEntries(Object.entries(problem.params).map(([k, v]) => [k, formatMm(v, i18n.language)]))
  96. : undefined;
  97. // Drawn while it is a sane grid, overflow included: that is what the drawing
  98. // is for. A grid past the limits is only explained, not drawn box by box.
  99. const drawable =
  100. layout !== null &&
  101. Number.isInteger(layout.cols) &&
  102. Number.isInteger(layout.rows) &&
  103. layout.cols >= 1 &&
  104. layout.cols <= MAX_COLS &&
  105. layout.rows >= 1 &&
  106. layout.rows <= MAX_ROWS &&
  107. layout.label_w_mm > 0 &&
  108. layout.label_h_mm > 0;
  109. const inputClass =
  110. 'w-full px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm focus:outline-none focus:border-bambu-green';
  111. return (
  112. <fieldset data-testid="label-sheet-editor" className="space-y-3">
  113. <legend className="text-sm font-medium text-white">
  114. {t('inventory.labels.custom.layoutTitle', 'Sheet layout')}
  115. </legend>
  116. <div className="grid grid-cols-2 gap-x-3 gap-y-2">
  117. <label className="col-span-2 space-y-0.5">
  118. <span className="text-xs text-bambu-gray">{t('inventory.labels.custom.page', 'Page')}</span>
  119. <select
  120. data-testid="label-sheet-page"
  121. value={draft.page}
  122. onChange={(e) => onChange({ ...draft, page: e.target.value as LabelPageSize })}
  123. className={inputClass}
  124. >
  125. <option value="a4">{t('inventory.labels.custom.pageA4', 'A4 (210 × 297 mm)')}</option>
  126. <option value="letter">{t('inventory.labels.custom.pageLetter', 'US Letter (215.9 × 279.4 mm)')}</option>
  127. </select>
  128. </label>
  129. {NUMBER_INPUTS.map(({ field, i18nKey, fallback, unit }) => (
  130. <label key={field} className="space-y-0.5">
  131. <span className="text-xs text-bambu-gray">
  132. {t(`inventory.labels.custom.${i18nKey}`, fallback)}
  133. {unit && ' (mm)'}
  134. </span>
  135. <input
  136. data-testid={`label-sheet-${field}`}
  137. type="text"
  138. inputMode={unit ? 'decimal' : 'numeric'}
  139. value={draft[field]}
  140. onChange={(e) => onChange({ ...draft, [field]: e.target.value })}
  141. aria-describedby={problem ? `${hintId} ${problemId}` : hintId}
  142. aria-invalid={problem ? true : undefined}
  143. className={inputClass}
  144. />
  145. </label>
  146. ))}
  147. </div>
  148. <p id={hintId} className="text-xs text-bambu-gray">
  149. {t(
  150. 'inventory.labels.custom.measureHint',
  151. 'Measure from the top-left corner of the page: margins to the first label, gaps between neighbouring labels.',
  152. )}
  153. </p>
  154. {problem && (
  155. <p id={problemId} data-testid="label-sheet-problem" aria-live="polite" className="text-xs text-red-400">
  156. {t(`inventory.labels.custom.problems.${problem.key}`, problem.fallback, problemParams)}
  157. </p>
  158. )}
  159. {drawable && layout && <LabelSheetDiagram layout={layout} startingPosition={startingPosition} />}
  160. </fieldset>
  161. );
  162. }