Skip to content

Commit 1202e24

Browse files
committed
fix: only consider prev fonts if no explicit props set
1 parent 18f8665 commit 1202e24

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

packages/layout-engine/pm-adapter/src/attributes/paragraph.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ describe('computeRunAttrs', () => {
210210
expect(result.fontSize).toBe(10);
211211
});
212212

213+
it('uses runProps font settings when previousParagraphFont is not provided', () => {
214+
const runProps = {
215+
fontFamily: { ascii: 'RunFont' },
216+
fontSize: 20,
217+
};
218+
219+
const result = computeRunAttrs(runProps as never);
220+
221+
expect(result.fontFamily).toContain('RunFont');
222+
expect(result.fontSize).toBeGreaterThan(10);
223+
});
224+
213225
it('passes through vertAlign', () => {
214226
const result = computeRunAttrs({ vertAlign: 'superscript', fontSize: 24 } as never);
215227
expect(result.vertAlign).toBe('superscript');

packages/layout-engine/pm-adapter/src/attributes/paragraph.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,19 @@ export const computeParagraphAttrs = (
298298
true,
299299
Boolean(paragraphProperties.numberingProperties),
300300
);
301+
302+
const hasExplicitRunProps =
303+
resolvedParagraphProperties.runProperties != null &&
304+
Object.keys(resolvedParagraphProperties.runProperties).length > 0;
305+
301306
paragraphAttrs.wordLayout = computeWordParagraphLayout({
302307
paragraph: paragraphAttrs,
303308
listRenderingAttrs: normalizedListRendering,
304-
markerRun: computeRunAttrs(markerRunProperties, converterContext, previousParagraphFont),
309+
markerRun: computeRunAttrs(
310+
markerRunProperties,
311+
converterContext,
312+
hasExplicitRunProps ? undefined : previousParagraphFont,
313+
),
305314
});
306315
}
307316

packages/layout-engine/pm-adapter/src/converters/paragraph.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -927,24 +927,23 @@ const SHAPE_CONVERTERS_REGISTRY: Record<
927927
/**
928928
* Returns the font of the last paragraph block's first run in the given blocks array.
929929
* Used to pass previous paragraph font into paragraphToFlowBlocks for new list items without explicit run properties.
930-
* Only returns when the run has valid fontFamily (string) and fontSize (number).
930+
*
931+
* Only returns when the run has both valid fontFamily (non-empty string) and fontSize (positive finite number).
932+
* If the latest paragraph's first run has partial or empty font info, the loop continues to the previous
933+
* paragraph so callers never receive a partial object and can fall back to defaults consistently.
931934
*/
932-
export function getLastParagraphFont(blocks: FlowBlock[]): { fontFamily?: string; fontSize?: number } | undefined {
935+
export function getLastParagraphFont(blocks: FlowBlock[]): { fontFamily: string; fontSize: number } | undefined {
933936
for (let i = blocks.length - 1; i >= 0; i--) {
934937
const block = blocks[i];
935938
if (block.kind === 'paragraph') {
936939
const para = block as ParagraphBlock;
937940
const firstRun = para.runs?.[0];
938-
if (firstRun) {
939-
const run = firstRun as { fontFamily: string; fontSize: number };
940-
const result = {} as { fontFamily?: string; fontSize?: number };
941-
if (run?.fontFamily) {
942-
result.fontFamily = run.fontFamily;
943-
}
944-
if (run?.fontSize) {
945-
result.fontSize = run.fontSize;
946-
}
947-
return result;
941+
if (!firstRun) continue;
942+
const run = firstRun as { fontFamily?: string; fontSize?: number };
943+
const fontFamily = typeof run.fontFamily === 'string' ? run.fontFamily.trim() : '';
944+
const fontSize = typeof run.fontSize === 'number' && Number.isFinite(run.fontSize) ? run.fontSize : NaN;
945+
if (fontFamily.length > 0 && fontSize > 0) {
946+
return { fontFamily, fontSize };
948947
}
949948
}
950949
}

0 commit comments

Comments
 (0)