|
| 1 | +import JSZip from "jszip"; |
| 2 | +import {ElementInfo, SlideInfo} from "../types/xml-types"; |
| 3 | +import {XmlHelper} from "./xml-helper"; |
| 4 | + |
| 5 | +export class XmlTemplateHelper { |
| 6 | + archive: JSZip; |
| 7 | + relType: string; |
| 8 | + path: string; |
| 9 | + |
| 10 | + constructor(archive: JSZip) { |
| 11 | + this.relType = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide' |
| 12 | + this.archive = archive |
| 13 | + this.path = 'ppt/_rels/presentation.xml.rels' |
| 14 | + } |
| 15 | + |
| 16 | + async getCreationIds(): Promise<SlideInfo[]> { |
| 17 | + const archive = await this.archive |
| 18 | + const relationships = await XmlHelper.getTargetsByRelationshipType( |
| 19 | + archive, |
| 20 | + this.path, |
| 21 | + this.relType |
| 22 | + ) |
| 23 | + |
| 24 | + const creationIds = [] |
| 25 | + for(const slideRel of relationships) { |
| 26 | + const slideXml = await XmlHelper.getXmlFromArchive( |
| 27 | + archive, |
| 28 | + 'ppt/' + slideRel.file |
| 29 | + ) |
| 30 | + |
| 31 | + const creationIdSlide = slideXml |
| 32 | + .getElementsByTagName('p14:creationId') |
| 33 | + .item(0) |
| 34 | + .getAttribute('val') |
| 35 | + |
| 36 | + const elementIds = this.elementCreationIds(slideXml) |
| 37 | + |
| 38 | + creationIds.push({ |
| 39 | + id: Number(creationIdSlide), |
| 40 | + number: this.parseSlideRelFile(slideRel.file), |
| 41 | + elements: elementIds |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + return creationIds |
| 46 | + } |
| 47 | + |
| 48 | + parseSlideRelFile(slideRelFile: string): number { |
| 49 | + return Number(slideRelFile |
| 50 | + .replace('slides/slide', '') |
| 51 | + .replace('.xml', '') |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + elementCreationIds(slideXml): ElementInfo[] { |
| 56 | + const slideElements = slideXml |
| 57 | + .getElementsByTagName('p:cNvPr') |
| 58 | + |
| 59 | + const elementIds = <ElementInfo[]> [] |
| 60 | + for(const item in slideElements) { |
| 61 | + const slideElement = slideElements[item] |
| 62 | + if(slideElement.getAttribute !== undefined) { |
| 63 | + const creationIdElement = slideElement |
| 64 | + .getElementsByTagName('a16:creationId') |
| 65 | + if(creationIdElement.item(0)) { |
| 66 | + elementIds.push( |
| 67 | + this.getElementInfo(slideElement) |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return elementIds |
| 73 | + } |
| 74 | + |
| 75 | + getElementInfo(slideElement): ElementInfo { |
| 76 | + const elementName = slideElement.getAttribute('name') |
| 77 | + const type = slideElement.parentNode.parentNode.tagName |
| 78 | + const creationId = slideElement |
| 79 | + .getElementsByTagName('a16:creationId') |
| 80 | + .item(0) |
| 81 | + .getAttribute('id') |
| 82 | + |
| 83 | + return { |
| 84 | + name: elementName, |
| 85 | + type: type.replace('p:', ''), |
| 86 | + id: creationId |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments