@@ -73,30 +73,43 @@ async function populateCodeJson(data) {
73
73
return codeJson ;
74
74
}
75
75
76
+ // Creates code.json object
77
+ async function createCodeJson ( data ) {
78
+ delete data . submit ;
79
+ const codeJson = await populateCodeJson ( data ) ;
80
+
81
+ const jsonString = JSON . stringify ( codeJson , null , 2 ) ;
82
+ // const blob = new Blob([jsonString], { type: "application/json" });
83
+ document . getElementById ( "json-result" ) . value = jsonString ;
84
+ }
85
+
86
+ // Copies code.json to clipboard
76
87
async function copyToClipboard ( event ) {
77
88
event . preventDefault ( ) ;
89
+
78
90
var textArea = document . getElementById ( "json-result" ) ;
79
91
textArea . select ( ) ;
80
92
document . execCommand ( "copy" )
81
93
}
82
94
83
- // Creates code.json and triggers file download
84
- async function downloadFile ( data ) {
85
- delete data . submit ;
86
- const codeJson = await populateCodeJson ( data ) ;
95
+ // Triggers local file download
96
+ async function downloadFile ( event ) {
97
+ event . preventDefault ( ) ;
87
98
88
- const jsonString = JSON . stringify ( codeJson , null , 2 ) ;
89
- // const blob = new Blob([jsonString], { type: "application/json" });
90
- document . getElementById ( "json-result" ) . value = jsonString ;
99
+ const codeJson = document . getElementById ( "json-result" ) . value
100
+ const jsonObject = JSON . parse ( codeJson ) ;
101
+ const jsonString = JSON . stringify ( jsonObject , null , 2 ) ;
102
+ const blob = new Blob ( [ jsonString ] , { type : "application/json" } ) ;
91
103
92
- // // Create anchor element and create download link
93
- // const link = document.createElement("a");
94
- // link.href = URL.createObjectURL(blob);
95
- // link.download = "code.json";
104
+ // Create anchor element and create download link
105
+ const link = document . createElement ( "a" ) ;
106
+ link . href = URL . createObjectURL ( blob ) ;
107
+ link . download = "code.json" ;
96
108
97
- // // Trigger the download
98
- // link.click();
109
+ // Trigger the download
110
+ link . click ( ) ;
99
111
}
100
112
101
- window . downloadFile = downloadFile ;
113
+ window . createCodeJson = createCodeJson ;
102
114
window . copyToClipboard = copyToClipboard ;
115
+ window . downloadFile = downloadFile ;
0 commit comments