-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreload.js
More file actions
83 lines (70 loc) · 1.97 KB
/
Copy pathpreload.js
File metadata and controls
83 lines (70 loc) · 1.97 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
import { readServices } from './lib/read-services' with { type: 'macro' };
import { contextBridge, ipcRenderer } from 'electron/renderer';
const electronObj = {
cwd() {
return ipcRenderer.sendSync('local:cwd');
},
home() {
return ipcRenderer.sendSync('local:home');
},
loadBlocksZip(zip) {
return ipcRenderer.invoke('local:blocks:select', zip);
},
readBlocksFiles(id) {
return new Promise((resolve) => {
ipcRenderer.on('local:blocks:zip:reply', (event, files) => resolve(files));
ipcRenderer.send('local:blocks:zip', id);
});
},
getLocalBlocks() {
return ipcRenderer.sendSync('local:blocks');
},
getLocalEditors() {
return ipcRenderer.sendSync('local:editors');
},
getLocalTutorials(editorOrBlockId, lang) {
return ipcRenderer.sendSync('local:tutorials', editorOrBlockId, lang);
},
get bluetooth() {
return {
onScan(callback) {
ipcRenderer.on('bluetooth:scan', (event, value) => callback(value));
},
cancel() {
return ipcRenderer.send('bluetooth:cancel');
},
connect(portId) {
return ipcRenderer.send('bluetooth:connect', portId);
},
};
},
get serial() {
return {
onScan(callback) {
ipcRenderer.on('serial:scan', (event, value) => callback(value));
},
cancel() {
return ipcRenderer.send('serial:cancel');
},
connect(portId) {
return ipcRenderer.send('serial:connect', portId);
},
};
},
onChangeFullscreen(callback) {
ipcRenderer.on('window:fullscreen', (event, value) => callback(value));
},
};
// 载入扩展服务
(async () => {
const services = readServices();
for (const { preload } of services) {
if (preload) {
const { default: getService } = await import(preload);
const service = getService(ipcRenderer);
Object.assign(electronObj, service);
}
}
// 添加 window.electron
contextBridge.exposeInMainWorld('electron', electronObj);
})();