diff --git a/src/lib/import/docx.ts b/src/lib/import/docx.ts index 4fcb151..f8dadff 100644 --- a/src/lib/import/docx.ts +++ b/src/lib/import/docx.ts @@ -300,7 +300,11 @@ function convertParagraph(el: Element, ctx: Ctx, kind: BlockKind, boldByDefault: const ppr = fc(el, 'pPr'); const pStyle = fc(ppr, 'pStyle'); const level = headingLevelOf(ppr, ctx); - const attrs = blockAttrs(ppr, kind, level); + // Alignment: direct w:pPr/w:jc wins, else resolve it from the style chain (the default + // paragraph style commonly carries justify), so style-level alignment isn't lost. + const directJc = fc(ppr, 'jc'); + const jcVal = directJc ? wVal(directJc) : ctx.styles.paragraphAlign(pStyle ? wVal(pStyle) : null); + const attrs = blockAttrs(ppr, kind, level, jcVal); const baseRun = mergeRunProps(ctx.styles.defaultRun(), ctx.styles.styleOwn(pStyle ? wVal(pStyle) : null)); const content = convertInline(el, ctx, baseRun, level, false, boldByDefault); @@ -351,17 +355,17 @@ function headingLevelOf(ppr: Element | null, ctx: Ctx): number | null { return null; } -// Block attrs from DIRECT w:pPr only — Word stores per-paragraph formatting inline -// (style-level defaults stay in the style and are left as the editor's defaults). -function blockAttrs(ppr: Element | null, kind: BlockKind, headingLevel: number | null): Record { +// Spacing/indent come from DIRECT w:pPr only — Word stores per-paragraph formatting +// inline (style-level defaults stay in the style and are left as the editor's defaults). +// Alignment (jcVal) is resolved through the style chain by the caller. +function blockAttrs(ppr: Element | null, kind: BlockKind, headingLevel: number | null, jcVal: string | null): Record { const attrs: Record = {}; - if (!ppr) return attrs; - const jc = fc(ppr, 'jc'); - const ta = jc ? wVal(jc) : null; - if (ta === 'center') attrs.textAlign = 'center'; - else if (ta === 'both' || ta === 'distribute') attrs.textAlign = 'justify'; - else if (ta === 'right' || ta === 'end') attrs.textAlign = 'right'; + if (jcVal === 'center') attrs.textAlign = 'center'; + else if (jcVal === 'both' || jcVal === 'distribute') attrs.textAlign = 'justify'; + else if (jcVal === 'right' || jcVal === 'end') attrs.textAlign = 'right'; + + if (!ppr) return attrs; const sp = fc(ppr, 'spacing'); if (sp) { diff --git a/src/lib/import/docxStyles.ts b/src/lib/import/docxStyles.ts index 54cbfd2..9dc02ae 100644 --- a/src/lib/import/docxStyles.ts +++ b/src/lib/import/docxStyles.ts @@ -85,6 +85,9 @@ export class DocxStyles { private memoOwn = new Map(); private styleNum = new Map(); private ownOutline = new Map(); // style's own w:outlineLvl (heading marker) + private ownAlign = new Map(); // style's own w:pPr/w:jc + private defaultParaStyle: string | null = null; // the w:default="1" paragraph style + private defaultsAlign: string | null = null; // docDefaults w:pPrDefault/w:jc private numToAbstract = new Map(); private abstractLevels = new Map>(); private minorFont?: string; // theme1.xml body font (e.g. Calibri) @@ -115,12 +118,18 @@ export class DocxStyles { if (defs) { const rPr = defs.getElementsByTagNameNS(W, 'rPrDefault')[0]?.getElementsByTagNameNS(W, 'rPr')[0]; this.defaultsRun = parseRunProps(rPr); + const pPr = defs.getElementsByTagNameNS(W, 'pPrDefault')[0]?.getElementsByTagNameNS(W, 'pPr')[0]; + const ddJc = pPr ? firstChild(pPr, 'jc') : null; + if (ddJc) this.defaultsAlign = wVal(ddJc); } for (const style of Array.from(doc.getElementsByTagNameNS(W, 'style'))) { const id = style.getAttributeNS(W, 'styleId'); if (!id) continue; this.basedOn.set(id, firstChild(style, 'basedOn') ? wVal(firstChild(style, 'basedOn')!) : null); this.ownRun.set(id, parseRunProps(firstChild(style, 'rPr'))); + if (style.getAttributeNS(W, 'type') === 'paragraph' && (style.getAttributeNS(W, 'default') === '1' || style.getAttributeNS(W, 'default') === 'true')) { + this.defaultParaStyle = id; + } const ppr = firstChild(style, 'pPr'); const numPr = ppr && firstChild(ppr, 'numPr'); if (numPr) { @@ -129,6 +138,8 @@ export class DocxStyles { } const ol = ppr && firstChild(ppr, 'outlineLvl'); if (ol) { const n = parseInt(wVal(ol) ?? '', 10); if (Number.isFinite(n)) this.ownOutline.set(id, n); } + const jc = ppr && firstChild(ppr, 'jc'); + if (jc) { const v = wVal(jc); if (v) this.ownAlign.set(id, v); } } } @@ -188,6 +199,28 @@ export class DocxStyles { return this.styleOutlineLvl(this.basedOn.get(styleId) ?? null, seen); } + // The style's own w:jc, nearest along the w:basedOn chain (null if none in the chain). + private styleAlign(styleId: string | null | undefined, seen = new Set()): string | null { + if (!styleId || seen.has(styleId)) return null; + seen.add(styleId); + const own = this.ownAlign.get(styleId); + if (own != null) return own; + return this.styleAlign(this.basedOn.get(styleId) ?? null, seen); + } + + // Effective paragraph alignment from styles only (direct w:pPr/w:jc is read and wins in + // the caller): the pStyle's basedOn chain, else the default paragraph style, else + // docDefaults. Lets a paragraph inheriting justify from its style keep it. null = unset. + paragraphAlign(pStyleId: string | null | undefined): string | null { + const own = this.styleAlign(pStyleId); + if (own != null) return own; + if (this.defaultParaStyle && this.defaultParaStyle !== pStyleId) { + const def = this.styleAlign(this.defaultParaStyle); + if (def != null) return def; + } + return this.defaultsAlign; + } + level(numId: number, ilvl: number): LevelDef { const abs = this.numToAbstract.get(String(numId)); if (!abs) return {}; diff --git a/tests/docx-roundtrip.test.ts b/tests/docx-roundtrip.test.ts index a6a0bd1..cc24ab0 100644 --- a/tests/docx-roundtrip.test.ts +++ b/tests/docx-roundtrip.test.ts @@ -219,6 +219,35 @@ describe('DOCX import: empty line keeps its paragraph-mark font size', () => { }); }); +describe('DOCX import: alignment inherited from a paragraph style', () => { + const W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"'; + // The default paragraph style (Standard) sets justify; body paragraphs and a heading + // based on it inherit it. A direct w:jc still wins over the style. + const documentXml = ` + Inherits justify from Standard + Direct center wins + Heading based on Standard + + `; + const stylesXml = ` + + + `; + const bytes = zipSync({ 'word/document.xml': strToU8(documentXml), 'word/styles.xml': strToU8(stylesXml) }); + const doc = importDocx(bytes).content as N; + + it('inherits justify onto a body paragraph with no direct alignment', () => { + expect(doc.content![0].attrs.textAlign).toBe('justify'); + }); + it('lets a direct w:jc override the style', () => { + expect(doc.content![1].attrs.textAlign).toBe('center'); + }); + it('inherits justify onto a heading based on the default style', () => { + expect(doc.content![2].type).toBe('heading'); + expect(doc.content![2].attrs.textAlign).toBe('justify'); + }); +}); + describe('DOCX import resolves theme fonts (Word default = Calibri)', () => { const W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"'; const A = 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"';