initial functionality

This commit is contained in:
Michael Myers
2018-07-24 15:56:13 -04:00
parent 0186450221
commit 8ceb38963e
7 changed files with 118 additions and 7 deletions

View File

@ -10,7 +10,7 @@ import { FootNotes } from "./footnotes";
import { HeaderWrapper } from "./header-wrapper";
import { Media } from "./media";
import { Numbering } from "./numbering";
import { Hyperlink, Paragraph, PictureRun } from "./paragraph";
import { Bookmark, Hyperlink, Paragraph, PictureRun } from "./paragraph";
import { Relationships } from "./relationships";
import { Styles } from "./styles";
import { ExternalStylesFactory } from "./styles/external-styles-factory";
@ -157,6 +157,18 @@ export class File {
return hyperlink;
}
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
text = text === undefined ? anchor : text;
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount, anchor);
return hyperlink;
}
public createBookmark(name: string, text?: string): Bookmark {
text = text === undefined ? name : text;
const bookmark = new Bookmark(name, text, this.docRelationships.RelationshipCount);
return bookmark;
}
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void {
this.document.Body.addSection(sectionPropertiesOptions);
}

View File

@ -0,0 +1,23 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface IBookmarkStartAttributesProperties {
id: string;
name: string;
}
export class BookmarkStartAttributes extends XmlAttributeComponent<IBookmarkStartAttributesProperties> {
protected xmlKeys = {
id: "w:id",
name: "w:name",
};
}
export interface IBookmarkEndAttributesProperties {
id: string;
}
export class BookmarkEndAttributes extends XmlAttributeComponent<IBookmarkEndAttributesProperties> {
protected xmlKeys = {
id: "w:id",
};
}

View File

@ -0,0 +1,57 @@
// http://officeopenxml.com/WPbookmark.php
import { XmlComponent } from "file/xml-components";
import { TextRun } from "../run";
import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes";
export class Bookmark {
public linkId: number;
public readonly start: BookmarkStart;
public readonly text: TextRun;
public readonly end: BookmarkEnd;
constructor(name: string, text: string, relationshipsCount: number) {
this.linkId = relationshipsCount + 1;
this.start = new BookmarkStart(name, text, this.linkId);
this.text = new TextRun(text);
this.end = new BookmarkEnd(this.linkId);
}
}
export class BookmarkStart extends XmlComponent {
public linkId: number;
constructor(name: string, text: string, relationshipsCount: number) {
super("w:bookmarkStart");
this.linkId = relationshipsCount;
const id = `${this.linkId}`;
const attributes = new BookmarkStartAttributes({
name,
id,
});
this.root.push(attributes);
}
}
export class BookmarkEnd extends XmlComponent {
public linkId: number;
constructor(relationshipsCount: number) {
super("w:bookmarkEnd");
this.linkId = relationshipsCount;
const id = `${this.linkId}`;
const attributes = new BookmarkEndAttributes({
id,
});
this.root.push(attributes);
}
}

View File

@ -2,6 +2,7 @@ import { XmlAttributeComponent } from "file/xml-components";
export interface IHyperlinkAttributesProperties {
id?: string;
anchor?: string;
history: number;
}
@ -9,5 +10,6 @@ export class HyperlinkAttributes extends XmlAttributeComponent<IHyperlinkAttribu
protected xmlKeys = {
id: "r:id",
history: "w:history",
anchor: "w:anchor",
};
}

View File

@ -2,19 +2,27 @@
import { XmlComponent } from "file/xml-components";
import { TextRun } from "../run";
import { HyperlinkAttributes } from "./hyperlink-attributes";
import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes";
export class Hyperlink extends XmlComponent {
public linkId: number;
constructor(text: string, relationshipsCount: number) {
constructor(text: string, relationshipsCount: number, anchor?: string) {
super("w:hyperlink");
this.linkId = relationshipsCount + 1;
const attributes = new HyperlinkAttributes({
id: `rId${this.linkId}`,
const props: IHyperlinkAttributesProperties = {
history: 1,
});
};
if (anchor) {
props.anchor = anchor;
} else {
props.id = `rId${this.linkId}`;
}
const attributes = new HyperlinkAttributes(props);
this.root.push(attributes);
this.root.push(new TextRun(text).style("Hyperlink"));
}

View File

@ -1 +1,2 @@
export * from "./hyperlink";
export * from "./bookmark";

View File

@ -13,7 +13,7 @@ import { ISpacingProperties, Spacing } from "./formatting/spacing";
import { Style } from "./formatting/style";
import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
import { Hyperlink } from "./links";
import { Bookmark, Hyperlink } from "./links";
import { ParagraphProperties } from "./properties";
import { PictureRun, Run, TextRun } from "./run";
@ -39,6 +39,14 @@ export class Paragraph extends XmlComponent {
return this;
}
public addBookmark(bookmark: Bookmark): Paragraph {
// Bookmarks by spec have three components, a start, text, and end
this.root.push(bookmark.start);
this.root.push(bookmark.text);
this.root.push(bookmark.end);
return this;
}
public createTextRun(text: string): TextRun {
const run = new TextRun(text);
this.addRun(run);