removed dependencies on electron remote

This commit is contained in:
Jan Prochazka
2021-12-16 15:41:34 +01:00
parent 588b8b23f9
commit 3d841ef8fe
15 changed files with 87 additions and 54 deletions

View File

@@ -148,6 +148,24 @@ ipcMain.on('update-commands', async (event, arg) => {
menu.enabled = command.enabled; menu.enabled = command.enabled;
} }
}); });
ipcMain.on('close-window', async (event, arg) => {
mainWindow.close();
});
ipcMain.handle('showOpenDialog', async (event, options) => {
const res = electron.dialog.showOpenDialogSync(mainWindow, options);
return res;
});
ipcMain.handle('showSaveDialog', async (event, options) => {
const res = electron.dialog.showSaveDialogSync(mainWindow, options);
return res;
});
ipcMain.handle('showItemInFolder', async (event, path) => {
electron.shell.showItemInFolder(path);
});
ipcMain.handle('openExternal', async (event, url) => {
electron.shell.openExternal(url);
});
function createWindow() { function createWindow() {
const bounds = store.get('winBounds'); const bounds = store.get('winBounds');

View File

@@ -1,6 +1,5 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
export const extractKey = props => props.name; export const extractKey = props => props.name;
const electron = getElectron();
export function getDatabaseMenuItems(connection, name, $extensions, $currentDatabase) { export function getDatabaseMenuItems(connection, name, $extensions, $currentDatabase) {
const handleNewQuery = () => { const handleNewQuery = () => {
@@ -89,6 +88,7 @@
}; };
const handleDisconnect = () => { const handleDisconnect = () => {
const electron = getElectron();
if (electron) { if (electron) {
axiosInstance().post('database-connections/disconnect', { conid: connection._id, database: name }); axiosInstance().post('database-connections/disconnect', { conid: connection._id, database: name });
} }

View File

@@ -1,6 +1,4 @@
<script context="module"> <script context="module">
const electron = getElectron();
registerCommand({ registerCommand({
id: 'commandPalette.show', id: 'commandPalette.show',
category: 'Command palette', category: 'Command palette',
@@ -20,7 +18,7 @@
category: 'Database', category: 'Database',
toolbarName: 'Database search', toolbarName: 'Database search',
name: 'Search', name: 'Search',
keyText: electron ? 'Ctrl+P' : 'F3', keyText: isElectronAvailable() ? 'Ctrl+P' : 'F3',
onClick: () => visibleCommandPalette.set('database'), onClick: () => visibleCommandPalette.set('database'),
testEnabled: () => getVisibleCommandPalette() != 'database', testEnabled: () => getVisibleCommandPalette() != 'database',
}); });
@@ -75,7 +73,7 @@
} from '../stores'; } from '../stores';
import clickOutside from '../utility/clickOutside'; import clickOutside from '../utility/clickOutside';
import getConnectionLabel from '../utility/getConnectionLabel'; import getConnectionLabel from '../utility/getConnectionLabel';
import getElectron from '../utility/getElectron'; import { isElectronAvailable } from '../utility/getElectron';
import keycodes from '../utility/keycodes'; import keycodes from '../utility/keycodes';
import { useConnectionList, useDatabaseInfo } from '../utility/metadataLoaders'; import { useConnectionList, useDatabaseInfo } from '../utility/metadataLoaders';
import { getLocalStorage } from '../utility/storageCache'; import { getLocalStorage } from '../utility/storageCache';

View File

@@ -4,8 +4,6 @@ import getElectron from '../utility/getElectron';
import registerCommand from './registerCommand'; import registerCommand from './registerCommand';
import axiosInstance from '../utility/axiosInstance'; import axiosInstance from '../utility/axiosInstance';
const electron = getElectron();
registerCommand({ registerCommand({
id: 'database.changeState', id: 'database.changeState',
category: 'Database', category: 'Database',
@@ -34,6 +32,7 @@ registerCommand({
{ {
text: 'Disconnect', text: 'Disconnect',
onClick: () => { onClick: () => {
const electron = getElectron();
if (electron) axiosInstance().post('database-connections/disconnect', dbid); if (electron) axiosInstance().post('database-connections/disconnect', dbid);
currentDatabase.set(null); currentDatabase.set(null);
}, },

View File

@@ -26,8 +26,6 @@ import InputTextModal from '../modals/InputTextModal.svelte';
import { removeLocalStorage } from '../utility/storageCache'; import { removeLocalStorage } from '../utility/storageCache';
import { showSnackbarSuccess } from '../utility/snackbar'; import { showSnackbarSuccess } from '../utility/snackbar';
const electron = getElectron();
function themeCommand(theme: ThemeDefinition) { function themeCommand(theme: ThemeDefinition) {
return { return {
text: theme.themeName, text: theme.themeName,
@@ -316,22 +314,22 @@ registerCommand({
group: 'redo', group: 'redo',
}); });
if (electron) { registerCommand({
registerCommand({ id: 'file.open',
id: 'file.open', category: 'File',
category: 'File', name: 'Open',
name: 'Open', keyText: 'Ctrl+O',
keyText: 'Ctrl+O', testEnabled: () => getElectron() != null,
onClick: openElectronFile, onClick: openElectronFile,
}); });
registerCommand({ registerCommand({
id: 'file.openArchive', id: 'file.openArchive',
category: 'File', category: 'File',
name: 'Open DB Model/Archive', name: 'Open DB Model/Archive',
onClick: openArchiveFolder, testEnabled: () => getElectron() != null,
}); onClick: openArchiveFolder,
} });
registerCommand({ registerCommand({
id: 'file.import', id: 'file.import',
@@ -419,14 +417,13 @@ if (hasPermission('settings/change')) {
}); });
} }
if (electron) { registerCommand({
registerCommand({ id: 'file.exit',
id: 'file.exit', category: 'File',
category: 'File', name: 'Exit',
name: 'Exit', testEnabled: () => getElectron() != null,
onClick: () => electron.remote.getCurrentWindow().close(), onClick: () => getElectron().send('close-window'),
}); });
}
export function registerFileCommands({ export function registerFileCommands({
idPrefix, idPrefix,

View File

@@ -11,10 +11,10 @@
const { values, setFieldValue } = getFormContext(); const { values, setFieldValue } = getFormContext();
function handleBrowse() { async function handleBrowse() {
const electron = getElectron(); const electron = getElectron();
if (!electron) return; if (!electron) return;
const filePaths = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), { const filePaths = await electron.showOpenDialog({
defaultPath: values[name], defaultPath: values[name],
properties: ['showHiddenFiles'], properties: ['showHiddenFiles'],
filters: [{ name: 'All Files', extensions: ['*'] }], filters: [{ name: 'All Files', extensions: ['*'] }],

View File

@@ -19,11 +19,11 @@
let isLoading = false; let isLoading = false;
const electron = getElectron();
const { values } = getFormContext(); const { values } = getFormContext();
const handleClick = async () => { const handleClick = async () => {
const files = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), { const electron = getElectron();
const files = await electron.showOpenDialog({
properties: ['openFile', 'multiSelections'], properties: ['openFile', 'multiSelections'],
filters: getFileFilters($extensions, $values.sourceStorageType), filters: getFileFilters($extensions, $values.sourceStorageType),
}); });

View File

@@ -14,8 +14,8 @@
import FormCheckboxField from '../forms/FormCheckboxField.svelte'; import FormCheckboxField from '../forms/FormCheckboxField.svelte';
import FormValues from '../forms/FormValues.svelte'; import FormValues from '../forms/FormValues.svelte';
import FormSelectField from '../forms/FormSelectField.svelte'; import FormSelectField from '../forms/FormSelectField.svelte';
import FormSubmit from '../forms/FormSubmit.svelte'; import FormSubmit from '../forms/FormSubmit.svelte';
import FormButton from '../forms/FormButton.svelte'; import FormButton from '../forms/FormButton.svelte';
export let editingData; export let editingData;
export let savingTab; export let savingTab;

View File

@@ -61,8 +61,8 @@
<FormStyledButton <FormStyledButton
type="button" type="button"
value="Save to disk" value="Save to disk"
on:click={() => { on:click={async () => {
const file = electron.remote.dialog.showSaveDialogSync(electron.remote.getCurrentWindow(), { const file = await electron.showSaveDialog({
filters: [ filters: [
{ name: `${fileExtension.toUpperCase()} files`, extensions: [fileExtension] }, { name: `${fileExtension.toUpperCase()} files`, extensions: [fileExtension] },
{ name: `All files`, extensions: ['*'] }, { name: `All files`, extensions: ['*'] },

View File

@@ -78,8 +78,8 @@
slot="1" slot="1"
let:row let:row
href="#" href="#"
on:click={() => { on:click={async () => {
const file = electron.remote.dialog.showSaveDialogSync(electron.remote.getCurrentWindow(), {}); const file = await electron.showSaveDialog({});
if (file) { if (file) {
const fs = window.require('fs'); const fs = window.require('fs');
fs.copyFile(row.path, file, () => {}); fs.copyFile(row.path, file, () => {});
@@ -94,7 +94,7 @@
let:row let:row
href="#" href="#"
on:click={() => { on:click={() => {
electron.remote.shell.showItemInFolder(row.path); electron.showItemInFolder(row.path);
}} }}
> >
show show

View File

@@ -9,7 +9,7 @@ export async function exportElectronFile(dataName, reader, format) {
const electron = getElectron(); const electron = getElectron();
const filters = [{ name: format.label, extensions: [format.extension] }]; const filters = [{ name: format.label, extensions: [format.extension] }];
const filePath = electron.remote.dialog.showSaveDialogSync(electron.remote.getCurrentWindow(), { const filePath = await electron.showSaveDialog({
filters, filters,
defaultPath: `${dataName}.${format.extension}`, defaultPath: `${dataName}.${format.extension}`,
properties: ['showOverwriteConfirmation'], properties: ['showOverwriteConfirmation'],
@@ -65,14 +65,14 @@ export async function saveFileToDisk(
if (electron) { if (electron) {
const filters = [{ name: formatLabel, extensions: [formatExtension] }]; const filters = [{ name: formatLabel, extensions: [formatExtension] }];
const filePath = electron.remote.dialog.showSaveDialogSync(electron.remote.getCurrentWindow(), { const filePath = await electron.showSaveDialog({
filters, filters,
defaultPath: `file.${formatExtension}`, defaultPath: `file.${formatExtension}`,
properties: ['showOverwriteConfirmation'], properties: ['showOverwriteConfirmation'],
}); });
if (!filePath) return; if (!filePath) return;
await filePathFunc(filePath); await filePathFunc(filePath);
electron.shell.openExternal('file:///' + filePath); electron.openExternal('file:///' + filePath);
} else { } else {
const resp = await axiosInstance().get('files/generate-uploads-file'); const resp = await axiosInstance().get('files/generate-uploads-file');
await filePathFunc(resp.data.filePath); await filePathFunc(resp.data.filePath);

View File

@@ -8,9 +8,28 @@ class ElectronApi {
this.authorization = args.authorization; this.authorization = args.authorization;
} }
send(msg, args) { send(msg, args = null) {
this.ipcRenderer.send(msg, args); this.ipcRenderer.send(msg, args);
} }
async showOpenDialog(options) {
const res = await this.ipcRenderer.invoke('showOpenDialog', options);
return res;
}
async showSaveDialog(options) {
const res = await this.ipcRenderer.invoke('showSaveDialog', options);
return res;
}
async showItemInFolder(path) {
const res = await this.ipcRenderer.invoke('showItemInFolder', path);
return res;
}
async openExternal(url) {
await this.ipcRenderer.invoke('openExternal', url);
}
} }
let apiInstance = null; let apiInstance = null;
@@ -39,6 +58,10 @@ export function shouldWaitForElectronInitialize() {
return !!getIpcRenderer() && !apiInstance; return !!getIpcRenderer() && !apiInstance;
} }
export function isElectronAvailable() {
return !!getIpcRenderer();
}
export default function getElectron(): ElectronApi { export default function getElectron(): ElectronApi {
return apiInstance; return apiInstance;
// try { // try {

View File

@@ -7,7 +7,7 @@ import { showSnackbarSuccess } from './snackbar';
export async function openArchiveFolder() { export async function openArchiveFolder() {
const electron = getElectron(); const electron = getElectron();
const ext = get(extensions); const ext = get(extensions);
const filePaths = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), { const filePaths = await electron.showOpenDialog({
properties: ['openDirectory'], properties: ['openDirectory'],
}); });
const linkedFolder = filePaths && filePaths[0]; const linkedFolder = filePaths && filePaths[0];

View File

@@ -107,10 +107,10 @@ function getFileFormatExtensions(extensions) {
return extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extension); return extensions.fileFormats.filter(x => x.readerFunc).map(x => x.extension);
} }
export function openElectronFile() { export async function openElectronFile() {
const electron = getElectron(); const electron = getElectron();
const ext = get(extensions); const ext = get(extensions);
const filePaths = electron.remote.dialog.showOpenDialogSync(electron.remote.getCurrentWindow(), { const filePaths = await electron.showOpenDialog({
filters: [ filters: [
{ name: `All supported files`, extensions: ['sql', 'sqlite', 'db', 'sqlite3', ...getFileFormatExtensions(ext)] }, { name: `All supported files`, extensions: ['sql', 'sqlite', 'db', 'sqlite3', ...getFileFormatExtensions(ext)] },
{ name: `SQL files`, extensions: ['sql'] }, { name: `SQL files`, extensions: ['sql'] },

View File

@@ -1,6 +1,4 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
const electron = getElectron();
const closeTabFunc = closeCondition => tabid => { const closeTabFunc = closeCondition => tabid => {
openedTabs.update(files => { openedTabs.update(files => {
const active = files.find(x => x.tabid == tabid); const active = files.find(x => x.tabid == tabid);
@@ -105,7 +103,7 @@
id: 'tabs.closeTab', id: 'tabs.closeTab',
category: 'Tabs', category: 'Tabs',
name: 'Close tab', name: 'Close tab',
keyText: electron ? 'Ctrl+W' : null, keyText: isElectronAvailable() ? 'Ctrl+W' : null,
testEnabled: () => getOpenedTabs().filter(x => !x.closedTime).length >= 1, testEnabled: () => getOpenedTabs().filter(x => !x.closedTime).length >= 1,
onClick: closeCurrentTab, onClick: closeCurrentTab,
}); });
@@ -139,7 +137,7 @@
import { setSelectedTab } from '../utility/common'; import { setSelectedTab } from '../utility/common';
import contextMenu from '../utility/contextMenu'; import contextMenu from '../utility/contextMenu';
import getConnectionLabel from '../utility/getConnectionLabel'; import getConnectionLabel from '../utility/getConnectionLabel';
import getElectron from '../utility/getElectron'; import { isElectronAvailable } from '../utility/getElectron';
import { getConnectionInfo, useConnectionList } from '../utility/metadataLoaders'; import { getConnectionInfo, useConnectionList } from '../utility/metadataLoaders';
import { duplicateTab } from '../utility/openNewTab'; import { duplicateTab } from '../utility/openNewTab';
import { useConnectionColorFactory } from '../utility/useConnectionColor'; import { useConnectionColorFactory } from '../utility/useConnectionColor';