Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Dec 4, 2024
1 parent 2e8253b commit ba95fb3
Show file tree
Hide file tree
Showing 88 changed files with 427 additions and 428 deletions.
7 changes: 3 additions & 4 deletions docs/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Picture, Pptx } from '../../src'
import { OXML, Pptx } from '../../src'

console.log(
Picture.tagToConstructor,
Picture.protoToDefinition,
new Picture().definition(),
OXML.tagToConstructor,
OXML.protoToDefinition,
)

const input = document.createElement('input')
Expand Down
116 changes: 76 additions & 40 deletions src/core/OXML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ export interface OXMLDefinition {
children?: OXMLChildDefinition[]
}

export function defineElement(tag: string, namespace?: string) {
export function defineElement(tag: string) {
return (constructor: any) => {
const proto = constructor.prototype
let definition = OXML.protoToDefinition.get(proto)
if (!definition) {
definition = {} as OXMLDefinition
OXML.protoToDefinition.set(proto, definition)
}
const tagArr = tag.split(':')
definition.tag = tag
definition.namespace = namespace
OXML.tagToConstructor.set([namespace, tag].filter(Boolean).join(':'), constructor)
definition.namespace = tagArr.length > 1 ? tagArr[0] : undefined
OXML.tagToConstructor.set(tag, constructor)
Object.defineProperty(proto, 'tag', {
value: tag,
configurable: true,
Expand Down Expand Up @@ -66,15 +67,15 @@ export function defineAttribute(
}
}

export function defineProperty(propName: string, type: any, defaultValue: any) {
export function defineProperty(propName: string) {
return function (proto: any, name: any) {
let definition = OXML.protoToDefinition.get(proto)
if (!definition) {
definition = {} as OXMLDefinition
OXML.protoToDefinition.set(proto, definition)
}
definition.properties ??= new Map()
definition.properties.set(propName, { type, defaultValue })
definition.properties.set(propName, {})
Object.defineProperty(proto, name, {
get() {
return (this as OXML).getAttribute(propName)
Expand Down Expand Up @@ -132,31 +133,74 @@ export class OXML {
return OXML.protoToDefinition.get(this.constructor.prototype as any)
}

setAttribute(name: string, value: any): void {
const definition = this.definition()?.attributes?.get(name)
let newValue
switch (definition?.type) {
getSetterValue(type: string, value: any): any {
switch (type) {
case 'boolean':
newValue = !!value
break
return value ? '1' : '0'
case 'degree':
newValue = Number(value) / 60000
break
return String(Number(value) * 60000)
case 'fontSize':
return String(Number(value) * 100)
case 'number':
newValue = Number(value)
break
return String(value)
case 'string':
newValue = value
break
return String(value)
case 'emu':
newValue = (Number(value) / 914400) * OXML.DPI
break
return String((Number(value) / OXML.DPI) * 914400)
case 'dxa':
return String((Number(value) / OXML.DPI) * 1440)
case 'percentage':
return String(Number(value) * 1000)
case 'rate':
return String(Number(value) * 100000)
case 'lineHeight':
return String((value * 100000) / 1.2018 - 0.0034)
default:
if (definition) {
console.warn(`${this.tag ?? this.element.tagName} ${name} type not found: ${definition.type}`, definition)
}
newValue = value
break
throw new Error(`type not found: ${type}`)
}
}

getGetterValue(type: any, value: any): any {
if (typeof type === 'string') {
switch (type) {
case 'boolean':
return !!value
case 'degree':
return Number(value) / 60000
case 'fontSize':
return Number(value) / 100
case 'number':
return Number(value)
case 'string':
return String(value)
case 'emu':
return (Number(value) / 914400) * OXML.DPI
case 'dxa':
return (Number(value) / 1440) * OXML.DPI
case 'percentage':
return Number(value) / 1000
case 'rate':
return Number(value) / 100000
case 'lineHeight':
return (Number(value) / 100000) * 1.2018 + 0.0034
}
}
else if (typeof type === 'object') {
return type[value]
}
throw new Error(`type not found: ${type}`)
}

setAttribute(name: string, value: any): void {
const definition = this.definition()?.attributes?.get(name)
let newValue = value
if (definition) {
try {
newValue = this.getSetterValue(definition.type, value)
}
catch (err: any) {
console.warn(`${this.tag ?? this.element.tagName} ${name} ${err.message}`, definition)
}
}
this.element.setAttribute(name, newValue)
}
Expand All @@ -167,23 +211,15 @@ export class OXML {
if (value === undefined) {
return value ?? definition?.defaultValue
}
switch (definition?.type) {
case 'boolean':
return !!value
case 'degree':
return Number(value) / 60000
case 'number':
return Number(value)
case 'string':
return value
case 'emu':
return (Number(value) / 914400) * OXML.DPI
default:
if (definition) {
console.warn(`${this.tag ?? this.element.tagName} ${name} type not found: ${definition.type}`, definition)
}
return value
if (definition) {
try {
return this.getGetterValue(definition?.type, value)
}
catch (err: any) {
console.warn(`${this.tag ?? this.element.tagName} ${name} ${err.message}`, definition)
}
}
return value
}

getAttributes(): Record<string, any> {
Expand Down
2 changes: 1 addition & 1 deletion src/office/doc/CoreProperties.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineElement, OXML } from '../../core'

@defineElement('coreProperties', 'cp')
@defineElement('cp:coreProperties')
export class CoreProperties extends OXML {
attrs = {
'xmlns': 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
Expand Down
4 changes: 2 additions & 2 deletions src/office/pptx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Zippable } from 'fflate'
import type { SlideLayout, Theme } from '../openxml'
import type { SlideLayout, Theme } from '../openxml/presentation'
import { unzipSync, zipSync } from 'fflate'
import { Picture, Presentation, PresentationProperties, Slide, SlideMaster, ViewProperties } from '../openxml'
import { Picture, Presentation, PresentationProperties, Slide, SlideMaster, ViewProperties } from '../openxml/presentation'
import { CoreProperties, Properties, Relationships, Types } from './doc'

export class Pptx {
Expand Down
2 changes: 1 addition & 1 deletion src/openxml/drawing/Alpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineAttribute, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.alpha
*/
@defineElement('alpha', 'a')
@defineElement('a:alpha')
export class Alpha extends OXML {
@defineAttribute('val') declare val: string
}
9 changes: 9 additions & 0 deletions src/openxml/drawing/AudioFromFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineAttribute, defineElement, OXML } from '../../core'

/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.audiofromfile
*/
@defineElement('a:audioFile')
export class AudioFromFile extends OXML {
@defineAttribute('r:link') declare rLink: string
}
2 changes: 1 addition & 1 deletion src/openxml/drawing/Blip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineAttribute, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.blip
*/
@defineElement('blip', 'a')
@defineElement('a:blip')
export class Blip extends OXML {
@defineAttribute('r:embed') declare rEmbed: string
}
19 changes: 19 additions & 0 deletions src/openxml/drawing/BlipFill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Blip } from './Blip'
import type { SourceRectangle } from './SourceRectangle'
import type { Stretch } from './Stretch'
import type { Tile } from './Tile'
import { defineAttribute, defineChild, defineElement, OXML } from '../../core'

/**
* https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.blipfill
*/
@defineElement('a:blipFill')
export class BlipFill extends OXML {
@defineAttribute('rotWithShape', 'boolean') rotWithShape?: boolean
@defineAttribute('dpi', 'number') dpi?: number

@defineChild('a:blip') declare blip: Blip
@defineChild('a:srcRect') declare srcRect: SourceRectangle
@defineChild('a:stretch') declare stretch: Stretch
@defineChild('a:tile') declare tile: Tile
}
2 changes: 1 addition & 1 deletion src/openxml/drawing/BodyProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TextAnchoringTypeValues } from './TextAnchoringTypeValues'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.bodyproperties
*/
@defineElement('bodyPr', 'a')
@defineElement('a:bodyPr')
export class BodyProperties extends OXML {
@defineAttribute('anchor', TextAnchoringTypeValues) anchor?: TextAnchoringTypeValues
@defineAttribute('anchorCtr', 'boolean') anchorCtr?: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/openxml/drawing/ChildExtents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineAttribute, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.childextents
*/
@defineElement('chExt', 'a')
@defineElement('a:chExt')
export class ChildExtents extends OXML {
@defineAttribute('cx', 'emu') declare cx: number
@defineAttribute('cy', 'emu') declare cy: number
Expand Down
2 changes: 1 addition & 1 deletion src/openxml/drawing/ChildOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineAttribute, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.childoffset
*/
@defineElement('chOff', 'a')
@defineElement('a:chOff')
export class ChildOffset extends OXML {
@defineAttribute('x', 'emu') declare x: number
@defineAttribute('y', 'emu') declare y: number
Expand Down
2 changes: 1 addition & 1 deletion src/openxml/drawing/CustomGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineElement, OXML } from '../../core'
/**
* https://learn.microsoft.com/dotnet/api/documentformat.openxml.presentation.customgeometry
*/
@defineElement('custGeom', 'a')
@defineElement('a:custGeom')
export class CustomGeometry extends OXML {
//
}
2 changes: 1 addition & 1 deletion src/openxml/drawing/EffectList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.effectlist
*/
@defineElement('effectLst', 'a')
@defineElement('a:effectLst')
export class EffectList extends OXML {
//
}
2 changes: 1 addition & 1 deletion src/openxml/drawing/EffectReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineChild, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.effectreference
*/
@defineElement('effectRef', 'a')
@defineElement('a:effectRef')
export class EffectReference extends OXML {
@defineChild('hslClr') hslClr?: OXML
@defineChild('prstClr') prstClr?: OXML
Expand Down
9 changes: 9 additions & 0 deletions src/openxml/drawing/Extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineAttribute, defineElement, OXML } from '../../core'

/**
* https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.extension
*/
@defineElement('a:ext')
export class Extension extends OXML {
@defineAttribute('uri', 'string') declare uri: string
}
10 changes: 10 additions & 0 deletions src/openxml/drawing/ExtensionList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Extension } from './Extension'
import { defineChildren, defineElement, OXML } from '../../core'

/**
* https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.extensionlist
*/
@defineElement('a:extLst')
export class ExtensionList extends OXML {
@defineChildren('a:ext') declare extList: Extension[]
}
2 changes: 1 addition & 1 deletion src/openxml/drawing/Extents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineAttribute, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.extents
*/
@defineElement('ext', 'a')
@defineElement('a:ext')
export class Extents extends OXML {
@defineAttribute('cx', 'emu') declare cx: number
@defineAttribute('cy', 'emu') declare cy: number
Expand Down
2 changes: 1 addition & 1 deletion src/openxml/drawing/FillRectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineAttribute, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.fillrectangle
*/
@defineElement('fillRect', 'a')
@defineElement('a:fillRect')
export class FillRectangle extends OXML {
@defineAttribute('b', 'rate') declare b: number
@defineAttribute('l', 'rate') declare l: number
Expand Down
14 changes: 7 additions & 7 deletions src/openxml/drawing/FillReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { defineChild, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.fillreference
*/
@defineElement('fillRef', 'a')
@defineElement('a:fillRef')
export class FillReference extends OXML {
@defineChild('hslClr', OXML) hslClr?: OXML
@defineChild('prstClr', OXML) prstClr?: OXML
@defineChild('schemeClr', OXML) schemeClr?: OXML
@defineChild('scrgbClr', OXML) scrgbClr?: OXML
@defineChild('srgbClr', OXML) srgbClr?: OXML
@defineChild('sysClr', OXML) sysClr?: OXML
@defineChild('a:hslClr', OXML) hslClr?: OXML
@defineChild('a:prstClr', OXML) prstClr?: OXML
@defineChild('a:schemeClr', OXML) schemeClr?: OXML
@defineChild('a:scrgbClr', OXML) scrgbClr?: OXML
@defineChild('a:srgbClr', OXML) srgbClr?: OXML
@defineChild('a:sysClr', OXML) sysClr?: OXML
}
14 changes: 7 additions & 7 deletions src/openxml/drawing/FontReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { defineChild, defineElement, OXML } from '../../core'
/**
* @link https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.fontreference
*/
@defineElement('fontRef', 'a')
@defineElement('a:fontRef')
export class FontReference extends OXML {
@defineChild('hslClr') hslClr?: OXML
@defineChild('prstClr') prstClr?: OXML
@defineChild('schemeClr') schemeClr?: OXML
@defineChild('scrgbClr') scrgbClr?: OXML
@defineChild('srgbClr') srgbClr?: OXML
@defineChild('sysClr') sysClr?: OXML
@defineChild('a:hslClr') hslClr?: OXML
@defineChild('a:prstClr') prstClr?: OXML
@defineChild('a:schemeClr') schemeClr?: OXML
@defineChild('a:scrgbClr') scrgbClr?: OXML
@defineChild('a:srgbClr') srgbClr?: OXML
@defineChild('a:sysClr') sysClr?: OXML
}
2 changes: 1 addition & 1 deletion src/openxml/drawing/GradientFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineChild, defineElement, OXML } from '../../core'
/**
* https://learn.microsoft.com/dotnet/api/documentformat.openxml.drawing.gradientfill
*/
@defineElement('gradFill', 'a')
@defineElement('a:gradFill')
export class GradientFill extends OXML {
@defineChild('a:gsLst', OXML) gsLst?: OXML
@defineChild('a:lin', OXML) lin?: OXML
Expand Down
Loading

0 comments on commit ba95fb3

Please sign in to comment.