Skip to content

Commit fb9bf1d

Browse files
fix(editor): align header footer fallback variant
1 parent c343f29 commit fb9bf1d

2 files changed

Lines changed: 61 additions & 8 deletions

File tree

packages/super-editor/src/editors/v1/core/presentation-editor/header-footer/HeaderFooterSessionManager.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ function storyIdFromHeaderFooterLayoutKey(key: string): string {
378378
function refForVariant(
379379
refs: Partial<Record<'default' | 'first' | 'even' | 'odd', string | null | undefined>> | undefined,
380380
variant: 'default' | 'first' | 'even' | 'odd',
381-
): string | undefined {
381+
): { refId: string; matchedVariant: 'default' | 'first' | 'even' | 'odd' } | undefined {
382382
const ref = refs?.[variant];
383-
if (ref) return ref;
384-
return variant === 'odd' ? (refs?.default ?? undefined) : undefined;
383+
if (ref) return { refId: ref, matchedVariant: variant };
384+
return variant === 'odd' && refs?.default ? { refId: refs.default, matchedVariant: 'default' } : undefined;
385385
}
386386

387387
function resolveResult(result: HeaderFooterLayoutResult, storyId?: string | null): ResolvedHeaderFooterLayout {
@@ -2403,11 +2403,10 @@ export class HeaderFooterSessionManager {
24032403
: null;
24042404
const pageSectionRefs = kind === 'header' ? page?.sectionRefs?.headerRefs : page?.sectionRefs?.footerRefs;
24052405
const legacyRefs = kind === 'header' ? legacyIdentifier.headerIds : legacyIdentifier.footerIds;
2406-
const sectionRId =
2407-
effectiveRef?.refId ??
2408-
refForVariant(pageSectionRefs, headerFooterType) ??
2409-
refForVariant(legacyRefs, headerFooterType);
2410-
const layoutVariantType = effectiveRef?.matchedVariant ?? headerFooterType;
2406+
const fallbackRef =
2407+
refForVariant(pageSectionRefs, headerFooterType) ?? refForVariant(legacyRefs, headerFooterType);
2408+
const sectionRId = effectiveRef?.refId ?? fallbackRef?.refId;
2409+
const layoutVariantType = effectiveRef?.matchedVariant ?? fallbackRef?.matchedVariant ?? headerFooterType;
24112410

24122411
// PRIORITY 1: Try per-rId layout (composite key first for per-section margins, then plain rId)
24132412
const compositeKey = sectionRId ? `${sectionRId}::s${sectionIndex}` : undefined;

packages/super-editor/src/editors/v1/core/presentation-editor/tests/HeaderFooterSessionManager.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,60 @@ describe('HeaderFooterSessionManager', () => {
640640
expect(payload!.items![0]!.blockId).toBe('p1');
641641
});
642642

643+
it('uses the default variant layout when odd ref lookup falls back to default', () => {
644+
const deps: SessionManagerDependencies = {
645+
getLayoutOptions: vi.fn(() => ({})),
646+
getPageElement: vi.fn(() => null),
647+
scrollPageIntoView: vi.fn(),
648+
waitForPageMount: vi.fn(async () => true),
649+
convertPageLocalToOverlayCoords: vi.fn(() => ({ x: 0, y: 0 })),
650+
isViewLocked: vi.fn(() => false),
651+
getBodyPageHeight: vi.fn(() => 800),
652+
notifyInputBridgeTargetChanged: vi.fn(),
653+
scheduleRerender: vi.fn(),
654+
setPendingDocChange: vi.fn(),
655+
getBodyPageCount: vi.fn(() => 1),
656+
};
657+
658+
manager = new HeaderFooterSessionManager({
659+
painterHost,
660+
visibleHost,
661+
selectionOverlay,
662+
editor: createMainEditorStub(),
663+
defaultPageSize: { w: 612, h: 792 },
664+
defaultMargins: { top: 72, right: 72, bottom: 72, left: 72, header: 36, footer: 36 },
665+
});
666+
manager.setDependencies(deps);
667+
manager.headerFooterIdentifier = {
668+
headerIds: { default: null, first: null, even: null, odd: 'rId-header-odd' },
669+
footerIds: { default: null, first: null, even: null, odd: null },
670+
titlePg: false,
671+
alternateHeaders: true,
672+
};
673+
manager.setLayoutResults([buildHeaderResult()], null);
674+
675+
const layout: Layout = {
676+
version: 1,
677+
flowMode: 'paginated',
678+
pageGap: 0,
679+
pageSize: { w: 612, h: 792 },
680+
pages: [
681+
{
682+
number: 1,
683+
sectionRefs: { headerRefs: { default: 'rId-header-default' }, footerRefs: {} },
684+
margins: { top: 72, right: 72, bottom: 72, left: 72, header: 36, footer: 36 },
685+
} as never,
686+
],
687+
} as unknown as Layout;
688+
const provider = manager.createDecorationProvider('header', layout as unknown as ResolvedLayout);
689+
const payload = provider!(1, layout.pages[0]!.margins, layout.pages[0] as unknown as ResolvedPage);
690+
691+
expect(payload).not.toBeNull();
692+
expect(payload!.headerFooterRefId).toBe('rId-header-default');
693+
expect(payload!.sectionType).toBe('odd');
694+
expect(payload!.items?.[0]?.blockId).toBe('p1');
695+
});
696+
643697
it('recomputes variant items when cached resolved items become misaligned', () => {
644698
const deps: SessionManagerDependencies = {
645699
getLayoutOptions: vi.fn(() => ({})),

0 commit comments

Comments
 (0)