Skip to content

Commit 5722b61

Browse files
fix(document-api): inherit converter header refs
1 parent 2cf5e3f commit 5722b61

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

packages/super-editor/src/editors/v1/document-api-adapters/helpers/header-footer-refs-mutation.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { describe, expect, it } from 'vitest';
22
import { resolveEffectiveRef } from './header-footer-refs-mutation.js';
33
import type { SectionProjection } from './sections-resolver.js';
44

5-
function projection(sectionIndex: number, refs: SectionProjection['range']['headerRefs']): SectionProjection {
5+
function projection(
6+
sectionIndex: number,
7+
refs: SectionProjection['range']['headerRefs'],
8+
domainRefs?: SectionProjection['domain']['headerRefs'],
9+
): SectionProjection {
610
return {
711
sectionId: `section-${sectionIndex}`,
812
address: { kind: 'section', sectionId: `section-${sectionIndex}` },
@@ -11,7 +15,9 @@ function projection(sectionIndex: number, refs: SectionProjection['range']['head
1115
headerRefs: refs,
1216
} as SectionProjection['range'],
1317
target: { kind: 'body' },
14-
domain: {},
18+
domain: {
19+
...(domainRefs && { headerRefs: domainRefs }),
20+
},
1521
};
1622
}
1723

@@ -38,6 +44,16 @@ describe('resolveEffectiveRef', () => {
3844
});
3945
});
4046

47+
it('inherits converter-preserved refs exposed through section domain metadata', () => {
48+
const sections = [projection(0, undefined, { default: 'h0-domain-default' }), projection(1, undefined)];
49+
50+
expect(resolveEffectiveRef(sections, 1, 'header', 'default')).toMatchObject({
51+
refId: 'h0-domain-default',
52+
resolvedFromSection: { kind: 'section', sectionId: 'section-0' },
53+
resolvedVariant: 'default',
54+
});
55+
});
56+
4157
it('returns null when resolving before the first section', () => {
4258
const sections = [projection(0, { default: 'h0-default' })];
4359

packages/super-editor/src/editors/v1/document-api-adapters/helpers/header-footer-refs-mutation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
type ConverterWithHeaderFooterParts,
2020
} from './header-footer-parts.js';
2121

22+
type HeaderFooterRefs = Partial<Record<HeaderFooterVariant, string | null>>;
23+
2224
// ---------------------------------------------------------------------------
2325
// Shared resolver
2426
// ---------------------------------------------------------------------------
@@ -34,12 +36,17 @@ export function resolveEffectiveRef(
3436
kind: HeaderFooterKind,
3537
variant: HeaderFooterVariant,
3638
): { refId: string; resolvedFromSection: SectionAddress; resolvedVariant: HeaderFooterVariant } | null {
39+
const refsFor = (section: SectionProjection, refKind: HeaderFooterKind): HeaderFooterRefs | undefined =>
40+
refKind === 'header'
41+
? ((section.range.headerRefs ?? section.domain.headerRefs) as HeaderFooterRefs | undefined)
42+
: ((section.range.footerRefs ?? section.domain.footerRefs) as HeaderFooterRefs | undefined);
43+
3744
const resolved = resolveEffectiveHeaderFooterRef({
3845
sections: sections.map((section) => ({
3946
sectionIndex: section.range.sectionIndex,
4047
titlePg: section.range.titlePg,
41-
headerRefs: section.range.headerRefs,
42-
footerRefs: section.range.footerRefs,
48+
headerRefs: refsFor(section, 'header'),
49+
footerRefs: refsFor(section, 'footer'),
4350
})),
4451
sectionIndex: startSectionIndex - 1,
4552
kind,

0 commit comments

Comments
 (0)