-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtemplate.ts
144 lines (121 loc) · 3.66 KB
/
template.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import JSZip, { InputType } from 'jszip';
import { FileHelper } from '../helper/file-helper';
import { CountHelper } from '../helper/count-helper';
import { ICounter } from '../interfaces/icounter';
import { ISlide } from '../interfaces/islide';
import { PresTemplate } from '../interfaces/pres-template';
import { RootPresTemplate } from '../interfaces/root-pres-template';
import { ITemplate } from '../interfaces/itemplate';
import { XmlTemplateHelper } from '../helper/xml-template-helper';
import { SlideInfo } from '../types/xml-types';
import { XmlHelper } from '../helper/xml-helper';
import { vd } from '../helper/general-helper';
export class Template implements ITemplate {
/**
* Path to local file
* @type string
*/
location: string;
/**
* An alias name to identify template and simplify
* @type string
*/
name: string;
/**
* Node file buffer
* @type InputType
*/
file: InputType;
/**
* this.file will be passed to JSZip
* @type Promise<JSZip>
*/
archive: Promise<JSZip>;
/**
* Array containing all slides coming from Automizer.addSlide()
* @type: ISlide[]
*/
slides: ISlide[];
/**
* Array containing all counters
* @type: ICounter[]
*/
counter: ICounter[];
creationIds: SlideInfo[];
existingSlides: number;
constructor(location: string) {
this.location = location;
const file = FileHelper.readFile(location);
this.archive = FileHelper.extractFileContent(file as unknown as Buffer);
}
static import(
location: string,
name?: string,
): PresTemplate | RootPresTemplate {
let newTemplate: PresTemplate | RootPresTemplate;
if (name) {
newTemplate = new Template(location) as PresTemplate;
newTemplate.name = name;
} else {
newTemplate = new Template(location) as RootPresTemplate;
newTemplate.slides = [];
newTemplate.counter = [
new CountHelper('slides', newTemplate),
new CountHelper('charts', newTemplate),
new CountHelper('images', newTemplate),
];
}
return newTemplate;
}
async setCreationIds(): Promise<SlideInfo[]> {
const archive = await this.archive;
const xmlTemplateHelper = new XmlTemplateHelper(archive);
this.creationIds = await xmlTemplateHelper.getCreationIds();
return this.creationIds;
}
async appendSlide(slide: ISlide): Promise<void> {
if (this.counter[0].get() === undefined) {
await this.initializeCounter();
}
await slide.append(this);
}
async countExistingSlides(): Promise<void> {
const xml = await this.getSlideIdList();
const sldIdLst = xml.getElementsByTagName('p:sldIdLst');
if (sldIdLst.length > 0) {
const existingSlides = sldIdLst[0].getElementsByTagName('p:sldId');
this.existingSlides = existingSlides.length;
}
}
async truncate(): Promise<void> {
if (this.existingSlides > 0) {
const xml = await this.getSlideIdList();
const existingSlides = xml.getElementsByTagName('p:sldId');
XmlHelper.sliceCollection(existingSlides, this.existingSlides, 0);
await XmlHelper.writeXmlToArchive(
await this.archive,
`ppt/presentation.xml`,
xml,
);
}
}
async getSlideIdList(): Promise<Document> {
const archive = await this.archive;
const xml = await XmlHelper.getXmlFromArchive(
archive,
`ppt/presentation.xml`,
);
return xml;
}
async initializeCounter(): Promise<void> {
for (const c of this.counter) {
await c.set();
}
}
incrementCounter(name: string): number {
return CountHelper.increment(name, this.counter);
}
count(name: string): number {
return CountHelper.count(name, this.counter);
}
}