-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_operation.js
More file actions
126 lines (112 loc) · 4 KB
/
file_operation.js
File metadata and controls
126 lines (112 loc) · 4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const {dialog, ipcMain} = require('electron');
const fs = require('fs');
const open_file = (item, focusedWindow) => {
// 打开文件
dialog.showOpenDialog (focusedWindow, {
title: "Open Mindmap",
properties: [ 'openFile' ],
filters: [
{ name: 'KityMinder File', extensions: ['km'] },
{ name: 'All Files', extensions: ['*'] }
]
}, function (filenames) {
if (!Object.is(filenames, undefined)) {
var filename = filenames[0];
fs.readFile(filename, 'utf8', function (err, data) {
focusedWindow.webContents.send('load-file', JSON.parse(data), filename);
});
}
});
}
const save_as = (item, focusedWindow) => {
var ipc = focusedWindow.webContents;
dialog.showSaveDialog (focusedWindow, {
title: "Save As",
dafaultPath: "",
filters: [
{ name: 'KityMinder File', extensions: ['km'] },
{ name: 'All Files', extensions: ['*'] }
]
}, function (filename) {
if (Object.is(filename, undefined)) {
return;
}
ipc.send('save-file', filename);
ipcMain.once('file-content', function (event, content, fname) {
event.sender.send('console', fname, content);
fs.writeFile(fname, JSON.stringify(content), 'utf8', function (err) {
if (err) throw err;
event.sender.send('console', 'saved!');
});
});
});
}
const save_file = (item, focusedWindow, callback) => {
var ipc = focusedWindow.webContents;
ipc.send('save-file');
ipcMain.once('file-content', function (event, content, fname) {
if (Object.is(fname, null)) {
fname = dialog.showSaveDialog (focusedWindow, {
title: "Save Mindmap",
properties: [ 'openFile' ],
filters: [
{ name: 'KityMinder File', extensions: ['km'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (!fname) return;
else ipc.send('set-filename', fname);
}
fs.writeFile(fname, JSON.stringify(content), 'utf8', function (err) {
if (err) throw err;
event.sender.send('console', 'saved, '+fname);
if (Object.prototype.toString.call(callback)=== '[object Function]') {
callback();
}
focusedWindow.statusStore.fileChanged = true;
});
});
}
const export_file = (menuItem, focusedWindow) => {
var ipc = focusedWindow.webContents;
var formats = {
'JSON': 'json',
'Plain Text': 'text',
'Markdown': 'markdown',
'SVG': 'svg',
'PNG': 'png'
}
var filters = {
'JSON': { name: 'JSON', extensions: ['json'] },
'Markdown': { name: 'Markdown', extensions: ['md']},
'Plain Text': { name: 'Plain Text', extensions: ['txt']},
'SVG': { name: 'SVG', extensions: ['svg']},
'PNG': { name: 'PNG', extensions: ['png']},
}
var format = formats[menuItem.label];
ipc.send('export-file', format);
ipcMain.once('file-content', function (event, data) {
event.sender.send('console', 'data: '+data);
if (format == 'png') {
data = data.slice(22);
data = new Buffer(data, 'base64');
}
var fname = dialog.showSaveDialog(focusedWindow, {
title: "Export",
dafaultPath: "",
filters: [filters[menuItem.label]]
}, function (fname) {
if (!fname) return;
fs.writeFile(fname, data, 'utf8', function (err) {
if (err) throw err;
event.sender.send('console', 'exported, '+fname);
});
});
})
}
module.exports = {
open_file,
save_file,
save_as,
export_file,
}