chore(core): cleanup getters

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
braks
2023-03-24 21:31:24 +01:00
committed by Braks
parent d59cc78496
commit 0cdff91322
+19 -6
View File
@@ -6,37 +6,50 @@ export function useGetters(state: State): ComputedGetters {
const edgeIds = computed(() => state.edges.map((e) => e.id)) const edgeIds = computed(() => state.edges.map((e) => e.id))
const getNode: ComputedGetters['getNode'] = computed(() => (id: string) => { const getNode: ComputedGetters['getNode'] = computed(() => (id: string) => {
if (state.nodes && !nodeIds.value.length) return state.nodes.find((node) => node.id === id) if (state.nodes && !nodeIds.value.length) {
return state.nodes.find((node) => node.id === id)
}
return state.nodes[nodeIds.value.indexOf(id)] return state.nodes[nodeIds.value.indexOf(id)]
}) })
const getEdge: ComputedGetters['getEdge'] = computed(() => (id: string) => { const getEdge: ComputedGetters['getEdge'] = computed(() => (id: string) => {
if (state.edges && !edgeIds.value.length) return state.edges.find((edge) => edge.id === id) if (state.edges && !edgeIds.value.length) {
return state.edges.find((edge) => edge.id === id)
}
return state.edges[edgeIds.value.indexOf(id)] return state.edges[edgeIds.value.indexOf(id)]
}) })
const getEdgeTypes = computed(() => { const getEdgeTypes: ComputedGetters['getEdgeTypes'] = computed(() => {
const edgeTypes: Record<string, any> = { const edgeTypes: Record<string, any> = {
...defaultEdgeTypes, ...defaultEdgeTypes,
...state.edgeTypes, ...state.edgeTypes,
} }
const keys = Object.keys(edgeTypes) const keys = Object.keys(edgeTypes)
state.edges?.forEach((e) => e.type && !keys.includes(e.type) && (edgeTypes[e.type] = e.type)) state.edges?.forEach((e) => e.type && !keys.includes(e.type) && (edgeTypes[e.type] = e.type))
return edgeTypes return edgeTypes
}) })
const getNodeTypes = computed(() => { const getNodeTypes: ComputedGetters['getNodeTypes'] = computed(() => {
const nodeTypes: Record<string, any> = { const nodeTypes: Record<string, any> = {
...defaultNodeTypes, ...defaultNodeTypes,
...state.nodeTypes, ...state.nodeTypes,
} }
const keys = Object.keys(nodeTypes) const keys = Object.keys(nodeTypes)
state.nodes?.forEach((n) => n.type && !keys.includes(n.type) && (nodeTypes[n.type] = n.type)) state.nodes?.forEach((n) => n.type && !keys.includes(n.type) && (nodeTypes[n.type] = n.type))
return nodeTypes return nodeTypes
}) })
const getNodes = computed<GraphNode[]>(() => { const getNodes: ComputedGetters['getNodes'] = computed(() => {
const nodes = state.nodes.filter((n) => !n.hidden) const nodes = state.nodes.filter((n) => !n.hidden)
return state.onlyRenderVisibleElements return state.onlyRenderVisibleElements
? nodes && ? nodes &&
getNodesInside( getNodesInside(
@@ -67,7 +80,7 @@ export function useGetters(state: State): ComputedGetters {
return !e.hidden && !target.hidden && !source.hidden return !e.hidden && !target.hidden && !source.hidden
} }
const getEdges = computed<GraphEdge[]>(() => { const getEdges: ComputedGetters['getEdges'] = computed(() => {
if (!state.onlyRenderVisibleElements) return state.edges.filter((edge) => edgeHidden(edge)) if (!state.onlyRenderVisibleElements) return state.edges.filter((edge) => edgeHidden(edge))
return state.edges.filter((e) => { return state.edges.filter((e) => {