App.vue 28.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
<template>
  <div class="app" style="height: 100vh">
    <vue-flow-editor
      ref="editor"
      modelWidth="30%"
      :data="state.data"
      :grid="showGrid"
      :miniMap="showMiniMap"
      :onRef="onRef"
      :multipleSelect="showMultipleSelect"
      :loading="state.editorLoading"
      :beforeDelete="handleBeforeDelete"
      :afterDelete="handleAfterDelete"
      :beforeAdd="handleBeforeAdd"
      :afterAdd="handleAfterAdd"
      @click-canvas="onClickCanvas"
      @dragend-node="onDragEndNode"
      @click-node="onClickNode"
      @dblclick-node="onDblClickNode"
      @dblclick-edge="onDblClickEdge"
      :controlConfig="state.controlConfig"
      :toolbarButtonHandler="toolbarButtonHandler"
    >
      <!-- :activityConfig="state.activityConfig" -->
      <!--  @click-node="onClickNode" -->
      <!-- 左侧菜单 -->
      <template v-slot:menu>
        <!-- <vue-flow-edit-menu-group label="活动节点" value>
          <vue-flow-edit-menu
            v-for="(value, key) in state.activityConfig"
            :key="key"
            :model="{ activity: key, text: value.text, desc: value.desc }"
          >
            <template v-slot:content>
              <div class="activity-menu">
                <img :src="value.img" />
                <span>{{ value.text }}</span>
              </div>
            </template>
          </vue-flow-edit-menu>
        </vue-flow-edit-menu-group> -->
        <vue-flow-edit-menu-group label="操作节点" value>
          <!-- 注意 key 值的绑定 -->
          <vue-flow-edit-menu
            v-for="(value, key) in state.controlConfig"
            :key="key"
            :model="{ control: key, text: value.text, desc: value.desc }"
          >
            <template v-slot:content>
              <div class="activity-menu">
                <img :src="value.img" />
                <span>{{ value.text }}</span>
              </div>
            </template>
          </vue-flow-edit-menu>
        </vue-flow-edit-menu-group>
        <!-- <vue-flow-edit-menu-group
          v-for="(group, groupIndex) in state.menuData"
          :label="group.label"
          :key="groupIndex"
          :value="true"
        >
          <vue-flow-edit-menu
            v-for="(menu, menuIndex) in group.menus"
            :key="menuIndex"
            :model="menu"
          />
        </vue-flow-edit-menu-group> -->
      </template>
      <!-- 右侧表单 -->
      <template v-slot:model>
        <el-form
          v-if="!!state.detailModel"
          ref="formRef"
          :model="state.detailModel"
          label-position="top"
          label-width="100px"
        >
          <!-- <template v-if="state.detailModel.activity === undefined"> -->
            <el-tabs
              v-model="state.activeName"
              class=""
              @tab-change="handleActiveChange"
              stretch
            >
              <el-tab-pane label="节点属性" name="node" style="padding: 0 1rem">
                <el-form-item prop="label">
                  <div slot="label">
                    节点名称 <span style="color: red;">*</span>
                  </div>
                  <el-input v-model="state.node_name" />
                </el-form-item>
                <div class="node-user">
                  <div style="font-size: 14px; margin-bottom: 10px;">
                    节点负责人 <span style="color: red;">*</span>
                  </div>
                  <div class="flow-tag__wrapper" @click="openUserForm">
                    <el-tag
                      v-if="state.userTags.length"
                      v-for="tag in state.userTags"
                      :key="tag.name"
                      style="margin-left: 0.25rem; margin-right: 0.25rem;"
                    >
                      {{ tag.name }}
                    </el-tag>
                    <div v-else class="text-empty">请选择成员</div>
                  </div>
                </div>
                <el-form-item prop="attr">
                  <el-radio-group
                    v-model="state.attr_radio"
                    size="large"
                    class="attr-radio-group"
                  >
                    <el-radio-button label="基础属性" />
                    <el-radio-button label="更多属性" />
                  </el-radio-group>
                </el-form-item>
                <el-form-item v-if="state.attr_radio === '基础属性'" prop="">
                  <div slot="label">
                    字段权限 <span style="color: red;">*</span>
                  </div>
                  <el-row
                    style="width: 100%; background-color: #f0f1f4; padding-left: 10px;"
                  >
                    <el-col :span="12">字段</el-col>
                    <el-col :span="6">可见</el-col>
                    <el-col :span="6">可编辑</el-col>
                  </el-row>
                  <el-row style="width: 100%; padding-left: 10px;">
                    <el-col :span="12" style="color: #409eff;">全选</el-col>
                    <el-col :span="6" style="padding-left: 5px;"
                      ><el-checkbox
                        @change="onAuthAllChange"
                        v-model="state.auth_all_checked"
                        label=""
                        size="large"
                    /></el-col>
                    <el-col :span="6" style="padding-left: 5px;"
                      ><el-checkbox
                        @change="onAuthAllEditChange"
                        v-model="state.auth_all_edit"
                        label=""
                        size="large"
                    /></el-col>
                  </el-row>
                  <el-row
                    v-for="(field, index) in state.field_auths"
                    :key="index"
                    style="width: 100%; padding-left: 10px;"
                  >
                    <el-col :span="12" style="">{{ field.name }}</el-col>
                    <el-col :span="6" style="padding-left: 5px;"
                      ><el-checkbox
                        v-model="field.visible.checked"
                        :disabled="field.visible.disabled"
                        label=""
                        size="large"
                        @change="onAuthVisibleChange(field, index)"
                    /></el-col>
                    <el-col :span="6" style="padding-left: 5px;"
                      ><el-checkbox
                        v-model="field.editable.checked"
                        :disabled="field.editable.disabled"
                        label=""
                        size="large"
                        @change="onAuthEditableChange(field, index)"
                    /></el-col>
                  </el-row>
                </el-form-item>
              </el-tab-pane>
              <el-tab-pane label="流程属性" name="flow" style="padding: 0 1rem">
                <el-form-item prop="label">
                  <div slot="label">
                    测试属性 <span style="color: red;">*</span>
                  </div>
                  <el-input v-model="state.detailModel.data.test" />
                </el-form-item>
              </el-tab-pane>
            </el-tabs>
            <!-- <template v-if="state.detailModel.type !== 'edge'">
              <el-form-item label="节点背景色" prop="style.fill">
                <el-color-picker v-model="state.detailModel.style.fill" />
              </el-form-item>
              <el-form-item label="节点边框色" prop="style.stroke">
                <el-color-picker v-model="state.detailModel.style.stroke" />
              </el-form-item>
              <el-form-item label="节点文字色" prop="labelCfg.style.stroke">
                <el-color-picker
                  v-model="state.detailModel.labelCfg.style.fill"
                />
              </el-form-item>
            </template> -->
            <!-- <div style="margin-left: 20px;">
              <el-button type="primary" @click="openUserForm">
                设置人员配置
              </el-button>
            </div> -->
          <!-- </template> -->
          <!-- <template v-else> -->
            <!-- <el-form-item label="活动标题">
              <el-input v-model="state.detailModel.text" />
            </el-form-item>
            <el-form-item label="活动副标题">
              <el-input v-model="state.detailModel.desc" />
            </el-form-item> -->
            <!-- <el-form-item label="活动类型">
              <el-select v-model="state.detailModel.activity">
                <el-option
                  v-for="(value, key) in state.activityConfig"
                  :key="key"
                  :label="value.text"
                  :value="key"
                />
              </el-select>
            </el-form-item> -->
          <!-- </template> -->
        </el-form>
      </template>
      <!-- 工具栏 -->
      <template v-slot:toolbar>
        <el-tooltip content="打印流程图数据结构">
          <div class="vue-flow-editor-toolbar-item" @click="logData">
            <i class="el-icon-search" style=" margin-top: 4px;" />
            <span style="font-size: 12px; transform: scale(0.8); margin-top: 2px;">log数据</span>
          </div>
        </el-tooltip>
      </template>
      <!-- 表单底部按钮 -->
      <template v-slot:foot>
        <el-button type="primary" @click="saveForm">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </template>
    </vue-flow-editor>
  </div>

  <select-user-view
    :visible="state.dialogUserFormVisible"
    @close="onCloseUserView"
    @confirm="onConfirmUserView"
  />
