refactor: use reactivity transform in edge-texts

This commit is contained in:
Braks
2022-05-27 23:36:01 +02:00
parent 0d17d00644
commit 0a2a467e56
2 changed files with 28 additions and 29 deletions
@@ -2,22 +2,21 @@
import type { EdgeTextProps } from '../../types/components' import type { EdgeTextProps } from '../../types/components'
import type { Rect } from '../../types' import type { Rect } from '../../types'
const props = withDefaults(defineProps<EdgeTextProps>(), { const {
labelStyle: () => ({}), x,
labelShowBg: true, y,
labelBgStyle: () => ({}), label,
labelBgPadding: () => [2, 4], labelStyle = {},
labelBgBorderRadius: 2, labelShowBg = true,
}) labelBgStyle = {},
labelBgPadding = [2, 4],
labelBgBorderRadius = 2,
} = defineProps<EdgeTextProps>()
const edgeRef = templateRef<SVGTextElement>('edge-text', null) const el = templateRef<SVGTextElement>('el', null)
const box = $(useElementBounding(el))
let edgeRefBbox = $ref<Rect>({ x: 0, y: 0, width: 0, height: 0 }) const transform = computed(() => `translate(${x - box.width / 2} ${y - box.height / 2})`)
onMounted(() => {
edgeRefBbox = edgeRef.value.getBBox()
})
const transform = computed(() => `translate(${props.x - edgeRefBbox.width / 2} ${props.y - edgeRefBbox.height / 2})`)
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -27,23 +26,23 @@ export default {
</script> </script>
<template> <template>
<g :transform="transform" :class="props.class" class="vue-flow__edge-textwrapper"> <g :transform="transform" class="vue-flow__edge-textwrapper">
<rect <rect
v-if="props.labelShowBg" v-if="labelShowBg"
class="vue-flow__edge-textbg" class="vue-flow__edge-textbg"
:width="`${edgeRefBbox.width + 2 * props.labelBgPadding[0]}px`" :width="`${box.width + 2 * labelBgPadding[0]}px`"
:height="`${edgeRefBbox.height + 2 * props.labelBgPadding[1]}px`" :height="`${box.height + 2 * labelBgPadding[1]}px`"
:x="-props.labelBgPadding[0]" :x="-labelBgPadding[0]"
:y="-props.labelBgPadding[1]" :y="-labelBgPadding[1]"
:style="props.labelBgStyle" :style="labelBgStyle"
:rx="props.labelBgBorderRadius" :rx="labelBgBorderRadius"
:ry="props.labelBgBorderRadius" :ry="labelBgBorderRadius"
/> />
<text ref="edge-text" class="vue-flow__edge-text" :y="edgeRefBbox.height / 2" dy="0.3em" :style="props.labelStyle"> <text v-bind="$attrs" ref="el" class="vue-flow__edge-text" :y="box.height / 2" dy="0.3em" :style="labelStyle">
<slot v-bind="props"> <slot>
<component :is="props.label" v-if="typeof props.label !== 'string' && typeof props.label" /> <component :is="label" v-if="typeof label !== 'string' && typeof label" />
<template v-else v-html="props.label"> <template v-else v-html="label">
{{ props.label }} {{ label }}
</template> </template>
</slot> </slot>
</text> </text>
+1 -1
View File
@@ -1,4 +1,4 @@
import type { CSSProperties, Component, DefineComponent, HTMLAttributes, SVGAttributes, VNode } from 'vue' import type { CSSProperties, Component, DefineComponent, SVGAttributes, VNode } from 'vue'
import type { BackgroundVariant, Dimensions, ElementData, XYPosition } from './flow' import type { BackgroundVariant, Dimensions, ElementData, XYPosition } from './flow'
import type { GraphNode, NodeProps } from './node' import type { GraphNode, NodeProps } from './node'
import type { EdgeProps } from './edge' import type { EdgeProps } from './edge'