Skip to content

Commit c3fded8

Browse files
Merge pull request #35 from DSACMS/nat/json-preview
Added code.json preview window with copy to clipboard functionality
2 parents a6a8422 + 770facd commit c3fded8

5 files changed

+36
-5
lines changed

css/styles.css

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
body {
22
margin: 20px;
3+
}
4+
5+
textarea {
6+
margin-bottom: 10px;
37
}

favicon.ico

14.7 KB
Binary file not shown.

index.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
}).then(function (form) {
4141
form.on("submit", function (submission) {
4242
console.log("form submission here:", submission);
43-
downloadFile(submission.data);
43+
createCodeJson(submission.data);
4444
});
4545
});
4646
})
@@ -52,5 +52,11 @@
5252
<body>
5353
<div id="form-header"></div>
5454
<div id="formio"></div>
55+
<div id="output">
56+
<label for="json-result">Your JSON Metadata </label>
57+
<textarea class="form-control" rows="10" id="json-result" readonly></textarea>
58+
<button type="button" class="btn btn-outline" href="#" onclick="copyToClipboard(event)">Copy</button>
59+
<button type="button" class="btn btn-outline" href="#" onclick="downloadFile(event)">Download</button>
60+
</div>
5561
</body>
5662
</html>

js/formDataToJson.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,31 @@ async function populateCodeJson(data) {
7373
return codeJson;
7474
}
7575

76-
// Creates code.json and triggers file download
77-
async function downloadFile(data) {
76+
// Creates code.json object
77+
async function createCodeJson(data) {
7878
delete data.submit;
7979
const codeJson = await populateCodeJson(data);
8080

8181
const jsonString = JSON.stringify(codeJson, null, 2);
82+
document.getElementById("json-result").value = jsonString;
83+
}
84+
85+
// Copies code.json to clipboard
86+
async function copyToClipboard(event){
87+
event.preventDefault();
88+
89+
var textArea = document.getElementById("json-result");
90+
textArea.select();
91+
document.execCommand("copy")
92+
}
93+
94+
// Triggers local file download
95+
async function downloadFile(event) {
96+
event.preventDefault();
97+
98+
const codeJson = document.getElementById("json-result").value
99+
const jsonObject = JSON.parse(codeJson);
100+
const jsonString = JSON.stringify(jsonObject, null, 2);
82101
const blob = new Blob([jsonString], { type: "application/json" });
83102

84103
// Create anchor element and create download link
@@ -90,4 +109,6 @@ async function downloadFile(data) {
90109
link.click();
91110
}
92111

112+
window.createCodeJson = createCodeJson;
113+
window.copyToClipboard = copyToClipboard;
93114
window.downloadFile = downloadFile;

js/generateFormComponents.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ async function createFormComponents() {
299299
// Add submit button to form
300300
components.push({
301301
type: "button",
302-
label: "Submit",
302+
label: "Generate code.json metadata",
303303
key: "submit",
304304
disableOnInvalid: false,
305305
input: true,
@@ -311,4 +311,4 @@ async function createFormComponents() {
311311
return components;
312312
}
313313

314-
window.createFormComponents = createFormComponents;
314+
window.createFormComponents = createFormComponents;

0 commit comments

Comments
 (0)