mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-03 09:24:00 +00:00
open sql file with drag & drop
This commit is contained in:
@@ -2,6 +2,7 @@ import React from 'react';
|
|||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import { FontIcon } from './icons';
|
import { FontIcon } from './icons';
|
||||||
import useTheme from './theme/useTheme';
|
import useTheme from './theme/useTheme';
|
||||||
|
import getElectron from './utility/getElectron';
|
||||||
import useExtensions from './utility/useExtensions';
|
import useExtensions from './utility/useExtensions';
|
||||||
|
|
||||||
const TargetStyled = styled.div`
|
const TargetStyled = styled.div`
|
||||||
@@ -41,6 +42,9 @@ const TitleWrapper = styled.div`
|
|||||||
export default function DragAndDropFileTarget({ isDragActive, inputProps }) {
|
export default function DragAndDropFileTarget({ isDragActive, inputProps }) {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { fileFormats } = useExtensions();
|
const { fileFormats } = useExtensions();
|
||||||
|
const electron = getElectron();
|
||||||
|
const fileTypeNames = fileFormats.filter(x => x.readerFunc).map(x => x.name);
|
||||||
|
if (electron) fileTypeNames.push('SQL');
|
||||||
return (
|
return (
|
||||||
!!isDragActive && (
|
!!isDragActive && (
|
||||||
<TargetStyled theme={theme}>
|
<TargetStyled theme={theme}>
|
||||||
@@ -49,13 +53,7 @@ export default function DragAndDropFileTarget({ isDragActive, inputProps }) {
|
|||||||
<FontIcon icon="icon cloud-upload" />
|
<FontIcon icon="icon cloud-upload" />
|
||||||
</IconWrapper>
|
</IconWrapper>
|
||||||
<TitleWrapper>Drop the files to upload to DbGate</TitleWrapper>
|
<TitleWrapper>Drop the files to upload to DbGate</TitleWrapper>
|
||||||
<InfoWrapper>
|
<InfoWrapper>Supported file types: {fileTypeNames.join(', ')}</InfoWrapper>
|
||||||
Supported file types:{' '}
|
|
||||||
{fileFormats
|
|
||||||
.filter(x => x.readerFunc)
|
|
||||||
.map(x => x.name)
|
|
||||||
.join(', ')}
|
|
||||||
</InfoWrapper>
|
|
||||||
</InfoBox>
|
</InfoBox>
|
||||||
<input {...inputProps} />
|
<input {...inputProps} />
|
||||||
</TargetStyled>
|
</TargetStyled>
|
||||||
|
|||||||
@@ -8,8 +8,12 @@ export default function SaveFileToolbarButton({ tabid, save, saveAs }) {
|
|||||||
if (save) {
|
if (save) {
|
||||||
return (
|
return (
|
||||||
<ToolbarDropDownButton icon="icon save" text="Save">
|
<ToolbarDropDownButton icon="icon save" text="Save">
|
||||||
<DropDownMenuItem onClick={save}>Save</DropDownMenuItem>
|
<DropDownMenuItem onClick={save} keyText="Ctrl+S">
|
||||||
<DropDownMenuItem onClick={saveAs}>Save As</DropDownMenuItem>
|
Save
|
||||||
|
</DropDownMenuItem>
|
||||||
|
<DropDownMenuItem onClick={saveAs} keyText="Ctrl+Shift+S">
|
||||||
|
Save As
|
||||||
|
</DropDownMenuItem>
|
||||||
</ToolbarDropDownButton>
|
</ToolbarDropDownButton>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ import { useDropzone } from 'react-dropzone';
|
|||||||
import ImportExportModal from '../modals/ImportExportModal';
|
import ImportExportModal from '../modals/ImportExportModal';
|
||||||
import useShowModal from '../modals/showModal';
|
import useShowModal from '../modals/showModal';
|
||||||
import { findFileFormat } from './fileformats';
|
import { findFileFormat } from './fileformats';
|
||||||
|
import getElectron from './getElectron';
|
||||||
import resolveApi from './resolveApi';
|
import resolveApi from './resolveApi';
|
||||||
import useExtensions from './useExtensions';
|
import useExtensions from './useExtensions';
|
||||||
|
import { useOpenElectronFileCore, canOpenByElectron } from './useOpenElectronFile';
|
||||||
|
|
||||||
const UploadsContext = React.createContext(null);
|
const UploadsContext = React.createContext(null);
|
||||||
|
|
||||||
@@ -21,6 +23,8 @@ export function useUploadFiles() {
|
|||||||
const { uploadListener } = useUploadsProvider();
|
const { uploadListener } = useUploadsProvider();
|
||||||
const showModal = useShowModal();
|
const showModal = useShowModal();
|
||||||
const extensions = useExtensions();
|
const extensions = useExtensions();
|
||||||
|
const electron = getElectron();
|
||||||
|
const openElectronFileCore = useOpenElectronFileCore();
|
||||||
|
|
||||||
const handleUploadFiles = React.useCallback(
|
const handleUploadFiles = React.useCallback(
|
||||||
files => {
|
files => {
|
||||||
@@ -31,6 +35,12 @@ export function useUploadFiles() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log('FILE', file);
|
console.log('FILE', file);
|
||||||
|
|
||||||
|
if (electron && canOpenByElectron(file.path)) {
|
||||||
|
openElectronFileCore(file.path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('data', file);
|
formData.append('data', file);
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
import useNewQuery from '../query/useNewQuery';
|
import useNewQuery from '../query/useNewQuery';
|
||||||
import getElectron from './getElectron';
|
import getElectron from './getElectron';
|
||||||
|
|
||||||
export default function useOpenElectronFile() {
|
export function canOpenByElectron(file) {
|
||||||
const electron = getElectron();
|
return file && file.toLowerCase().endsWith('.sql');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useOpenElectronFileCore() {
|
||||||
const newQuery = useNewQuery();
|
const newQuery = useNewQuery();
|
||||||
|
|
||||||
return () => {
|
return filePath => {
|
||||||
const filePaths = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), {
|
if (filePath.toLowerCase().endsWith('.sql')) {
|
||||||
filters: { name: `SQL files`, extensions: ['sql'] },
|
|
||||||
});
|
|
||||||
const filePath = filePaths && filePaths[0];
|
|
||||||
if (filePath) {
|
|
||||||
if (filePath.match(/.sql$/i)) {
|
|
||||||
const path = window.require('path');
|
const path = window.require('path');
|
||||||
const fs = window.require('fs');
|
const fs = window.require('fs');
|
||||||
const parsed = path.parse(filePath);
|
const parsed = path.parse(filePath);
|
||||||
@@ -26,6 +23,20 @@ export default function useOpenElectronFile() {
|
|||||||
savedFormat: 'text',
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user