Added onInit and useInitialized hooks
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
|
let _onMount: (() => void) | undefined = undefined;
|
||||||
|
export { _onMount as onMount };
|
||||||
|
|
||||||
|
let _onDestroy: (() => void) | undefined = undefined;
|
||||||
|
export { _onDestroy as onDestroy };
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
_onMount?.();
|
||||||
|
return _onDestroy;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export { default as CallOnMount } from './CallOnMount.svelte';
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { EdgeWrapper } from '$lib/components/EdgeWrapper';
|
import { EdgeWrapper } from '$lib/components/EdgeWrapper';
|
||||||
|
import { CallOnMount } from '$lib/components/CallOnMount';
|
||||||
import { MarkerDefinition } from '$lib/container/EdgeRenderer/MarkerDefinition';
|
import { MarkerDefinition } from '$lib/container/EdgeRenderer/MarkerDefinition';
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
import type { DefaultEdgeOptions } from '$lib/types';
|
import type { DefaultEdgeOptions } from '$lib/types';
|
||||||
@@ -10,6 +11,7 @@
|
|||||||
const {
|
const {
|
||||||
elementsSelectable,
|
elementsSelectable,
|
||||||
visibleEdges,
|
visibleEdges,
|
||||||
|
edgesInitialized,
|
||||||
edges: { setDefaultOptions }
|
edges: { setDefaultOptions }
|
||||||
} = useStore();
|
} = useStore();
|
||||||
|
|
||||||
@@ -29,7 +31,6 @@
|
|||||||
edge.selectable ||
|
edge.selectable ||
|
||||||
($elementsSelectable && typeof edge.selectable === 'undefined')
|
($elementsSelectable && typeof edge.selectable === 'undefined')
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<EdgeWrapper
|
<EdgeWrapper
|
||||||
id={edge.id}
|
id={edge.id}
|
||||||
source={edge.source}
|
source={edge.source}
|
||||||
@@ -61,4 +62,15 @@
|
|||||||
on:edgecontextmenu
|
on:edgecontextmenu
|
||||||
/>
|
/>
|
||||||
{/each}
|
{/each}
|
||||||
|
|
||||||
|
{#if $visibleEdges.length > 0}
|
||||||
|
<CallOnMount
|
||||||
|
onMount={() => {
|
||||||
|
$edgesInitialized = true;
|
||||||
|
}}
|
||||||
|
onDestroy={() => {
|
||||||
|
$edgesInitialized = false;
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -77,6 +77,7 @@
|
|||||||
export let onconnectstart: $$Props['onconnectstart'] = undefined;
|
export let onconnectstart: $$Props['onconnectstart'] = undefined;
|
||||||
export let onconnectend: $$Props['onconnectend'] = undefined;
|
export let onconnectend: $$Props['onconnectend'] = undefined;
|
||||||
export let onbeforedelete: $$Props['onbeforedelete'] = undefined;
|
export let onbeforedelete: $$Props['onbeforedelete'] = undefined;
|
||||||
|
export let oninit: $$Props['oninit'] = undefined;
|
||||||
|
|
||||||
export let defaultMarkerColor = '#b1b1b7';
|
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
|
// this updates the store for simple changes
|
||||||
// where the prop names equals the store name
|
// where the prop names equals the store name
|
||||||
$: {
|
$: {
|
||||||
|
|||||||
@@ -333,4 +333,6 @@ export type SvelteFlowProps = DOMAttributes<HTMLDivElement> & {
|
|||||||
onconnectstart?: OnConnectStart;
|
onconnectstart?: OnConnectStart;
|
||||||
/** When a user stops dragging a connection line, this event gets fired. */
|
/** When a user stops dragging a connection line, this event gets fired. */
|
||||||
onconnectend?: OnConnectEnd;
|
onconnectend?: OnConnectEnd;
|
||||||
|
/** This handler gets called when the flow is finished initializing */
|
||||||
|
oninit?: () => void;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
import zoom from '$lib/actions/zoom';
|
import zoom from '$lib/actions/zoom';
|
||||||
import type { ZoomProps } from './types';
|
import type { ZoomProps } from './types';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
type $$Props = ZoomProps;
|
type $$Props = ZoomProps;
|
||||||
|
|
||||||
@@ -29,12 +30,17 @@
|
|||||||
translateExtent,
|
translateExtent,
|
||||||
lib,
|
lib,
|
||||||
panActivationKeyPressed,
|
panActivationKeyPressed,
|
||||||
zoomActivationKeyPressed
|
zoomActivationKeyPressed,
|
||||||
|
viewportInitialized
|
||||||
} = useStore();
|
} = useStore();
|
||||||
|
|
||||||
$: viewPort = initialViewport || { x: 0, y: 0, zoom: 1 };
|
$: viewPort = initialViewport || { x: 0, y: 0, zoom: 1 };
|
||||||
$: _panOnDrag = $panActivationKeyPressed || panOnDrag;
|
$: _panOnDrag = $panActivationKeyPressed || panOnDrag;
|
||||||
$: _panOnScroll = $panActivationKeyPressed || panOnScroll;
|
$: _panOnScroll = $panActivationKeyPressed || panOnScroll;
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
$viewportInitialized = true;
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { useStore } from '$lib/store';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook for seeing if nodes are initialized
|
||||||
|
* @returns - nodesInitialized Writable
|
||||||
|
*/
|
||||||
|
export function useNodesInitialized() {
|
||||||
|
const { nodesInitialized } = useStore();
|
||||||
|
return nodesInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook for seeing if edges are initialized
|
||||||
|
* @returns - edgesInitialized Writable
|
||||||
|
*/
|
||||||
|
export function useEdgesInitialized() {
|
||||||
|
const { edgesInitialized } = useStore();
|
||||||
|
return edgesInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook for seeing if the flow is initialized
|
||||||
|
* @returns - initialized Writable
|
||||||
|
*/
|
||||||
|
export function useInitialized() {
|
||||||
|
const { initialized } = useStore();
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ export * from '$lib/hooks/useConnection';
|
|||||||
export * from '$lib/hooks/useNodesEdges';
|
export * from '$lib/hooks/useNodesEdges';
|
||||||
export * from '$lib/hooks/useHandleConnections';
|
export * from '$lib/hooks/useHandleConnections';
|
||||||
export * from '$lib/hooks/useNodesData';
|
export * from '$lib/hooks/useNodesData';
|
||||||
|
export * from '$lib/hooks/useInitialized';
|
||||||
|
|
||||||
// types
|
// types
|
||||||
export type {
|
export type {
|
||||||
|
|||||||
@@ -112,6 +112,10 @@ export function createStore({
|
|||||||
}
|
}
|
||||||
|
|
||||||
store.nodes.set(nextNodes);
|
store.nodes.set(nextNodes);
|
||||||
|
|
||||||
|
if (!get(store.nodesInitialized)) {
|
||||||
|
store.nodesInitialized.set(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fitView(nodes: Node[], options?: FitViewOptions) {
|
function fitView(nodes: Node[], options?: FitViewOptions) {
|
||||||
@@ -354,6 +358,33 @@ export function createStore({
|
|||||||
[store.edges, store.defaultMarkerColor, store.flowId],
|
[store.edges, store.defaultMarkerColor, store.flowId],
|
||||||
([edges, defaultColor, id]) => createMarkerIds(edges, { defaultColor, id })
|
([edges, defaultColor, id]) => createMarkerIds(edges, { defaultColor, id })
|
||||||
),
|
),
|
||||||
|
initialized: (() => {
|
||||||
|
console.log('This closure gets called');
|
||||||
|
let initialized = false;
|
||||||
|
const initialNodesLength = get(store.nodes).length;
|
||||||
|
const initialEdgesLength = get(store.edges).length;
|
||||||
|
return derived(
|
||||||
|
[store.nodesInitialized, store.edgesInitialized, store.viewportInitialized],
|
||||||
|
([nodesInitialized, edgesInitialized, viewportInitialized]) => {
|
||||||
|
console.log('Get the derived store even called?');
|
||||||
|
// If it was already initialized once return true from then on
|
||||||
|
if (initialized) return initialized;
|
||||||
|
|
||||||
|
// if it hasn't been initialised check if is now
|
||||||
|
if (initialNodesLength === 0) {
|
||||||
|
initialized = viewportInitialized;
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
|
if (initialEdgesLength === 0) {
|
||||||
|
initialized = viewportInitialized && nodesInitialized;
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialized = viewportInitialized && nodesInitialized && edgesInitialized;
|
||||||
|
return initialized;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
})(),
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
syncNodeStores: (nodes) => syncNodeStores(store.nodes, nodes),
|
syncNodeStores: (nodes) => syncNodeStores(store.nodes, nodes),
|
||||||
|
|||||||
@@ -152,6 +152,10 @@ export const getInitialStore = ({
|
|||||||
onconnect: writable<OnConnect>(undefined),
|
onconnect: writable<OnConnect>(undefined),
|
||||||
onconnectstart: writable<OnConnectStart>(undefined),
|
onconnectstart: writable<OnConnectStart>(undefined),
|
||||||
onconnectend: writable<OnConnectEnd>(undefined),
|
onconnectend: writable<OnConnectEnd>(undefined),
|
||||||
onbeforedelete: writable<OnBeforeDelete>(undefined)
|
onbeforedelete: writable<OnBeforeDelete>(undefined),
|
||||||
|
nodesInitialized: writable<boolean>(false),
|
||||||
|
edgesInitialized: writable<boolean>(false),
|
||||||
|
viewportInitialized: writable<boolean>(false),
|
||||||
|
initialized: readable<boolean>(false)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user