@@ -2,6 +2,7 @@ import JSZip from 'jszip';
22import { FileHelper } from '../helper/file-helper' ;
33import { XmlHelper } from '../helper/xml-helper' ;
44import { Shape } from '../classes/shape' ;
5+ import path from 'path' ;
56
67import { RelationshipAttribute , HelperElement } from '../types/xml-types' ;
78import { ImportedElement , Target , Workbook } from '../types/types' ;
@@ -17,6 +18,7 @@ export class Chart extends Shape implements IChart {
1718 wbExtension : string ;
1819 relTypeChartColorStyle : string ;
1920 relTypeChartStyle : string ;
21+ relTypeChartImage : string ;
2022 wbRelsPath : string ;
2123 styleRelationFiles : {
2224 [ key : string ] : string ;
@@ -36,6 +38,8 @@ export class Chart extends Shape implements IChart {
3638 'http://schemas.microsoft.com/office/2011/relationships/chartColorStyle' ;
3739 this . relTypeChartStyle =
3840 'http://schemas.microsoft.com/office/2011/relationships/chartStyle' ;
41+ this . relTypeChartImage =
42+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image' ;
3943 this . styleRelationFiles = { } ;
4044 }
4145
@@ -116,7 +120,7 @@ export class Chart extends Shape implements IChart {
116120 'nodebuffer' ,
117121 ) ;
118122 const archive = await FileHelper . extractFileContent (
119- ( worksheet as unknown ) as Buffer ,
123+ worksheet as unknown as Buffer ,
120124 ) ;
121125 const sheet = await XmlHelper . getXmlFromArchive (
122126 archive ,
@@ -247,10 +251,43 @@ export class Chart extends Shape implements IChart {
247251 `ppt/charts/colors${ this . targetNumber } .xml` ,
248252 ) ;
249253 }
254+
255+ if ( this . styleRelationFiles . relTypeChartImage ) {
256+ const imageInfo = await this . getTargetChartImageUri (
257+ this . styleRelationFiles . relTypeChartImage ,
258+ ) ;
259+ await this . appendImageExtensionToContentType ( imageInfo . extension ) ;
260+ await FileHelper . zipCopy (
261+ this . sourceArchive ,
262+ imageInfo . source ,
263+ this . targetArchive ,
264+ imageInfo . target ,
265+ ) ;
266+ }
267+ }
268+
269+ getTargetChartImageUri ( origin : string ) : {
270+ source : string ;
271+ target : string ;
272+ rel : string ;
273+ extension : string ;
274+ } {
275+ const file = origin . replace ( '../media/' , '' ) ;
276+ const extension = path . extname ( file ) . replace ( '.' , '' ) ;
277+ return {
278+ source : `ppt/media/${ file } ` ,
279+ target : `ppt/media/${ file } -chart-${ this . targetNumber } .${ extension } ` ,
280+ rel : `../media/${ file } -chart-${ this . targetNumber } .${ extension } ` ,
281+ extension : extension ,
282+ } ;
250283 }
251284
252285 async getChartStyles ( ) : Promise < void > {
253- const styleTypes = [ 'relTypeChartStyle' , 'relTypeChartColorStyle' ] ;
286+ const styleTypes = [
287+ 'relTypeChartStyle' ,
288+ 'relTypeChartColorStyle' ,
289+ 'relTypeChartImage' ,
290+ ] ;
254291
255292 for ( const i in styleTypes ) {
256293 const styleType = styleTypes [ i ] ;
@@ -273,8 +310,7 @@ export class Chart extends Shape implements IChart {
273310 ) ;
274311 const attributes = {
275312 Id : this . createdRid ,
276- Type :
277- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart' ,
313+ Type : 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart' ,
278314 Target : `../charts/chart${ this . targetNumber } .xml` ,
279315 } as RelationshipAttribute ;
280316
@@ -313,6 +349,12 @@ export class Chart extends Shape implements IChart {
313349 case this . relTypeChartStyle :
314350 element . setAttribute ( 'Target' , `style${ this . targetNumber } .xml` ) ;
315351 break ;
352+ case this . relTypeChartImage :
353+ const target = element . getAttribute ( 'Target' ) ;
354+ const imageInfo = this . getTargetChartImageUri ( target ) ;
355+ vd ( imageInfo ) ;
356+ element . setAttribute ( 'Target' , imageInfo . rel ) ;
357+ break ;
316358 }
317359 } ) ;
318360
0 commit comments