Skip to content

Commit

Permalink
Fixing Merge Issues
Browse files Browse the repository at this point in the history
  • Loading branch information
theimo1221 committed Nov 24, 2021
1 parent 5e13258 commit 28c9325
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 207 deletions.
562 changes: 449 additions & 113 deletions dist/docx-preview.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/docx-preview.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/docx-preview.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/docx-preview.min.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/common/relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface Relationship {
export enum RelationshipTypes {
OfficeDocument = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
FontTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable",
Footer = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
Image = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
Numbering = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
Styles = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
Expand Down
43 changes: 1 addition & 42 deletions src/document-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,6 @@ export class DocumentParser {

parseDefaultProperties(elem: Element, style: Record<string, string> = null, childStyle: Record<string, string> = null, handler: (prop: Element) => boolean = null): Record<string, string> {
style = style || {};


xml.foreach(elem, c => {
switch (c.localName) {
case "jc":
Expand Down Expand Up @@ -1055,7 +1053,7 @@ export class DocumentParser {
return;
}

var asciiTheme = xml.stringAttr(node, "w:asciiTheme");
var asciiTheme = xml.stringAttr(node, "asciiTheme");
if (asciiTheme) {
style["asciiTheme"] = asciiTheme;
}
Expand Down Expand Up @@ -1161,45 +1159,6 @@ export class DocumentParser {
}
});
}

parseFooterFile(xmlString: string): {root: Element, content: ParagraphElement[]} {

var xFooter = globalXmlParser.parse(xmlString, this.skipDeclaration);
const result: {root: Element, content: ParagraphElement[]} = {root: xFooter, content: []};
var paragraphs = xFooter.getElementsByTagName("w:p");
if(!paragraphs || paragraphs.length === 0) {
return result;
}
for(let i=0; i<paragraphs.length; i++) {
result.content.push(this.parseParagraph(paragraphs[i]) as ParagraphElement);
}

return result;
}

parseThemesFile(xmlString: string): ImportantFonts {
const result: ImportantFonts = {};

var xthemes = globalXmlParser.parse(xmlString, this.skipDeclaration);
var majorFontElements = xthemes.getElementsByTagName("a:majorFont");
var minorFontElements = xthemes.getElementsByTagName("a:minorFont");
if(majorFontElements && majorFontElements.length === 1) {
result.majorLatin = this.parseLatinTypeface(majorFontElements[0]);
}
if(minorFontElements && minorFontElements.length === 1) {
result.minorLatin = this.parseLatinTypeface(minorFontElements[0]);
}

return result;
}

private parseLatinTypeface(fontElement: Element): string {
const latinFontElements = fontElement.getElementsByTagName("a:latin");
if(latinFontElements && latinFontElements.length === 1) {
return latinFontElements[0].getAttribute("typeface") ?? "";
}
return "";
}
}

enum SizeType {
Expand Down
1 change: 1 addition & 0 deletions src/footer/footer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DomType, OpenXmlElement } from "../dom/dom";

export class WmlFooter implements OpenXmlElement {
id?: string = "";
type: DomType = DomType.Footer;
children?: OpenXmlElement[] = [];
cssStyle?: Record<string, string> = {};
Expand Down
49 changes: 36 additions & 13 deletions src/html-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { RunElement, RunProperties } from './dom/run';
import { BookmarkStartElement } from './dom/bookmark';
import { IDomStyle } from './dom/style';
import { FooterPart } from "./footer/footer-part";
import { ThemePart } from "./theme/theme-part";
import { WmlFooter } from "./footer/footer";

interface CssChangeObject {
cssRuleCamel: string;
Expand Down Expand Up @@ -227,10 +229,15 @@ export class HtmlRenderer {

this.processElement(document);

for (let section of this.splitBySection(document.children)) {
var sectionElement = this.createSection(this.className, section.sectProps || document.props);
for (let section of this.splitBySection(document.children, document.props)) {
var sectionElement = this.createSection(this.className, section.sectProps);
this.renderStyleValues(document.cssStyle, sectionElement);
this.renderElements(section.elements, document, sectionElement);
this.renderElements(section.elements, sectionElement);
if (this.hasFooter(section.sectProps.footer)) {
sectionElement.appendChild(
this.createFooter(this.className, section.sectProps as SectionRenderProperties)
);
}
result.push(sectionElement);
}

Expand Down Expand Up @@ -432,8 +439,8 @@ ${c}-footer-container { position: absolute; bottom: 0px; left: 0px; width: 100%;
"white-space": {cssRuleCamel: "whiteSpace", newVal: "preWrap"},
};
if(this.options.experimental) {
styleText +=`\n.${c} p { word-spacing: -0.55pt; }`;
this.noCssDict[`.${c} p`]["word-spacing"] = {cssRuleCamel: "wordSpacing", newVal: "-0.55pt"};
styleText +=`\n.${c} p { word-spacing: -0.54pt; }`;
this.noCssDict[`.${c} p`]["word-spacing"] = {cssRuleCamel: "wordSpacing", newVal: "-0.54pt"};
}
}

Expand Down Expand Up @@ -1023,8 +1030,9 @@ ${c}-footer-container { position: absolute; bottom: 0px; left: 0px; width: 100%;
}

private replaceAsciiTheme(style: IDomStyle, addDefault: boolean = false) {
var translatedFonts = this.document.themesPart.importantFonts;
const minorLatinFont = translatedFonts.minorLatin;
const themePart = this.document.parts.find((x) => x.path.indexOf("theme")>=0) as ThemePart;
var translatedFonts = themePart.theme.fontScheme;
const minorLatinFont = translatedFonts.minorFont.latinTypeface;
const hasMinorLatin = minorLatinFont !== "" && minorLatinFont !== undefined
for (var j = 0; j < style.styles.length; j++) {
var substyle = style.styles[j];
Expand All @@ -1040,11 +1048,11 @@ ${c}-footer-container { position: absolute; bottom: 0px; left: 0px; width: 100%;
if (hasFontFamily) {
continue;
}
if (value === "minorHAnsi" && translatedFonts.minorLatin) {
if (value === "minorHAnsi" && minorLatinFont) {
substyle.values["font-family"] = minorLatinFont;
}
else if (value === "majorHAnsi" && translatedFonts.minorLatin) {
substyle.values["font-family"] = translatedFonts.majorLatin;
else if (value === "majorHAnsi" && translatedFonts.majorFont?.latinTypeface) {
substyle.values["font-family"] = translatedFonts.majorFont.latinTypeface;
}
}
}
Expand Down Expand Up @@ -1077,11 +1085,26 @@ ${c}-footer-container { position: absolute; bottom: 0px; left: 0px; width: 100%;



private createFooter(className: string, sectProps: SectionRenderProperties): HTMLElement {
const elem = this.htmlDocument.createElement("div");
elem.className = `${className}-footer-container`;
const footerElem: WmlFooter =
this.getNeededFooter(sectProps.footer, sectProps.pageWithinSection)
if(!footerElem) {
return elem;
}
this.renderElements(footerElem.children, elem);

return elem;
}



hasFooter(footer: SectionFooter): boolean {
return (footer.default !== undefined || footer.first !== undefined || footer.even !== undefined);
}

getNeededFooter(footer: SectionFooter, pageWithinSection: number): FooterPart {
getNeededFooter(footer: SectionFooter, pageWithinSection: number): WmlFooter {
let footerId: string | undefined;
if(footer.forceFirstDifferent) {
footerId = footer.first;
Expand All @@ -1094,10 +1117,10 @@ ${c}-footer-container { position: absolute; bottom: 0px; left: 0px; width: 100%;
} else {
footerId = footer.default;
}
if(footerId === undefined || this.document.footerParts[footerId] === undefined) {
if(footerId === undefined || this.document.footer[footerId] === undefined) {
return undefined;
}
return this.document.footerParts[footerId];
return this.document.footer[footerId];
}
}

Expand Down
23 changes: 0 additions & 23 deletions src/themes/themes-part.ts

This file was deleted.

25 changes: 13 additions & 12 deletions src/word-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import { DocumentPart } from './dom/document-part';
import { resolvePath, splitPath } from './utils';
import { NumberingPart } from './numbering/numbering-part';
import { StylesPart } from './styles/styles-part';
import { ThemesPart } from "./themes/themes-part";
import { FooterPart } from "./footer/footer-part";
import { HeaderPart } from "./header/header-part";
import { ExtendedPropsPart } from "./document-props/extended-props-part";
import { CorePropsPart } from "./document-props/core-props-part";
import { ThemePart } from "./theme/theme-part";
import { WmlFooter } from "./footer/footer";

const topLevelRels = [
{ type: RelationshipTypes.OfficeDocument, target: "word/document.xml" },
{ type: RelationshipTypes.ExtendedProperties, target: "docProps/app.xml" },
{ type: RelationshipTypes.CoreProperties, target: "docProps/core.xml" },
{ type: RelationshipTypes.OfficeDocument, target: "word/document.xml", id: "" },
{ type: RelationshipTypes.ExtendedProperties, target: "docProps/app.xml", id: "" },
{ type: RelationshipTypes.CoreProperties, target: "docProps/core.xml", id: "" },
];

export class WordDocument {
Expand All @@ -34,9 +34,9 @@ export class WordDocument {
fontTablePart: FontTablePart;
numberingPart: NumberingPart;
stylesPart: StylesPart;
themesPart: ThemesPart;
corePropsPart: CorePropsPart;
extendedPropsPart: ExtendedPropsPart;
footer: { [id: string]: WmlFooter } = {};

static load(blob, parser: DocumentParser, options: any): Promise<WordDocument> {
var d = new WordDocument();
Expand All @@ -53,7 +53,7 @@ export class WordDocument {

const tasks = topLevelRels.map(rel => {
const r = rels.find(x => x.type === rel.type) ?? rel; //fallback
return d.loadRelationshipPart(r.target, r.type);
return d.loadRelationshipPart(r.target, r.type, r.id);
});

return Promise.all(tasks);
Expand All @@ -64,7 +64,7 @@ export class WordDocument {
return this._package.save(type);
}

private loadRelationshipPart(path: string, type: string): Promise<Part> {
private loadRelationshipPart(path: string, type: string, id: string): Promise<Part> {
if (this.partsMap[path])
return Promise.resolve(this.partsMap[path]);

Expand Down Expand Up @@ -109,10 +109,6 @@ export class WordDocument {
case RelationshipTypes.ExtendedProperties:
this.extendedPropsPart = part = new ExtendedPropsPart(this._package, path);
break;

case RelationshipTypes.Theme:
this.themesPart = part = new ThemesPart(path, this._parser);
break;
}

if (part == null)
Expand All @@ -122,12 +118,17 @@ export class WordDocument {
this.parts.push(part);

return part.load().then(() => {
if(type === RelationshipTypes.Footer) {
const footerElem = (part as FooterPart).footerElement;
footerElem.id = id;
this.footer[id] = footerElem;
}
if (part.rels == null || part.rels.length == 0)
return part;

const [folder] = splitPath(part.path);
const rels = part.rels.map(rel => {
return this.loadRelationshipPart(resolvePath(rel.target, folder), rel.type)
return this.loadRelationshipPart(resolvePath(rel.target, folder), rel.type, rel.id)
});

return Promise.all(rels).then(() => part);
Expand Down

0 comments on commit 28c9325

Please sign in to comment.