-
-
Save marekhrabe/64bf5c3cf497c410bc9b0194f7efdd2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path'; | |
import { getHomeDirectory, isDirectorySync } from 'fs-plus'; | |
import isElectron from 'is-electron'; | |
import glob from 'glob'; | |
export const openFileSelector = (callback) => { | |
if (isElectron()) { | |
const remote = window.require('electron').remote; | |
remote.dialog.showOpenDialog( | |
remote.BrowserWindow.getFocusedWindow(), | |
{ | |
title: 'Import', | |
buttonLabel: 'Import', | |
properties: [ 'openFile' ], | |
defaultPath: remote.app.getPath('desktop'), | |
}, | |
(results) => { | |
if (results && results.length > 0) { | |
const result = results[0]; | |
callback(null, encodeURIComponent('file://' + result), path.basename(result)); | |
storage.setItem('importFolder', path.dirname(result)); | |
} else { | |
callback(null, null, null); | |
} | |
} | |
) | |
} else { | |
let fileInput = document.querySelector('#global-file-input'); | |
if (!fileInput) { | |
fileInput = document.createElement('input'); | |
fileInput.type = 'file'; | |
fileInput.style.display = 'none'; | |
fileInput.style.position = 'absolute'; | |
document.body.appendChild(fileInput); | |
} | |
const fileHandler = (e) => { | |
fileInput.removeEventListener('change', fileHandler); | |
if (fileInput.files.length > 0) { | |
callback(null, URL.createObjectURL(fileInput.files[0]), fileInput.files[0].name); | |
} else { | |
callback(null, null, null); | |
} | |
} | |
fileInput.addEventListener('change', fileHandler, false); | |
fileInput.click(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment