From 8959c630aeea1b7c091f12e9d4d0c2c65784d623 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 14 Dec 2023 22:39:44 +0100 Subject: [PATCH] move css variable injection to themes + prepare new HeadCommon in extra file --- packages/codemirror/index.mjs | 1 + packages/codemirror/themes.mjs | 39 ++++++++++++++- website/src/components/HeadCommonNew.astro | 58 ++++++++++++++++++++++ website/src/pages/vanilla/index.astro | 4 +- website/src/repl/vanilla/vanilla.css | 4 -- website/src/repl/vanilla/vanilla.mjs | 5 +- 6 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 website/src/components/HeadCommonNew.astro diff --git a/packages/codemirror/index.mjs b/packages/codemirror/index.mjs index c847c32c..8f2d1630 100644 --- a/packages/codemirror/index.mjs +++ b/packages/codemirror/index.mjs @@ -2,3 +2,4 @@ export * from './codemirror.mjs'; export * from './highlight.mjs'; export * from './flash.mjs'; export * from './slider.mjs'; +export * from './themes.mjs'; diff --git a/packages/codemirror/themes.mjs b/packages/codemirror/themes.mjs index 1f520623..71fb7642 100644 --- a/packages/codemirror/themes.mjs +++ b/packages/codemirror/themes.mjs @@ -473,6 +473,9 @@ function stringifySafe(json) { return JSON.stringify(json, getCircularReplacer()); } +export const theme = (theme) => themes[theme] || themes.strudelTheme; + +// css style injection helpers export function injectStyle(rule) { const newStyle = document.createElement('style'); document.head.appendChild(newStyle); @@ -481,4 +484,38 @@ export function injectStyle(rule) { return () => styleSheet.deleteRule(ruleIndex); } -export const theme = (theme) => themes[theme] || themes.strudelTheme; +let currentTheme, resetThemeStyle, themeStyle; +export function initTheme(theme) { + themeStyle = document.createElement('style'); + themeStyle.id = 'strudel-theme'; + document.head.append(themeStyle); + activateTheme(theme); +} + +export function activateTheme(name) { + if (currentTheme === name) { + return; + } + if (!settings[name]) { + console.warn('theme', name, 'has no settings.. defaulting to strudelTheme settings'); + } + const themeSettings = settings[name] || settings.strudelTheme; + // set css variables + themeStyle.innerHTML = `:root { + ${Object.entries(themeSettings) + // important to override fallback + .map(([key, value]) => `--${key}: ${value} !important;`) + .join('\n')} + }`; + // tailwind dark mode + if (themeSettings.light) { + document.documentElement.classList.remove('dark'); + } else { + document.documentElement.classList.add('dark'); + } + resetThemeStyle?.(); + resetThemeStyle = undefined; + if (themeSettings.customStyle) { + resetThemeStyle = injectStyle(themeSettings.customStyle); + } +} diff --git a/website/src/components/HeadCommonNew.astro b/website/src/components/HeadCommonNew.astro new file mode 100644 index 00000000..9f323a7a --- /dev/null +++ b/website/src/components/HeadCommonNew.astro @@ -0,0 +1,58 @@ +--- +import { pwaInfo } from 'virtual:pwa-info'; +import '../styles/index.css'; + +const { BASE_URL } = import.meta.env; +const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL; +--- + + + + + + + + + + + + + + + + + + + + + + + +{pwaInfo && } + + diff --git a/website/src/pages/vanilla/index.astro b/website/src/pages/vanilla/index.astro index 5377c08f..de138380 100644 --- a/website/src/pages/vanilla/index.astro +++ b/website/src/pages/vanilla/index.astro @@ -1,10 +1,10 @@ --- -import HeadCommon from '../../components/HeadCommon.astro'; +import HeadCommonNew from '../../components/HeadCommonNew.astro'; --- - + Strudel Vanilla REPL diff --git a/website/src/repl/vanilla/vanilla.css b/website/src/repl/vanilla/vanilla.css index 7c985be7..584dd924 100644 --- a/website/src/repl/vanilla/vanilla.css +++ b/website/src/repl/vanilla/vanilla.css @@ -1,7 +1,3 @@ -:root { - --foreground: white; -} - body, input { font-family: monospace; diff --git a/website/src/repl/vanilla/vanilla.mjs b/website/src/repl/vanilla/vanilla.mjs index 6e5c54a3..488fcf8c 100644 --- a/website/src/repl/vanilla/vanilla.mjs +++ b/website/src/repl/vanilla/vanilla.mjs @@ -1,5 +1,5 @@ import { logger, getDrawContext, silence, controls, evalScope, hash2code, code2hash } from '@strudel.cycles/core'; -import { StrudelMirror } from '@strudel/codemirror'; +import { StrudelMirror, initTheme, activateTheme } from '@strudel/codemirror'; import { transpiler } from '@strudel.cycles/transpiler'; import { getAudioContext, @@ -24,6 +24,7 @@ const initialSettings = { fontFamily: 'monospace', fontSize: 18, }; +initTheme(initialSettings.theme); async function run() { const container = document.getElementById('code'); @@ -196,4 +197,6 @@ form.addEventListener('change', () => { const values = getFormValues(form, initialSettings); // console.log('values', values); editor.updateSettings(values); + // TODO: only activateTheme when it changes + activateTheme(values.theme); });