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).
This commit is contained in:
Lago committed 2026-10-16 07:57:50 +02:00
1 parent ab25c102dc
commit 6b277a2a67
3 files changed
+156 -18

No files matched your search

+3 -1
View File
@@ -420,8 +420,10 @@ table.data tr:hover td { background: rgba(244,245,249,.6); }
.badge-el { position: absolute; overflow: visible; cursor: move; } .badge-el { position: absolute; overflow: visible; cursor: move; }
.badge-el.selected { outline: 1.5px solid var(--accent); } .badge-el.selected { outline: 1.5px solid var(--accent); }
.badge-text-body { width: 100%; height: 100%; display: flex; overflow: hidden; } .badge-text-body { width: 100%; height: 100%; display: flex; overflow: hidden; }
.badge-text-body > span { max-width: 100%; overflow-wrap: anywhere; } .badge-text-body > span { width: 100%; overflow-wrap: anywhere; }
.badge-el-placeholder { color: #cbd5e1; font-style: italic; } .badge-el-placeholder { color: #cbd5e1; font-style: italic; }
.badge-align-group { margin-top: 8px; }
.badge-align-group-label { display: block; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 4px; }
.badge-qr-body { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; } .badge-qr-body { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }
.badge-qr-body svg { width: 100%; height: 100%; } .badge-qr-body svg { width: 100%; height: 100%; }
.badge-image-body { width: 100%; height: 100%; display: block; } .badge-image-body { width: 100%; height: 100%; display: block; }
+58 -17
View File
@@ -19,6 +19,12 @@ import {
AlignEndHorizontal, AlignEndHorizontal,
AlignHorizontalDistributeCenter, AlignHorizontalDistributeCenter,
AlignVerticalDistributeCenter, AlignVerticalDistributeCenter,
AlignLeft,
AlignCenter,
AlignRight,
ArrowUpToLine,
ArrowDownToLine,
ChevronsUpDown,
} from 'lucide-react'; } from 'lucide-react';
import { import {
deleteBadgeLayout, deleteBadgeLayout,
@@ -64,6 +70,18 @@ const QR_LABEL: Record<BadgeQrElement['value'], string> = {
static: 'Static value', 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 { function newId(): string {
return Math.random().toString(36).slice(2, 10) + Date.now().toString(36).slice(-4); return Math.random().toString(36).slice(2, 10) + Date.now().toString(36).slice(-4);
} }
@@ -462,14 +480,23 @@ export function BadgeEditor() {
commit({ ...doc, elements }); commit({ ...doc, elements });
} }
// Keyboard: Delete/Backspace removes the selection, Escape clears it — but // Keyboard: Delete/Backspace removes the selection, Escape clears it, and
// only when focus is not in a text field (so typing in the panel is safe). // 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(() => { useEffect(() => {
function onKey(e: KeyboardEvent) { function onKey(e: KeyboardEvent) {
const t = e.target as HTMLElement | null; const t = e.target as HTMLElement | null;
const typing = !!t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.tagName === 'SELECT' || t.isContentEditable); const typing = !!t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA' || t.tagName === 'SELECT' || t.isContentEditable);
if (typing) return; 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(); e.preventDefault();
deleteSelected(); deleteSelected();
} else if (e.key === 'Escape') { } 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={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> <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" /> <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={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"><Redo2 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>
<div className="badge-tools" role="group" aria-label="Align"> <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="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) })} /> <Num label="Height (mm)" value={doc.page_height_mm} min={1} step={1} onChange={(n) => setDocPage({ page_height_mm: Math.max(1, n) })} />
</div> </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>
</div> </div>
); );
@@ -1009,17 +1036,31 @@ function BadgePropsPanel({
<span>Text color</span> <span>Text color</span>
<input className="badge-color" type="color" value={t.color} onChange={(e) => patchElement(t.id, { color: e.target.value })} /> <input className="badge-color" type="color" value={t.color} onChange={(e) => patchElement(t.id, { color: e.target.value })} />
</label> </label>
<div className="badge-btn-row"> <div className="badge-field">
<button className={`btn small ${t.bold ? 'active' : ''}`} onClick={() => patchElement(t.id, { bold: !t.bold })} title="Bold"><strong>B</strong></button> <span>Style</span>
<button className={`btn small ${t.italic ? 'active' : ''}`} onClick={() => patchElement(t.id, { italic: !t.italic })} title="Italic"><em>I</em></button> <div className="badge-btn-row">
<span className="badge-tool-sep" /> <button className={`btn small ${t.bold ? 'active' : ''}`} onClick={() => patchElement(t.id, { bold: !t.bold })} title="Bold"><strong>B</strong></button>
{(['left', 'center', 'right'] as const).map((a) => ( <button className={`btn small ${t.italic ? 'active' : ''}`} onClick={() => patchElement(t.id, { italic: !t.italic })} title="Italic"><em>I</em></button>
<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> </div>
))} </div>
<span className="badge-tool-sep" /> <div className="badge-field">
{(['top', 'middle', 'bottom'] as const).map((a) => ( <span>Align text in box</span>
<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-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> </div>
<label className="badge-field"> <label className="badge-field">
<span>Transform</span> <span>Transform</span>
+95
View File
@@ -136,6 +136,33 @@ test('the Delete key removes the selected element', async ({ page }) => {
await expect(page.locator('.badge-el')).toHaveCount(1); await expect(page.locator('.badge-el')).toHaveCount(1);
}); });
test('Ctrl+Z undoes and Ctrl+Y redoes element changes', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await openBadgeEditor(page);
await page.getByRole('button', { name: 'Text', exact: true }).click();
await expect(page.locator('.badge-el')).toHaveCount(3);
await page.keyboard.press('Control+z');
await expect(page.locator('.badge-el')).toHaveCount(2);
await page.keyboard.press('Control+y');
await expect(page.locator('.badge-el')).toHaveCount(3);
// Ctrl+Shift+Z is accepted as an alternative redo.
await page.keyboard.press('Control+z');
await page.keyboard.press('Control+Shift+z');
await expect(page.locator('.badge-el')).toHaveCount(3);
});
test('Ctrl+Z is ignored while typing in a panel field', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await openBadgeEditor(page);
await page.locator('.badge-el').first().click();
await page.getByRole('button', { name: 'Text', exact: true }).click();
await expect(page.locator('.badge-el')).toHaveCount(3);
// Focus a text input so the native field undo wins and the editor stays put.
await page.getByLabel('x (mm)').click();
await page.keyboard.press('Control+z');
await expect(page.locator('.badge-el')).toHaveCount(3);
});
test('a text box can bind the company / organizer field', async ({ page }) => { test('a text box can bind the company / organizer field', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 }); await page.setViewportSize({ width: 1280, height: 900 });
await openBadgeEditor(page); await openBadgeEditor(page);
@@ -181,3 +208,71 @@ test('dragging near the page centre shows a smart guide and snaps', async ({ pag
// After snapping, the QR centre sits on the page centre (x = 50 - w/2 = 39mm). // After snapping, the QR centre sits on the page centre (x = 50 - w/2 = 39mm).
await expect(page.getByLabel('x (mm)')).toHaveValue('39'); await expect(page.getByLabel('x (mm)')).toHaveValue('39');
}); });
test('the text panel exposes a labelled "Align text in box" section', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await openBadgeEditor(page);
await page.locator('.badge-el').first().click();
await expect(page.getByText('Align text in box', { exact: true })).toBeVisible();
await expect(page.getByText('Horizontal', { exact: true })).toBeVisible();
await expect(page.getByText('Vertical', { exact: true })).toBeVisible();
// The default layout text box starts left/middle, so those buttons are pressed.
await expect(page.getByRole('button', { name: 'Align text left' })).toHaveAttribute('aria-pressed', 'true');
await expect(page.getByRole('button', { name: 'Center text vertically' })).toHaveAttribute('aria-pressed', 'true');
});
test('aligning text right inside its box updates the rendered text', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await openBadgeEditor(page);
await page.locator('.badge-el').first().click();
const body = page.locator('.badge-text-body').first();
await expect(body).toHaveCSS('justify-content', 'flex-start');
await page.getByRole('button', { name: 'Align text right' }).click();
await expect(body).toHaveCSS('justify-content', 'flex-end');
await expect(body).toHaveCSS('text-align', 'right');
await expect(page.getByRole('button', { name: 'Align text right' })).toHaveAttribute('aria-pressed', 'true');
await expect(page.getByRole('button', { name: 'Align text left' })).toHaveAttribute('aria-pressed', 'false');
});
test('aligning text to the bottom of its box updates the rendered text', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await openBadgeEditor(page);
await page.locator('.badge-el').first().click();
const body = page.locator('.badge-text-body').first();
await expect(body).toHaveCSS('align-items', 'center');
await page.getByRole('button', { name: 'Align text to bottom' }).click();
await expect(body).toHaveCSS('align-items', 'flex-end');
});
test('text alignment inside the box persists through save', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
const { saves } = await openBadgeEditor(page);
await page.locator('.badge-el').first().click();
await page.getByRole('button', { name: 'Center text', exact: true }).click();
await page.getByRole('button', { name: 'Align text to top' }).click();
await page.getByRole('button', { name: 'Save' }).click();
await expect.poll(() => saves.length).toBe(1);
const elements = saves[0].p_elements as Record<string, unknown>[];
expect(elements[0]).toMatchObject({ id: 'e-text', align_h: 'center', align_v: 'top' });
});
test('text alignment survives a reload of the saved layout', async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await openBadgeEditor(page, {
layouts: [{
id: 'l-default', event_id: eventId, name: 'Default', is_default: true,
page_width_mm: 100, page_height_mm: 70, created_at: '2026-10-23T10:00:00Z', updated_at: '2026-10-23T10:00:00Z',
elements: [
{ id: 'e-text', type: 'text', x: 10, y: 10, width: 60, height: 30, rotation: 0, content: 'static', text: 'Hello', font: 'Open Sans', font_size: 13, line_height: 1, color: '#000000', bold: false, italic: false, align_h: 'right', align_v: 'bottom', transform: 'none' },
],
}],
});
await page.locator('.badge-el').first().click();
await expect(page.getByRole('button', { name: 'Align text right' })).toHaveAttribute('aria-pressed', 'true');
await expect(page.getByRole('button', { name: 'Align text to bottom' })).toHaveAttribute('aria-pressed', 'true');
const body = page.locator('.badge-text-body').first();
await expect(body).toHaveCSS('justify-content', 'flex-end');
await expect(body).toHaveCSS('align-items', 'flex-end');
await expect(body).toHaveCSS('text-align', 'right');
});