-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
98 lines (87 loc) · 2.28 KB
/
Copy pathpreload.js
File metadata and controls
98 lines (87 loc) · 2.28 KB
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
const { contextBridge, ipcRenderer } = require('electron');
const fs = require('fs');
const path = require('path');
const YAML = require('yaml');
const settingsPath = path.join(__dirname, 'settings.json');
function getSettings() {
const defaults = {
startWithEmptyInput: true,
defaultInput: ''
};
try {
const raw = fs.readFileSync(settingsPath, 'utf8');
const parsed = JSON.parse(raw);
return {
...defaults,
...parsed
};
} catch (error) {
return defaults;
}
}
function parseInput(text) {
const source = (text || '').trim();
if (!source) {
return {
ok: false,
error: 'Paste JSON or YAML to begin.'
};
}
try {
return {
ok: true,
format: 'JSON',
data: JSON.parse(source)
};
} catch (jsonError) {
try {
return {
ok: true,
format: 'YAML',
data: YAML.parse(source)
};
} catch (yamlError) {
const yamlMessage = yamlError instanceof Error ? yamlError.message : String(yamlError);
return {
ok: false,
error: `Unable to parse input as JSON or YAML. JSON error: ${jsonError.message}. YAML error: ${yamlMessage}`
};
}
}
}
function stringifyYaml(value) {
return YAML.stringify(value);
}
contextBridge.exposeInMainWorld('structViewApi', {
parseInput,
parseInputAsync: (text) => ipcRenderer.invoke('parse-input-async', text || ''),
searchStructureAsync: (payload) => ipcRenderer.invoke('search-structure-async', payload || {}),
getSettings,
stringifyYaml,
onOpenFile: (handler) => {
if (typeof handler !== 'function') {
return () => {};
}
const listener = (_event, payload) => {
handler(payload);
};
ipcRenderer.on('menu-open-file', listener);
return () => {
ipcRenderer.removeListener('menu-open-file', listener);
};
},
openFileDialog: () => ipcRenderer.invoke('open-file-dialog'),
saveFileDialog: (request) => ipcRenderer.invoke('save-file-dialog', request || {}),
onRequestSave: (handler) => {
if (typeof handler !== 'function') {
return () => {};
}
const listener = () => {
handler();
};
ipcRenderer.on('menu-save-file-request', listener);
return () => {
ipcRenderer.removeListener('menu-save-file-request', listener);
};
}
});