gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
 
import { inject, nextTick, ref, watch } from 'vue';
 
import { confirm, useVbenModal } from '..\..\..\..\..\..\..\packages\effects\common-ui\src';
import { IconifyIcon } from '..\..\..\..\..\..\..\packages\icons\src';
import { cloneDeep } from '..\..\..\..\..\..\..\packages\utils\src';
 
import { Button, Divider, Form, FormItem, Input } from 'ant-design-vue';
 
import { useVbenVxeGrid } from '#/adapter/vxe-table';
 
defineOptions({ name: 'ElementProperties' });
 
const props = defineProps({
  id: {
    type: String,
    default: '',
  },
  type: {
    type: String,
    default: '',
  },
});
 
const prefix = inject('prefix');
 
const elementPropertyList = ref<Array<{ name: string; value: string }>>([]);
const propertyForm = ref<{ name?: string; value?: string }>({});
const editingPropertyIndex = ref(-1);
const otherExtensionList = ref<any[]>([]);
const bpmnElementProperties = ref<any[]>([]);
const bpmnElementPropertyList = ref<any[]>([]);
const attributeFormRef = ref<any>();
const bpmnInstances = () => (window as any)?.bpmnInstances;
 
const resetAttributesList = () => {
  const instances = bpmnInstances();
  if (!instances || !instances.bpmnElement) return;
 
  // 直接使用原始BPMN元素,避免Vue响应式代理问题
  const bpmnElement = instances.bpmnElement;
  const businessObject = bpmnElement.businessObject;
 
  otherExtensionList.value = []; // 其他扩展配置
  bpmnElementProperties.value =
    businessObject?.extensionElements?.values?.filter((ex: any) => {
      if (ex.$type !== `${prefix}:Properties`) {
        otherExtensionList.value.push(ex);
      }
      return ex.$type === `${prefix}:Properties`;
    }) ?? [];
 
  bpmnElementPropertyList.value = bpmnElementProperties.value.flatMap(
    (current: any) => current.values,
  );
  elementPropertyList.value = cloneDeep(bpmnElementPropertyList.value ?? []);
};
 
const removeAttributes = (
  _attr: { name: string; value: string },
  index: number,
) => {
  confirm({
    title: '提示',
    content: '确认移除该属性吗?',
  }).then(() => {
    elementPropertyList.value.splice(index, 1);
    bpmnElementPropertyList.value.splice(index, 1);
    const propertiesObject = bpmnInstances().moddle.create(
      `${prefix}:Properties`,
      {
        values: bpmnElementPropertyList.value,
      },
    );
    updateElementExtensions(propertiesObject);
    resetAttributesList();
  });
};
 
const saveAttribute = async () => {
  try {
    await attributeFormRef.value?.validate();
  } catch {
    // 校验未通过,直接返回
    return;
  }
 
  const { name, value } = propertyForm.value;
  const instances = bpmnInstances();
  if (!instances || !instances.bpmnElement) return;
 
  const bpmnElement = instances.bpmnElement;
 
  if (editingPropertyIndex.value === -1) {
    // 新建属性字段
    const newPropertyObject = instances.moddle.create(`${prefix}:Property`, {
      name,
      value,
    });
    // 新建一个属性字段的保存列表
    const propertiesObject = instances.moddle.create(`${prefix}:Properties`, {
      values: [...bpmnElementPropertyList.value, newPropertyObject],
    });
    updateElementExtensions(propertiesObject);
  } else {
    instances.modeling.updateModdleProperties(
      bpmnElement,
      bpmnElementPropertyList.value[editingPropertyIndex.value],
      {
        name,
        value,
      },
    );
  }
  fieldModalApi.close();
  resetAttributesList();
};
 
const updateElementExtensions = (properties: any) => {
  const instances = bpmnInstances();
  if (!instances || !instances.bpmnElement) return;
 
  const bpmnElement = instances.bpmnElement;
  const extensions = instances.moddle.create('bpmn:ExtensionElements', {
    values: [...otherExtensionList.value, properties],
  });
  instances.modeling.updateProperties(bpmnElement, {
    extensionElements: extensions,
  });
};
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: [
      { type: 'seq', width: 50, title: '序号' },
      { field: 'name', title: '属性名', minWidth: 120 },
      { field: 'value', title: '属性值', minWidth: 120 },
      {
        title: '操作',
        width: 120,
        slots: { default: 'action' },
        fixed: 'right',
      },
    ],
    border: true,
    showOverflow: true,
    height: 'auto',
    toolbarConfig: {
      enabled: false,
    },
    pagerConfig: {
      enabled: false,
    },
  } as VxeTableGridOptions<{ name: string; value: string }>,
});
 
const [FieldModal, fieldModalApi] = useVbenModal({
  title: '属性配置',
  onConfirm: saveAttribute,
});
 
const openAttributesForm = (
  attr: null | { name: string; value: string },
  index: number,
) => {
  editingPropertyIndex.value = index;
  propertyForm.value = index === -1 ? {} : cloneDeep(attr || {});
  fieldModalApi.open();
  nextTick(() => {
    if (attributeFormRef.value) attributeFormRef.value.clearValidate();
  });
};
 
watch(
  elementPropertyList,
  (val) => {
    gridApi.setGridOptions({ data: val });
  },
  { deep: true },
);
 
watch(
  () => props.id,
  (val) => {
    if (val && val.length > 0) {
      resetAttributesList();
    }
  },
  { immediate: true },
);
</script>
 
<template>
  <div class="-mx-2">
    <Grid :data="elementPropertyList">
      <template #action="{ row, rowIndex }">
        <Button
          size="small"
          type="link"
          @click="openAttributesForm(row, rowIndex)"
        >
          编辑
        </Button>
        <Divider type="vertical" />
        <Button
          size="small"
          type="link"
          danger
          @click="removeAttributes(row, rowIndex)"
        >
          移除
        </Button>
      </template>
    </Grid>
    <div class="mt-1 flex w-full items-center justify-center gap-2 px-2">
      <Button
        class="flex flex-1 items-center justify-center"
        type="primary"
        size="small"
        @click="openAttributesForm(null, -1)"
      >
        <template #icon>
          <IconifyIcon icon="ep:plus" />
        </template>
        添加属性
      </Button>
    </div>
 
    <FieldModal class="w-3/5">
      <Form
        :model="propertyForm"
        ref="attributeFormRef"
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 17 }"
      >
        <FormItem
          label="属性名:"
          name="name"
          :rules="[{ required: true, message: '请输入属性名' }]"
        >
          <Input v-model:value="propertyForm.name" allow-clear />
        </FormItem>
        <FormItem
          label="属性值:"
          name="value"
          :rules="[{ required: true, message: '请输入属性值' }]"
        >
          <Input v-model:value="propertyForm.value" allow-clear />
        </FormItem>
      </Form>
    </FieldModal>
  </div>
</template>