-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
216 lines (191 loc) · 6.52 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const { app, BrowserWindow, nativeTheme, Menu, shell, dialog } = require('electron');
const fs = require('fs');
const ipcMain = require('electron').ipcMain;
const isDev = process.env.APP_DEV ? (process.env.APP_DEV.trim() == 'true') : false;
const { setupTitlebar, attachTitlebarToWindow } = require('custom-electron-titlebar/main');
const path = require('path');
// setup the titlebar main process
setupTitlebar();
let globals = {};
app.on('open-file', function (event, filePath) {
globals.openedFile = fs.readFileSync(filePath).buffer;
globals.openedFilePath = path.join(__dirname, filePath);
});
function getUserData() {
if (!fs.existsSync(app.getPath('documents') + '/Photopea files')) fs.mkdirSync(app.getPath('documents') + '/Photopea files');
if (!fs.existsSync(app.getPath('documents') + '/Photopea files/Plugins')) fs.mkdirSync(app.getPath('documents') + '/Photopea files/Plugins');
if (!fs.existsSync(app.getPath('documents') + '/Photopea files/Resources')) fs.mkdirSync(app.getPath('documents') + '/Photopea files/Resources');
if (!fs.existsSync(app.getPath('documents') + '/Photopea files/config.json')) fs.writeFileSync(app.getPath('documents') + '/Photopea files/config.json', "{\n \n}");
var jsonconfig = fs.readFileSync(`${app.getPath('documents')}/Photopea files/config.json`, 'utf-8');
globals.options = JSON.parse(jsonconfig);
globals.resources = [];
for (var resource of fs.readdirSync(`${app.getPath('documents')}/Photopea files/Resources`)) {
var fileext = resource.split('.').pop();
if (!(['md', 'DS_Store'].includes(fileext))) globals.resources.push(fs.readFileSync(`${app.getPath('documents')}/Photopea files/Resources/${resource}`).buffer);
}
if (globals.options.environment) globals.options.environment.plugins = [];
else {
globals.options.environment = {
plugins: []
};
};
// globals.otions.plugins = [];
for (var file of fs.readdirSync(`${app.getPath('documents')}/Photopea files/Plugins`)) {
var fileext = file.split('.').pop();
if (fileext.toLowerCase() == "json") {
globals.options.environment.plugins.push(JSON.parse(fs.readFileSync(`${app.getPath('documents')}/Photopea files/Plugins/${file}`, 'utf-8')));
}
}
globals.options.enableIO = true;
if (process.argv.length >= 2) {
// console.log(process.argv)
var filePath = process.argv[isDev ? 2 : 1];
fs.readFile(filePath, null, function (err, data) {
if (err) console.log(err);
if (data) {
globals.openedFile = data.buffer;
globals.openedFilePath = path.join(__dirname, filePath);
};
});
}
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 900,
titleBarStyle: "hidden",
frame: process.platform == "darwin",
webPreferences: {
// nodeIntegration: true,
// contextIsolation: false,
// enableRemoteModule: true,
preload: `${__dirname}/preload.js`
}
});
mainWindow.setMenuBarVisibility(false);
mainWindow.loadFile('window/index.html');
mainWindow.maximize();
mainWindow.webContents.on('new-window', function (e, url) {
console.log('new window');
e.preventDefault();
require('electron').shell.openExternal(url);
});
nativeTheme.themeSource = 'dark';
isDev && mainWindow.webContents.openDevTools();
//attach fullscreen(f11 and not 'maximized') && focus listeners
attachTitlebarToWindow(mainWindow);
}
function setMenu() {
var template = [
{
label: 'Keyboard Shortcuts',
submenu: [
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'selectall' }
]
},
{
label: 'Developer',
submenu: [
{ role: 'toggleDevTools' }
]
}
];
if (process.platform == "darwin") {
template.unshift({
label: app.name,
submenu: [
{ label: 'About Photopea', click() { shell.openExternal("https://github.com/photopea/photopea/blob/master/README.md#photopeacom") } },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
});
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}
app.whenReady().then(() => {
getUserData();
createWindow();
setMenu();
});
// app.on('window-all-closed', function () { app.exit() });
//No more remote....
/// This should be part of potopea.js api files as thats whats expecting it.
ipcMain.handle('openDialog', async (event, options) => {
let response = await dialog.showOpenDialog(options);
if (!response.canceled) {
return response.filePaths[0];
} else {
console.log("no file selected");
return;
};
});
ipcMain.handle('saveDialog', async (event, options) => {
let response = await dialog.showSaveDialog(options);
if (!response.canceled) {
return response.filePath;
} else {
console.log("no file selected");
return;
};
});
ipcMain.handle('openFile', async (event, options) => {
let response = await dialog.showOpenDialog(options);
if (!response.canceled) {
let file = fs.readFileSync(response.filePaths[0]).buffer;
return { file: file, path: response.filePaths[0] };
} else {
console.log("no file selected");
return;
}
});
ipcMain.handle('saveFile', async (event, path, file) => {
fs.writeFileSync(path, Buffer.from(file));
return true;
});
ipcMain.handle('getGlobal', async (event, ...theArgs) => {
if (theArgs.length > 1) {
let result = theArgs.reduce(function (newObj, key) {
newObj[key] = globals[key];
return newObj;
}, {});
return result;
}
return globals[theArgs[0]];
})
ipcMain.handle('setGlobal', async (event, key, value) => {
globals[key] = value;
});
///
ipcMain.handle('customTBar', function (event, command) {
switch (command) {
// case 'minimize':
// BrowserWindow.fromWebContents(event.sender).minimize();
// break;
// case 'maximize':
// const window = BrowserWindow.fromWebContents(event.sender);
// window.isMaximized() ? window.unmaximize() : window.maximize();
// break;
case 'close':
var choice = dialog.showMessageBoxSync(BrowserWindow.getFocusedWindow(), {
type: "question",
buttons: ["Yes", "No"],
title: "Exit?",
message: "Unsaved work will be lost."
});
if (choice == 0) app.exit();
break;
// case 'is-maximized':
// return BrowserWindow.fromWebContents(event.sender).isMaximized()
// break;
default:
console.log("Unknown customTBar command", command);
}
})