Skip to content

Commit 5134ae1

Browse files
committed
refactor(hyperlink): use generic helper functions only
1 parent 673c29d commit 5134ae1

File tree

6 files changed

+380
-403
lines changed

6 files changed

+380
-403
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Automizer, { modify } from '../src/index';
2+
import { vd } from '../src/helper/general-helper';
3+
4+
test('delete internal hyperlink - using removeHyperlink helper', async () => {
5+
const automizer = new Automizer({
6+
templateDir: `${__dirname}/pptx-templates`,
7+
outputDir: `${__dirname}/pptx-output`,
8+
verbosity: 0
9+
});
10+
11+
const pres = automizer
12+
.loadRoot(`RootTemplate.pptx`)
13+
.load(`SlideWithLink.pptx`, 'link');
14+
15+
const outputFile = `delete-hyperlink-internal.test.pptx`;
16+
17+
const result = await pres
18+
.addSlide('link', 1, (slide) => {
19+
slide.modifyElement('LinkToSlide', [modify.removeHyperlink()]);
20+
})
21+
.write(outputFile);
22+
23+
console.log(result);
24+
});

src/classes/shape.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,6 @@ export class Shape {
8989
}
9090

9191
async setTargetElement(): Promise<void> {
92-
if (!this.sourceElement) {
93-
// If we don't have a source element, we might be trying to remove a hyperlink
94-
console.log(
95-
`Warning: No source element for shape ${this.name}. Creating empty element for operations.`,
96-
);
97-
if (this.shape && this.shape.mode === 'remove' && this.targetArchive) {
98-
// For remove operations, we don't need a source element
99-
// Just continue without setting targetElement
100-
return;
101-
}
102-
103-
// For non-remove operations or if other conditions aren't met, throw the error
104-
console.log(this.shape);
105-
throw `No source element for shape ${this.name}`;
106-
}
10792
this.targetElement = this.sourceElement.cloneNode(true) as XmlElement;
10893
}
10994

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export default class HyperlinkElement {
2+
private doc: Document;
3+
private relId: string;
4+
private isInternal: boolean;
5+
6+
constructor(doc: Document, relId: string, isInternal: boolean) {
7+
this.doc = doc;
8+
this.relId = relId;
9+
this.isInternal = isInternal;
10+
}
11+
12+
createHlinkClick(): Element {
13+
const hlinkClick = this.doc.createElement('a:hlinkClick');
14+
hlinkClick.setAttribute('r:id', this.relId);
15+
hlinkClick.setAttribute(
16+
'xmlns:r',
17+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
18+
);
19+
20+
if (this.isInternal) {
21+
hlinkClick.setAttribute('action', 'ppaction://hlinksldjump');
22+
hlinkClick.setAttribute(
23+
'xmlns:a',
24+
'http://schemas.openxmlformats.org/drawingml/2006/main',
25+
);
26+
hlinkClick.setAttribute(
27+
'xmlns:p14',
28+
'http://schemas.microsoft.com/office/powerpoint/2010/main',
29+
);
30+
}
31+
32+
return hlinkClick;
33+
}
34+
35+
createTextRun(text: string): Element {
36+
const run = this.doc.createElement('a:r');
37+
const rPr = this.doc.createElement('a:rPr');
38+
const t = this.doc.createElement('a:t');
39+
40+
rPr.appendChild(this.createHlinkClick());
41+
t.textContent = text;
42+
43+
run.appendChild(rPr);
44+
run.appendChild(t);
45+
46+
return run;
47+
}
48+
}

0 commit comments

Comments
 (0)