diff --git a/sophora-components/src/lib/utils/getDataFromUrl.ts b/sophora-components/src/lib/utils/getDataFromUrl.ts index 9448278c..ac1fe0fa 100644 --- a/sophora-components/src/lib/utils/getDataFromUrl.ts +++ b/sophora-components/src/lib/utils/getDataFromUrl.ts @@ -1,21 +1,16 @@ export default function getDataFromUrl(target: HTMLElement): Record { - let url: URL; - if ( - // SvelteKit DEV mode, preview server, or static hosting: - import.meta.env.DEV || - window.location.origin === 'http://localhost:4173' || - window.location.origin === 'https://static.datenhub.net' || - window.location.href.includes('apidata.googleusercontent.com') || - window.location.href.includes('storage.googleapis.com') - ) { + const parent = target.parentNode?.parentNode as HTMLElement | null; + + // Default: Embedded mode – use URL used to embed the component + // `data-url` is the embeds the grandparent element, provided by Sophora + let embedURL = parent?.dataset.url; + + if (!embedURL) { // Preview mode – use URL of current page - url = new URL(window.location.href); - } else { - // Embedded mode – use URL used to embed the component - // `data-url` is set on the grandparent element, provided by Sophora - const parent = target.parentNode?.parentNode as HTMLElement | null; - url = new URL(parent?.dataset.url || ''); + embedURL = window?.location.href; } - const params: Record = Object.fromEntries(url.searchParams); - return params; + + return URL.canParse(embedURL) + ? Object.fromEntries(new URL(embedURL).searchParams.entries()) + : (console.error('Could not parse Embed-URL:', embedURL), {}); } diff --git a/sophora-components/src/routes/datawrapper-switcher/DatawrapperSwitcher.svelte b/sophora-components/src/routes/datawrapper-switcher/DatawrapperSwitcher.svelte index 0732296f..85dcc32c 100644 --- a/sophora-components/src/routes/datawrapper-switcher/DatawrapperSwitcher.svelte +++ b/sophora-components/src/routes/datawrapper-switcher/DatawrapperSwitcher.svelte @@ -10,12 +10,22 @@ let fixedHeight = $state(null); let layout = $state('auto'); + let url = $state(''); + let error = $state(null); + onMount(() => { - const entries = getDataFromUrl(root); - labels = entries.labels.split(',') || []; - ids = entries.ids.split(',') || []; - fixedHeight = entries.fixedHeight || null; - layout = entries.layout || 'auto'; + url = window?.location.href; + + try { + const entries = getDataFromUrl(root); + labels = entries.labels?.split(',') || []; + ids = entries.ids?.split(',') || []; + fixedHeight = entries.fixedHeight || null; + layout = entries.layout || 'auto'; + } catch (e) { + console.error(e); + error = e; + } }); @@ -47,6 +57,15 @@ {/each} + +

Debug Information:

+

actual url: {url || 'n/a'}

+

data-url: {root?.parentNode?.parentNode?.dataset.url || 'n/a'}

+
+
{JSON.stringify({ labels, ids, fixedHeight, layout }, null, 2)}
+ {#if error} +
{error}
+ {/if} diff --git a/sophora-components/src/routes/highlight-cards/HighlightCards.svelte b/sophora-components/src/routes/highlight-cards/HighlightCards.svelte index bc6657de..f1ec2683 100644 --- a/sophora-components/src/routes/highlight-cards/HighlightCards.svelte +++ b/sophora-components/src/routes/highlight-cards/HighlightCards.svelte @@ -35,5 +35,10 @@ @media (min-width: 1200px) { grid-template-columns: repeat(2, minmax(0, 1fr)); } + + /* Explicitly set background color for dark mode, workaround for SWR Aktuell App */ + @media (prefers-color-scheme: dark) { + background-color: #0c0c0c; + } }