move handleEdgeSelection into store, fix hooks

This commit is contained in:
peterkogo
2024-12-18 16:20:37 +01:00
parent 988a0cb7d7
commit 19e233af2d
10 changed files with 66 additions and 63 deletions
@@ -1,30 +0,0 @@
import { errorMessages } from '@xyflow/system';
import { useStore } from '$lib/store';
export function useHandleEdgeSelect() {
const store = useStore();
return (id: string) => {
const edge = store.edgeLookup.get(id);
if (!edge) {
console.warn('012', errorMessages['error012'](id));
return;
}
const selectable =
edge.selectable || (store.elementsSelectable && typeof edge.selectable === 'undefined');
if (selectable) {
store.selectionRect = null;
store.selectionRectMode = null;
if (!edge.selected) {
store.addSelectedEdges([id]);
} else if (edge.selected && store.multiselectionKeyPressed) {
store.unselectNodesAndEdges({ nodes: [], edges: [edge] });
}
}
};
}
@@ -1,15 +1,16 @@
import { useStore } from '$lib/store';
import type { Readable } from 'svelte/store';
/**
* Hook for seeing if nodes are initialized
* @returns - nodesInitialized Writable
*/
export function useNodesInitialized() {
const { nodesInitialized } = useStore();
const { nodesInitialized } = $derived(useStore());
return {
subscribe: nodesInitialized.subscribe
} as Readable<boolean>;
get current() {
return nodesInitialized;
}
};
}
/**
@@ -17,8 +18,10 @@ export function useNodesInitialized() {
* @returns - initialized Writable
*/
export function useInitialized() {
const { initialized } = useStore();
const { initialized } = $derived(useStore());
return {
subscribe: initialized.subscribe
} as Readable<boolean>;
get current() {
return initialized;
}
};
}
@@ -0,0 +1,25 @@
import { useStore } from '$lib/store';
import type { InternalNode } from '$lib/types';
/**
* Hook to get an internal node by id.
*
* @public
* @param id - the node id
* @returns a readable with an internal node or undefined
*/
export function useInternalNode(id: string): { current: InternalNode | undefined } {
const { nodeLookup, nodes } = $derived(useStore());
const node = $derived.by(() => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
nodes;
return nodeLookup.get(id);
});
return {
get current() {
return node;
}
};
}
@@ -1,17 +0,0 @@
import { derived, type Readable } from 'svelte/store';
import { useStore } from '$lib/store';
import type { InternalNode } from '$lib/types';
/**
* Hook to get an internal node by id.
*
* @public
* @param id - the node id
* @returns a readable with an internal node or undefined
*/
export function useInternalNode(id: string): Readable<InternalNode | undefined> {
const { nodeLookup, nodes } = useStore();
return derived([nodeLookup, nodes], ([nodeLookup]) => nodeLookup.get(id));
}