GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: the variables panel sets a variable's prompt and marks what is … · u8array/ZPLab@0e0e200

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • components/Variables
      • VariablesPanel.prompt.test.tsx
      • VariablesPanel.tsx
    • locales
      • ar.ts
      • bg.ts
      • ca.ts
      • cs.ts
      • da.ts
      • de.ts
      • el.ts
      • en.ts
      • es.ts
      • et.ts
      • fa.ts
      • fi.ts
      • fr.ts
      • he.ts
      • hr.ts
      • hu.ts
      • it.ts
      • ja.ts
      • ko.ts
      • lt.ts
      • lv.ts
      • nl.ts
      • no.ts
      • pl.ts
      • pt.ts
      • ro.ts
      • sk.ts
      • sl.ts
      • sr.ts
      • sv.ts
      • tr.ts
      • zh-hans.ts
      • zh-hant.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/components/Variables/VariablesPanel.prompt.test.tsx‎

Copy file name to clipboard
+123Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// @vitest-environment jsdom
2+
import { describe, it, expect, afterEach, beforeEach } from "vitest";
3+
import { render, cleanup, act, fireEvent } from "@testing-library/react";
4+
import { VariablesPanel } from "./VariablesPanel";
5+
import { useLabelStore } from "../../store/labelStore";
6+
import { fallbackTranslations as en } from "../../locales";
7+
import type { LabelObject } from "@zplab/core/types/Group";
8+
import type { Variable } from "@zplab/core/types/Variable";
9+
10+
afterEach(cleanup);
11+
12+
const label = { widthMm: 100, heightMm: 60, dpmm: 8 };
13+
const lot: Variable = { id: "v1", name: "LOT", fnNumber: 1, defaultValue: "L42" };
14+
const field = { id: "t1", type: "text", x: 10, y: 10, rotation: 0, props: { content: "«LOT»", fontHeight: 30, fontWidth: 0, rotation: "N" } } as unknown as LabelObject;
15+
const csv = { kind: "csv" as const, filename: "d.csv", importedAt: "", encoding: "utf-8", delimiter: ",", rowCount: 1 };
16+
17+
const stored = () => useLabelStore.getState().variables[0];
18+
19+
function promptInput(container: HTMLElement): HTMLInputElement {
20+
const el = container.querySelector<HTMLInputElement>(`input[aria-label="${en.variables.promptLabel}"]`);
21+
if (!el) throw new Error("prompt input not found");
22+
return el;
23+
}
24+
25+
function type(container: HTMLElement, text: string) {
26+
const input = promptInput(container);
27+
fireEvent.change(input, { target: { value: text } });
28+
fireEvent.blur(input);
29+
}
30+
31+
beforeEach(() => {
32+
act(() => {
33+
useLabelStore.getState().loadDesign(label, [{ objects: [field] }], [lot]);
34+
useLabelStore.setState({ dataset: null, columnMapping: null });
35+
});
36+
});
37+
38+
describe("the prompt of a variable row", () => {
39+
it("writes the text on blur", () => {
40+
const { container } = render(<VariablesPanel />);
41+
type(container, "Lot number");
42+
expect(stored()?.prompt).toBe("Lot number");
43+
});
44+
45+
it("refuses a character the command does not take and shows the hint", () => {
46+
const { container, queryByText } = render(<VariablesPanel />);
47+
type(container, "Lot?");
48+
expect(stored()).not.toHaveProperty("prompt");
49+
expect(queryByText(en.variables.promptInvalid)).not.toBeNull();
50+
});
51+
52+
it("drops the prompt when the text is empty", () => {
53+
act(() => useLabelStore.getState().updateVariable("v1", { prompt: "Lot number" }));
54+
const { container } = render(<VariablesPanel />);
55+
type(container, "");
56+
expect(stored()).not.toHaveProperty("prompt");
57+
});
58+
59+
it("treats a blank text as empty", () => {
60+
act(() => useLabelStore.getState().updateVariable("v1", { prompt: "Lot number" }));
61+
const { container, queryByText } = render(<VariablesPanel />);
62+
type(container, " ");
63+
expect(stored()).not.toHaveProperty("prompt");
64+
expect(queryByText(en.variables.promptInvalid)).toBeNull();
65+
});
66+
67+
it("keeps the spaces at the edges", () => {
68+
const { container } = render(<VariablesPanel />);
69+
type(container, " Lot number ");
70+
expect(stored()?.prompt).toBe(" Lot number ");
71+
});
72+
73+
it("leaves a prompt with a trailing space alone when the field is left unchanged", () => {
74+
act(() => useLabelStore.getState().updateVariable("v1", { prompt: "Batch " }));
75+
const before = stored();
76+
const { container } = render(<VariablesPanel />);
77+
fireEvent.focus(promptInput(container));
78+
fireEvent.blur(promptInput(container));
79+
expect(stored()).toBe(before);
80+
});
81+
82+
it("clears the refusal once the text changes", () => {
83+
const { container, queryByText } = render(<VariablesPanel />);
84+
type(container, "Lot?");
85+
fireEvent.change(promptInput(container), { target: { value: "Lot" } });
86+
expect(queryByText(en.variables.promptInvalid)).toBeNull();
87+
});
88+
89+
it("shows a prompt set outside the row", () => {
90+
const { container } = render(<VariablesPanel />);
91+
act(() => useLabelStore.getState().updateVariable("v1", { prompt: "Lot number" }));
92+
expect(promptInput(container).value).toBe("Lot number");
93+
});
94+
});
95+
96+
describe("the asked marker", () => {
97+
const marked = () => render(<VariablesPanel />).queryByText(en.variables.askedAtPrint) !== null;
98+
99+
it("marks a variable with a prompt that a field prints", () => {
100+
act(() => useLabelStore.getState().updateVariable("v1", { prompt: "Lot number" }));
101+
expect(marked()).toBe(true);
102+
});
103+
104+
it("is not marked without a prompt", () => {
105+
expect(marked()).toBe(false);
106+
});
107+
108+
it("is not marked while no field prints the variable", () => {
109+
act(() => {
110+
useLabelStore.getState().loadDesign(label, [{ objects: [] }], [{ ...lot, prompt: "Lot number" }]);
111+
});
112+
expect(marked()).toBe(false);
113+
});
114+
115+
it("is not marked while a column fills the variable", () => {
116+
act(() => {
117+
useLabelStore.getState().updateVariable("v1", { prompt: "Lot number" });
118+
useLabelStore.getState().loadDataset({ headers: ["lot"], rows: [["A1"]], source: csv });
119+
useLabelStore.getState().setColumnMapping({ bindings: { v1: "lot" }, headerSnapshot: ["lot"] });
120+
});
121+
expect(marked()).toBe(false);
122+
});
123+
});
Collapse file

