super-productivity/electron/preload.ts
Johannes Millan a935da9346 Merge branch 'master' into release/sync-lib-test
* master:
  feat: don't show go to project snack if task is in current list
  feat: remove task drawer since it is not working at the moment anyway
  Update README.md
  feat: enhance updateCurrentTask to include Pomodoro state and session time
  feat: update indicator to use time estimate for countdown message
  feat: add countdown message to the indicator
  Fix #4184: Added fractional time parsing when creating a new task
  Add functionality if a Issue on Gitlab is closed the Issue on Super is also closed after syncron.
  fix: minor fixes to italian translation
  feat: updated italian translations

# Conflicts:
#	src/app/features/tasks/store/task-electron.effects.ts
2025-04-15 17:12:09 +02:00

94 lines
3.9 KiB
TypeScript

import { contextBridge, ipcRenderer, IpcRendererEvent, webFrame } from 'electron';
import { ElectronAPI } from './electronAPI.d';
import { IPCEventValue } from './shared-with-frontend/ipc-events.const';
import { LocalBackupMeta } from '../src/app/imex/local-backup/local-backup.model';
import { SyncGetRevResult } from '../src/app/imex/sync/sync.model';
const _send: (channel: IPCEventValue, ...args: any[]) => void = (channel, ...args) =>
ipcRenderer.send(channel, ...args);
const _invoke: (channel: IPCEventValue, ...args: any[]) => Promise<unknown> = (
channel,
...args
) => ipcRenderer.invoke(channel, ...args);
const ea: ElectronAPI = {
on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) => {
// NOTE: there is no proper way to unsubscribe apart from unsubscribing all
ipcRenderer.on(channel, listener);
},
// INVOKE
// ------
getUserDataPath: () => _invoke('GET_PATH', 'userData') as Promise<string>,
getBackupPath: () => _invoke('GET_BACKUP_PATH') as Promise<string>,
checkBackupAvailable: () =>
_invoke('BACKUP_IS_AVAILABLE') as Promise<false | LocalBackupMeta>,
loadBackupData: (backupPath) =>
_invoke('BACKUP_LOAD_DATA', backupPath) as Promise<string>,
fileSyncGetRevAndClientUpdate: (backupPath) =>
_invoke('FILE_SYNC_GET_REV_AND_CLIENT_UPDATE', backupPath) as Promise<
{ rev: string; clientUpdate?: number } | SyncGetRevResult
>,
fileSyncSave: (filePath) =>
_invoke('FILE_SYNC_SAVE', filePath) as Promise<string | Error>,
fileSyncLoad: (filePath) =>
_invoke('FILE_SYNC_LOAD', filePath) as Promise<{
rev: string;
dataStr: string | undefined;
}>,
fileSyncRemove: (filePath) => _invoke('FILE_SYNC_REMOVE', filePath) as Promise<void>,
checkDirExists: (dirPath) =>
_invoke('CHECK_DIR_EXISTS', dirPath) as Promise<true | Error>,
pickDirectory: () => _invoke('PICK_DIRECTORY') as Promise<string | undefined>,
// STANDARD
// --------
setZoomFactor: (zoomFactor: number) => {
webFrame.setZoomFactor(zoomFactor);
},
getZoomFactor: () => webFrame.getZoomFactor(),
isLinux: () => process.platform === 'linux',
isMacOS: () => process.platform === 'darwin',
isSnap: () => process && process.env && !!process.env.SNAP,
// SEND
// ----
relaunch: () => _send('RELAUNCH'),
exit: () => _send('EXIT'),
flashFrame: () => _send('FLASH_FRAME'),
showOrFocus: () => _send('SHOW_OR_FOCUS'),
lockScreen: () => _send('LOCK_SCREEN'),
shutdownNow: () => _send('SHUTDOWN_NOW'),
reloadMainWin: () => _send('RELOAD_MAIN_WIN'),
openDevTools: () => _send('OPEN_DEV_TOOLS'),
showEmojiPanel: () => _send('SHOW_EMOJI_PANEL'),
informAboutAppReady: () => _send('APP_READY'),
openPath: (path: string) => _send('OPEN_PATH', path),
openExternalUrl: (url: string) => _send('OPEN_EXTERNAL', url),
scheduleRegisterBeforeClose: (id) => _send('REGISTER_BEFORE_CLOSE', { id }),
unscheduleRegisterBeforeClose: (id) => _send('UNREGISTER_BEFORE_CLOSE', { id }),
setDoneRegisterBeforeClose: (id) => _send('BEFORE_CLOSE_DONE', { id }),
setProgressBar: (args) => _send('SET_PROGRESS_BAR', args),
sendAppSettingsToElectron: (globalCfg) =>
_send('TRANSFER_SETTINGS_TO_ELECTRON', globalCfg),
registerGlobalShortcuts: (keyboardCfg) =>
_send('REGISTER_GLOBAL_SHORTCUTS', keyboardCfg),
showFullScreenBlocker: (args) => _send('FULL_SCREEN_BLOCKER', args),
makeJiraRequest: (args) => _send('JIRA_MAKE_REQUEST_EVENT', args),
jiraSetupImgHeaders: (args) => _send('JIRA_SETUP_IMG_HEADERS', args),
backupAppData: (appData) => _send('BACKUP', appData),
updateCurrentTask: (task, isPomodoroEnabled, currentPomodoroSessionTime) =>
_send('CURRENT_TASK_UPDATED', task, isPomodoroEnabled, currentPomodoroSessionTime),
exec: (command: string) => _send('EXEC', command),
};
contextBridge.exposeInMainWorld('ea', ea);
// contextBridge.exposeInIsolatedWorld();
console.log('preload script loading complete');