fix: fix aligment for docx import
This commit is contained in:
1 parent
86376ca7e3
commit
6731134d06
3 files changed
+76
-10
No files matched your search
+14
-10
@@ -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<string, unknown> {
|
||||
// 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<string, unknown> {
|
||||
const attrs: Record<string, unknown> = {};
|
||||
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) {
|
||||
|
||||
@@ -85,6 +85,9 @@ export class DocxStyles {
|
||||
private memoOwn = new Map<string, RunProps>();
|
||||
private styleNum = new Map<string, { numId: number; ilvl: number }>();
|
||||
private ownOutline = new Map<string, number>(); // style's own w:outlineLvl (heading marker)
|
||||
private ownAlign = new Map<string, string>(); // 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<string, string>();
|
||||
private abstractLevels = new Map<string, Map<number, LevelDef>>();
|
||||
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>()): 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 {};
|
||||
|
||||
@@ -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 = `<?xml version="1.0"?><w:document ${W}><w:body>
|
||||
<w:p><w:r><w:t>Inherits justify from Standard</w:t></w:r></w:p>
|
||||
<w:p><w:pPr><w:jc w:val="center"/></w:pPr><w:r><w:t>Direct center wins</w:t></w:r></w:p>
|
||||
<w:p><w:pPr><w:pStyle w:val="Heading1"/></w:pPr><w:r><w:t>Heading based on Standard</w:t></w:r></w:p>
|
||||
<w:sectPr><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1440" w:bottom="1440" w:left="1440" w:right="1440"/></w:sectPr>
|
||||
</w:body></w:document>`;
|
||||
const stylesXml = `<?xml version="1.0"?><w:styles ${W}>
|
||||
<w:style w:type="paragraph" w:default="1" w:styleId="Standard"><w:name w:val="Normal"/><w:pPr><w:jc w:val="both"/></w:pPr></w:style>
|
||||
<w:style w:type="paragraph" w:styleId="Heading1"><w:basedOn w:val="Standard"/><w:pPr><w:outlineLvl w:val="0"/></w:pPr></w:style>
|
||||
</w:styles>`;
|
||||
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"';
|
||||
|
||||
Reference in new issue
Block a user