Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ params:
default: "My Title"
style: # overrides defaults for this param
fg: 2
align: center # default line alignment: left | center | right ([ALIGN=x] in text wins)
font:
name: BarlowCondensed
size: 96
Expand Down
29 changes: 29 additions & 0 deletions components/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function computeStyleDelta(current: StyleDef, original: StyleDef): Partial<Style
if (current.fg !== undefined && current.fg !== original.fg) delta.fg = current.fg;
if (current.bg !== undefined && current.bg !== original.bg) delta.bg = current.bg;
if (current.split !== undefined && current.split !== original.split) delta.split = current.split;
if (current.align !== undefined && current.align !== original.align) delta.align = current.align;
const cFont = current.font ?? {};
const oFont = original.font ?? {};
const fontDelta: any = {};
Expand Down Expand Up @@ -1273,6 +1274,7 @@ function DefaultsPanel({
onDefaultsChange: (d: StyleDef) => void;
}) {
const weightOptions = [100, 200, 300, 400, 500, 600, 700, 800, 900];
const alignOptions = ['left', 'center', 'right'] as const;
const inputClass =
'bg-gray-800 border border-gray-700 text-gray-100 rounded px-1.5 py-0.5 focus:outline-none focus:border-blue-500 font-mono text-[11px]';

Expand Down Expand Up @@ -1328,6 +1330,17 @@ function DefaultsPanel({
{weightOptions.map((w) => <option key={w} value={w}>{w}</option>)}
</select>
</div>
<div className="flex items-center gap-1">
<span className="text-gray-500">align</span>
<select
value={defaults.align ?? ''}
onChange={(e) => onDefaultsChange({ ...defaults, align: (e.target.value || undefined) as StyleDef['align'] })}
className={inputClass}
>
<option value="">—</option>
{alignOptions.map((a) => <option key={a} value={a}>{a}</option>)}
</select>
</div>
</div>
</div>
);
Expand Down Expand Up @@ -1355,6 +1368,7 @@ function StyleInfoPanel({

const isOverridden = (curr: any, def: any) => curr !== undefined && curr !== def;
const weightOptions = [100, 200, 300, 400, 500, 600, 700, 800, 900];
const alignOptions = ['left', 'center', 'right'] as const;
const inputClass =
'bg-gray-800 border border-gray-700 text-gray-100 rounded px-1.5 py-0.5 focus:outline-none focus:border-blue-500 font-mono text-[11px]';

Expand Down Expand Up @@ -1453,6 +1467,21 @@ function StyleInfoPanel({
</select>
</div>

{/* align */}
<div className="flex items-center gap-2">
<span className={`w-10 shrink-0 ${isOverridden(ps.align, td.align) ? 'text-amber-400' : 'text-gray-500'}`}>
{isOverridden(ps.align, td.align) && <span className="mr-0.5 opacity-60">✱</span>}align
</span>
<select
value={ps.align ?? ''}
onChange={(e) => update({ align: (e.target.value || undefined) as StyleDef['align'] })}
className={inputClass}
>
<option value="">{td.align ? `— (${td.align})` : '—'}</option>
{alignOptions.map((a) => <option key={a} value={a}>{a}</option>)}
</select>
</div>

{/* split */}
{(originalStyle.split !== undefined || ps.split !== undefined) && (
<div className="flex items-center gap-2">
Expand Down
49 changes: 49 additions & 0 deletions lib/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,52 @@ describe('expandSpecialElements — inline markup', () => {
expect(children[1].props.style.flex).toBe(1); // fills remaining horizontal space
});
});

// ── expandSpecialElements — style align ──────────────────────────────────────

describe('expandSpecialElements — style align', () => {
const linesNode = (children: string, containerStyle: Record<string, any> = {}) => ({
type: 'lines',
props: { param: 'text', style: { fontSize: 28 }, containerStyle, children },
});
const paramDefs = (align?: 'left' | 'center' | 'right') =>
({ text: { description: '', required: false, default: '', style: align ? { align } : {} } }) as any;

it('param style.align sets justifyContent and stretches the line wrapper', () => {
const result = expandSpecialElements(linesNode('Hello'), [], paramDefs('center'));
const lineDiv = result.props.children[0];
expect(lineDiv.props.style.justifyContent).toBe('center');
expect(lineDiv.props.style.alignSelf).toBe('stretch');
});

it('[ALIGN=x] in text overrides param style.align', () => {
const result = expandSpecialElements(linesNode('[ALIGN=left]Hello\nWorld'), [], paramDefs('center'));
expect(result.props.children[0].props.style.justifyContent).toBe('flex-start');
expect(result.props.children[1].props.style.justifyContent).toBe('center');
});

it('ALIGN inside a span tag aligns the whole line', () => {
const result = expandSpecialElements(linesNode('[FG=1 FS=170 ALIGN=right]mediapart[/]'), ['#FF0000'], paramDefs('center'));
const lineDiv = result.props.children[0];
expect(lineDiv.props.style.justifyContent).toBe('flex-end');
expect(lineDiv.props.style.alignSelf).toBe('stretch');
});

it('explicit alignment stretches even when containerStyle sets alignItems: center', () => {
const result = expandSpecialElements(linesNode('[ALIGN=right]Hi', { alignItems: 'center' }));
expect(result.props.style.alignItems).toBe('center');
expect(result.props.children[0].props.style.alignSelf).toBe('stretch');
expect(result.props.children[0].props.style.justifyContent).toBe('flex-end');
});

it('template defaults.align is used when the param has no align', () => {
const result = expandSpecialElements(linesNode('Hi'), [], paramDefs(), { align: 'right' });
expect(result.props.children[0].props.style.justifyContent).toBe('flex-end');
});

it('no alignment anywhere keeps the legacy wrapper (no alignSelf)', () => {
const result = expandSpecialElements(linesNode('Hi'), [], paramDefs());
expect(result.props.children[0].props.style.alignSelf).toBeUndefined();
expect(result.props.children[0].props.style.justifyContent).toBe('flex-start');
});
});
29 changes: 19 additions & 10 deletions lib/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function parseInlineMarkup(
text: string,
baseStyle: Record<string, any>,
palette: string[]
): { element: any; align: Align } {
): { element: any; align: Align | null } {
const resolveColor = (ref: string): string | null => {
const idx = parseInt(ref, 10);
if (!isNaN(idx) && idx >= 1 && idx <= palette.length) return palette[idx - 1];
Expand All @@ -180,8 +180,8 @@ function parseInlineMarkup(
const preserveSpaces = (s: string) =>
s.replace(/^( +)/, m => ' '.repeat(m.length)).replace(/( +)$/, m => ' '.repeat(m.length));

// Extract leading [ALIGN=x] tag (no closing [/])
let align: Align = 'left';
// Extract leading [ALIGN=x] tag (no closing [/]); null = not specified
let align: Align | null = null;
let workText = text;
const alignMatch = /^\[ALIGN=(left|center|right)\]/i.exec(workText);
if (alignMatch) {
Expand Down Expand Up @@ -219,7 +219,11 @@ function parseInlineMarkup(
else if (key === 'FS') { const n = parseInt(val, 10); if (!isNaN(n)) override.fontSize = n; }
else if (key === 'FW') { const n = parseInt(val, 10); if (!isNaN(n)) override.fontWeight = n; }
else if (key === 'ROT') { const n = parseInt(val, 10); if (!isNaN(n)) override.transform = `rotate(${n}deg)`; }
else if (isDivBlock && key === 'ALIGN') { blockAlign = val.toLowerCase() as Align; }
else if (key === 'ALIGN' && /^(left|center|right)$/i.test(val)) {
const a = val.toLowerCase() as Align;
if (isDivBlock) blockAlign = a;
else if (align === null) align = a; // ALIGN in a span tag aligns the whole line
}
}

const tagText = preserveSpaces(match[2]);
Expand Down Expand Up @@ -304,12 +308,14 @@ export function expandSpecialElements(
// Resolve styles from param: reference if present
let paramStyle: Record<string, any> = {};
let paramSplit: boolean | undefined = undefined;
let defaultAlign: Align | undefined = templateDefaults?.align;

if (node.props?.param) {
const paramKey: string = node.props.param;
const paramDef = paramDefs[paramKey];
paramStyle = resolveStyle(paramDef, templateDefaults, palette);
paramSplit = paramDef?.style?.split;
defaultAlign = paramDef?.style?.align ?? templateDefaults?.align;
}

if (node.type === 'image') {
Expand Down Expand Up @@ -375,7 +381,8 @@ export function expandSpecialElements(
const doSplit = splitProp !== false && splitProp !== 'false';

if (!doSplit) {
const { element, align } = parseInlineMarkup(text, { ...style, whiteSpace: 'pre-wrap' }, palette);
const { element, align: lineAlign } = parseInlineMarkup(text, { ...style, whiteSpace: 'pre-wrap' }, palette);
const align = lineAlign ?? defaultAlign ?? 'left';
if (align === 'left') {
return { ...element, props: { ...element.props, style: { ...element.props.style, alignSelf: 'flex-start' } } };
}
Expand All @@ -400,11 +407,13 @@ export function expandSpecialElements(
if (line.trim() === '') {
return { type: 'div', props: { style: { display: 'flex', height: lineHeight } } };
}
const { element, align } = parseInlineMarkup(line, { ...style, whiteSpace: 'pre-wrap' }, palette);
return {
type: 'div',
props: { style: { display: 'flex', justifyContent: alignToJustify(align) }, children: [element] },
};
const { element, align: lineAlign } = parseInlineMarkup(line, { ...style, whiteSpace: 'pre-wrap' }, palette);
const align = lineAlign ?? defaultAlign;
// Explicit alignment: stretch the wrapper so justifyContent works whatever the container's alignItems
const wrapperStyle: Record<string, any> = align
? { display: 'flex', alignSelf: 'stretch', justifyContent: alignToJustify(align), textAlign: align }
: { display: 'flex', justifyContent: 'flex-start' };
return { type: 'div', props: { style: wrapperStyle, children: [element] } };
}),
},
};
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const StyleDefSchema = z.object({
}).optional(),
orientation: z.number().optional(),
split: z.boolean().optional(),
align: z.enum(['left', 'center', 'right']).optional(),
});

const ParamDefSchema = z.object({
Expand Down
6 changes: 4 additions & 2 deletions profiles/manifesto/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ params:
type: textarea
style:
fg: 2
align: center
font:
name: BarlowCondensed
size: 128
Expand Down Expand Up @@ -214,16 +215,17 @@ element:
flexDirection: column
justifyContent: center
alignItems: center
paddingLeft: 10
paddingRight: 10
children:
- type: lines
props:
param: text
style:
textAlign: center
lineHeight: 0.95
textTransform: uppercase
containerStyle:
alignItems: center
alignSelf: stretch
gap: 0
children: "{{ text }}"

Expand Down
Loading