Add children to hyperlink

This commit is contained in:
Rowan Cockett
2021-08-27 16:53:11 -06:00
parent 9efee181ba
commit c1cc211c7f
4 changed files with 21 additions and 15 deletions

View File

@ -10,10 +10,12 @@ describe("ConcreteHyperlink", () => {
beforeEach(() => { beforeEach(() => {
hyperlink = new ConcreteHyperlink( hyperlink = new ConcreteHyperlink(
new TextRun({ [
text: "https://example.com", new TextRun({
style: "Hyperlink", text: "https://example.com",
}), style: "Hyperlink",
}),
],
"superid", "superid",
); );
}); });
@ -42,10 +44,12 @@ describe("ConcreteHyperlink", () => {
describe("with optional anchor parameter", () => { describe("with optional anchor parameter", () => {
beforeEach(() => { beforeEach(() => {
hyperlink = new ConcreteHyperlink( hyperlink = new ConcreteHyperlink(
new TextRun({ [
text: "Anchor Text", new TextRun({
style: "Hyperlink", text: "Anchor Text",
}), style: "Hyperlink",
}),
],
"superid2", "superid2",
"anchor", "anchor",
); );
@ -78,7 +82,7 @@ describe("ExternalHyperlink", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create", () => { it("should create", () => {
const externalHyperlink = new ExternalHyperlink({ const externalHyperlink = new ExternalHyperlink({
child: new TextRun("test"), children: [new TextRun("test")],
link: "http://www.google.com", link: "http://www.google.com",
}); });

View File

@ -13,7 +13,7 @@ export enum HyperlinkType {
export class ConcreteHyperlink extends XmlComponent { export class ConcreteHyperlink extends XmlComponent {
public readonly linkId: string; public readonly linkId: string;
constructor(child: ParagraphChild, relationshipId: string, anchor?: string) { constructor(children: ParagraphChild[], relationshipId: string, anchor?: string) {
super("w:hyperlink"); super("w:hyperlink");
this.linkId = relationshipId; this.linkId = relationshipId;
@ -26,16 +26,18 @@ export class ConcreteHyperlink extends XmlComponent {
const attributes = new HyperlinkAttributes(props); const attributes = new HyperlinkAttributes(props);
this.root.push(attributes); this.root.push(attributes);
this.root.push(child); children.forEach((child) => {
this.root.push(child);
});
} }
} }
export class InternalHyperlink extends ConcreteHyperlink { export class InternalHyperlink extends ConcreteHyperlink {
constructor(options: { readonly child: ParagraphChild; readonly anchor: string }) { constructor(options: { readonly child: ParagraphChild; readonly anchor: string }) {
super(options.child, uniqueId(), options.anchor); super([options.child], uniqueId(), options.anchor);
} }
} }
export class ExternalHyperlink { export class ExternalHyperlink {
constructor(public readonly options: { readonly child: ParagraphChild; readonly link: string }) {} constructor(public readonly options: { readonly children: ParagraphChild[]; readonly link: string }) {}
} }

View File

@ -927,7 +927,7 @@ describe("Paragraph", () => {
const paragraph = new Paragraph({ const paragraph = new Paragraph({
children: [ children: [
new ExternalHyperlink({ new ExternalHyperlink({
child: new TextRun("test"), children: [new TextRun("test")],
link: "http://www.google.com", link: "http://www.google.com",
}), }),
], ],

View File

@ -75,7 +75,7 @@ export class Paragraph extends XmlComponent {
for (const element of this.root) { for (const element of this.root) {
if (element instanceof ExternalHyperlink) { if (element instanceof ExternalHyperlink) {
const index = this.root.indexOf(element); const index = this.root.indexOf(element);
const concreteHyperlink = new ConcreteHyperlink(element.options.child, uniqueId()); const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId());
context.viewWrapper.Relationships.createRelationship( context.viewWrapper.Relationships.createRelationship(
concreteHyperlink.linkId, concreteHyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",