Skip to content

Commit 132c511

Browse files
Added @elek-io/core with test button and IPC communication
1 parent cf541b2 commit 132c511

File tree

6 files changed

+334
-92
lines changed

6 files changed

+334
-92
lines changed

index.html

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<body>
88
<h1>💖 Hello World!</h1>
99
<p>Welcome to your Electron application.</p>
10+
<button
11+
onclick="window.ipc.core.user.get().then((result) => console.log('Result:', result)).catch((err) => console.log('Error:', err))"
12+
>
13+
Test IPC
14+
</button>
1015
<script type="module" src="/src/renderer/index.ts"></script>
1116
</body>
1217
</html>

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"release": "electron-forge publish"
2424
},
2525
"dependencies": {
26+
"@elek-io/core": "^0.1.0",
27+
"dugite": "^2.5.2",
2628
"electron-squirrel-startup": "^1.0.0"
2729
},
2830
"devDependencies": {

src/main/index.ts

+91-89
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// import ElekIoCore from '@elek-io/core';
1+
import ElekIoCore from '@elek-io/core';
22
import {
33
app,
44
BrowserWindow,
55
BrowserWindowConstructorOptions,
6+
dialog,
7+
ipcMain,
68
protocol,
79
screen,
810
} from 'electron';
@@ -61,8 +63,8 @@ class Main {
6163
// If no, show the user the /user/create page
6264
// If yes use this to init core and show /projects
6365

64-
// const core = new ElekIoCore();
65-
// await this.registerIpcMain(core);
66+
const core = new ElekIoCore();
67+
this.registerIpcMain(core);
6668

6769
// const user = await core.user.get();
6870
// let mainWindow: BrowserWindow;
@@ -156,92 +158,92 @@ class Main {
156158
return window;
157159
}
158160

159-
// private async registerIpcMain(core: ElekIoCore) {
160-
// ipcMain.handle('electron:dialog:showOpenDialog', async (event, args) => {
161-
// return await dialog.showOpenDialog(args[0], args[1]);
162-
// });
163-
// ipcMain.handle('core:user:get', async (event, args) => {
164-
// return await core.user.get();
165-
// });
166-
// ipcMain.handle('core:user:set', async (event, args) => {
167-
// return await core.user.set(args[0]);
168-
// });
169-
// ipcMain.handle('core:projects:count', async (event, args) => {
170-
// return await core.projects.count();
171-
// });
172-
// ipcMain.handle('core:projects:create', async (event, args) => {
173-
// return await core.projects.create(args[0]);
174-
// });
175-
// ipcMain.handle('core:projects:list', async (event, args) => {
176-
// return await core.projects.list(args[0]);
177-
// });
178-
// ipcMain.handle('core:projects:read', async (event, args) => {
179-
// return await core.projects.read(args[0]);
180-
// });
181-
// ipcMain.handle('core:projects:update', async (event, args) => {
182-
// return await core.projects.update(args[0]);
183-
// });
184-
// ipcMain.handle('core:projects:delete', async (event, args) => {
185-
// return await core.projects.delete(args[0]);
186-
// });
187-
// ipcMain.handle('core:projects:search', async (event, args) => {
188-
// return await core.projects.search(args[0], args[1], args[2]);
189-
// });
190-
// ipcMain.handle('core:assets:list', async (event, args) => {
191-
// return await core.assets.list(args[0]);
192-
// });
193-
// ipcMain.handle('core:assets:create', async (event, args) => {
194-
// return await core.assets.create(args[0]);
195-
// });
196-
// ipcMain.handle('core:assets:delete', async (event, args) => {
197-
// return await core.assets.delete(args[0]);
198-
// });
199-
// // ipcMain.handle('core:snapshots:list', async (event, args) => {
200-
// // return await core.snapshots.list(
201-
// // args[0],
202-
// // args[1],
203-
// // args[2],
204-
// // args[3],
205-
// // args[4]
206-
// // );
207-
// // });
208-
// // ipcMain.handle('core:snapshots:commitHistory', async (event, args) => {
209-
// // return await core.snapshots.commitHistory(args[0]);
210-
// // });
211-
// ipcMain.handle('core:collections:list', async (event, args) => {
212-
// return await core.collections.list(args[0]);
213-
// });
214-
// ipcMain.handle('core:collections:create', async (event, args) => {
215-
// return await core.collections.create(args[0]);
216-
// });
217-
// ipcMain.handle('core:collections:read', async (event, args) => {
218-
// return await core.collections.read(args[0]);
219-
// });
220-
// ipcMain.handle('core:collections:update', async (event, args) => {
221-
// return await core.collections.update(args[0]);
222-
// });
223-
// ipcMain.handle('core:collections:delete', async (event, args) => {
224-
// return await core.collections.delete(args[0]);
225-
// });
226-
// ipcMain.handle('core:entries:list', async (event, args) => {
227-
// return await core.entries.list(args[0]);
228-
// });
229-
// ipcMain.handle('core:entries:create', async (event, args) => {
230-
// return await core.entries.create(args[0]);
231-
// });
232-
// ipcMain.handle('core:entries:read', async (event, args) => {
233-
// return await core.entries.read(args[0]);
234-
// });
235-
// ipcMain.handle('core:entries:update', async (event, args) => {
236-
// return await core.entries.update(args[0]);
237-
// });
238-
// ipcMain.handle('core:entries:delete', async (event, args) => {
239-
// return await core.entries.delete(args[0]);
240-
// });
241-
// // this.handleIpcMain<Parameters<AssetService['list']>>('core:assets:list', async (event, args) => {
242-
// // return await core.assets.list(args.projectId);
243-
// // })
244-
// }
161+
private registerIpcMain(core: ElekIoCore) {
162+
ipcMain.handle('electron:dialog:showOpenDialog', async (event, args) => {
163+
return await dialog.showOpenDialog(args[0], args[1]);
164+
});
165+
ipcMain.handle('core:user:get', async (event, args) => {
166+
return await core.user.get();
167+
});
168+
ipcMain.handle('core:user:set', async (event, args) => {
169+
return await core.user.set(args[0]);
170+
});
171+
ipcMain.handle('core:projects:count', async (event, args) => {
172+
return await core.projects.count();
173+
});
174+
ipcMain.handle('core:projects:create', async (event, args) => {
175+
return await core.projects.create(args[0]);
176+
});
177+
ipcMain.handle('core:projects:list', async (event, args) => {
178+
return await core.projects.list(args[0]);
179+
});
180+
ipcMain.handle('core:projects:read', async (event, args) => {
181+
return await core.projects.read(args[0]);
182+
});
183+
ipcMain.handle('core:projects:update', async (event, args) => {
184+
return await core.projects.update(args[0]);
185+
});
186+
ipcMain.handle('core:projects:delete', async (event, args) => {
187+
return await core.projects.delete(args[0]);
188+
});
189+
ipcMain.handle('core:projects:search', async (event, args) => {
190+
return await core.projects.search(args[0], args[1], args[2]);
191+
});
192+
ipcMain.handle('core:assets:list', async (event, args) => {
193+
return await core.assets.list(args[0]);
194+
});
195+
ipcMain.handle('core:assets:create', async (event, args) => {
196+
return await core.assets.create(args[0]);
197+
});
198+
ipcMain.handle('core:assets:delete', async (event, args) => {
199+
return await core.assets.delete(args[0]);
200+
});
201+
// ipcMain.handle('core:snapshots:list', async (event, args) => {
202+
// return await core.snapshots.list(
203+
// args[0],
204+
// args[1],
205+
// args[2],
206+
// args[3],
207+
// args[4]
208+
// );
209+
// });
210+
// ipcMain.handle('core:snapshots:commitHistory', async (event, args) => {
211+
// return await core.snapshots.commitHistory(args[0]);
212+
// });
213+
ipcMain.handle('core:collections:list', async (event, args) => {
214+
return await core.collections.list(args[0]);
215+
});
216+
ipcMain.handle('core:collections:create', async (event, args) => {
217+
return await core.collections.create(args[0]);
218+
});
219+
ipcMain.handle('core:collections:read', async (event, args) => {
220+
return await core.collections.read(args[0]);
221+
});
222+
ipcMain.handle('core:collections:update', async (event, args) => {
223+
return await core.collections.update(args[0]);
224+
});
225+
ipcMain.handle('core:collections:delete', async (event, args) => {
226+
return await core.collections.delete(args[0]);
227+
});
228+
ipcMain.handle('core:entries:list', async (event, args) => {
229+
return await core.entries.list(args[0]);
230+
});
231+
ipcMain.handle('core:entries:create', async (event, args) => {
232+
return await core.entries.create(args[0]);
233+
});
234+
ipcMain.handle('core:entries:read', async (event, args) => {
235+
return await core.entries.read(args[0]);
236+
});
237+
ipcMain.handle('core:entries:update', async (event, args) => {
238+
return await core.entries.update(args[0]);
239+
});
240+
ipcMain.handle('core:entries:delete', async (event, args) => {
241+
return await core.entries.delete(args[0]);
242+
});
243+
// this.handleIpcMain<Parameters<AssetService['list']>>('core:assets:list', async (event, args) => {
244+
// return await core.assets.list(args.projectId);
245+
// })
246+
}
245247
}
246248

247249
export default new Main();

src/renderer/preload.ts

+95
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,97 @@
11
// See the Electron documentation for details on how to use preload scripts:
22
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
3+
import type Core from '@elek-io/core';
4+
import { contextBridge, ipcRenderer, type Dialog } from 'electron';
5+
6+
export interface ContextBridgeApi {
7+
electron: {
8+
dialog: {
9+
showOpenDialog: Dialog['showOpenDialog'];
10+
};
11+
};
12+
core: {
13+
user: {
14+
get: Core['user']['get'];
15+
set: Core['user']['set'];
16+
};
17+
projects: {
18+
create: Core['projects']['create'];
19+
count: Core['projects']['count'];
20+
list: Core['projects']['list'];
21+
read: Core['projects']['read'];
22+
update: Core['projects']['update'];
23+
delete: Core['projects']['delete'];
24+
search: Core['projects']['search'];
25+
};
26+
assets: {
27+
list: Core['assets']['list'];
28+
create: Core['assets']['create'];
29+
delete: Core['assets']['delete'];
30+
};
31+
collections: {
32+
list: Core['collections']['list'];
33+
create: Core['collections']['create'];
34+
read: Core['collections']['read'];
35+
update: Core['collections']['update'];
36+
delete: Core['collections']['delete'];
37+
};
38+
entries: {
39+
list: Core['entries']['list'];
40+
create: Core['entries']['create'];
41+
read: Core['entries']['read'];
42+
update: Core['entries']['update'];
43+
delete: Core['entries']['delete'];
44+
};
45+
};
46+
}
47+
48+
const exposedApi: ContextBridgeApi = {
49+
electron: {
50+
dialog: {
51+
// @ts-ignore -> Why?
52+
showOpenDialog: (...args) =>
53+
ipcRenderer.invoke('electron:dialog:showOpenDialog', args),
54+
},
55+
},
56+
core: {
57+
user: {
58+
get: (...args) => ipcRenderer.invoke('core:user:get', args),
59+
set: (...args) => ipcRenderer.invoke('core:user:set', args),
60+
},
61+
projects: {
62+
count: (...args) => ipcRenderer.invoke('core:projects:count', args),
63+
create: (...args) => ipcRenderer.invoke('core:projects:create', args),
64+
list: (...args) => ipcRenderer.invoke('core:projects:list', args),
65+
read: (...args) => ipcRenderer.invoke('core:projects:read', args),
66+
update: (...args) => ipcRenderer.invoke('core:projects:update', args),
67+
delete: (...args) => ipcRenderer.invoke('core:projects:delete', args),
68+
search: (...args) => ipcRenderer.invoke('core:projects:search', args),
69+
},
70+
assets: {
71+
list: (...args) => ipcRenderer.invoke('core:assets:list', args),
72+
create: (...args) => ipcRenderer.invoke('core:assets:create', args),
73+
delete: (...args) => ipcRenderer.invoke('core:assets:delete', args),
74+
},
75+
// snapshots: {
76+
// commitHistory: (...args) =>
77+
// ipcRenderer.invoke('core:snapshots:commitHistory', args),
78+
// list: (...args) => ipcRenderer.invoke('core:snapshots:list', args),
79+
// },
80+
collections: {
81+
list: (...args) => ipcRenderer.invoke('core:collections:list', args),
82+
create: (...args) => ipcRenderer.invoke('core:collections:create', args),
83+
read: (...args) => ipcRenderer.invoke('core:collections:read', args),
84+
update: (...args) => ipcRenderer.invoke('core:collections:update', args),
85+
delete: (...args) => ipcRenderer.invoke('core:collections:delete', args),
86+
},
87+
entries: {
88+
list: (...args) => ipcRenderer.invoke('core:entries:list', args),
89+
create: (...args) => ipcRenderer.invoke('core:entries:create', args),
90+
read: (...args) => ipcRenderer.invoke('core:entries:read', args),
91+
update: (...args) => ipcRenderer.invoke('core:entries:update', args),
92+
delete: (...args) => ipcRenderer.invoke('core:entries:delete', args),
93+
},
94+
},
95+
};
96+
97+
contextBridge.exposeInMainWorld('ipc', exposedApi);

window.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This file should augment the properties of the `Window` with the type of the
2+
// `ContextBridgeApi` from `Electron.contextBridge` declared in `./preload.ts`.
3+
import type { ContextBridgeApi } from './src/renderer/preload';
4+
5+
declare global {
6+
interface Window {
7+
ipc: ContextBridgeApi;
8+
}
9+
}

0 commit comments

Comments
 (0)