11import JSZip from 'jszip' ;
22
3- interface OutputFrame {
4- element : HTMLElement ;
5- filename : string ;
6- }
3+ import { FileImportData , UserAsset } from '@deckdeckgo/editor' ;
74
8- interface OutputFrameGroup {
9- canvas : OutputFrame ;
10- aside : OutputFrame | undefined ;
11- }
5+ import { importData , userAssets } from './meta.utils' ;
126
137export const downloadFrames = async ( ) => {
148 const container : HTMLDivElement | null = document . querySelector ( 'div.container' ) ;
@@ -29,7 +23,10 @@ export const downloadFrames = async () => {
2923 return ;
3024 }
3125
32- const blob : Blob = await zip ( svgList ) ;
26+ const data : FileImportData = importData ( svgList ) ;
27+ const assets : UserAsset [ ] = await Promise . all ( userAssets ( svgList ) ) ;
28+
29+ const blob : Blob = await zip ( { assets, data} ) ;
3330
3431 download ( blob ) ;
3532} ;
@@ -60,76 +57,47 @@ const prepare = (frames: HTMLDivElement[]): OutputFrameGroup[] => {
6057 return svgList ;
6158} ;
6259
63- const zip = async ( svgList : OutputFrameGroup [ ] ) : Promise < Blob > => {
60+ const zip = async ( { assets , data } : { assets : UserAsset [ ] ; data : FileImportData } ) : Promise < Blob > => {
6461 const zip = new JSZip ( ) ;
6562
66- const addImageZip = async ( { dataUrl, filename} : { dataUrl : string ; filename : string } ) => {
67- const blob : Blob = await ( await fetch ( dataUrl ) ) . blob ( ) ;
68-
69- zip . file ( filename , blob , {
63+ const addImageZip = async ( { key, blob} : UserAsset ) => {
64+ zip . file ( key , blob , {
7065 base64 : true
7166 } ) ;
7267 } ;
7368
74- const addHtmlZip = ( { data , filename } : { data : string ; filename : string } ) => {
75- const blob : Blob = new Blob ( [ data ] , { type : 'text/html ' } ) ;
69+ const addDeckMetaZip = ( deck : FileImportData ) => {
70+ const blob : Blob = new Blob ( [ JSON . stringify ( deck ) ] , { type : 'application/json ' } ) ;
7671
77- zip . file ( filename , blob , {
72+ zip . file ( 'deck.json' , blob , {
7873 base64 : true
7974 } ) ;
8075 } ;
8176
82- const addMetaZip = ( meta : Metadata ) => {
83- const blob : Blob = new Blob ( [ JSON . stringify ( meta ) ] , { type : 'application/json' } ) ;
77+ const addAssetMetaZip = ( assets : UserAsset [ ] ) => {
78+ const blob : Blob = new Blob ( [ JSON . stringify ( assets . map ( ( { key } ) => ( { key } ) ) ) ] , { type : 'application/json' } ) ;
8479
85- zip . file ( 'meta .json' , blob , {
80+ zip . file ( 'assets .json' , blob , {
8681 base64 : true
8782 } ) ;
8883 } ;
8984
90- const promises : Promise < void > [ ] = svgList . map ( async ( { canvas, aside} : OutputFrameGroup ) => {
91- await addImageZip ( {
92- dataUrl : ( canvas . element as HTMLCanvasElement ) . toDataURL ( 'image/webp' ) ,
93- filename : canvas . filename
94- } ) ;
95-
96- if ( aside ) {
97- addHtmlZip ( {
98- data : aside . element . innerHTML ,
99- filename : aside . filename
100- } ) ;
101- }
102- } ) ;
85+ const imagePromises : Promise < void > [ ] = assets . map ( ( userAsset : UserAsset ) => addImageZip ( userAsset ) ) ;
86+ await Promise . all ( imagePromises ) ;
10387
104- await Promise . all ( promises ) ;
88+ addDeckMetaZip ( data ) ;
10589
106- addMetaZip ( meta ( svgList ) ) ;
90+ addAssetMetaZip ( assets ) ;
10791
10892 return zip . generateAsync ( { type : 'blob' } ) ;
10993} ;
11094
111- const meta = ( svgList : OutputFrameGroup [ ] ) : Metadata => {
112- const fonts : FontsComponent | null = document . querySelector ( 'deckgo-fonts' ) ;
113-
114- const slides : MetadataSlide [ ] = svgList . map ( ( { canvas, aside} : OutputFrameGroup ) => {
115- return {
116- background : canvas . filename ,
117- ...( aside && { text : aside . filename } )
118- } ;
119- } ) ;
120-
121- return {
122- slides,
123- ...( ( fonts ?. value && fonts ?. value !== 'Default' ) && { fontFamily : fonts . value } )
124- }
125- } ;
126-
12795const download = ( blob ) => {
12896 const blobURL = window . URL . createObjectURL ( blob ) ;
12997
13098 const link = document . createElement ( 'a' ) ;
13199 link . href = blobURL ;
132- link . download = `deckdeckgo.zip ` ;
100+ link . download = `deckdeckgo.ddg ` ;
133101 link . click ( ) ;
134102
135103 window . URL . revokeObjectURL ( blobURL ) ;
0 commit comments