dbgate/packages/web/src/stores.ts

100 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-03-06 17:01:39 +00:00
import { writable, derived, readable } from 'svelte/store';
2021-02-22 17:50:54 +00:00
import { ExtensionsDirectory } from 'dbgate-types';
2021-03-15 18:33:37 +00:00
import invalidateCommands from './commands/invalidateCommands';
2021-03-15 19:48:43 +00:00
import getElectron from './utility/getElectron';
import { GlobalCommand } from './commands/registerCommand';
2021-02-17 19:08:16 +00:00
2021-02-22 16:34:24 +00:00
interface TabDefinition {
title: string;
closedTime?: number;
icon: string;
props: any;
selected: boolean;
busy: boolean;
tabid: string;
2021-03-07 06:57:17 +00:00
tabComponent: string;
2021-02-22 16:34:24 +00:00
}
export function writableWithStorage<T>(defaultValue: T, storageName) {
const init = localStorage.getItem(storageName);
const res = writable<T>(init ? JSON.parse(init) : defaultValue);
res.subscribe(value => {
localStorage.setItem(storageName, JSON.stringify(value));
});
return res;
}
2021-02-26 18:25:35 +00:00
function subscribeCssVariable(store, transform, cssVariable) {
store.subscribe(value => document.documentElement.style.setProperty(cssVariable, transform(value)));
}
2021-02-17 19:08:16 +00:00
export const selectedWidget = writable('database');
2021-02-20 18:14:22 +00:00
export const openedConnections = writable([]);
export const currentDatabase = writable(null);
2021-02-22 16:34:24 +00:00
export const openedTabs = writableWithStorage<TabDefinition[]>([], 'openedTabs');
2021-02-22 17:50:54 +00:00
export const extensions = writable<ExtensionsDirectory>(null);
2021-02-25 17:05:44 +00:00
export const visibleCommandPalette = writable(false);
export const commands = writable({});
export const currentTheme = writableWithStorage('theme-light', 'currentTheme');
2021-02-25 20:43:23 +00:00
export const activeTabId = derived([openedTabs], ([$openedTabs]) => $openedTabs.find(x => x.selected)?.tabid);
2021-03-22 17:31:33 +00:00
export const activeTab = derived([openedTabs], ([$openedTabs]) => $openedTabs.find(x => x.selected));
2021-03-15 18:33:37 +00:00
2021-02-26 18:25:35 +00:00
export const visibleToolbar = writableWithStorage(1, 'visibleToolbar');
2021-02-28 08:27:57 +00:00
export const leftPanelWidth = writable(300);
2021-02-28 09:17:52 +00:00
export const currentDropDownMenu = writable(null);
2021-03-04 09:04:34 +00:00
export const openedModals = writable([]);
2021-03-06 17:01:39 +00:00
export const nullStore = readable(null, () => {});
2021-03-13 08:20:27 +00:00
export const currentArchive = writable('default');
2021-03-13 19:28:06 +00:00
export const isFileDragActive = writable(false);
2021-02-26 18:25:35 +00:00
2021-03-15 19:48:43 +00:00
const electron = getElectron();
2021-02-26 18:25:35 +00:00
subscribeCssVariable(selectedWidget, x => (x ? 1 : 0), '--dim-visible-left-panel');
subscribeCssVariable(visibleToolbar, x => (x ? 1 : 0), '--dim-visible-toolbar');
2021-02-28 08:27:57 +00:00
subscribeCssVariable(leftPanelWidth, x => `${x}px`, '--dim-left-panel-width');
2021-03-15 18:33:37 +00:00
let activeTabIdValue = null;
activeTabId.subscribe(value => {
activeTabIdValue = value;
invalidateCommands();
});
export const getActiveTabId = () => activeTabIdValue;
let visibleCommandPaletteValue = null;
visibleCommandPalette.subscribe(value => {
visibleCommandPaletteValue = value;
invalidateCommands();
});
export const getVisibleCommandPalette = () => visibleCommandPaletteValue;
let visibleToolbarValue = null;
visibleToolbar.subscribe(value => {
visibleToolbarValue = value;
invalidateCommands();
});
export const getVisibleToolbar = () => visibleToolbarValue;
let openedTabsValue = null;
openedTabs.subscribe(value => {
openedTabsValue = value;
invalidateCommands();
});
export const getOpenedTabs = () => openedTabsValue;
2021-03-15 19:48:43 +00:00
let commandsValue = null;
commands.subscribe(value => {
commandsValue = value;
if (electron) {
const { ipcRenderer } = electron;
ipcRenderer.send('update-commands', JSON.stringify(value));
}
});
export const getCommands = () => commandsValue;
2021-03-22 17:31:33 +00:00
let activeTabValue = null;
activeTab.subscribe(value => {
activeTabValue = value;
});
export const getActiveTab = () => activeTabValue;