BasicEdge.vue 3 KB
<script setup lang="ts">
  import { BaseEdge, EdgeLabelRenderer, getBezierPath } from '@vue-flow/core';
  import type { EdgeProps } from '@vue-flow/core';
  import { computed, toRaw, unref } from 'vue';
  import type { EdgeData, NodeData } from '../../../types/node';
  import { useConnectionFocus } from '../../../hook/useConnectionFocus';
  import { useFlowContext } from '../../../hook/useFlowContext';
  import { useTextWidth } from '../../../hook/useTextWidth';
  import { ElementsTypeEnum } from '../../../enum';

  const props = defineProps<EdgeProps>();

  const { flowActionType, updateEdgeDrawerActionType } = useFlowContext();

  const { getMarkerEnd, getSelected } = useConnectionFocus(props);

  const getData = computed(() => props.data as EdgeData);

  const getLabel = computed(() => {
    let { data } = unref(getData);
    const type = data?.type || [];
    return type.join(' / ');
  });

  const getLabelWidth = computed(() => useTextWidth(unref(getLabel), 14) + 10);

  const path = computed(() => getBezierPath(props));

  const handleOpenEdit = async () => {
    if (unref(props.sourceNode.data as NodeData).config?.disableAction) return;

    const { flag, data } =
      (await unref(updateEdgeDrawerActionType)?.open(
        toRaw(unref(props.sourceNode?.data as unknown as NodeData)),
        toRaw(unref(props.data as EdgeData)),
        { id: props.id, type: ElementsTypeEnum.EDGE }
      )) || {};

    if (!flag) return;

    const currentEdge = flowActionType.findEdge(props.id);

    (currentEdge!.data as EdgeData).data = data;
  };

  const handleDelete = () => {
    flowActionType?.removeEdges(props.id);
  };
</script>

<template>
  <BaseEdge
    v-bind="props"
    :path="path[0]"
    :marker-end="getMarkerEnd"
    class="dark:stroke-light-500"
    :label="getLabel"
    :label-x="path[1]"
    :label-y="path[2]"
    :label-style="{ fill: '#003a79', fontWeight: 700, fontSize: 14 }"
    :label-show-bg="true"
    :label-bg-style="{ fill: '#fff', stroke: '#003a79', strokeWidth: 2, width: getLabelWidth }"
    :label-bg-padding="[5, 5]"
    :label-bg-border-radius="10"
  />
  <EdgeLabelRenderer>
    <section
      :style="{
        pointerEvents: 'all',
        position: 'absolute',
        transform: `translate(0, -3rem) translate(${path[1]}px,${path[2]}px)`,
      }"
      class="nodrag nopan label-container relative"
    >
      <div
        v-if="getSelected"
        class="actions flex absolute gap-1 -top-full transform translate-y-1 right w-16 h-7 justify-center"
      >
        <div
          class="border-2 rounded-1 border-light-50 bg-red-500 w-7 h-7 flex justify-center items-center cursor-pointer"
          @click="handleOpenEdit"
        >
          <Icon icon="mdi:lead-pencil" color="#fff" />
        </div>
        <div
          class="border-2 rounded-1 border-light-50 bg-red-500 w-7 h-7 flex justify-center items-center cursor-pointer"
          @click="handleDelete"
        >
          <Icon icon="mdi:close" color="#fff" />
        </div>
      </div>
    </section>
  </EdgeLabelRenderer>
</template>