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:
1 parent
ab25c102dc
commit
6b277a2a67
3 files changed
+156
-18
No files matched your search
@@ -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.selected { outline: 1.5px solid var(--accent); }
|
||||
.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-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 svg { width: 100%; height: 100%; }
|
||||
.badge-image-body { width: 100%; height: 100%; display: block; }
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user