Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix: use showOpenDialog with promise instead of callback (#1715)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated authored Feb 26, 2020
1 parent cbb4c96 commit fad72b6
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/renderer/mixins/electron.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import electron from 'electron'
import { readFile, writeFileSync } from 'fs'
import { readFileSync, writeFileSync } from 'fs'

export default {
methods: {
Expand Down Expand Up @@ -28,25 +28,18 @@ export default {
return filePath
},

electron_readFile (options = {}) {
const filters = options.filters || [
{ name: 'JSON', extensions: ['json'] },
{ name: 'All Files', extensions: ['*'] }
]

return new Promise((resolve, reject) => {
electron.remote.dialog.showOpenDialog({
properties: ['openFile'],
filters
}, filePaths => {
if (!filePaths) return

readFile(filePaths[0], 'utf8', (err, data) => {
if (err) reject(err)
resolve(data)
})
})
async electron_readFile (options = {}) {
const { filePaths } = await electron.remote.dialog.showOpenDialog({
properties: ['openFile'],
filters: options.filters || [
{ name: 'JSON', extensions: ['json'] },
{ name: 'All Files', extensions: ['*'] }
]
})

if (!filePaths) return

return readFileSync(filePaths[0], 'utf8')
}
}
}

0 comments on commit fad72b6

Please sign in to comment.