</template>

<script lang="ts">
import { ref, reactive, onMounted, watch, nextTick } from 'vue'
import { AppData } from './data.js'
import { staticPath } from './utils'
import { ElNotification } from 'element-plus'
import axios from './axios'
import $ from 'jquery'
import { Calendar, Search } from '@element-plus/icons-vue'
import SelectUserView from './selectUserView.vue'
import { Function } from 'lodash'
import { extend } from '@vue/shared'
import type { FormInstance, FormRules } from 'element-plus'

const G6 = (window as any).G6.default as any

function delay(time: number) {
  return new Promise((resolve) => setTimeout(resolve, time))
}

interface RuleForm {
  label: string
}

interface myObj {
  source: string
  id: string
  label: string
  control: string
  target: string
}

interface myEvent {
  item: {
    get(
      T: string,
    ): {
      source: any
      target: any
      style: any
      labelCfg: any
      label: any
    }
  }
}

export default {
  components: {
    Calendar,
    Search,
    SelectUserView,
  },
  setup(props, context) {

    const formRef = ref<any>(null);

    const rules = reactive<FormRules<RuleForm>>({
      label: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
        ],
    })

    const state = reactive({
      data: AppData, // 渲染的数据,数据格式参考G6文档
      detailModel: null,
      editorLoading: false, // 开始编辑器的loading状态
      selectOptions: [
        { label: '待确认', value: '0' },
        { label: '填写表单', value: '1' },
        { label: '部门负责人审批', value: '2' },
        { label: '总经理审批', value: '3' },
      ],
      menuData: [
        {
          label: '流程节点',
          menus: [
            { label: '开始', shape: 'ellipse', id: 'start-node' },
            { label: '结束', shape: 'ellipse', id: 'end-node' },
            { label: '审批节点', busType: '123' },
            { label: '判断节点', shape: 'diamond' },
          ],
        },
        {
          label: '其他形状节点',
          menus: [
            { label: '矩形节点', shape: 'rect' },
            { label: '圆形节点', shape: 'circle' },
            { label: '椭圆节点', shape: 'ellipse' },
            { label: '菱形节点', shape: 'diamond' },
            { label: '三角形节点', shape: 'triangle' },
            { label: '星形节点', shape: 'star' },
          ],
        },
      ],
      // activityConfig: {
      //   advertisement: {
      //     text: "广告宣传1",
      //     desc: "通过广告宣传新品",
      //     color: "#9283ed",
      //     img: "https://cdn.ipadbiz.cn/oa/advertisement-node.svg"
      //   },
      //   coupon: {
      //     text: "优惠券",
      //     desc: "发放奖励优惠券",
      //     color: "#ed8383",
      //     img: "https://cdn.ipadbiz.cn/oa/coupon-node.svg"
      //   },
      //   crowd: {
      //     text: "用户反馈",
      //     desc: "收集用户反馈信息",
      //     color: "#92dba8",
      //     img: "https://cdn.ipadbiz.cn/oa/crowd-node.svg"
      //   }
      // },
      controlConfig: {
        start: {
          id: 'start-node',
          text: '开始',
          desc: '开始',
          color: '#9283ed',
          img: 'https://cdn.ipadbiz.cn/oa/advertisement-node.svg',
        },
        flow: {
          text: '流程节点',
          desc: '流程节点',
          color: '#ed8383',
          img: 'https://cdn.ipadbiz.cn/oa/coupon-node.svg',
        },
        end: {
          id: 'end-node',
          text: '结束',
          desc: '结束',
          color: '#92dba8',
          img: 'https://cdn.ipadbiz.cn/oa/crowd-node.svg',
        },
      },
      dialogUserFormVisible: false,
      activeName: 'node',
      attr_radio: '基础属性',
      node_name: '', // 节点名称
      userTags: [ // 节点负责人
        { id: "user-1-1", name: "用户1-1" }
      ],
      auth_all_checked: false,
      auth_all_edit: false,
      field_auths: [ // 字段权限
        {
          name: '字段1',
          visible: {
            checked: false,
            disabled: true,
          },
          editable: {
            checked: false,
            disabled: true,
          },
        },
        {
          name: '字段2',
          visible: {
            checked: true,
            disabled: false,
          },
          editable: {
            checked: false,
            disabled: false,
          },
        },
        {
          name: '字段3',
          visible: {
            checked: false,
            disabled: false,
          },
          editable: {
            checked: false,
            disabled: false,
          },
        },
      ],
    })

    onMounted(() => {
      document.title = '可视化流程设计器'
      // 显示提示框的标志位
      var showConfirmation = true;
      // 监听 beforeunload 事件
      window.addEventListener('beforeunload', function (event) {
        if (showConfirmation) {
          // 取消事件的默认行为(弹出确认对话框)
          event.preventDefault();
          // Chrome 和 Firefox 需要返回一个值以显示确认对话框
          event.returnValue = '';
          // 显示自定义的提示信息
          var confirmationMessage = '确定要离开此页面吗?';
          (event || window.event).returnValue = confirmationMessage;  // 兼容旧版浏览器
          return confirmationMessage;
        }
      });
      // 监听 unload 事件
      window.addEventListener('unload', function () {
        // 设置标志位为 false,避免在刷新页面时再次显示提示框
        showConfirmation = false;
      });
    })

    function handleActiveChange(name) {
      console.warn(name)
    }

    /************** 字段权限操作 ***************/
    const onAuthVisibleChange = (val: any, index: number) => {
      // 可见按钮回调
      console.log('onAuthVisibleChange', val, index);
      let count = state.field_auths.filter((ele) => {
        if (ele.visible.checked && !ele.visible.disabled) {
          return ele;
        }
      });
      if (count.length === state.field_auths.length) {
        state.auth_all_checked = true;
      } else {
        state.auth_all_checked = false;
      }
    }
    const onAuthEditChange = (val: any, index: number) => {
      console.log('onAuthEditChange', val, index)
    }

    const onAuthAllChange = (val: any) => {
      // 全选可见按钮回调
      if (val) {
        // 全部选中
        state.field_auths.forEach((ele) => {
          ele.visible.checked = true
        })
      } else {
        // 全部取消选中
        state.field_auths.forEach((ele) => {
          ele.visible.checked = false
        })
      }
    }
    const onAuthAllEditChange = (val: any) => {
      // 全选可编辑按钮回调
      console.warn(val)
    }
    /******************* END *******************/

    /****** 用户选择控件弹框 ******/
    const openUserForm = () => {
      // 打开设置用户弹框
      state.dialogUserFormVisible = true
    }

    const onCloseUserView = (status: boolean) => {
      state.dialogUserFormVisible = status
    }
    const onConfirmUserView = (data: any) => {
      state.userTags = data;
      console.log(data)
    }
    /******************* END *******************/

    /********** 流程图功能函数 **********/

    let editor: {
      openModel: () => void
      closeModel: () => void
      updateModel: (arg0: any) => void
      editorState: {
        graph: {
          removeItem: any
          save: () => { nodes: any; edges: any }
        }
      }
    }

    /**
     * 双击节点回调
     *
     * @param {Object} e - The event object
     */
    function onDblClickNode(e: myEvent) {
      const model = G6.Util.clone(e.item.get('model'))
      model.style = model.style || {}
      model.labelCfg = model.labelCfg || { style: {} }

      model.data = model.data ? model.data : {}

      // 判断是否是流程节点
      let model_id = model.id
      if (model_id !== 'start-node' && model_id!== 'end-node') {
        state.detailModel = model
        editor.openModel()
      }

    }

    /**
     * 点击节点回调
     *
     * @param {Event} e - The event object representing the click event.
     */
    function onClickNode(e: myEvent) {
      const model = G6.Util.clone(e.item.get('model'))
      model.style = model.style || {}
      model.labelCfg = model.labelCfg || { style: {} }

      model.data = model.data ? model.data : {}
      state.detailModel = model

      // 判断是否是流程节点
      let model_id = model.id
      if (model_id !== 'start-node' && model_id!== 'end-node') {
        state.detailModel = model;
        // 获取节点名称
        state.node_name = state.detailModel.text;
        // // 检查字段权限选中情况
        // let checked_count = state.field_auths.filter((ele) => {
        //   if (ele.visible.checked && !ele.visible.disabled) {
        //     return ele;
        //   }
        // });
        // if (checked_count.length === state.field_auths.length) {
        //   state.auth_all_checked = true;
        // } else {
        //   state.auth_all_checked = false;
        // }
        // let edit_count = state.field_auths.filter((ele) => {
        //   if (ele.editable.checked && !ele.editable.disabled) {
        //     return ele;
        //   }
        // });
        // if (edit_count.length === state.field_auths.length) {
        //   state.auth_all_edit = true;
        // } else {
        //   state.auth_all_edit = false;
        // }

        // 打开属性表单
        editor.openModel()
      } else {
        editor.closeModel()
      }
    }

    /**
     * 双击连接线回调
     *
     * @param {Event} e - The event object representing the double click event.
     */
    function onDblClickEdge(e: myEvent) {
      const { source, target, style, labelCfg, label } = e.item.get('model')
      const model = {
        label,
        source,
        target,
        style: style || {},
        labelCfg: labelCfg || { style: {} },
        type: null,
        id: null,
      }
      model.type = e.item.get('type')
      model.id = e.item.get('id')

      state.detailModel = model
      editor.openModel()
    }

    /**
     * Cancels the operation and closes the editor model.
     *
     */
    function cancel() {
      editor.closeModel()
    }

    /**
     * 保存表单信息
     *
     */
    async function saveForm() {
      // if (!formRef.value) return
      // await formRef.value.validate((valid) => {
      //   if (!valid) {
      //     return false
      //   }
      // })
      state.detailModel.text = state.node_name

      // state.detailModel.label = state.node_name
      // 更新流程图信息
      editor.updateModel(state.detailModel)
      // editor.closeModel()
      console.log('节点名称', state.node_name)
      console.log('节点负责人', state.userTags)
      console.log('字段权限', state.field_auths)
    }

    /**
     * 删除前校验
     *
     * @param {Object} model - The model object.
     * @param {string} type - The type of the model.
     * @return {Promise} A promise that resolves when the event is handled.
     */
    async function handleBeforeDelete(
      model: myObj,
      type: string,
    ): Promise<any> {
      let { nodes, edges } = editor.editorState.graph.save();
      // 不可以删除开始与结束连接线
      let node_id = model.id;
      for (let index = 0; index < edges.length; index++) {
        const element = edges[index]
        if(
          (element.target === node_id && element.source === 'start-node') ||
          (element.source === node_id && element.target === 'end-node')
          )
        {
          ElNotification.error('不可以删除【开始】与【结束】连接线')
          return Promise.reject('reject')
        }
      }
      if (type === 'node') {
        if (model.id === 'start-node') {
          // state.editorLoading = true
          // await delay(1000)
          // state.editorLoading = false
          ElNotification.error('不可以删除【开始】节点')
          return Promise.reject('reject')
        }
        if (model.id === 'end-node') {
          ElNotification.error('不可以删除【结束】节点')
          return Promise.reject('reject')
        }
        // 流程图中必须有一个流程节点
        let is_flow_node = nodes.filter(node => node.control === 'flow');
        if (is_flow_node.length === 1) {
          ElNotification.error('流程图中必须有一个流程节点')
          return Promise.reject('reject')
        }
      }
      if (type === 'edge') {
        if (model.source === 'start-node') {
          ElNotification.error('不可以删除【开始】连接线')
          return Promise.reject('reject')
        }
        if (model.target === 'end-node') {
          ElNotification.error('不可以删除【结束】连接线')
          return Promise.reject('reject')
        }
      }
    }

    /**
     * 删除后动作
     *
     * @param {Object} model - The model being deleted.
     * @param {string} type - The type of the model being deleted.
     */
    function handleAfterDelete(model: myObj, type: string) {
      if (type === 'edge') {
        console.log('delete edge')
      } else {
        console.log('after delete', model.label, { ...model })
      }
    }

    /**
     * 添加前校验
     *
     * @param {object} model - The model object.
     * @param {string} type - The type of the model.
     * @return {Promise} A promise that resolves to a result or rejects with an error.
     */
    function handleBeforeAdd(model: myObj, type: string): Promise<any> {
      const source = model.source;
      const target = model.target;
      let { nodes, edges } = editor.editorState.graph.save();

      if (type === 'edge') {
        if (model.source === 'end-node') {
          ElNotification.error('结束节点不能输出连线其他节点')
          return Promise.reject('reject')
        }
        for (let index = 0; index < edges.length; index++) {
          const element = edges[index]
          if (element.source === source && element.target === target) {
            ElNotification.error('不可以重复添加连线')
            return Promise.reject('reject')
          }
        }
      }
      if (type === 'node') {
        if (model.control === 'start' || model.control === 'end') {
          const data = editor.editorState.graph.save()
          for (let i = 0; i < data.nodes.length; i++) {
            const node = data.nodes[i]
            if (node.control === model.control) {
              ElNotification.error(
                `只能有一个${model.control === 'start' ? '开始' : '结束'}节点`,
              )
              return Promise.reject('reject')
            }
          }
        }
      }
    }

    /**
     * 添加后动作
     *
     * @param {model} model - The model being handled.
     * @param {type} type - The type of the event.
     */
    function handleAfterAdd(model: myObj, type: string) {
      // console.log('handleAfterAdd', model);

      // TODO: 因为resize会重新绘制,所以可能需要保存操作
      if (type === 'node') {
        console.log(`新增节点`)
      }
      if (type === 'edge') {
        console.log(`新增连接线`)
      }

      state.data.nodes = editor.editorState.graph.save().nodes
      state.data.edges = editor.editorState.graph.save().edges
    }

    function onClickCanvas(e: myEvent) {
      console.log('单击画布');
      editor.closeModel()
    }

    /**
     * 拖动节点结束回调
     *
     * @param {myEvent} e - The event object containing information about the drag and drop.
     */
    function onDragEndNode(e: myEvent) {
      // TODO:可能需要接口保存相应位置,避免拖动窗口时数据丢失
      const model = e.item.get('model')
      console.log('onDragEndNode', model)
    }

    /**
     * 打印流程图数据结构
     *
     * @return {void} No return value.
     */
    function logData(): void {
      let { nodes, edges } = editor.editorState.graph.save()
      // console.log("nodes", nodes);
      // console.log("edges", edges);
      // 使用时需要把自定义节点的类型带过去 activity/control
      nodes.forEach((node: { [x: string]: string; shape: string }) => {
        // if (node.shape === 'activity') {
        //   node['shape'] = 'activity_' + node['activity']
        // }
        if (node.shape === 'control') {
          // node['shape'] = 'control_' + node['control']
          node['control'] = node['control']
        }
      })

      nodes = nodes.map(
        ({ data, id, label, shape, x, y, text, desc, img, control }) => ({
          data,
          id,
          label,
          shape,
          x,
          y,
          text,
          desc,
          img,
          control,
        }),
      )
      edges = edges.map(({ source, sourceAnchor, target, targetAnchor }) => ({
        source,
        sourceAnchor,
        target,
        targetAnchor,
      }))
      // console.log(JSON.stringify({ nodes, edges }, null, 2));
      console.log(nodes)
      console.log(edges)
    }

    /**
     * 格式化工具栏按钮
     *
     * @param {Array} buttons - The array of buttons to be filtered
     * @return {Array} - The filtered array of buttons
     */
    function toolbarButtonHandler(buttons: any[]): Array<any> {
      // TAG:测试隐藏缩略图
      let map = buttons.filter((item) => item.key !== 'miniMapSwitcher')
      return map
    }

    return {
      state,
      rules,
      formRef,

      showGrid: true, // 是否开启网格
      showMiniMap: false, // 是否开启缩略图
      showMultipleSelect: true, // 编辑器是否可以多选

      onClickCanvas,
      onClickNode,
      onDblClickNode,
      onDragEndNode,
      onDblClickEdge,
      cancel,
      saveForm,
      handleBeforeDelete,
      handleAfterDelete,
      handleBeforeAdd,
      handleAfterAdd,

      handleActiveChange,
      onAuthVisibleChange,
      onAuthEditChange,
      onAuthAllChange,
      onAuthAllEditChange,

      openUserForm,
      onCloseUserView,
      onConfirmUserView,

      logData,
      toolbarButtonHandler,

      onRef: (e: any) => (editor = e),
      staticPath,
    }
  },
}
</script>

<style lang="scss">
html,
body {
  padding: 0;
  margin: 0;

  .activity-menu {
    display: flex;
    align-items: center;

    img {
      margin-right: 1em;
      width: 30px;
      height: 30px;
    }
  }
}

.attr-radio-group {
  width: 100% !important;
  .el-radio-button.el-radio-button--large {
    width: 50% !important;
    span {
      width: 100% !important;
    }
  }
}

.demo-tabs > .el-tabs__content {
  /* padding: 32px; */
}

.flow-tag__wrapper {
  border: 1px dashed #dcdfe6;
  padding: 10px;
  margin-bottom: 10px;
  &:hover {
    cursor: pointer;
  }
  .text-empty {
    text-align: center;
    color: #dcdfe6;
  }
}
</style>