2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 06:44:06 +00:00

incr: add preload scripts

This commit is contained in:
18alantom 2023-07-10 13:04:14 +05:30
parent ea079db92e
commit 675066d6f2
6 changed files with 157 additions and 3 deletions

View File

@ -51,7 +51,7 @@ function updatePaths() {
async function buildMainProcessSource() {
const result = await esbuild.build({
...commonConfig,
outfile: path.join(buildDirPath, mainFileName),
outdir: path.join(buildDirPath),
});
if (result.errors.length) {

View File

@ -42,7 +42,7 @@ const viteProcess = $$`yarn vite`;
*/
const ctx = await esbuild.context({
...getMainProcessCommonConfig(root),
outfile: path.join(root, 'dist_electron', 'dev', 'main.js'),
outdir: path.join(root, 'dist_electron', 'dev'),
});
/**

View File

@ -10,7 +10,10 @@ import path from 'path';
*/
export function getMainProcessCommonConfig(root) {
return {
entryPoints: [path.join(root, 'main.ts')],
entryPoints: [
path.join(root, 'main.ts'),
path.join(root, 'main', 'preload.ts'),
],
bundle: true,
sourcemap: true,
sourcesContent: false,

View File

@ -87,6 +87,7 @@ export class Main {
}
getOptions(): BrowserWindowConstructorOptions {
const preload = path.join(__dirname, 'main', 'preload.js');
const options: BrowserWindowConstructorOptions = {
width: this.WIDTH,
height: this.HEIGHT,
@ -96,6 +97,7 @@ export class Main {
webPreferences: {
contextIsolation: false, // TODO: Switch this off
nodeIntegration: true,
preload,
},
autoHideMenuBar: true,
frame: !this.isMac,

143
main/preload.ts Normal file
View File

@ -0,0 +1,143 @@
import type {
OpenDialogOptions,
OpenDialogReturnValue,
SaveDialogOptions,
SaveDialogReturnValue,
} from 'electron';
import { ipcRenderer } from 'electron';
import { BackendResponse } from 'utils/ipc/types';
import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages';
import type {
ConfigFilesWithModified,
LanguageMap,
SelectFileOptions,
SelectFileReturn,
TemplateFile,
} from 'utils/types';
type IPCRendererListener = Parameters<typeof ipcRenderer.on>[1];
const ipc = {
desktop: true,
reloadWindow() {
return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW);
},
async getLanguageMap(code: string) {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_LANGUAGE_MAP, code)) as {
languageMap: LanguageMap;
success: boolean;
message: string;
};
},
async getTemplates(): Promise<TemplateFile[]> {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_TEMPLATES
)) as TemplateFile[];
},
async selectFile(options: SelectFileOptions): Promise<SelectFileReturn> {
return (await ipcRenderer.invoke(
IPC_ACTIONS.SELECT_FILE,
options
)) as SelectFileReturn;
},
async getSaveFilePath(options: SaveDialogOptions) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_SAVE_FILEPATH,
options
)) as SaveDialogReturnValue;
},
async getOpenFilePath(options: OpenDialogOptions) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_OPEN_FILEPATH,
options
)) as OpenDialogReturnValue;
},
async checkDbAccess(filePath: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.CHECK_DB_ACCESS,
filePath
)) as boolean;
},
async checkForUpdates() {
await ipcRenderer.invoke(IPC_ACTIONS.CHECK_FOR_UPDATES);
},
openLink(link: string) {
ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, link);
},
async deleteFile(filePath: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DELETE_FILE,
filePath
)) as BackendResponse;
},
async saveData(data: string, savePath: string) {
await ipcRenderer.invoke(IPC_ACTIONS.SAVE_DATA, data, savePath);
},
showItemInFolder(filePath: string) {
ipcRenderer.send(IPC_MESSAGES.SHOW_ITEM_IN_FOLDER, filePath);
},
async makePDF(
html: string,
savePath: string,
width: number,
height: number
): Promise<boolean> {
return (await ipcRenderer.invoke(
IPC_ACTIONS.SAVE_HTML_AS_PDF,
html,
savePath,
width,
height
)) as boolean;
},
async getDbList() {
return (await ipcRenderer.invoke(
IPC_ACTIONS.GET_DB_LIST
)) as ConfigFilesWithModified[];
},
async getEnv() {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_ENV)) as {
isDevelopment: boolean;
platform: string;
version: string;
};
},
openExternalUrl(url: string) {
ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, url);
},
async showError(title: string, content: string) {
await ipcRenderer.invoke(IPC_ACTIONS.SHOW_ERROR, { title, content });
},
async sendError(body: string) {
await ipcRenderer.invoke(IPC_ACTIONS.SEND_ERROR, body);
},
registerMainProcessErrorListener(listener: IPCRendererListener) {
ipcRenderer.on(IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, listener);
},
registerConsoleLogListener(listener: IPCRendererListener) {
ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, listener);
},
} as const;
// contextBridge.exposeInMainWorld('api', ipc);
window.ipc = ipc;
export type IPC = typeof ipc;

6
src/shims-tsx.d.ts vendored
View File

@ -1,5 +1,7 @@
import type { IPC } from 'main/preload';
import Vue, { VNode } from 'vue';
declare const ipc: IPC;
declare global {
namespace JSX {
type Element = VNode;
@ -9,4 +11,8 @@ declare global {
[elem: string]: any;
}
}
interface Window {
ipc: IPC;
}
}