@@ -7,9 +7,13 @@ import Button from '@mui/material/Button';
7
7
import exportProject from '../../utils/exportProject.util' ;
8
8
import createModal from './createModal' ;
9
9
import { useSelector } from 'react-redux' ;
10
+ import { RootState } from '../../redux/store' ;
11
+ const JSZip = require ( "jszip" ) ;
12
+ import { saveAs } from 'file-saver' ;
13
+
10
14
export default function ExportButton ( ) {
11
15
const [ modal , setModal ] = useState ( null ) ;
12
- const state = useSelector ( store => store . appState )
16
+ const state = useSelector ( ( store : RootState ) => store . appState ) ;
13
17
14
18
const genOptions : string [ ] = [
15
19
'Export components' ,
@@ -26,7 +30,6 @@ export default function ExportButton() {
26
30
{ genOptions . map ( ( option : string , i : number ) => (
27
31
< ListItem
28
32
key = { i }
29
- button
30
33
onClick = { ( ) => chooseGenOptions ( i ) }
31
34
style = { {
32
35
border : '1px solid #3f51b5' ,
@@ -49,32 +52,25 @@ export default function ExportButton() {
49
52
const chooseGenOptions = ( genOpt : number ) => {
50
53
// set export option: 0 --> export only components, 1 --> export full project
51
54
genOption = genOpt ;
52
- window . api . chooseAppDir ( ) ;
53
- testchecked = document . getElementById ( 'tests' ) . checked ;
55
+
56
+ //This is exclusive to the electron app
57
+ // window.api.chooseAppDir();
58
+ // testchecked = document.getElementById('tests').checked;
59
+
60
+ var zip = new JSZip ( ) ;
61
+ let componentFolder = zip . folder ( 'componentfolder' )
62
+ for ( let i in state . components ) {
63
+ componentFolder . file ( `${ state . components [ i ] . name } .jsx` , state . components [ i ] . code )
64
+ }
65
+ zip . generateAsync ( { type :"blob" } )
66
+ . then ( function ( content ) {
67
+ // see FileSaver.js
68
+ saveAs ( content , "ReacTypeApp.zip" ) ;
69
+ } ) ;
70
+
54
71
closeModal ( ) ;
55
72
} ;
56
73
57
- // removes all listeners for the app_dir_selected event
58
- // this is important because otherwise listeners will pile up and events will trigger multiple events
59
- window . api . removeAllAppDirChosenListeners ( ) ;
60
-
61
- // add listener for when an app directory is chosen
62
- // when a directory is chosen, the callback will export the project to the chosen folder
63
- // Note: this listener is imported from the main process via preload.js
64
- window . api . addAppDirChosenListener ( path => {
65
- exportProject (
66
- path ,
67
- state . name
68
- ? state . name
69
- : 'New_ReacType_Project_' + Math . ceil ( Math . random ( ) * 99 ) . toString ( ) ,
70
- genOption ,
71
- testchecked ,
72
- state . projectType ,
73
- state . components ,
74
- state . rootComponents
75
- ) ;
76
- } ) ;
77
-
78
74
setModal (
79
75
createModal ( {
80
76
closeModal,
@@ -114,3 +110,128 @@ export default function ExportButton() {
114
110
</ div >
115
111
) ;
116
112
}
113
+
114
+ //The below code is exclusive to the Electron App's export function
115
+
116
+ // import React, { useState, useCallback, useEffect } from 'react';
117
+ // import List from '@mui/material/List';
118
+ // import ListItem from '@mui/material/ListItem';
119
+ // import ListItemText from '@mui/material/ListItemText';
120
+ // import GetAppIcon from '@mui/icons-material/GetApp';
121
+ // import Button from '@mui/material/Button';
122
+ // import exportProject from '../../utils/exportProject.util';
123
+ // import createModal from './createModal';
124
+ // import { useSelector } from 'react-redux';
125
+ // export default function ExportButton() {
126
+ // const [modal, setModal] = useState(null);
127
+ // const state = useSelector(store => store.appState)
128
+
129
+ // const genOptions: string[] = [
130
+ // 'Export components',
131
+ // 'Export components with application files'
132
+ // ];
133
+ // let genOption = 0;
134
+
135
+ // // Closes out the open modal
136
+ // const closeModal = () => setModal('');
137
+
138
+ // const showGenerateAppModal = () => {
139
+ // const children = (
140
+ // <List className="export-preference">
141
+ // {genOptions.map((option: string, i: number) => (
142
+ // <ListItem
143
+ // key={i}
144
+ // // button
145
+ // onClick={() => chooseGenOptions(i)}
146
+ // style={{
147
+ // border: '1px solid #3f51b5',
148
+ // marginBottom: '2%',
149
+ // marginTop: '5%'
150
+ // }}
151
+ // >
152
+ // <ListItemText primary={option} style={{ textAlign: 'center' }} />
153
+ // </ListItem>
154
+ // ))}
155
+ // <ListItem>
156
+ // <input type="checkbox" id="tests" name="tests"></input>
157
+ // <label for="tests">Include Tests</label>
158
+ // </ListItem>
159
+ // </List>
160
+ // );
161
+ // let testchecked = 0;
162
+ // // helper function called by showGenerateAppModal
163
+ // // this function will prompt the user to choose an app directory once they've chosen their export option
164
+ // const chooseGenOptions = (genOpt: number) => {
165
+ // // set export option: 0 --> export only components, 1 --> export full project
166
+ // genOption = genOpt;
167
+
168
+ // //This is exclusive to the electron app
169
+ // window.api.chooseAppDir();
170
+ // testchecked = document.getElementById('tests').checked;
171
+ // testerFunc();
172
+ // closeModal();
173
+ // };
174
+
175
+ // // removes all listeners for the app_dir_selected event
176
+ // // this is important because otherwise listeners will pile up and events will trigger multiple events
177
+ // //This is exclusive to the electron app
178
+ // window.api.removeAllAppDirChosenListeners();
179
+
180
+ // // add listener for when an app directory is chosen
181
+ // // when a directory is chosen, the callback will export the project to the chosen folder
182
+ // // Note: this listener is imported from the main process via preload.js
183
+
184
+ // // This is exclusive to the electron app
185
+ // window.api.addAppDirChosenListener(path => {
186
+ // exportProject(
187
+ // path,
188
+ // state.name
189
+ // ? state.name
190
+ // : 'New_ReacType_Project_' + Math.ceil(Math.random() * 99).toString(),
191
+ // genOption,
192
+ // testchecked,
193
+ // state.projectType,
194
+ // state.components,
195
+ // state.rootComponents
196
+ // );
197
+ // });
198
+
199
+ // setModal(
200
+ // createModal({
201
+ // closeModal,
202
+ // children,
203
+ // message: 'Choose export preference:',
204
+ // primBtnLabel: null,
205
+ // primBtnAction: null,
206
+ // secBtnAction: null,
207
+ // secBtnLabel: null,
208
+ // open: true
209
+ // })
210
+ // );
211
+ // };
212
+
213
+ // const exportKeyBind = useCallback(e => {
214
+ // //Export Project
215
+ // (e.key === 'e' && e.metaKey) || (e.key === 'e' && e.ctrlKey)
216
+ // ? showGenerateAppModal()
217
+ // : '';
218
+ // }, []);
219
+
220
+ // useEffect(() => {
221
+ // document.addEventListener('keydown', exportKeyBind);
222
+ // return () => {
223
+ // document.removeEventListener('keydown', exportKeyBind);
224
+ // };
225
+ // }, []);
226
+ // return (
227
+ // <div>
228
+ // <button onClick={showGenerateAppModal}>Export Project
229
+ // <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" className="bi bi-download" viewBox="0 0 16 16">
230
+ // <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
231
+ // <path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
232
+ // </svg>
233
+ // </button>
234
+ // {modal}
235
+ // </div>
236
+ // );
237
+ // }
0 commit comments