diff --git a/packages/web/src/DragAndDropFileTarget.js b/packages/web/src/DragAndDropFileTarget.js index 334eedbae..5741ca470 100644 --- a/packages/web/src/DragAndDropFileTarget.js +++ b/packages/web/src/DragAndDropFileTarget.js @@ -2,6 +2,7 @@ import React from 'react'; import styled from 'styled-components'; import { FontIcon } from './icons'; import useTheme from './theme/useTheme'; +import getElectron from './utility/getElectron'; import useExtensions from './utility/useExtensions'; const TargetStyled = styled.div` @@ -41,6 +42,9 @@ const TitleWrapper = styled.div` export default function DragAndDropFileTarget({ isDragActive, inputProps }) { const theme = useTheme(); const { fileFormats } = useExtensions(); + const electron = getElectron(); + const fileTypeNames = fileFormats.filter(x => x.readerFunc).map(x => x.name); + if (electron) fileTypeNames.push('SQL'); return ( !!isDragActive && ( @@ -49,13 +53,7 @@ export default function DragAndDropFileTarget({ isDragActive, inputProps }) { Drop the files to upload to DbGate - - Supported file types:{' '} - {fileFormats - .filter(x => x.readerFunc) - .map(x => x.name) - .join(', ')} - + Supported file types: {fileTypeNames.join(', ')} diff --git a/packages/web/src/utility/SaveFileToolbarButton.js b/packages/web/src/utility/SaveFileToolbarButton.js index 7e35cda3e..d4af2f855 100644 --- a/packages/web/src/utility/SaveFileToolbarButton.js +++ b/packages/web/src/utility/SaveFileToolbarButton.js @@ -8,8 +8,12 @@ export default function SaveFileToolbarButton({ tabid, save, saveAs }) { if (save) { return ( - Save - Save As + + Save + + + Save As + ); } diff --git a/packages/web/src/utility/UploadsProvider.js b/packages/web/src/utility/UploadsProvider.js index 3e4d31de3..bd4e49e80 100644 --- a/packages/web/src/utility/UploadsProvider.js +++ b/packages/web/src/utility/UploadsProvider.js @@ -3,8 +3,10 @@ import { useDropzone } from 'react-dropzone'; import ImportExportModal from '../modals/ImportExportModal'; import useShowModal from '../modals/showModal'; import { findFileFormat } from './fileformats'; +import getElectron from './getElectron'; import resolveApi from './resolveApi'; import useExtensions from './useExtensions'; +import { useOpenElectronFileCore, canOpenByElectron } from './useOpenElectronFile'; const UploadsContext = React.createContext(null); @@ -21,6 +23,8 @@ export function useUploadFiles() { const { uploadListener } = useUploadsProvider(); const showModal = useShowModal(); const extensions = useExtensions(); + const electron = getElectron(); + const openElectronFileCore = useOpenElectronFileCore(); const handleUploadFiles = React.useCallback( files => { @@ -31,6 +35,12 @@ export function useUploadFiles() { } console.log('FILE', file); + + if (electron && canOpenByElectron(file.path)) { + openElectronFileCore(file.path); + return; + } + const formData = new FormData(); formData.append('data', file); diff --git a/packages/web/src/utility/useOpenElectronFile.js b/packages/web/src/utility/useOpenElectronFile.js index b3133f5df..04af37064 100644 --- a/packages/web/src/utility/useOpenElectronFile.js +++ b/packages/web/src/utility/useOpenElectronFile.js @@ -1,31 +1,42 @@ import useNewQuery from '../query/useNewQuery'; import getElectron from './getElectron'; -export default function useOpenElectronFile() { - const electron = getElectron(); +export function canOpenByElectron(file) { + return file && file.toLowerCase().endsWith('.sql'); +} +export function useOpenElectronFileCore() { const newQuery = useNewQuery(); - return () => { - const filePaths = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), { - filters: { name: `SQL files`, extensions: ['sql'] }, - }); - const filePath = filePaths && filePaths[0]; - if (filePath) { - if (filePath.match(/.sql$/i)) { - const path = window.require('path'); - const fs = window.require('fs'); - const parsed = path.parse(filePath); - const data = fs.readFileSync(filePath, { encoding: 'utf-8' }); + return filePath => { + if (filePath.toLowerCase().endsWith('.sql')) { + const path = window.require('path'); + const fs = window.require('fs'); + const parsed = path.parse(filePath); + const data = fs.readFileSync(filePath, { encoding: 'utf-8' }); - newQuery({ - title: parsed.name, - initialData: data, - // @ts-ignore - savedFilePath: filePath, - savedFormat: 'text', - }); - } + newQuery({ + title: parsed.name, + initialData: data, + // @ts-ignore + savedFilePath: filePath, + savedFormat: 'text', + }); + } + }; +} + +export default function useOpenElectronFile() { + const electron = getElectron(); + const openElectronFileCore = useOpenElectronFileCore(); + + return () => { + const filePaths = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), { + filters: [{ name: `SQL files`, extensions: ['sql'] }], + }); + const filePath = filePaths && filePaths[0]; + if (canOpenByElectron(filePath)) { + openElectronFileCore(filePath); } }; }