feat(svelte): reset store when component unmounts
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { setContext } from 'svelte';
|
import { onDestroy, setContext } from 'svelte';
|
||||||
|
|
||||||
import { createStore, key } from '$lib/store';
|
import { createStore, key } from '$lib/store';
|
||||||
import type { SvelteFlowProviderProps } from './types';
|
import type { SvelteFlowProviderProps } from './types';
|
||||||
@@ -17,6 +17,10 @@
|
|||||||
setContext(key, {
|
setContext(key, {
|
||||||
getStore: () => store
|
getStore: () => store
|
||||||
});
|
});
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
store.reset();
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<slot />
|
<slot />
|
||||||
|
|||||||
@@ -41,6 +41,10 @@
|
|||||||
store.width.set(width);
|
store.width.set(width);
|
||||||
store.height.set(height);
|
store.height.set(height);
|
||||||
store.domNode.set(domNode);
|
store.domNode.set(domNode);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
store.reset();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
|
|||||||
@@ -287,6 +287,15 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
|||||||
updateConnection(initConnectionData);
|
updateConnection(initConnectionData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
store.fitViewOnInitDone.set(false);
|
||||||
|
store.selectionRect.set(null);
|
||||||
|
store.selectionRectMode.set(null);
|
||||||
|
|
||||||
|
resetSelectedElements();
|
||||||
|
cancelConnection();
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// state
|
// state
|
||||||
...store,
|
...store,
|
||||||
@@ -310,7 +319,8 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
|||||||
addSelectedNodes,
|
addSelectedNodes,
|
||||||
panBy,
|
panBy,
|
||||||
updateConnection,
|
updateConnection,
|
||||||
cancelConnection
|
cancelConnection,
|
||||||
|
reset
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ export type SvelteFlowStoreActions = {
|
|||||||
panBy: (delta: XYPosition) => void;
|
panBy: (delta: XYPosition) => void;
|
||||||
updateConnection: (connection: Partial<ConnectionData>) => void;
|
updateConnection: (connection: Partial<ConnectionData>) => void;
|
||||||
cancelConnection: () => void;
|
cancelConnection: () => void;
|
||||||
|
reset(): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SvelteFlowStoreState = typeof initialStoreState & {
|
export type SvelteFlowStoreState = typeof initialStoreState & {
|
||||||
|
|||||||
Reference in New Issue
Block a user