database & connection color overrides

This commit is contained in:
Jan Prochazka
2021-12-05 11:08:17 +01:00
parent 5ca54220b5
commit 50dbb9d1bd
9 changed files with 127 additions and 30 deletions

View File

@@ -3,35 +3,76 @@ import { derived } from 'svelte/store';
import { currentThemeDefinition } from '../stores';
import { useConnectionList } from '../utility/metadataLoaders';
export function getConnectionColor(connections, dbid, themeType, colorIndex, backgroundStyle = false) {
export function getConnectionColor(
connections,
dbid,
themeType,
colorIndex,
backgroundStyle = false,
useConnectionFallback = true
) {
if (!dbid || !connections) return undefined;
const current = connections.find(x => x._id == dbid.conid);
if (!current?.connectionColor) return undefined;
const { database } = dbid;
let colorName = useConnectionFallback || !database ? current?.connectionColor : null;
if (
database &&
current.databaseConfig &&
current.databaseConfig[database] &&
current.databaseConfig[database].databaseColor
) {
colorName = current.databaseConfig[database].databaseColor;
}
if (!colorName) return undefined;
const palettes = themeType == 'dark' ? presetDarkPalettes : presetPalettes;
if (colorIndex == null) return current?.connectionColor;
const color = palettes[current?.connectionColor][colorIndex];
if (colorIndex == null) return colorName;
const color = palettes[colorName][colorIndex];
if (backgroundStyle) return `background:${color}`;
return color;
}
export function useConnectionColor(dbid, colorIndex, themeType = null, backgroundStyle = false) {
export function useConnectionColor(
dbid,
colorIndex,
themeType = null,
backgroundStyle = false,
useConnectionFallback = true
) {
const connections = useConnectionList();
return derived([connections, currentThemeDefinition], ([$connections, $themeDef]) =>
getConnectionColor($connections, dbid, themeType ?? $themeDef?.themeType, colorIndex, backgroundStyle)
getConnectionColor(
$connections,
dbid,
themeType ?? $themeDef?.themeType,
colorIndex,
backgroundStyle,
useConnectionFallback
)
);
}
export function useConnectionColorFactory(colorIndex, themeType = null, backgroundStyle = false) {
export function useConnectionColorFactory(
colorIndex,
themeType = null,
backgroundStyle = false,
useConnectionFallback = true
) {
const connections = useConnectionList();
return derived(
[connections, currentThemeDefinition],
([$connections, $themeDef]) => (dbid, colorIndexOverride = null) =>
([$connections, $themeDef]) => (
dbid,
colorIndexOverride = null,
backgroundStyleOverride = null,
useConnectionFallbackOverride = null
) =>
getConnectionColor(
$connections,
dbid,
themeType ?? $themeDef?.themeType,
colorIndexOverride || colorIndex,
backgroundStyle
colorIndexOverride ?? colorIndex,
backgroundStyleOverride ?? backgroundStyle,
useConnectionFallbackOverride ?? useConnectionFallback
)
);
}