‎src/components/Variables/VariablesPanel.tsx‎

Copy file name to clipboardExpand all lines: src/components/Variables/VariablesPanel.tsx
+33-3Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { isDesktopShell } from '../../lib/platform';
2121
import {
2222
FN_NUMBER_MIN,
2323
FN_NUMBER_MAX,
24+
PROMPT_MAX_LENGTH,
2425
nextFreeFnNumber,
2526
nextDefaultVariableName,
2627
isValidVariableName,
@@ -38,6 +39,7 @@ import {
3839
type VariableSource,
3940
} from '@zplab/core/lib/variableBinding';
4041
import { countBindings } from '@zplab/core/lib/variableField';
42+
import { askedVariables } from '@zplab/core/lib/printValues';
4143
import { VariableSourceBadge } from './VariableSourceBadge';
4244

4345
export function VariablesPanel() {
@@ -116,6 +118,7 @@ export function VariablesPanel() {
116118
}
117119

118120
const bindingCounts = countBindings(pages, variables);
121+
const asked = new Set(askedVariables(variables, pages, dataset, columnMapping));
119122
const allSlotsTaken =
120123
nextFreeFnNumber(variables.map((v) => v.fnNumber)) === null;
121124

@@ -378,6 +381,7 @@ export function VariablesPanel() {
378381
key={entry.id}
379382
variable={entry}
380383
bindings={bindingCounts.get(entry.id) ?? 0}
384+
asked={asked.has(entry)}
381385
source={source}
382386
boundHeader={boundHeader}
383387
boundCellValue={activeCellValue(entry, activeRow)}
@@ -398,6 +402,7 @@ export function VariablesPanel() {
398402
onChangeDefault={(defaultValue) =>
399403
tryUpdate(entry.id, { defaultValue }, '')
400404
}
405+
onChangePrompt={(prompt) => tryUpdate(entry.id, { prompt }, tv.promptInvalid)}
401406
onRequestDelete={() => setPendingDelete(entry)}
402407
onDirtyChange={() =>
403408
setRowError((prev) => {
@@ -479,6 +484,7 @@ export function VariablesPanel() {
479484
interface RowProps {
480485
variable: Variable;
481486
bindings: number;
487+
asked: boolean;
482488
source: VariableSource;
483489
boundHeader: string | undefined;
484490
/** Active-row cell for a bound variable; shown as the default field's
@@ -494,6 +500,7 @@ interface RowProps {
494500
onChangeName: (next: string) => void;
495501
onChangeFnNumber: (next: number) => void;
496502
onChangeDefault: (next: string) => void;
503+
onChangePrompt: (next: string) => void;
497504
onRequestDelete: () => void;
498505
/** Called when any input value diverges from the committed value so
499506
* the panel can clear a stale rowError. Without this a duplicate-
@@ -504,6 +511,7 @@ interface RowProps {
504511
function VariableRow({
505512
variable,
506513
bindings,
514+
asked,
507515
source,
508516
boundHeader,
509517
boundCellValue,
@@ -513,6 +521,7 @@ function VariableRow({
513521
onChangeName,
514522
onChangeFnNumber,
515523
onChangeDefault,
524+
onChangePrompt,
516525
onRequestDelete,
517526
onDirtyChange,
518527
disabled,
@@ -523,6 +532,7 @@ function VariableRow({
523532
const [name, setName] = useState(variable.name);
524533
const [fn, setFn] = useState(String(variable.fnNumber));
525534
const [def, setDef] = useState(variable.defaultValue);
535+
const [prompt, setPrompt] = useState(variable.prompt ?? '');
526536
// Render-time adjust (the React derived-state pattern): an external store
527537
// update to this id (undo, file load, MCP push) must flow back into the
528538
// mirror, or a later blur would write the stale values over it.
@@ -532,6 +542,7 @@ function VariableRow({
532542
setName(variable.name);
533543
setFn(String(variable.fnNumber));
534544
setDef(variable.defaultValue);
545+
setPrompt(variable.prompt ?? '');
535546
}
536547

537548
const commitName = () => {
@@ -557,6 +568,11 @@ function VariableRow({
557568
onChangeDefault(def);
558569
};
559570

571+
const commitPrompt = () => {
572+
if (prompt === (variable.prompt ?? '')) return;
573+
onChangePrompt(prompt);
574+
};
575+
560576
return (
561577
<li className="flex flex-col gap-1.5">
562578
<div className="flex items-center gap-2">
@@ -615,22 +631,36 @@ function VariableRow({
615631
onChange={(e: ChangeEvent<HTMLInputElement>) => setDef(e.target.value)}
616632
onBlur={commitDef}
617633
/>
618-
<div className="flex justify-between items-center font-mono text-[9px] uppercase tracking-wider text-muted gap-2">
619-
<span className={bindings === 0 ? 'opacity-60' : undefined}>
634+
<input
635+
className={inputCls}
636+
aria-label={tv.promptLabel}
637+
disabled={disabled}
638+
placeholder={tv.promptLabel}
639+
maxLength={PROMPT_MAX_LENGTH}
640+
value={prompt}
641+
onChange={(e: ChangeEvent<HTMLInputElement>) => {
642+
setPrompt(e.target.value);
643+
onDirtyChange();
644+
}}
645+
onBlur={commitPrompt}
646+
/>
647+
<div className="flex justify-between items-start font-mono text-[9px] uppercase tracking-wider text-muted gap-2">
648+
<span className={`whitespace-nowrap ${bindings === 0 ? 'opacity-60' : ''}`}>
620649
{bindings === 0
621650
? tv.noBindings
622651
: bindings === 1
623652
? tv.bindingsSingular
624653
: tv.bindingsPluralFmt.replace('{n}', String(bindings))}
625654
</span>
626-
<span className="flex items-center gap-2 shrink-0">
655+
<span className="flex flex-wrap justify-end items-center gap-x-2 text-right">
627656
{/* Length a «marker» of this variable reserves in builders/char
628657
counts (inherited from the default; live while typing). */}
629658
<Tooltip content={tv.inheritsLengthHint}>
630659
<span className={def.length === 0 ? 'opacity-60' : undefined}>
631660
{tv.inheritsLengthFmt.replace('{n}', String(def.length))}
632661
</span>
633662
</Tooltip>
663+
{asked && <span className="text-indigo">{tv.askedAtPrint}</span>}
634664
{/* getVariableSource returns 'default' when there is no dataset, so
635665
this hides the redundant default badge when nothing is loaded. */}
636666
{error ? (
Collapse file

‎src/locales/ar.ts‎

Copy file name to clipboardExpand all lines: src/locales/ar.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,13 +1725,16 @@ const ar = {
17251725
nameLabel: 'الاسم',
17261726
fnLabel: 'الفتحة',
17271727
defaultLabel: 'الافتراضي',
1728+
promptLabel: 'نص الطلب عند الطباعة',
17281729
noBindings: 'غير مستخدم',
1730+
askedAtPrint: 'يُطلب عند الطباعة',
17291731
confirmDelete: 'حذف',
17301732
cancel: 'إلغاء',
17311733
noSlotsLeft: 'جميع الفتحات الـ99 مأخوذة.',
17321734
nameInUse: 'الاسم مستخدم بالفعل.',
17331735
nameInvalid: 'اسم غير صالح.',
17341736
slotInUse: 'الفتحة مستخدمة بالفعل.',
1737+
promptInvalid: 'فقط الحروف من A إلى Z والأرقام من 1 إلى 9 والمسافات.',
17351738
notBound: 'غير مرتبط',
17361739
createNew: '+ إنشاء متغير جديد…',
17371740
unbindAria: 'إلغاء ربط المتغير',
Collapse file

‎src/locales/bg.ts‎

Copy file name to clipboardExpand all lines: src/locales/bg.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,13 +1725,16 @@ const bg = {
17251725
nameLabel: 'Име',
17261726
fnLabel: 'Слот',
17271727
defaultLabel: 'По подразбиране',
1728+
promptLabel: 'Подкана при печат',
17281729
noBindings: 'неизползвана',
1730+
askedAtPrint: 'пита се при печат',
17291731
confirmDelete: 'Изтрий',
17301732
cancel: 'Отказ',
17311733
noSlotsLeft: 'Всичките 99 слота са заети.',
17321734
nameInUse: 'Името вече се използва.',
17331735
nameInvalid: 'Невалидно име.',
17341736
slotInUse: 'Слотът вече се използва.',
1737+
promptInvalid: 'Само букви от A до Z, цифри от 1 до 9 и интервали.',
17351738
notBound: 'Не е свързано',
17361739
createNew: '+ Създай нова променлива…',
17371740
unbindAria: 'Премахни връзката',
Collapse file

‎src/locales/ca.ts‎

Copy file name to clipboardExpand all lines: src/locales/ca.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,13 +1795,16 @@ const ca = {
17951795
nameLabel: 'Nom',
17961796
fnLabel: 'Posició',
17971797
defaultLabel: 'Per defecte',
1798+
promptLabel: 'Text de petició en imprimir',
17981799
noBindings: 'sense ús',
1800+
askedAtPrint: 'es demana en imprimir',
17991801
confirmDelete: 'Elimina',
18001802
cancel: 'Cancel·la',
18011803
noSlotsLeft: 'Les 99 posicions estan ocupades.',
18021804
nameInUse: 'El nom ja es fa servir.',
18031805
nameInvalid: 'Nom no vàlid.',
18041806
slotInUse: 'La posició ja es fa servir.',
1807+
promptInvalid: "Només lletres de la A a la Z, xifres de l'1 al 9 i espais.",
18051808
notBound: 'Sense vincular',
18061809
createNew: '+ Crea una variable…',
18071810
unbindAria: 'Desvincula la variable',
Collapse file

‎src/locales/cs.ts‎

Copy file name to clipboardExpand all lines: src/locales/cs.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,13 +1725,16 @@ const cs = {
17251725
nameLabel: 'Název',
17261726
fnLabel: 'Slot',
17271727
defaultLabel: 'Výchozí',
1728+
promptLabel: 'Výzva při tisku',
17281729
noBindings: 'nepoužito',
1730+
askedAtPrint: 'zadává se při tisku',
17291731
confirmDelete: 'Smazat',
17301732
cancel: 'Zrušit',
17311733
noSlotsLeft: 'Všech 99 slotů je obsazeno.',
17321734
nameInUse: 'Název už existuje.',
17331735
nameInvalid: 'Neplatný název.',
17341736
slotInUse: 'Slot už existuje.',
1737+
promptInvalid: 'Jen písmena A až Z, číslice 1 až 9 a mezery.',
17351738
notBound: 'Nepřipojeno',
17361739
createNew: '+ Vytvořit novou proměnnou…',
17371740
unbindAria: 'Odpojit proměnnou',
Collapse file

‎src/locales/da.ts‎

Copy file name to clipboardExpand all lines: src/locales/da.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,13 +1725,16 @@ const da = {
17251725
nameLabel: 'Navn',
17261726
fnLabel: 'Slot',
17271727
defaultLabel: 'Standard',
1728+
promptLabel: 'Prompt ved udskrivning',
17281729
noBindings: 'ubrugt',
1730+
askedAtPrint: 'spørges ved udskrivning',
17291731
confirmDelete: 'Slet',
17301732
cancel: 'Annuller',
17311733
noSlotsLeft: 'Alle 99 pladser er taget.',
17321734
nameInUse: 'Navnet er allerede i brug.',
17331735
nameInvalid: 'Ugyldigt navn.',
17341736
slotInUse: 'Pladsen er allerede i brug.',
1737+
promptInvalid: 'Kun bogstaverne A til Z, cifrene 1 til 9 og mellemrum.',
17351738
notBound: 'Ikke bundet',
17361739
createNew: '+ Opret ny variabel…',
17371740
unbindAria: 'Frigør variabel',
Collapse file

‎src/locales/de.ts‎

Copy file name to clipboardExpand all lines: src/locales/de.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,13 +1806,16 @@ const de = {
18061806
nameLabel: 'Name',
18071807
fnLabel: 'Slot',
18081808
defaultLabel: 'Standard',
1809+
promptLabel: 'Abfragetext beim Drucken',
18091810
noBindings: 'ungenutzt',
1811+
askedAtPrint: 'wird abgefragt',
18101812
confirmDelete: 'Löschen',
18111813
cancel: 'Abbrechen',
18121814
noSlotsLeft: 'Alle 99 Slots sind belegt.',
18131815
nameInUse: 'Name bereits in Benutzung.',
18141816
nameInvalid: 'Ungültiger Name.',
18151817
slotInUse: 'Slot bereits in Benutzung.',
1818+
promptInvalid: 'Nur Buchstaben A bis Z, Ziffern 1 bis 9 und Leerzeichen.',
18161819
notBound: 'Nicht gebunden',
18171820
createNew: '+ Neue Variable erstellen…',
18181821
unbindAria: 'Variable lösen',
Collapse file

‎src/locales/el.ts‎

Copy file name to clipboardExpand all lines: src/locales/el.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,13 +1725,16 @@ const el = {
17251725
nameLabel: 'Όνομα',
17261726
fnLabel: 'Υποδοχή',
17271727
defaultLabel: 'Προεπιλογή',
1728+
promptLabel: 'Προτροπή κατά την εκτύπωση',
17281729
noBindings: 'αχρησιμοποίητη',
1730+
askedAtPrint: 'ζητείται στην εκτύπωση',
17291731
confirmDelete: 'Διαγραφή',
17301732
cancel: 'Άκυρο',
17311733
noSlotsLeft: 'Και τα 99 slot είναι κατειλημμένα.',
17321734
nameInUse: 'Το όνομα χρησιμοποιείται ήδη.',
17331735
nameInvalid: 'Μη έγκυρο όνομα.',
17341736
slotInUse: 'Η υποδοχή χρησιμοποιείται ήδη.',
1737+
promptInvalid: 'Μόνο γράμματα A έως Z, ψηφία 1 έως 9 και κενά.',
17351738
notBound: 'Μη συνδεδεμένο',
17361739
createNew: '+ Δημιουργία νέας μεταβλητής…',
17371740
unbindAria: 'Αποσύνδεση μεταβλητής',
Collapse file

‎src/locales/en.ts‎

Copy file name to clipboardExpand all lines: src/locales/en.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,13 +1806,16 @@ const en = {
18061806
nameLabel: 'Name',
18071807
fnLabel: 'Slot',
18081808
defaultLabel: 'Default',
1809+
promptLabel: 'Prompt at print',
18091810
noBindings: 'unused',
1811+
askedAtPrint: 'asked at print',
18101812
confirmDelete: 'Delete',
18111813
cancel: 'Cancel',
18121814
noSlotsLeft: 'All 99 slots are taken.',
18131815
nameInUse: 'Name already in use.',
18141816
nameInvalid: 'Invalid name.',
18151817
slotInUse: 'Slot already in use.',
1818+
promptInvalid: 'Only letters A to Z, digits 1 to 9 and spaces.',
18161819
notBound: 'Not bound',
18171820
createNew: '+ Create new variable…',
18181821
unbindAria: 'Unbind variable',

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.