Browse Source

modified observer

master
limina1 1 year ago
parent
commit
614dddf2b1
  1. 32
      src/lib/components/EventNetwork.svelte

32
src/lib/components/EventNetwork.svelte

@ -7,11 +7,12 @@
let svg; let svg;
let isDarkMode = false; let isDarkMode = false;
const width = 1200;
const height = 600;
const nodeRadius = 20; const nodeRadius = 20;
const dragRadius = 45; const dragRadius = 45;
const linkDistance = 120; const linkDistance = 120;
let container: HTMLDivElement;
let width: number;
let height: number;
interface NetworkNode { interface NetworkNode {
id: string; id: string;
event?: NDKEvent; event?: NDKEvent;
@ -309,7 +310,7 @@
isDarkMode = document.body.classList.contains("dark"); isDarkMode = document.body.classList.contains("dark");
// Watch for theme changes // Watch for theme changes
const observer = new MutationObserver((mutations) => { const themeObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => { mutations.forEach((mutation) => {
if (mutation.attributeName === "class") { if (mutation.attributeName === "class") {
const newIsDarkMode = document.body.classList.contains("dark"); const newIsDarkMode = document.body.classList.contains("dark");
@ -321,22 +322,33 @@
}); });
}); });
observer.observe(document.documentElement, { const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
width = entry.contentRect.width;
height = entry.contentRect.height || width * 0.6;
}
if (svg) drawNetwork();
});
// Start observers
themeObserver.observe(document.documentElement, {
attributes: true, attributes: true,
attributeFilter: ["class"], attributeFilter: ["class"],
}); });
resizeObserver.observe(container);
return () => observer.disconnect(); // Clean up
return () => {
themeObserver.disconnect();
resizeObserver.disconnect();
};
}); });
</script> </script>
# /lib/components/EventNetwork.svelte # /lib/components/EventNetwork.svelte
<div class="w-full"> <div class="w-full h-[600px]" bind:this={container}>
<svg <svg
bind:this={svg} bind:this={svg}
class="w-full border border-gray-300 dark:border-gray-700 rounded" class="w-full h-full border border-gray-300 dark:border-gray-700 rounded"
{width}
{height}
/> />
<div <div
class="mt-4 p-4 bg-primary-0 dark:bg-primary-1000 rounded-lg shadow border border-gray-200 dark:border-gray-800" class="mt-4 p-4 bg-primary-0 dark:bg-primary-1000 rounded-lg shadow border border-gray-200 dark:border-gray-800"

Loading…
Cancel
Save