Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Dec 2, 2024
1 parent cf47c44 commit 750af2b
Show file tree
Hide file tree
Showing 24 changed files with 339 additions and 375 deletions.
12 changes: 1 addition & 11 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
<title>Docs</title>
</head>
<body>
<script type="module">
// import { Pptx } from '../src'

// encode
// const pptx = new Pptx()
// pptx.toBuffer()
// pptx.toBlob()

// decode
// const pptx = Pptx.from(buffer)
</script>
<script type="module" src="./src/main.ts"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions docs/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Pptx } from '../../src'

const input = document.createElement('input')
document.body.appendChild(input)
input.accept = '.pptx'
input.type = 'file'
input.onchange = async () => {
const file = input.files?.[0]

const pptx = Pptx.parse(new Uint8Array(await file?.arrayBuffer()))

console.log(pptx.slides[0].elements)

console.log(pptx)
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 47 additions & 9 deletions src/core/XmlObject.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
export function defineProperty(name: string, type: any) {
export function defineChild(tag: string, Klass: new () => XmlObject, isArray = false) {
return function (proto: any, name: any) {
// TODO
Object.defineProperty(proto, name, {
get() {
if (isArray) {
return (this as XmlObject).getChildren(tag).map(node => new Klass().parse(node))
}
else {
const node = (this as XmlObject).getChild(tag)
if (node) {
return new Klass().parse(node)
}
return undefined
}
},
})
}
}

export function defineChild(tag: string, object: new () => XmlObject) {
export function defineProperty(tag: string, type: any) {
return function (proto: any, name: any) {
// TODO
Object.defineProperty(proto, name, {
get() {
return (this as XmlObject).getAttr(tag, type)
},
})
}
}

export abstract class XmlObject {
readonly namespace?: string
abstract readonly tag: string

declare node: HTMLElement
properties: Record<string, any> = {}
children: XmlObject[] = []

get textContent(): string {
// TODO
return ''
getAttr(name: string): HTMLElement | undefined {
return this.node.getAttribute(name)
}

getChild(name: string): HTMLElement | undefined {
return Array.from(this.node.childNodes).find(node => node.nodeName === name)
}

getChildren(name: string): HTMLElement[] {
return Array.from(this.node.childNodes).filter(node => node.nodeName === name)
}

setProperties(properties: Record<string, any>): this {
Expand All @@ -29,6 +52,21 @@ export abstract class XmlObject {
return this
}

parse(source: string | HTMLElement): this {
if (typeof source === 'string') {
const doc = new DOMParser().parseFromString(source, 'text/xml') as XMLDocument
const error = doc.querySelector('parsererror')
if (error) {
throw new Error(error.textContent ?? 'parser error')
}
this.node = doc.documentElement
}
else {
this.node = source
}
return this
}

toXmlString(): string {
// TODO
return ''
Expand Down
135 changes: 0 additions & 135 deletions src/office/doc/ContentTypes.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/office/doc/Default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineProperty, XmlObject } from '../../core'

export class Default extends XmlObject {
readonly tag = 'Default'

@defineProperty('ContentType', 'string') declare contentType: string
@defineProperty('Extension', 'string') declare extension: string
}
8 changes: 8 additions & 0 deletions src/office/doc/Override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineProperty, XmlObject } from '../../core'

export class Override extends XmlObject {
readonly tag = 'Override'

@defineProperty('ContentType', 'string') declare contentType: string
@defineProperty('PartName', 'string') declare partName: string
}
13 changes: 0 additions & 13 deletions src/office/doc/Relationship.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { defineProperty, XmlObject } from '../../core'

const RELATIONSHIP = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'

export interface RelationshipProperties {
id: string
type: string
target: string
}

export class Relationship extends XmlObject {
readonly tag = 'Relationship'

@defineProperty('Id', 'string') declare id: string
@defineProperty('Type', 'string') declare type: string
@defineProperty('Target', 'string') declare target: string

constructor(properties: Partial<RelationshipProperties> = {}) {
super()
this.setProperties(properties)
}
}
32 changes: 24 additions & 8 deletions src/office/doc/Relationships.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import type { RelationshipProperties } from './Relationship'
import { XmlObject } from '../../core'
import { defineChild, XmlObject } from '../../core'
import { Relationship } from './Relationship'

export class Relationships extends XmlObject {
static officeDocument = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
static package = 'http://schemas.openxmlformats.org/package/2006/relationships'
static types = {
presentation: `${this.officeDocument}/officeDocument`,
app: `${this.officeDocument}/extended-properties`,
core: `${this.package}/metadata/core-properties`,
custom: `${this.officeDocument}/custom-properties`,
slide: `${this.officeDocument}/slide`,
slideLayout: `${this.officeDocument}/slideLayout`,
slideMaster: `${this.officeDocument}/slideMaster`,
theme: `${this.officeDocument}/theme`,
tags: `${this.officeDocument}/tags`,
tableStyles: `${this.officeDocument}/tableStyles`,
presProps: `${this.officeDocument}/presProps`,
viewProps: `${this.officeDocument}/viewProps`,
notesMaster: `${this.officeDocument}/notesMaster`,
handoutMaster: `${this.officeDocument}/handoutMaster`,
commentAuthors: `${this.officeDocument}/commentAuthors`,
}

readonly tag = 'Relationships'

attrs = {
xmlns: 'http://schemas.openxmlformats.org/package/2006/relationships',
}

relationships: Relationship[]
@defineChild('Relationship', Relationship, true) declare children: Relationship[]

constructor(relationships: Partial<RelationshipProperties>[] = []) {
super()
this.relationships = relationships.map((v, index) => {
return new Relationship({ id: `rId${index + 1}`, ...v })
})
get value(): { id: string, type: string, target: string }[] {
return this.children.map(v => ({ id: v.id, type: v.type, target: v.target }))
}
}
Loading

0 comments on commit 750af2b

Please sign in to comment.