Skip to content

Commit 2ba16b5

Browse files
committed
feat(preload): add 'webUtils.getPathForFile' api
1 parent 5220c19 commit 2ba16b5

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

packages/preload/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @electron-toolkit/preload
22

3-
> Easy to expose Electron APIs (ipcRenderer,webFrame,process) to renderer.
3+
> Easy to expose Electron APIs (ipcRenderer,webFrame,webUtils,process) to renderer.
44
55
---
66

@@ -93,6 +93,10 @@ declare global {
9393
- `setZoomFactor`
9494
- `setZoomLevel`
9595

96+
### WebUtils
97+
98+
- `getPathForFile`
99+
96100
### NodeProcess
97101

98102
- `platform` property

packages/preload/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ipcRenderer, webFrame, contextBridge } from 'electron'
1+
import { ipcRenderer, webFrame, webUtils, contextBridge } from 'electron'
22
import { ElectronAPI } from './types'
33

4-
export type { ElectronAPI, IpcRenderer, IpcRendererListener, WebFrame, NodeProcess } from './types'
4+
export type { ElectronAPI, IpcRenderer, IpcRendererListener, WebFrame, WebUtils, NodeProcess } from './types'
55

66
export const electronAPI: ElectronAPI = {
77
ipcRenderer: {
@@ -64,6 +64,11 @@ export const electronAPI: ElectronAPI = {
6464
}
6565
}
6666
},
67+
webUtils: {
68+
getPathForFile(file) {
69+
return webUtils.getPathForFile(file)
70+
}
71+
},
6772
process: {
6873
get platform() {
6974
return process.platform

packages/preload/src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ export interface WebFrame {
167167
setZoomLevel(level: number): void
168168
}
169169

170+
export interface WebUtils {
171+
// Docs: https://electronjs.org/docs/api/web-utils
172+
173+
/**
174+
* The file system path that this `File` object points to. In the case where the
175+
* object passed in is not a `File` object an exception is thrown. In the case
176+
* where the File object passed in was constructed in JS and is not backed by a
177+
* file on disk an empty string is returned.
178+
*
179+
* This method superseded the previous augmentation to the `File` object with the
180+
* `path` property. An example is included below.
181+
*/
182+
getPathForFile(file: File): string
183+
}
184+
170185
export interface NodeProcess {
171186
/**
172187
* The process.platform property returns a string identifying the operating system platform
@@ -188,5 +203,6 @@ export interface NodeProcess {
188203
export interface ElectronAPI {
189204
ipcRenderer: IpcRenderer
190205
webFrame: WebFrame
206+
webUtils: WebUtils
191207
process: NodeProcess
192208
}

0 commit comments

Comments
 (0)