index.vue 2.42 KB
<!--
 * @Date: 2025-03-11 17:20:37
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-03-11 18:15:05
 * @FilePath: /logic-flow2/src/views/node-vue/index.vue
 * @Description: 文件描述
-->
<template>
  <div class="container">
    <div ref="containerRef" id="graph" class="flow-container"></div>
  </div>
  <TeleportContainer :flow-id="flowId" />
</template>

<script setup>
import { forEach, map, has } from "lodash-es";
import LogicFlow, { ElementState, LogicFlowUtil } from "@logicflow/core";
import { register, getTeleport } from "@logicflow/vue-node-registry";

import ProgressNode from "./ProgressNode.vue";

const containerRef = ref(null);
const TeleportContainer = getTeleport();
const flowId = ref("");

onMounted(() => {
  if (containerRef.value) {
    const lf = new LogicFlow({
      container: containerRef.value,
      grid: true,
    });

    // 注册自定义 vue 节点
    register(
      {
        type: "custom-vue-node",
        component: ProgressNode,
      },
      lf
    );

    lf.on("graph:rendered", ({ graphModel }) => {
      flowId.value = graphModel?.flowId || "";
    });

    // 注册事件
    lf.render({});
    const node1 = lf.addNode({
      id: "vue-node-1",
      type: "custom-vue-node",
      x: 80,
      y: 80,
      properties: {
        progress: 60,
        width: 80,
        height: 80,
      },
    });
    console.log("node1 --->>>", node1);

    const node2 = lf.addNode({
      id: "vue-node-2",
      type: "custom-vue-node",
      x: 360,
      y: 80,
      properties: {
        progress: 60,
        width: 80,
        height: 80,
      },
    });

    // setInterval(() => {
    //   const { properties } = node2.getData();
    //   console.log("properties.progress --->>>", properties?.progress);
    //   if (has(properties, "progress")) {
    //     const progress = properties?.progress;
    //     node2.setProperty("progress", (progress + 10) % 100);
    //   }
    // }, 2000);

    setTimeout(() => {
      const { properties } = node2.getData();
      console.log("properties.progress --->>>", properties?.progress);
      if (has(properties, "progress")) {
        const progress = properties?.progress;
        node2.setProperty("progress", (progress + 10) % 100);
      }
    }, 2000);
  }
});
</script>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.flow-container {
  flex: 1;
  width: 100%;
  height: 100%;
}
</style>