dbgate/packages/web/src/stores.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-02-25 20:43:23 +00:00
import { writable, derived } from 'svelte/store';
2021-02-22 17:50:54 +00:00
import { ExtensionsDirectory } from 'dbgate-types';
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;
}
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-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-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-02-20 18:14:22 +00:00