-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathhandlers.js
More file actions
103 lines (82 loc) · 2.11 KB
/
handlers.js
File metadata and controls
103 lines (82 loc) · 2.11 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
99
100
101
102
103
const { ipcRenderer, nativeImage } = require('electron');
const fs = require('fs');
const path = require('path');
const handlers = {};
handlers.layoutOpen = (callback) => {
const filenames = ipcRenderer.sendSync('dialog:openFile', {
filters: [
{name: 'Thinreports Layout File', extensions: ['tlf']}
],
properties: ['openFile']
});
if (!filenames) {
callback.onCancel();
return;
}
const content = fs.readFileSync(filenames[0], { encoding: 'utf-8' });
callback.onSuccess(content, {
id: filenames[0],
name: path.basename(filenames[0]),
path: filenames[0]
});
}
handlers.layoutSave = (callback, data, attrs) => {
fs.writeFileSync(attrs.path, data, { encoding: 'utf-8' });
callback.onSuccess(attrs);
}
handlers.layoutSaveAs = (callback, data) => {
const filename = ipcRenderer.sendSync('dialog:saveFile', {
filters: [
{name: 'Thinreports Layout File', extensions: ['tlf']}
]
});
if (!filename) {
callback.onCancel();
return;
}
fs.writeFileSync(filename, data, { encoding: 'utf-8' });
callback.onSuccess(data, {
id: filename,
name: path.basename(filename),
path: filename
});
}
handlers.imageOpen = (callback) => {
const imagefiles = ipcRenderer.sendSync('dialog:openFile', {
filters: [
{name: 'Images', extensions: ['jpg', 'png']}
],
properties: ['openFile']
});
if (!imagefiles) {
callback.onCancel();
return;
}
const image = nativeImage.createFromPath(imagefiles[0]);
callback.onSuccess(image.toDataURL());
}
handlers.exportAs = (callback, type, content) => {
let extName, description;
switch (type) {
case 'csv':
extName = 'csv';
description = 'Text CSV';
break;
case 'html':
extName = 'html';
description = 'HTML document';
break;
}
const filename = ipcRenderer.sendSync('dialog:saveFile', {
filters: [
{extensions: [extName], name: description}
]
});
if (!filename) {
callback.onCancel();
return;
}
fs.writeFileSync(filename, content, { encoding: 'utf-8' });
callback.onSuccess();
}
window.handlers = handlers;