refactor(core): remove primtive typeguards

This commit is contained in:
braks
2023-10-30 13:36:07 +01:00
committed by Braks
parent 785f147236
commit 3b175a553e
10 changed files with 35 additions and 54 deletions
@@ -1,7 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import { ref, useAttrs } from 'vue' import { ref, useAttrs } from 'vue'
import type { BaseEdgeProps } from '../../types' import type { BaseEdgeProps } from '../../types'
import { isNumber } from '../../utils'
import EdgeText from './EdgeText.vue' import EdgeText from './EdgeText.vue'
const { interactionWidth = 20, labelShowBg = true, ...props } = defineProps<BaseEdgeProps>() const { interactionWidth = 20, labelShowBg = true, ...props } = defineProps<BaseEdgeProps>()
@@ -52,7 +51,7 @@ export default {
/> />
<EdgeText <EdgeText
v-if="label && isNumber(labelX) && isNumber(labelY)" v-if="label && labelX && labelY"
ref="labelEl" ref="labelEl"
:x="labelX" :x="labelX"
:y="labelY" :y="labelY"
@@ -2,7 +2,6 @@
import { computed, onMounted, ref, watch } from 'vue' import { computed, onMounted, ref, watch } from 'vue'
import type { EdgeTextProps } from '../../types/components' import type { EdgeTextProps } from '../../types/components'
import type { Rect as RectType } from '../../types' import type { Rect as RectType } from '../../types'
import { isString } from '../../utils'
const { const {
x, x,
@@ -61,7 +60,7 @@ export default {
<text v-bind="$attrs" ref="el" class="vue-flow__edge-text" :y="box.height / 2" dy="0.3em" :style="labelStyle"> <text v-bind="$attrs" ref="el" class="vue-flow__edge-text" :y="box.height / 2" dy="0.3em" :style="labelStyle">
<slot> <slot>
<component :is="label as any" v-if="!isString(label)" /> <component :is="label" v-if="typeof label !== 'string'" />
<template v-else> <template v-else>
{{ label }} {{ label }}
</template> </template>
@@ -4,7 +4,7 @@ import { computed, onUnmounted, ref } from 'vue'
import type { HandleProps } from '../../types/handle' import type { HandleProps } from '../../types/handle'
import { Position } from '../../types' import { Position } from '../../types'
import { useHandle, useNode, useVueFlow } from '../../composables' import { useHandle, useNode, useVueFlow } from '../../composables'
import { getDimensions, isDef, isFunction, isMouseEvent, isNumber, isString } from '../../utils' import { getDimensions, isDef, isMouseEvent } from '../../utils'
const { const {
position = Position.Top, position = Position.Top,
@@ -47,7 +47,7 @@ const { handlePointerDown, handleClick } = useHandle({
}) })
const isConnectable = computed(() => { const isConnectable = computed(() => {
if (isString(connectable) && connectable === 'single') { if (typeof connectable === 'string' && connectable === 'single') {
return !connectedEdges.value.some((edge) => { return !connectedEdges.value.some((edge) => {
const id = edge[`${type.value}Handle`] const id = edge[`${type.value}Handle`]
@@ -59,7 +59,7 @@ const isConnectable = computed(() => {
}) })
} }
if (isNumber(connectable)) { if (typeof connectable === 'number') {
return ( return (
connectedEdges.value.filter((edge) => { connectedEdges.value.filter((edge) => {
const id = edge[`${type.value}Handle`] const id = edge[`${type.value}Handle`]
@@ -73,14 +73,14 @@ const isConnectable = computed(() => {
) )
} }
if (isFunction(connectable)) { if (typeof connectable === 'function') {
return connectable(node, connectedEdges.value) return connectable(node, connectedEdges.value)
} }
return isDef(connectable) ? connectable : nodesConnectable.value return isDef(connectable) ? connectable : nodesConnectable.value
}) })
const isConnecting = computed( const isConnecting = toRef(
() => () =>
(connectionStartHandle.value?.nodeId === nodeId && (connectionStartHandle.value?.nodeId === nodeId &&
connectionStartHandle.value?.handleId === handleId.value && connectionStartHandle.value?.handleId === handleId.value &&
@@ -90,7 +90,7 @@ const isConnecting = computed(
connectionEndHandle.value?.type === type.value), connectionEndHandle.value?.type === type.value),
) )
const isClickConnecting = computed( const isClickConnecting = toRef(
() => () =>
connectionClickStartHandle.value?.nodeId === nodeId && connectionClickStartHandle.value?.nodeId === nodeId &&
connectionClickStartHandle.value?.handleId === handleId.value && connectionClickStartHandle.value?.handleId === handleId.value &&
@@ -11,7 +11,6 @@ import {
getConnectedEdges, getConnectedEdges,
getXYZPos, getXYZPos,
handleNodeClick, handleNodeClick,
isNumber,
} from '~/utils' } from '~/utils'
interface Props { interface Props {
@@ -153,7 +152,7 @@ const NodeWrapper = defineComponent({
z: nodeZIndex + (elevateNodesOnSelect.value ? (node.value.selected ? 1000 : 0) : 0), z: nodeZIndex + (elevateNodesOnSelect.value ? (node.value.selected ? 1000 : 0) : 0),
} }
if (isNumber(parentX) && isNumber(parentY)) { if (typeof parentX !== 'undefined' && typeof parentY !== 'undefined') {
node.value.computedPosition = getXYZPos({ x: parentX, y: parentY, z: parentZ! }, xyzPos) node.value.computedPosition = getXYZPos({ x: parentX, y: parentY, z: parentZ! }, xyzPos)
} else { } else {
node.value.computedPosition = xyzPos node.value.computedPosition = xyzPos
@@ -3,7 +3,7 @@ import { effectScope, nextTick, onScopeDispose, watch } from 'vue'
import type { WatchPausableReturn } from '@vueuse/core' import type { WatchPausableReturn } from '@vueuse/core'
import { toRef, watchPausable } from '@vueuse/core' import { toRef, watchPausable } from '@vueuse/core'
import type { Connection, FlowProps, VueFlowStore } from '~/types' import type { Connection, FlowProps, VueFlowStore } from '~/types'
import { isDef, isFunction } from '~/utils' import { isDef } from '~/utils'
export function useWatchProps( export function useWatchProps(
models: ToRefs<Pick<FlowProps, 'nodes' | 'edges' | 'modelValue'>>, models: ToRefs<Pick<FlowProps, 'nodes' | 'edges' | 'modelValue'>>,
@@ -221,7 +221,7 @@ export function useWatchProps(
const autoConnector = async (params: Connection) => { const autoConnector = async (params: Connection) => {
let connection: boolean | Connection = params let connection: boolean | Connection = params
if (isFunction(props.autoConnect)) { if (typeof props.autoConnect === 'function') {
connection = await props.autoConnect(params) connection = await props.autoConnect(params)
} }
+7 -7
View File
@@ -1,5 +1,5 @@
import { nextTick } from 'vue' import { nextTick } from 'vue'
import { isDef, isFunction, isGraphNode, isString } from '.' import { isDef, isGraphNode } from '.'
import type { import type {
EdgeAddChange, EdgeAddChange,
EdgeChange, EdgeChange,
@@ -26,7 +26,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
if (extendWidth > 0 || extendHeight > 0 || updateItem.position.x < 0 || updateItem.position.y < 0) { if (extendWidth > 0 || extendHeight > 0 || updateItem.position.x < 0 || updateItem.position.y < 0) {
let parentStyles: Styles = {} let parentStyles: Styles = {}
if (isFunction(parent.style)) { if (typeof parent.style === 'function') {
parentStyles = { ...parent.style(parent) } parentStyles = { ...parent.style(parent) }
} else if (parent.style) { } else if (parent.style) {
parentStyles = { ...parent.style } parentStyles = { ...parent.style }
@@ -36,7 +36,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
parentStyles.height = parentStyles.height ?? `${parent.dimensions.height}px` parentStyles.height = parentStyles.height ?? `${parent.dimensions.height}px`
if (extendWidth > 0) { if (extendWidth > 0) {
if (isString(parentStyles.width)) { if (typeof parentStyles.width === 'string') {
const currWidth = Number(parentStyles.width.replace('px', '')) const currWidth = Number(parentStyles.width.replace('px', ''))
parentStyles.width = `${currWidth + extendWidth}px` parentStyles.width = `${currWidth + extendWidth}px`
} else { } else {
@@ -45,7 +45,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
} }
if (extendHeight > 0) { if (extendHeight > 0) {
if (isString(parentStyles.height)) { if (typeof parentStyles.height === 'string') {
const currWidth = Number(parentStyles.height.replace('px', '')) const currWidth = Number(parentStyles.height.replace('px', ''))
parentStyles.height = `${currWidth + extendHeight}px` parentStyles.height = `${currWidth + extendHeight}px`
} else { } else {
@@ -57,7 +57,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
const xDiff = Math.abs(updateItem.position.x) const xDiff = Math.abs(updateItem.position.x)
parent.position.x = parent.position.x - xDiff parent.position.x = parent.position.x - xDiff
if (isString(parentStyles.width)) { if (typeof parentStyles.width === 'string') {
const currWidth = Number(parentStyles.width.replace('px', '')) const currWidth = Number(parentStyles.width.replace('px', ''))
parentStyles.width = `${currWidth + xDiff}px` parentStyles.width = `${currWidth + xDiff}px`
} else { } else {
@@ -71,7 +71,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
const yDiff = Math.abs(updateItem.position.y) const yDiff = Math.abs(updateItem.position.y)
parent.position.y = parent.position.y - yDiff parent.position.y = parent.position.y - yDiff
if (isString(parentStyles.height)) { if (typeof parentStyles.height === 'string') {
const currWidth = Number(parentStyles.height.replace('px', '')) const currWidth = Number(parentStyles.height.replace('px', ''))
parentStyles.height = `${currWidth + yDiff}px` parentStyles.height = `${currWidth + yDiff}px`
} else { } else {
@@ -84,7 +84,7 @@ function handleParentExpand(updateItem: GraphNode, parent: GraphNode) {
parent.dimensions.width = Number(parentStyles.width.toString().replace('px', '')) parent.dimensions.width = Number(parentStyles.width.toString().replace('px', ''))
parent.dimensions.height = Number(parentStyles.height.toString().replace('px', '')) parent.dimensions.height = Number(parentStyles.height.toString().replace('px', ''))
if (isFunction(parent.style)) { if (typeof parent.style === 'function') {
parent.style = (p) => { parent.style = (p) => {
const styleFunc = parent.style as StyleFunc const styleFunc = parent.style as StyleFunc
+5 -5
View File
@@ -1,5 +1,5 @@
import { markRaw } from 'vue' import { markRaw } from 'vue'
import { ErrorCode, VueFlowError, clampPosition, isNumber, isParentSelected } from '.' import { ErrorCode, VueFlowError, clampPosition, isParentSelected } from '.'
import type { import type {
Actions, Actions,
CoordinateExtent, CoordinateExtent,
@@ -108,10 +108,10 @@ function getParentExtent(
if ( if (
parent && parent &&
isNumber(parent.computedPosition.x) && typeof parent.computedPosition.x !== 'undefined' &&
isNumber(parent.computedPosition.y) && typeof parent.computedPosition.y !== 'undefined' &&
isNumber(parent.dimensions.width) && typeof parent.dimensions.width !== 'undefined' &&
isNumber(parent.dimensions.height) typeof parent.dimensions.height !== 'undefined'
) { ) {
return [ return [
[parent.computedPosition.x + left, parent.computedPosition.y + top], [parent.computedPosition.x + left, parent.computedPosition.y + top],
+2 -2
View File
@@ -1,4 +1,4 @@
import { isNumber, rectToBox } from '.' import { rectToBox } from '.'
import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, Rect, ViewportTransform, XYPosition } from '~/types' import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, Rect, ViewportTransform, XYPosition } from '~/types'
import { Position } from '~/types' import { Position } from '~/types'
@@ -132,7 +132,7 @@ export function isEdgeVisible({
} }
export function getEdgeZIndex(edge: GraphEdge, findNode: Actions['findNode'], elevateEdgesOnSelect = false) { export function getEdgeZIndex(edge: GraphEdge, findNode: Actions['findNode'], elevateEdgesOnSelect = false) {
const hasZIndex = isNumber(edge.zIndex) const hasZIndex = typeof edge.zIndex === 'number'
let z = hasZIndex ? edge.zIndex! : 0 let z = hasZIndex ? edge.zIndex! : 0
const source = findNode(edge.source) const source = findNode(edge.source)
-16
View File
@@ -13,20 +13,4 @@ export function getEventPosition(event: MouseEvent | TouchEvent, bounds?: DOMRec
} }
} }
export function isString(val: any): val is string {
return typeof val === 'string'
}
export function isFunction(val: any): val is Function {
return typeof val === 'function'
}
export function isBoolean(val: any): val is boolean {
return typeof val === 'boolean'
}
export function isNumber(val: any): val is number {
return typeof val === 'number'
}
export const isMacOs = () => typeof navigator !== 'undefined' && navigator?.userAgent?.indexOf('Mac') >= 0 export const isMacOs = () => typeof navigator !== 'undefined' && navigator?.userAgent?.indexOf('Mac') >= 0
+10 -10
View File
@@ -1,5 +1,5 @@
import { markRaw } from 'vue' import { markRaw } from 'vue'
import { isDef, isString, warn } from '.' import { isDef, warn } from '.'
import type { import type {
Actions, Actions,
Box, Box,
@@ -145,7 +145,7 @@ export function parseEdge(edge: Edge, defaults: Partial<GraphEdge> = {}): GraphE
focusable: edge.focusable ?? defaults.focusable, focusable: edge.focusable ?? defaults.focusable,
data, data,
events: markRaw(events), events: markRaw(events),
label: (edge.label && !isString(edge.label) ? markRaw(edge.label) : edge.label) || defaults.label, label: (edge.label && typeof edge.label !== 'string' ? markRaw(edge.label) : edge.label) || defaults.label,
interactionWidth: edge.interactionWidth || defaults.interactionWidth, interactionWidth: edge.interactionWidth || defaults.interactionWidth,
} as GraphEdge) } as GraphEdge)
: defaults : defaults
@@ -159,7 +159,7 @@ function getConnectedElements<T extends Node = Node>(
edges: Edge[], edges: Edge[],
dir: 'source' | 'target', dir: 'source' | 'target',
): T[] { ): T[] {
const id = isString(nodeOrId) ? nodeOrId : nodeOrId.id const id = typeof nodeOrId === 'string' ? nodeOrId : nodeOrId.id
const connectedIds = new Set() const connectedIds = new Set()
@@ -186,9 +186,9 @@ export function getOutgoers(...args: any[]) {
} }
const [nodeOrId, elements] = args const [nodeOrId, elements] = args
const node: Node = isString(nodeOrId) ? { id: nodeOrId } : nodeOrId const nodeId = typeof nodeOrId === 'string' ? nodeOrId : nodeOrId.id
const outgoers = elements.filter((el: Element) => isEdge(el) && el.source === node.id) const outgoers = elements.filter((el: Element) => isEdge(el) && el.source === nodeId)
return outgoers.map((edge: Edge) => elements.find((el: Element) => isNode(el) && el.id === edge.target)) return outgoers.map((edge: Edge) => elements.find((el: Element) => isNode(el) && el.id === edge.target))
} }
@@ -205,9 +205,9 @@ export function getIncomers(...args: any[]) {
} }
const [nodeOrId, elements] = args const [nodeOrId, elements] = args
const node: Node = isString(nodeOrId) ? { id: nodeOrId } : nodeOrId const nodeId = typeof nodeOrId === 'string' ? nodeOrId : nodeOrId.id
const incomers = elements.filter((el: Element) => isEdge(el) && el.target === node.id) const incomers = elements.filter((el: Element) => isEdge(el) && el.target === nodeId)
return incomers.map((edge: Edge) => elements.find((el: Element) => isNode(el) && el.id === edge.source)) return incomers.map((edge: Edge) => elements.find((el: Element) => isNode(el) && el.id === edge.source))
} }
@@ -408,7 +408,7 @@ export function getNodesInside(
export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, edges: E[]) { export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, edges: E[]) {
const nodeIds = new Set() const nodeIds = new Set()
if (isString(nodesOrId)) { if (typeof nodesOrId === 'string') {
nodeIds.add(nodesOrId) nodeIds.add(nodesOrId)
} else if (nodesOrId.length >= 1) { } else if (nodesOrId.length >= 1) {
nodesOrId.forEach((n) => nodeIds.add(n.id)) nodesOrId.forEach((n) => nodeIds.add(n.id))
@@ -420,7 +420,7 @@ export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, ed
export function getConnectedNodes<N extends Node | { id: string } | string>(nodes: N[], edges: Edge[]) { export function getConnectedNodes<N extends Node | { id: string } | string>(nodes: N[], edges: Edge[]) {
const nodeIds = new Set() const nodeIds = new Set()
nodes.forEach((node) => nodeIds.add(isString(node) ? node : node.id)) nodes.forEach((node) => nodeIds.add(typeof node === 'string' ? node : node.id))
const connectedNodeIds = edges.reduce((acc, edge) => { const connectedNodeIds = edges.reduce((acc, edge) => {
if (nodeIds.has(edge.source)) { if (nodeIds.has(edge.source)) {
@@ -434,7 +434,7 @@ export function getConnectedNodes<N extends Node | { id: string } | string>(node
return acc return acc
}, new Set()) }, new Set())
return nodes.filter((node) => connectedNodeIds.has(isString(node) ? node : node.id)) return nodes.filter((node) => connectedNodeIds.has(typeof node === 'string' ? node : node.id))
} }
export function getTransformForBounds( export function getTransformForBounds(