Skip to content

Instantly share code, notes, and snippets.

@marekhrabe
Created March 10, 2017 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marekhrabe/64bf5c3cf497c410bc9b0194f7efdd2d to your computer and use it in GitHub Desktop.
Save marekhrabe/64bf5c3cf497c410bc9b0194f7efdd2d to your computer and use it in GitHub Desktop.
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