@blueking/bkflow-canvas-editor 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,453 @@
1
+ # bkflow-canvas-editor
2
+
3
+ bkflow流程编辑组件,支持流程的编辑、查看以及创建并执行任务。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install bkflow-canvas-editor
9
+ ```
10
+
11
+ ## 使用
12
+
13
+ ### 基础使用
14
+
15
+ ```vue
16
+ <template>
17
+ <FlowView
18
+ :flow-id="flowId"
19
+ :api-config="apiConfig"
20
+ :permissions="{ canEdit: true }"
21
+ @edit="handleEdit"
22
+ @back="handleBack" />
23
+ </template>
24
+
25
+ <script setup>
26
+ import { FlowView } from 'bkflow-canvas-editor';
27
+ import 'bkflow-canvas-editor/style';
28
+ import axios from 'axios';
29
+
30
+ const flowId = '123';
31
+
32
+ // 配置 API
33
+ const apiConfig = {
34
+ baseURL: window.API_HOST, // API 基础路径(可选,默认使用 window.API_HOST)
35
+ scopeData: {
36
+ scope_type: 'space',
37
+ scope_value: 123,
38
+ }, // 作用域数据(必需)
39
+ fetchUserApi: `${window.BK_COMPONENT_API_URL}/api/c/compapi/v2/usermanage/fs_list_users/`, // 用户查询 API(可选)
40
+ // 可选:使用自定义 axios 实例
41
+ // axiosInstance: customAxiosInstance,
42
+ // 可选:axios 配置
43
+ // axiosConfig: {
44
+ // timeout: 10000,
45
+ // headers: { ... }
46
+ // },
47
+ };
48
+
49
+ const handleEdit = () => {
50
+ // 跳转到编辑页面
51
+ };
52
+
53
+ const handleBack = () => {
54
+ // 返回处理
55
+ };
56
+ </script>
57
+ ```
58
+
59
+ ## 组件 API
60
+
61
+ ### FlowView 组件
62
+
63
+ 流程查看组件,用于查看流程的详细信息。
64
+
65
+ #### Props
66
+
67
+ | 参数 | 类型 | 默认值 | 说明 |
68
+ | ------------- | ------------------------------------------ | ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
69
+ | `flowId` | `string` | - | **必需**,流程 ID |
70
+ | `apiConfig` | `FlowApiConfig` | - | **必需**,API 配置对象,详见 [API 配置](#api-配置) |
71
+ | `permissions` | `{ canEdit?: boolean, canSave?: boolean }` | `{ canEdit: true, canSave: true }` | 权限配置对象,`canEdit`用来控制流程查看态时头部编辑按钮是否使用禁用样式,`canSave`用来控制流程编辑态时头部保存按钮是否可点击 |
72
+ | `onEdit` | `() => void` | - | 编辑按钮点击回调(可选,也可以通过 `@edit` 事件监听) |
73
+ | `onBack` | `() => void` | - | 返回按钮点击回调(可选,也可以通过 `@back` 事件监听) |
74
+
75
+ #### Slots
76
+
77
+ | 插槽名 | 作用域参数 | 说明 |
78
+ | ------------- | ---------- | ---------------------- |
79
+ | `header` | - | 自定义头部内容 |
80
+ | `inputParams` | 见下方说明 | 自定义节点输入参数组件 |
81
+
82
+ ##### inputParams 插槽作用域参数
83
+
84
+ | 参数 | 类型 | 说明 |
85
+ | ------------------------ | ----------------------------------------------------- | ------------------------------------------------------ |
86
+ | `node` | `Activity` | 当前节点数据 |
87
+ | `uniformApiPluginDetail` | `UniformApiPluginDetail \| null` | 统一 API 插件详情(仅 uniform_api 类型节点) |
88
+ | `inputs` | `UniformApiPluginInputsItem[]` | 输入参数表单配置列表 |
89
+ | `editable` | `boolean` | 是否可编辑 |
90
+ | `fullVariableList` | `{ key: string; name: string }[]` | 完整变量列表(系统变量 + 常量) |
91
+ | `updateInputParams` | `(data: Record<string, PluginInputDataItem>) => void` | 更新节点输入参数的方法(暂存,需点击确定按钮才会保存) |
92
+
93
+ #### Events
94
+
95
+ | 事件名 | 参数 | 说明 |
96
+ | ------ | ---- | ------------------ |
97
+ | `edit` | - | 点击编辑按钮时触发 |
98
+ | `back` | - | 点击返回按钮时触发 |
99
+
100
+ ---
101
+
102
+ ### FlowEdit 组件
103
+
104
+ 流程编辑组件,用于编辑流程的配置和节点信息。
105
+
106
+ #### Props
107
+
108
+ | 参数 | 类型 | 默认值 | 说明 |
109
+ | -------------------- | ----------------------------------------- | ----------------------- | -------------------------------------------------------- |
110
+ | `flowId` | `string` | - | **必需**,流程 ID |
111
+ | `apiConfig` | `FlowApiConfig` | - | **必需**,API 配置对象,详见 [API 配置](#api-配置) |
112
+ | `permissions` | `{ canEdit?: boolean }` | `{}` | 权限配置对象 |
113
+ | `pluginGroupIconMap` | `Record<string, string>` | `PLUGIN_GROUP_ICON_MAP` | 插件分组图标映射 |
114
+ | `onSave` | `(flowData: FlowTemplate) => void` | - | 保存回调(可选,也可以通过 `@save` 事件监听) |
115
+ | `onSaveSuccess` | `() => void` | - | 保存成功回调(可选,也可以通过 `@saveSuccess` 事件监听) |
116
+ | `onBack` | `() => void` | - | 返回按钮点击回调(可选,也可以通过 `@back` 事件监听) |
117
+ | `onBeforeLeave` | `(isEdited: boolean) => Promise<boolean>` | - | 离开前回调,返回 `false` 可阻止离开 |
118
+
119
+ #### Slots
120
+
121
+ | 插槽名 | 作用域参数 | 说明 |
122
+ | ------------- | ---------- | ---------------------- |
123
+ | `header` | - | 自定义头部内容 |
124
+ | `inputParams` | 见下方说明 | 自定义节点输入参数组件 |
125
+
126
+ ##### inputParams 插槽作用域参数
127
+
128
+ | 参数 | 类型 | 说明 |
129
+ | ------------------------ | ------------------------------------------------------------ | ---------------------------------------------------------------------------------------- |
130
+ | `node` | `Activity` | 当前节点数据 |
131
+ | `uniformApiPluginDetail` | `UniformApiPluginDetail \| null` | 统一 API 插件详情(仅 uniform_api 类型节点) |
132
+ | `inputs` | `UniformApiPluginInputsItem[]` | 输入参数表单配置列表 |
133
+ | `editable` | `boolean` | 是否可编辑 |
134
+ | `fullVariableList` | `{ key: string; name: string }[]` | 完整变量列表(系统变量 + 常量) |
135
+ | `updateInputParams` | `(data: Record<string, PluginInputDataItem>) => void` | 更新节点输入参数的方法(暂存到节点配置面板,需点击确定按钮才会保存) |
136
+ | `updateNodeInputParams` | `(inputParams: Record<string, PluginInputDataItem>) => void` | 更新节点输入参数的方法(通过 edit 组件调用,暂存到节点配置面板,需点击确定按钮才会保存) |
137
+
138
+ #### Events
139
+
140
+ | 事件名 | 参数 | 说明 |
141
+ | ------------- | ------------------------ | -------------------------- |
142
+ | `save` | `flowData: FlowTemplate` | 保存时触发,参数为流程数据 |
143
+ | `saveSuccess` | - | 保存成功时触发 |
144
+ | `back` | - | 点击返回按钮时触发 |
145
+
146
+ #### 暴露的方法
147
+
148
+ 通过 `ref` 可以访问组件实例:
149
+
150
+ | 方法名 | 参数 | 返回值 | 说明 |
151
+ | ----------------------- | -------------------------------------------------- | ---------------------- | ---------------------------------------------------------- |
152
+ | `updateNodeInputParams` | `inputParams: Record<string, PluginInputDataItem>` | `void` | 更新当前打开节点的输入参数(暂存,需点击确定按钮才会保存) |
153
+ | `isFlowEdited` | - | `ComputedRef<boolean>` | 流程是否已编辑(只读) |
154
+
155
+ ---
156
+
157
+ ### FlowExecute 组件
158
+
159
+ 流程执行组件,用于执行流程任务。
160
+
161
+ #### Props
162
+
163
+ | 参数 | 类型 | 默认值 | 说明 |
164
+ | ------------------ | ---------------------------------------------- | ------- | ------------------------------------------------------------------------------------------ |
165
+ | `flowId` | `string` | - | **必需**,流程 ID |
166
+ | `show` | `boolean` | - | **必需**,是否显示执行面板 |
167
+ | `apiConfig` | `FlowApiConfig` | - | **必需**,API 配置对象,详见 [API 配置](#api-配置)。注意:需要通过 `useFlowApi` 转换后传入 |
168
+ | `editable` | `boolean` | `true` | 是否可编辑 |
169
+ | `showFlowEntry` | `boolean` | `false` | 是否显示流程入口 |
170
+ | `onExecuteSuccess` | `(taskId: string, templateId: string) => void` | - | 执行成功回调 |
171
+
172
+ #### Events
173
+
174
+ | 事件名 | 参数 | 说明 |
175
+ | ------------- | ------------------------------------ | ------------------ |
176
+ | `confirm` | `taskId: string, templateId: string` | 执行确认时触发 |
177
+ | `close` | - | 关闭面板时触发 |
178
+ | `update:show` | `show: boolean` | 显示状态更新时触发 |
179
+
180
+ ---
181
+
182
+ ## 使用示例
183
+
184
+ ### 基础使用
185
+
186
+ ```vue
187
+ <template>
188
+ <FlowEdit
189
+ :flow-id="flowId"
190
+ :api-config="apiConfig"
191
+ :permissions="{ canEdit: true }"
192
+ @save="handleSave"
193
+ @save-success="handleSaveSuccess"
194
+ @back="handleBack" />
195
+ </template>
196
+
197
+ <script setup>
198
+ import { FlowEdit } from 'bkflow-canvas-editor';
199
+
200
+ const flowId = '123';
201
+ const apiConfig = {
202
+ baseURL: window.API_HOST,
203
+ scopeData: {
204
+ scope_type: 'space',
205
+ scope_value: 123,
206
+ },
207
+ };
208
+
209
+ const handleSave = (flowData) => {
210
+ console.log('保存流程:', flowData);
211
+ };
212
+
213
+ const handleSaveSuccess = () => {
214
+ console.log('保存成功');
215
+ };
216
+
217
+ const handleBack = () => {
218
+ // 返回处理
219
+ };
220
+ </script>
221
+ ```
222
+
223
+ ### 使用自定义 InputParams 插槽
224
+
225
+ 当需要自定义节点输入参数的渲染时,可以使用 `inputParams` 插槽:
226
+
227
+ ```vue
228
+ <template>
229
+ <FlowEdit
230
+ ref="flowEditRef"
231
+ :flow-id="flowId"
232
+ :api-config="apiConfig"
233
+ :permissions="{ canEdit: true }"
234
+ @save="handleSave">
235
+ <!-- 自定义输入参数组件 -->
236
+ <template
237
+ #inputParams="{
238
+ node,
239
+ uniformApiPluginDetail,
240
+ inputs,
241
+ editable,
242
+ fullVariableList,
243
+ updateInputParams,
244
+ updateNodeInputParams,
245
+ }">
246
+ <CustomInputParams
247
+ :node="node"
248
+ :plugin-detail="uniformApiPluginDetail"
249
+ :inputs="inputs"
250
+ :editable="editable"
251
+ :full-variable-list="fullVariableList"
252
+ @change="handleInputParamsChange($event, updateInputParams)" />
253
+ </template>
254
+ </FlowEdit>
255
+ </template>
256
+
257
+ <script setup>
258
+ import { ref } from 'vue';
259
+ import { FlowEdit, type PluginInputDataItem } from "bkflow-canvas-editor";
260
+ import CustomInputParams from "./components/CustomInputParams.vue";
261
+
262
+ const flowEditRef = ref();
263
+ const flowId = "123";
264
+ const apiConfig = {
265
+ baseURL: window.API_HOST,
266
+ scopeData: {
267
+ scope_type: "space",
268
+ scope_value: 123,
269
+ },
270
+ };
271
+
272
+ // 处理输入参数变化
273
+ const handleInputParamsChange = (
274
+ data: Record<string, PluginInputDataItem>,
275
+ updateInputParams: (data: Record<string, PluginInputDataItem>) => void
276
+ ) => {
277
+ // 方式1:使用插槽提供的 updateInputParams 方法(推荐)
278
+ // 这会暂存数据到节点配置面板,用户点击"确定"按钮后才会真正保存
279
+ updateInputParams(data);
280
+
281
+ // 方式2:通过 ref 调用组件方法(适用于需要在外部更新)
282
+ // flowEditRef.value?.updateNodeInputParams(data);
283
+ };
284
+ </script>
285
+ ```
286
+
287
+ ### 使用自定义 axios 实例
288
+
289
+ 如果需要使用已配置好的 axios 实例(包含拦截器等),可以传入 `axiosInstance`:
290
+
291
+ ```vue
292
+ <script setup>
293
+ import { FlowView } from 'bkflow-canvas-editor';
294
+ import http from '@/api'; // 你的 axios 实例
295
+
296
+ const apiConfig = {
297
+ axiosInstance: http, // 使用自定义 axios 实例
298
+ scopeData: {
299
+ scope_type: 'space',
300
+ scope_value: 123,
301
+ },
302
+ };
303
+ </script>
304
+ ```
305
+
306
+ ### 监听流程编辑状态
307
+
308
+ ```vue
309
+ <template>
310
+ <FlowEdit ref="flowEditRef" :flow-id="flowId" :api-config="apiConfig" @back="handleBack" />
311
+ </template>
312
+
313
+ <script setup>
314
+ import { ref, watch } from 'vue';
315
+ import { FlowEdit } from 'bkflow-canvas-editor';
316
+
317
+ const flowEditRef = ref();
318
+
319
+ // 监听流程编辑状态
320
+ watch(
321
+ () => flowEditRef.value?.isFlowEdited,
322
+ (isEdited) => {
323
+ if (isEdited) {
324
+ console.log('流程已被编辑');
325
+ }
326
+ },
327
+ );
328
+ </script>
329
+ ```
330
+
331
+ ### 使用自定义头部
332
+
333
+ ```vue
334
+ <template>
335
+ <FlowEdit :flow-id="flowId" :api-config="apiConfig">
336
+ <template #header>
337
+ <div class="custom-header">
338
+ <h2>自定义头部</h2>
339
+ </div>
340
+ </template>
341
+ </FlowEdit>
342
+ </template>
343
+ ```
344
+
345
+ ## API 配置
346
+
347
+ ### FlowApiConfig
348
+
349
+ 组件需要的 API 配置接口:
350
+
351
+ ```typescript
352
+ interface FlowApiConfig {
353
+ /** API 基础路径(可选,默认使用 window.API_HOST) */
354
+ baseURL?: string;
355
+ /** 自定义 axios 实例(可选,如果提供则优先使用) */
356
+ axiosInstance?: AxiosInstance;
357
+ /** axios 配置(可选,用于创建新的 axios 实例) */
358
+ axiosConfig?: AxiosRequestConfig;
359
+ /** 作用域数据(必需,由外部传入) */
360
+ scopeData: {
361
+ scope_type: string;
362
+ scope_value: number;
363
+ };
364
+ /** 用户查询 API 地址(可选) */
365
+ fetchUserApi?: string;
366
+ }
367
+ ```
368
+
369
+ 组件内部会自动实现所有 API 方法,包括:
370
+
371
+ - `fetchFlowDetail` - 获取流程详情
372
+ - `fetchSpaceFlowConfig` - 获取空间配置
373
+ - `fetchSystemVariables` - 获取系统变量
374
+ - `saveFlow` - 保存流程(仅 FlowEdit)
375
+ - `fetchPluginCategoryList` - 获取插件分类列表(仅 FlowEdit)
376
+ - `fetchCategoryPlugins` - 获取分类插件(仅 FlowEdit)
377
+ - `fetchPluginDetail` - 获取插件详情(可选)
378
+ - `fetchApplyInstructionData` - 获取指令数据(可选)
379
+ - `fetchVariableRef` - 获取变量引用(可选)
380
+ - `fetchCustomVariableTypes` - 获取自定义变量类型(可选)
381
+ - `fetchFlowOperateRecord` - 获取操作记录(可选)
382
+ - `createFlowTask` - 创建流程任务(可选)
383
+ - `executeFlowTask` - 执行流程任务(可选)
384
+
385
+ 所有 API 请求会自动合并 `scopeData` 到请求参数中。
386
+
387
+ ## 类型定义
388
+
389
+ 所有类型定义都可以从包中导入:
390
+
391
+ ```typescript
392
+ import type {
393
+ FlowTemplate,
394
+ SpaceFlowConfig,
395
+ PluginGroupConfig,
396
+ GroupPluginItem,
397
+ UpdateFlowParams,
398
+ FlowApiConfig,
399
+ Variable,
400
+ PipelineTree,
401
+ Activity,
402
+ PluginInputDataItem,
403
+ UniformApiPluginInputsItem,
404
+ UniformApiPluginDetail,
405
+ // ... 更多类型
406
+ } from 'bkflow-canvas-editor';
407
+ ```
408
+
409
+ > **注意**:`FlowViewApiConfig` 和 `FlowEditApiConfig` 是组件内部使用的类型,外部使用者只需要使用 `FlowApiConfig`。组件内部会通过 `useFlowApi` 自动将 `FlowApiConfig` 转换为所需的 API 方法接口。
410
+
411
+ ## 本地开发
412
+
413
+ 如果需要在当前项目中使用本地开发版本,可以在 `frontend/package.json` 中添加:
414
+
415
+ ```json
416
+ {
417
+ "dependencies": {
418
+ "bkflow-canvas-editor": "file:../bkflow-canvas-editor"
419
+ }
420
+ }
421
+ ```
422
+
423
+ 然后在 `frontend/vite.config.ts` 中添加别名:
424
+
425
+ ```typescript
426
+ resolve: {
427
+ alias: {
428
+ 'bkflow-canvas-editor': resolve(__dirname, '../bkflow-canvas-editor/src'),
429
+ },
430
+ }
431
+ ```
432
+
433
+ ## 注意事项
434
+
435
+ 1. **scopeData 必需**:`scopeData` 是必需的配置项,必须由外部传入,组件内部不会处理或设置默认值
436
+ 2. **API 自动处理**:所有 API 调用由组件内部自动处理,无需手动实现 API 方法
437
+ 3. **axios 实例优先级**:如果提供了 `axiosInstance`,组件会优先使用该实例;否则会根据 `baseURL` 和 `axiosConfig` 创建新的 axios 实例
438
+ 4. **自动合并 scopeData**:所有 API 请求会自动合并 `scopeData` 到请求参数中(GET 请求合并到 params,POST/PUT 请求合并到请求体)
439
+ 5. **权限检查**:权限检查通过 `permissions` props 传入布尔值
440
+ 6. **路由跳转**:路由跳转需要通过事件回调处理,组件内部不直接使用 vue-router
441
+ 7. **样式引入**:样式文件需要单独引入:`import 'bkflow-canvas-editor/style'`
442
+ 8. **输入参数更新机制**:
443
+ - 使用 `inputParams` 插槽时,通过 `updateInputParams` 或 `updateNodeInputParams` 方法更新的数据会暂存到节点配置面板
444
+ - 用户需要点击节点配置面板的"确定"按钮,数据才会真正保存到流程中
445
+ - 这种设计允许用户在保存前多次修改和预览
446
+ 9. **API 配置类型说明**:
447
+ - 外部使用者只需要使用 `FlowApiConfig` 类型
448
+ - `FlowViewApiConfig` 和 `FlowEditApiConfig` 是组件内部使用的类型,通过 `useFlowApi` 自动转换
449
+ - 如果直接使用 `FlowExecute` 组件,需要通过 `useFlowApi` 将 `FlowApiConfig` 转换后传入
450
+
451
+ ## License
452
+
453
+ MIT