diff --git a/electron/electronAPI.d.ts b/electron/electronAPI.d.ts index c9bd56e84..6f2d53511 100644 --- a/electron/electronAPI.d.ts +++ b/electron/electronAPI.d.ts @@ -51,7 +51,7 @@ export interface ElectronAPI { checkDirExists(args: { dirPath: string }): Promise; - pickDirectory(): Promise; + pickDirectory(options?: DirectoryDialogOptions): Promise; // checkDirExists(dirPath: string): Promise; @@ -152,3 +152,10 @@ export interface ElectronAPI { request: PluginNodeScriptRequest, ): Promise; } + +export interface DirectoryDialogOptions { + defaultPath?: string | null; + title?: string; + message?: string; + buttonLabel?: string; +} diff --git a/electron/local-file-sync.ts b/electron/local-file-sync.ts index 93d8e4d43..09af72a08 100644 --- a/electron/local-file-sync.ts +++ b/electron/local-file-sync.ts @@ -2,9 +2,14 @@ import { IPC } from './shared-with-frontend/ipc-events.const'; import { SyncGetRevResult } from '../src/app/imex/sync/sync.model'; import { readdirSync, readFileSync, statSync, writeFileSync, unlinkSync } from 'fs'; import { error, log } from 'electron-log/main'; -import { dialog, ipcMain } from 'electron'; +import { dialog, ipcMain, OpenDialogOptions } from 'electron'; import { getWin } from './main-window'; +type DirectoryDialogOptions = Pick< + OpenDialogOptions, + 'defaultPath' | 'title' | 'message' | 'buttonLabel' +>; + export const initLocalFileSyncAdapter = (): void => { ipcMain.handle( IPC.FILE_SYNC_SAVE, @@ -136,16 +141,19 @@ export const initLocalFileSyncAdapter = (): void => { }, ); - ipcMain.handle(IPC.PICK_DIRECTORY, async (): Promise => { - const { canceled, filePaths } = await dialog.showOpenDialog(getWin(), { - properties: ['openDirectory'], - }); - if (canceled) { - return undefined; - } else { + ipcMain.handle( + IPC.PICK_DIRECTORY, + async (_ev, options?: DirectoryDialogOptions): Promise => { + const { canceled, filePaths } = await dialog.showOpenDialog(getWin(), { + properties: ['openDirectory'], + ...options, + }); + if (canceled) { + return undefined; + } return filePaths[0]; - } - }); + }, + ); }; const getRev = (filePath: string): string => { diff --git a/electron/preload.ts b/electron/preload.ts index c64c965fd..587c79af7 100644 --- a/electron/preload.ts +++ b/electron/preload.ts @@ -1,5 +1,5 @@ import { ipcRenderer, IpcRendererEvent, webFrame, contextBridge } from 'electron'; -import { ElectronAPI } from './electronAPI.d'; +import { DirectoryDialogOptions, 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'; @@ -48,7 +48,8 @@ const ea: ElectronAPI = { checkDirExists: (dirPath) => _invoke('CHECK_DIR_EXISTS', dirPath) as Promise, - pickDirectory: () => _invoke('PICK_DIRECTORY') as Promise, + pickDirectory: (options?: DirectoryDialogOptions) => + _invoke('PICK_DIRECTORY', options) as Promise, // STANDARD // -------- diff --git a/src/app/pfapi/api/sync/providers/local-file-sync/local-file-sync-electron.ts b/src/app/pfapi/api/sync/providers/local-file-sync/local-file-sync-electron.ts index c9dfc27d5..5b6fd31f9 100644 --- a/src/app/pfapi/api/sync/providers/local-file-sync/local-file-sync-electron.ts +++ b/src/app/pfapi/api/sync/providers/local-file-sync/local-file-sync-electron.ts @@ -86,7 +86,10 @@ export class LocalFileSyncElectron extends LocalFileSyncBase { PFLog.normal(`${LocalFileSyncElectron.L}.pickDirectory()`); try { - const dir = await (window as any).ea.pickDirectory(); + const privateCfg = await this.privateCfg.load(); + const dir = await (window as any).ea.pickDirectory({ + defaultPath: privateCfg?.syncFolderPath || undefined, + }); if (dir) { await this.privateCfg.upsertPartial({ syncFolderPath: dir }); }