Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions sophora-components/src/lib/utils/getDataFromUrl.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
export default function getDataFromUrl(target: HTMLElement): Record<string, string> {
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<string, string> = 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), {});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
</script>

Expand Down Expand Up @@ -47,6 +57,15 @@
</div>
{/each}
</div>

<h3>Debug Information:</h3>
<p>actual url: {url || 'n/a'}</p>
<p>data-url: {root?.parentNode?.parentNode?.dataset.url || 'n/a'}</p>
<br />
<pre>{JSON.stringify({ labels, ids, fixedHeight, layout }, null, 2)}</pre>
{#if error}
<pre style="color: red">{error}</pre>
{/if}
</DesignTokens>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
</style>
Loading