Added onInit and useInitialized hooks

This commit is contained in:
peterkogo
2024-01-18 12:14:12 +01:00
parent 7c49121c6e
commit 1fb668d936
10 changed files with 113 additions and 3 deletions
@@ -1,6 +1,7 @@
<script lang="ts">
import { onMount } from 'svelte';
import { EdgeWrapper } from '$lib/components/EdgeWrapper';
import { CallOnMount } from '$lib/components/CallOnMount';
import { MarkerDefinition } from '$lib/container/EdgeRenderer/MarkerDefinition';
import { useStore } from '$lib/store';
import type { DefaultEdgeOptions } from '$lib/types';
@@ -10,6 +11,7 @@
const {
elementsSelectable,
visibleEdges,
edgesInitialized,
edges: { setDefaultOptions }
} = useStore();
@@ -29,7 +31,6 @@
edge.selectable ||
($elementsSelectable && typeof edge.selectable === 'undefined')
)}
<EdgeWrapper
id={edge.id}
source={edge.source}
@@ -61,4 +62,15 @@
on:edgecontextmenu
/>
{/each}
{#if $visibleEdges.length > 0}
<CallOnMount
onMount={() => {
$edgesInitialized = true;
}}
onDestroy={() => {
$edgesInitialized = false;
}}
/>
{/if}
</div>
@@ -77,6 +77,7 @@
export let onconnectstart: $$Props['onconnectstart'] = undefined;
export let onconnectend: $$Props['onconnectend'] = undefined;
export let onbeforedelete: $$Props['onbeforedelete'] = undefined;
export let oninit: $$Props['oninit'] = undefined;
export let defaultMarkerColor = '#b1b1b7';
@@ -130,6 +131,16 @@
}
}
// Call oninit once when flow is intialized
const { initialized } = store;
let onInitCalled = false;
$: {
if (!onInitCalled && $initialized) {
oninit?.();
onInitCalled = true;
}
}
// this updates the store for simple changes
// where the prop names equals the store name
$: {
@@ -333,4 +333,6 @@ export type SvelteFlowProps = DOMAttributes<HTMLDivElement> & {
onconnectstart?: OnConnectStart;
/** When a user stops dragging a connection line, this event gets fired. */
onconnectend?: OnConnectEnd;
/** This handler gets called when the flow is finished initializing */
oninit?: () => void;
};
@@ -4,6 +4,7 @@
import { useStore } from '$lib/store';
import zoom from '$lib/actions/zoom';
import type { ZoomProps } from './types';
import { onMount } from 'svelte';
type $$Props = ZoomProps;
@@ -29,12 +30,17 @@
translateExtent,
lib,
panActivationKeyPressed,
zoomActivationKeyPressed
zoomActivationKeyPressed,
viewportInitialized
} = useStore();
$: viewPort = initialViewport || { x: 0, y: 0, zoom: 1 };
$: _panOnDrag = $panActivationKeyPressed || panOnDrag;
$: _panOnScroll = $panActivationKeyPressed || panOnScroll;
onMount(() => {
$viewportInitialized = true;
});
</script>
<div