feat(badges): labeled align-text-in-box section and Ctrl+Z/Ctrl+Y shortcuts

Replace the cryptic single-letter text alignment buttons with a labeled 'Align text in box' section (horizontal + vertical icon groups); make wrapped text honor textAlign via full-width span; add undo/redo keyboard shortcuts (Ctrl/Cmd+Z, Ctrl+Y, Ctrl/Cmd+Shift+Z) gated behind the typing guard; 7 new e2e tests (20 total).

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
This commit is contained in:
committed 2026-10-16 07:57:50 +02:00
1 parent 5cb67afb5e
commit 30ed4388c0
3 files changed
+156 -18

No files matched your search

+58 -17
View File
@@ -19,6 +19,12 @@ import {
AlignEndHorizontal,
AlignHorizontalDistributeCenter,
AlignVerticalDistributeCenter,
AlignLeft,
AlignCenter,
AlignRight,
ArrowUpToLine,
ArrowDownToLine,
ChevronsUpDown,
} from 'lucide-react';
import {
deleteBadgeLayout,
@@ -64,6 +70,18 @@ const QR_LABEL: Record<BadgeQrElement['value'], string> = {
static: 'Static value',
};
// Text alignment *within* an element's own box (not the box on the page).
const ALIGN_H: { v: BadgeTextElement['align_h']; label: string; Icon: typeof AlignLeft }[] = [
{ v: 'left', label: 'Align text left', Icon: AlignLeft },
{ v: 'center', label: 'Center text', Icon: AlignCenter },
{ v: 'right', label: 'Align text right', Icon: AlignRight },
];
const ALIGN_V: { v: BadgeTextElement['align_v']; label: string; Icon: typeof ArrowUpToLine }[] = [
{ v: 'top', label: 'Align text to top', Icon: ArrowUpToLine },
{ v: 'middle', label: 'Center text vertically', Icon: ChevronsUpDown },
{ v: 'bottom', label: 'Align text to bottom', Icon: ArrowDownToLine },
];
function newId(): string {
return Math.random().toString(36).slice(2, 10) + Date.now().toString(36).slice(-4);
}
@@ -462,14 +480,23 @@ export function BadgeEditor() {
commit({ ...doc, elements });
}
// Keyboard: Delete/Backspace removes the selection, Escape clears it — but
// only when focus is not in a text field (so typing in the panel is safe).
// Keyboard: Delete/Backspace removes the selection, Escape clears it, and
// Ctrl/Cmd+Z undoes / Ctrl+Y (or Ctrl/Cmd+Shift+Z) redoes — but only when
// focus is not in a text field (so typing or native field undo is safe).
useEffect(() => {
function onKey(e: KeyboardEvent) {
const t = e.target as HTMLElement | null;
const typing = !!t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.tagName === 'SELECT' || t.isContentEditable);
if (typing) return;
if ((e.key === 'Delete' || e.key === 'Backspace') && selectedIds.length > 0) {
const mod = e.ctrlKey || e.metaKey;
const key = e.key.toLowerCase();
if (mod && key === 'z' && !e.shiftKey) {
e.preventDefault();
undo();
} else if ((mod && key === 'y') || (mod && key === 'z' && e.shiftKey)) {
e.preventDefault();
redo();
} else if ((e.key === 'Delete' || e.key === 'Backspace') && selectedIds.length > 0) {
e.preventDefault();
deleteSelected();
} else if (e.key === 'Escape') {
@@ -720,8 +747,8 @@ export function BadgeEditor() {
<button className="btn small" onClick={duplicateSelected} disabled={selected.length === 0} title="Duplicate"><Copy size={14} aria-hidden="true" /></button>
<button className="btn small" onClick={deleteSelected} disabled={selected.length === 0} title="Delete (Del)"><Trash2 size={14} aria-hidden="true" /></button>
<span className="badge-tool-sep" />
<button className="btn small" onClick={undo} disabled={past.current.length === 0} title="Undo"><Undo2 size={14} aria-hidden="true" /></button>
<button className="btn small" onClick={redo} disabled={future.current.length === 0} title="Redo"><Redo2 size={14} aria-hidden="true" /></button>
<button className="btn small" onClick={undo} disabled={past.current.length === 0} title="Undo (Ctrl+Z)"><Undo2 size={14} aria-hidden="true" /></button>
<button className="btn small" onClick={redo} disabled={future.current.length === 0} title="Redo (Ctrl+Y)"><Redo2 size={14} aria-hidden="true" /></button>
</div>
<div className="badge-tools" role="group" aria-label="Align">
@@ -934,7 +961,7 @@ function BadgePropsPanel({
<Num label="Width (mm)" value={doc.page_width_mm} min={1} step={1} onChange={(n) => setDocPage({ page_width_mm: Math.max(1, n) })} />
<Num label="Height (mm)" value={doc.page_height_mm} min={1} step={1} onChange={(n) => setDocPage({ page_height_mm: Math.max(1, n) })} />
</div>
<p className="muted small">Select an element on the canvas to edit its properties. Shift-click to select several, then use the align tools. Press <kbd>Del</kbd> to remove.</p>
<p className="muted small">Select an element on the canvas to edit its properties. Shift-click to select several, then use the align tools. Press <kbd>Del</kbd> to remove, <kbd>Ctrl</kbd>+<kbd>Z</kbd> to undo and <kbd>Ctrl</kbd>+<kbd>Y</kbd> to redo.</p>
</div>
</div>
);
@@ -1009,17 +1036,31 @@ function BadgePropsPanel({
<span>Text color</span>
<input className="badge-color" type="color" value={t.color} onChange={(e) => patchElement(t.id, { color: e.target.value })} />
</label>
<div className="badge-btn-row">
<button className={`btn small ${t.bold ? 'active' : ''}`} onClick={() => patchElement(t.id, { bold: !t.bold })} title="Bold"><strong>B</strong></button>
<button className={`btn small ${t.italic ? 'active' : ''}`} onClick={() => patchElement(t.id, { italic: !t.italic })} title="Italic"><em>I</em></button>
<span className="badge-tool-sep" />
{(['left', 'center', 'right'] as const).map((a) => (
<button key={a} className={`btn small ${t.align_h === a ? 'active' : ''}`} onClick={() => patchElement(t.id, { align_h: a })} title={`Align ${a}`}>{a[0].toUpperCase()}</button>
))}
<span className="badge-tool-sep" />
{(['top', 'middle', 'bottom'] as const).map((a) => (
<button key={a} className={`btn small ${t.align_v === a ? 'active' : ''}`} onClick={() => patchElement(t.id, { align_v: a })} title={`V-align ${a}`}>{a[0].toUpperCase()}</button>
))}
<div className="badge-field">
<span>Style</span>
<div className="badge-btn-row">
<button className={`btn small ${t.bold ? 'active' : ''}`} onClick={() => patchElement(t.id, { bold: !t.bold })} title="Bold"><strong>B</strong></button>
<button className={`btn small ${t.italic ? 'active' : ''}`} onClick={() => patchElement(t.id, { italic: !t.italic })} title="Italic"><em>I</em></button>
</div>
</div>
<div className="badge-field">
<span>Align text in box</span>
<div className="badge-align-group">
<span className="badge-align-group-label">Horizontal</span>
<div className="badge-btn-row">
{ALIGN_H.map(({ v, label, Icon }) => (
<button key={v} className={`btn small ${t.align_h === v ? 'active' : ''}`} onClick={() => patchElement(t.id, { align_h: v })} title={label} aria-label={label} aria-pressed={t.align_h === v}><Icon size={16} aria-hidden="true" /></button>
))}
</div>
</div>
<div className="badge-align-group">
<span className="badge-align-group-label">Vertical</span>
<div className="badge-btn-row">
{ALIGN_V.map(({ v, label, Icon }) => (
<button key={v} className={`btn small ${t.align_v === v ? 'active' : ''}`} onClick={() => patchElement(t.id, { align_v: v })} title={label} aria-label={label} aria-pressed={t.align_v === v}><Icon size={16} aria-hidden="true" /></button>
))}
</div>
</div>
</div>
<label className="badge-field">
<span>Transform</span>