You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
56 lines
1.6 KiB
const { contextBridge, ipcRenderer } = require("electron");
|
|
|
|
const validSendChannels = ["note_command", "element-clicked"];
|
|
const validReceiveChannels = ["note_response"];
|
|
|
|
const send = (channel, data) => {
|
|
if (validSendChannels.includes(channel)) {
|
|
ipcRenderer.send(channel, JSON.stringify(data));
|
|
return Promise.resolve();
|
|
}
|
|
return Promise.reject();
|
|
};
|
|
|
|
const receive = channel => {
|
|
if (validReceiveChannels.includes(channel)) {
|
|
return new Promise(resolve => {
|
|
ipcRenderer.on(channel, (_, ...args) => resolve(...args));
|
|
});
|
|
}
|
|
return Promise.reject();
|
|
};
|
|
|
|
const receiveOnce = channel => {
|
|
if (validReceiveChannels.includes(channel)) {
|
|
return new Promise(resolve => {
|
|
ipcRenderer.once(channel, (_, ...args) => resolve(...args));
|
|
});
|
|
}
|
|
return Promise.reject();
|
|
};
|
|
|
|
contextBridge.exposeInMainWorld("api", {
|
|
send,
|
|
receive,
|
|
receiveOnce,
|
|
sendLinkNav: target => ipcRenderer.send("element-clicked", target),
|
|
getSettings: async () => {
|
|
send("note_command", { type: "getSettings" });
|
|
const response = await receiveOnce("note_response");
|
|
return JSON.parse(response);
|
|
},
|
|
getNotes: async () => {
|
|
send("note_command", { type: "getNotes" });
|
|
const response = await receiveOnce("note_response");
|
|
return JSON.parse(response);
|
|
},
|
|
saveNote: async ({ title, content, tags }) => {
|
|
send("note_command", {
|
|
type: "saveNote",
|
|
payload: { title, content, tags }
|
|
});
|
|
const response = await receiveOnce("note_response");
|
|
return JSON.parse(response);
|
|
}
|
|
});
|