-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
109 lines (87 loc) · 3.09 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const { contextBridge, ipcRenderer } = require('electron')
const path = require('path')
const formBuilder = require('./formBuilder.js')
contextBridge.exposeInMainWorld(
'electron',
{
loadFolder: () => ipcRenderer.send("loadFolder")
}
)
ipcRenderer.on('clearItems',(err) => {
const dl = document.querySelector('dl')
dl.innerHTML = ""
})
ipcRenderer.on('clearData',(err) => {
const fileDataDiv = document.getElementById('fileData')
fileDataDiv.innerHTML = ""
})
ipcRenderer.on('addItem',(err, folder, file) => {
const dl = document.querySelector('dl')
//console.log("folder: " + folder)
//console.log("file: " + file)
if (folder == file) {
const dt = document.createElement('dt')
dt.appendChild(document.createTextNode(file))
dt.setAttribute("value", folder)
dt.addEventListener("click", displayItem)
dl.appendChild(dt)
}else{
const dd = document.createElement('dd')
dd.appendChild(document.createTextNode(" - " + file))
dd.setAttribute("value", path.join(`${folder}`,`${file}`))
dd.addEventListener("click", displayItem)
dl.appendChild(dd)
}
})
ipcRenderer.on('isJSON',(err, file, content) => {
//console.log(`${file}:${JSON.stringify(content)}`)
const fileDataDiv = document.getElementById("fileData");
const form = formBuilder.getFormFromJSON(content);
const submitButton = document.createElement('button');
submitButton.type = 'submit';
submitButton.textContent = 'Submit';
submitButton.style.display = "block";
//submitButton.style.margin = "0 auto";
submitButton.addEventListener("click", submitForm)
form.appendChild(submitButton);
fileDataDiv.appendChild(form)
})
ipcRenderer.on('notJSON',(err, file, error) => {
fileDataDiv = document.getElementById("fileData")
console.log(file, typeof(file))
form = document.createElement('form')
form.innerHTML = `<br><strong>file:</strong> ${file} <br><strong>error:</strong> ${error}`
fileDataDiv.appendChild(form)
})
ipcRenderer.on('toggleTheme', (event, isDarkMode) => {
const body = document.body;
if (isDarkMode) {
body.classList.remove('light-mode');
body.classList.add('dark-mode');
} else {
body.classList.remove('dark-mode');
body.classList.add('light-mode');
}
});
function displayItem(){
//console.log("displayItem:" + this.getAttribute("value"))
ipcRenderer.send("displayItem", this.getAttribute("value"))
}
function submitForm() {
event.preventDefault(); // Prevent the default form submission behavior
const form = document.querySelector('form'); // Assuming your form has been created and populated properly
// Collect form data
const formData = new FormData(form);
// Convert FormData to a plain object, considering visibility
const formDataObject = {};
formData.forEach((value, key) => {
const field = form.querySelector(`[name="${key}"]`);
const fieldStyles = window.getComputedStyle(field);
// Check if the field is visible (you might need to adjust this based on your styling)
if (fieldStyles.display !== "none" && fieldStyles.visibility !== "hidden") {
formDataObject[key] = value;
}
});
// Send the visible form data to the main process
ipcRenderer.send("saveForm", formDataObject);
}