@@ -34,9 +34,7 @@ const PictureXform = require('../../xlsx/xform/sheet/picture-xform');
34
34
const ConditionalFormattingsXform = require ( '../../xlsx/xform/sheet/cf/conditional-formattings-xform' ) ;
35
35
const HeaderFooterXform = require ( '../../xlsx/xform/sheet/header-footer-xform' ) ;
36
36
const RowBreaksXform = require ( '../../xlsx/xform/sheet/row-breaks-xform' ) ;
37
- const ImageXForm = require ( '../../xlsx/xform/sheet/image-xform' ) ;
38
37
const DrawingXform = require ( '../../xlsx/xform/drawing/drawing-xform' ) ;
39
- const RelationshipsXform = require ( '../../xlsx/xform/core/relationships-xform' ) ;
40
38
const Image = require ( '../../doc/image' ) ;
41
39
42
40
// since prepare and render are functional, we can use singletons
@@ -53,7 +51,7 @@ const xform = {
53
51
pageSeteup : new PageSetupXform ( ) ,
54
52
autoFilter : new AutoFilterXform ( ) ,
55
53
picture : new PictureXform ( ) ,
56
- drawing : new ImageXForm ( ) ,
54
+ drawing : new DrawingXform ( ) ,
57
55
conditionalFormattings : new ConditionalFormattingsXform ( ) ,
58
56
headerFooter : new HeaderFooterXform ( ) ,
59
57
rowBreaks : new RowBreaksXform ( ) ,
@@ -250,12 +248,13 @@ class WorksheetWriter {
250
248
this . _writePageMargins ( ) ;
251
249
this . _writePageSetup ( ) ;
252
250
this . _writeBackground ( ) ;
251
+ this . _writeDrawings ( ) ;
253
252
this . _writeHeaderFooter ( ) ;
254
253
this . _writeRowBreaks ( ) ;
255
254
256
255
// Legacy Data tag for comments
257
256
this . _writeLegacyData ( ) ;
258
- this . _writeDrawings ( ) ;
257
+
259
258
this . _writeCloseWorksheet ( ) ;
260
259
// signal end of stream to workbook
261
260
this . stream . end ( ) ;
@@ -487,7 +486,9 @@ class WorksheetWriter {
487
486
this . anchors . push ( im ) ;
488
487
}
489
488
490
- // =========================================================================
489
+ getImages ( ) {
490
+ return this . _media . filter ( m => m . type === 'image' ) ;
491
+ }
491
492
492
493
addBackgroundImage ( imageId ) {
493
494
this . _background = {
@@ -714,29 +715,33 @@ class WorksheetWriter {
714
715
}
715
716
716
717
_writeDrawings ( ) {
717
- if ( this . _media . length > 0 ) {
718
- const { zip} = this . workbook ;
719
-
720
- const drawingXform = new DrawingXform ( ) ;
721
- const lastDrawingId = this . workbook . drawings . length + 1 ;
722
- const relsXform = new RelationshipsXform ( ) ;
723
- const xml = drawingXform . toXml ( this ) ;
724
- zip . append ( xml , { name : `xl/drawings/drawing${ lastDrawingId } .xml` } ) ;
725
- const xmlRels = relsXform . toXml ( this . anchors . map ( ( a , index ) => {
726
- const image = this . workbook . getImage ( a . imageId ) ;
727
- return {
728
- Id : `rId${ index + 1 } ` ,
729
- Type : RelType . Image ,
730
- Target : `../media/${ image . name } ` ,
718
+ if ( this . _media . length ) {
719
+ this . getImages ( ) . forEach ( ( image , i ) => {
720
+ const lastDrawingId = this . workbook . drawings . length + 1 ;
721
+ const drawingId = this . _sheetRelsWriter . addMedia ( {
722
+ Target : `../drawings/drawing${ lastDrawingId } .xml` ,
723
+ Type : RelType . Drawing ,
724
+ } ) ;
725
+ const imageObj = this . workbook . getImage ( image . imageId ) ;
726
+ const drawing = {
727
+ rId : drawingId ,
728
+ name : `drawing${ lastDrawingId } ` ,
729
+ rels : [ {
730
+ Id : 'rId1' ,
731
+ Type : RelType . Image ,
732
+ Target : `../media/${ imageObj . name } ` ,
733
+ } ] ,
734
+ anchors : [ {
735
+ picture : {
736
+ rId : image . imageId ,
737
+ } ,
738
+ range : image . range ,
739
+ } ] ,
731
740
} ;
732
- } ) ) ;
733
- this . workbook . drawings . push ( { name : `drawing${ lastDrawingId } ` } ) ;
734
- zip . append ( xmlRels , { name : `xl/drawings/_rels/drawing${ lastDrawingId } .xml.rels` } ) ;
735
- const drawingId = this . _sheetRelsWriter . addMedia ( {
736
- Target : `../drawings/drawing${ lastDrawingId } .xml` ,
737
- Type : RelType . Drawing ,
741
+ this . workbook . drawings . push ( drawing ) ;
742
+ xform . drawing . prepare ( drawing , { } ) ;
743
+ this . stream . write ( xform . drawing . toXml ( drawing ) ) ;
738
744
} ) ;
739
- this . stream . write ( xform . drawing . toXml ( { rId : drawingId } ) ) ;
740
745
}
741
746
}
742
747
0 commit comments