1
0
Fork 0
mirror of https://github.com/anyproto/anytype-ts.git synced 2025-06-08 14:07:01 +09:00
anytype-ts/electron/js/preload.js
Andrew Simachev aa1a47a34e
merge
2024-03-11 17:33:24 +01:00

58 lines
No EOL
1.6 KiB
JavaScript

const { ipcRenderer, contextBridge } = require('electron');
const { app, getCurrentWindow, getGlobal, dialog, BrowserWindow } = require('@electron/remote');
const fs = require('fs');
const os = require('os');
const path = require('path');
const tmpPath = () => app.getPath('temp');
contextBridge.exposeInMainWorld('Electron', {
version: {
app: app.getVersion(),
os: [ os.platform(), process.arch, process.getSystemVersion() ].join(' '),
system: process.getSystemVersion(),
device: os.hostname(),
},
platform: os.platform(),
arch: process.arch,
isPackaged: app.isPackaged,
userPath: () => app.getPath('userData'),
tmpPath,
logPath: () => path.join(app.getPath('userData'), 'logs'),
currentWindow: () => getCurrentWindow(),
isMaximized: () => BrowserWindow.getFocusedWindow()?.isMaximized(),
isFocused: () => getCurrentWindow().isFocused(),
focus: () => getCurrentWindow().focus(),
getGlobal: (key) => getGlobal(key),
showOpenDialog: dialog.showOpenDialog,
fileWrite: (name, data, options) => {
name = String(name || 'temp');
options = options || {};
const fn = path.parse(name).base;
const fp = path.join(tmpPath(), fn);
options.mode = 0o666;
fs.writeFileSync(fp, data, options);
return fp;
},
filePath (...args) {
return path.join(...args);
},
dirname: fp => path.dirname(fp),
on: (event, callBack) => ipcRenderer.on(event, callBack),
removeAllListeners: (event) => ipcRenderer.removeAllListeners(event),
Api: (id, cmd, args) => {
id = Number(id) || 0;
cmd = String(cmd || '');
args = args || [];
return ipcRenderer.invoke('Api', id, cmd, args);
},
});