Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

1.6 KB
Raw Blame
import type { Monaco } from '@monaco-editor/react';

export const JSON_EDITOR_THEME = 'signoz-json';

function token(el: HTMLElement, name: string): string {
	return getComputedStyle(el).getPropertyValue(name).trim().replace('#', '');
}

function isDark(hex: string): boolean {
	if (hex.length < 6) {
		return true;
	}
	const r = parseInt(hex.slice(0, 2), 16);
	const g = parseInt(hex.slice(2, 4), 16);
	const b = parseInt(hex.slice(4, 6), 16);
	return 0.299 * r + 0.587 * g + 0.114 * b < 128;
}

export function defineJsonEditorTheme(monaco: Monaco, el: HTMLElement): void {
	const background = token(el, '--l1-background');
	const foreground = token(el, '--l1-foreground');
	const keyColor = token(el, '--bg-vanilla-400');
	const valueColor = token(el, '--bg-robin-400');

	// Both token colours are picked to read on a dark surface, so on a light one
	// they wash out, the keys worst of all. Monaco's own `vs` palette is built
	// for that surface, so let it keep its colours there, as the role viewer does.
	const rules: { token: string; foreground: string }[] = [];
	if (isDark(background)) {
		if (keyColor) {
			rules.push({ token: 'string.key.json', foreground: keyColor });
		}
		if (valueColor) {
			rules.push({ token: 'string.value.json', foreground: valueColor });
		}
	}

	const colors: Record<string, string> = {};
	if (background) {
		colors['editor.background'] = `#${background}`;
	}
	if (foreground) {
		colors['editor.foreground'] = `#${foreground}`;
	}

	monaco.editor.defineTheme(JSON_EDITOR_THEME, {
		base: isDark(background) ? 'vs-dark' : 'vs',
		inherit: true,
		rules,
		colors,
	});
}