electron: save file to custom location

This commit is contained in:
Jan Prochazka
2021-01-28 18:52:59 +01:00
parent df976a84d2
commit fe1c5f5801
14 changed files with 132 additions and 13 deletions

View File

@@ -0,0 +1,31 @@
import useNewQuery from '../query/useNewQuery';
import getElectron from './getElectron';
export default function useOpenElectronFile() {
const electron = getElectron();
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' });
newQuery({
title: parsed.name,
initialData: data,
// @ts-ignore
savedFilePath: filePath,
savedFormat: 'text',
});
}
}
};
}

View File

@@ -23,11 +23,16 @@ export default function useOpenNewTab() {
async (newTab, initialData = undefined, options) => {
let existing = null;
const { savedFile } = newTab.props || {};
if (savedFile) {
const { savedFile, savedFolder, savedFilePath } = newTab.props || {};
if (savedFile || savedFilePath) {
existing = openedTabs.find(
x =>
x.props && x.tabComponent == newTab.tabComponent && x.closedTime == null && x.props.savedFile == savedFile
x.props &&
x.tabComponent == newTab.tabComponent &&
x.closedTime == null &&
x.props.savedFile == savedFile &&
x.props.savedFolder == savedFolder &&
x.props.savedFilePath == savedFilePath
);
}