CRITICAL:bpmn-js Modeler.saveXML 把被 activiti.json `extends` 的 bpmn 类型序列化成
PascalCase 元素名(<Process>/<UserTask>/<StartEvent>…),未扩展的 <sequenceFlow>/<endEvent>
仍小写。Flowable 大小写敏感 → <Process> 不被识别为流程,ProcessValidator 报
flowable-process-definition-not-executable,部署完全失败。
根因:moddle 把「被扩展类型」的合并描述符 $pkg 设为**最后一个 trait 的包**(moddle
index.esm.js:577)。activiti 包缺 `xml.tagAlias:"lowerCase"`,故 moddle-xml 写元素名时不做
首字母小写 → PascalCase。且 tagAlias 是**双向**的(也驱动 fromXML 解析),要求自定义元素类型名
用 activiti/camunda 惯例的 PascalCase,否则解析 <activiti:taskListener> 找不到类型而丢弃。
修法(对齐 activiti/camunda-bpmn-moddle 标准):
- activiti.json + flowable.json 加 `"xml": { "tagAlias": "lowerCase" }`;
- 自定义元素类型名改 PascalCase:taskListener→TaskListener / executionListener→ExecutionListener
/ field→Field / formProperty→FormProperty / value→Value / in→In / out→Out(属性 type 引用同步)。
XML 元素名仍小写(tagAlias),故 fixture 与「toContain('activiti:taskListener')」类 XML 断言不变。
- Eip4Panel.tsx:moddle.create/$type 用 'activiti:TaskListener';测试 fake 类型名同步。
为何旧测试没抓到:T1.1/T1.2 只断言 activiti 扩展属性/元素多重集,从不断言基础 BPMN 元素名大小写;
fromXML→toXML 与 create 都走同一 moddle-xml Writer 且都产 PascalCase(本 commit 实证)。
新增 serialization.test.ts(4 例,RED→GREEN):以 BpmnModdle(= Modeler.saveXML 的 moddle 层,
jsdom 跑不动 diagram-js SVG)驱动 fromXML→toXML 与 create→toXML,断言基础元素名全小写、无 PascalCase、
isExecutable 保留、扩展元素/属性不丢;flowable 隔离同验。
serialization 4 + fidelity 12 + Eip4Panel 37;全量 231 单测绿,tsc -b rc=0 + build 干净。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
806 lines
33 KiB
TypeScript
806 lines
33 KiB
TypeScript
// EIP4 定制侧板:选中 userTask 时叠加办理人/会签/formKey/扩展属性/任务监听器五控件。
|
||
//
|
||
// 设计要点(计划 T2.4 + §0.5 P5 + 评审收口):
|
||
// - 全部写操作经 bpmn-js `modeling.updateProperties(element,{...})`,undo/redo 天然生效。
|
||
// - 办理人/会签共用 items 编辑器(ItemsEditor),表达式编解码走 candidateExpression codec。
|
||
// - 🔴 meta 透传硬要求:codec parse 出的外层业务字段(taskSuccessService 等)存入 state,
|
||
// 写回时原样带回 build,否则重新部署会打掉已部署流程的任务后置服务钩子。顶部只读徽标显示,
|
||
// 删光 items 时 confirm 确认才写 undefined(评审 I-3)。
|
||
// - 🔴 会签保真(评审 I-1):isSequential/elementVariable/completionCondition 原始值读进 state、
|
||
// 写回用读到的值(真实库有 isSequential=true / elementVariable=assign / 完成条件 >0.6 等
|
||
// 非预设形态,强写会静默改坏派任);完成条件 body 不匹配预设时置「自定义(保留)」态,
|
||
// 仅用户主动选预设才改写。
|
||
// - 扩展属性只列长尾 $attrs;新增强制 activiti:/flowable: 前缀并拉黑有专用控件的键。
|
||
// - taskListener 重建时原样保留其 field 子元素(moddle 元素引用不动)。
|
||
// - undo/redo:订阅 commandStack.changed,非自身写入时对当前选中重灌本地态。
|
||
import { useEffect, useRef, useState } from 'react';
|
||
import {
|
||
buildCandidateExpression,
|
||
parseCandidateExpression,
|
||
type CandidateVariant,
|
||
type UserPropertyItem,
|
||
} from './candidateExpression';
|
||
import { fetchWfTemplates, type WfTemplateOption } from '../api/processModeler';
|
||
import { toast } from '../components/ui';
|
||
import type { BpmnElement, SelectionModeler } from './useSelectedElement';
|
||
import { useSelectedElement } from './useSelectedElement';
|
||
|
||
// ── bpmn-js 服务契约(最小) ───────────────────────────────────────────────────
|
||
interface Modeling {
|
||
updateProperties(element: BpmnElement, props: Record<string, unknown>): void;
|
||
}
|
||
interface Moddle {
|
||
create(type: string, props?: Record<string, unknown>): Record<string, unknown>;
|
||
}
|
||
export interface PanelModeler extends SelectionModeler {
|
||
get(service: string): unknown;
|
||
}
|
||
|
||
// ── 办理人 type 取值(§0 实测 10 种)+ 中文标签 ────────────────────────────────
|
||
const CANDIDATE_TYPES: Array<{ value: string; label: string }> = [
|
||
{ value: 'user', label: '指定人员' },
|
||
{ value: 'depart', label: '指定部门' },
|
||
{ value: 'position', label: '指定岗位' },
|
||
{ value: 'formSpecifiedDimension', label: '表单指定维度' },
|
||
{ value: 'initiator', label: '流程发起人' },
|
||
{ value: 'sameToTask', label: '同某任务办理人' },
|
||
{ value: 'beforeTaskLeader', label: '上一办理人领导' },
|
||
{ value: 'beforeTaskDepartManager', label: '上一办理人部门经理' },
|
||
{ value: 'scriptUser', label: '脚本/表达式' },
|
||
{ value: 'projectRoleType', label: '项目角色' },
|
||
];
|
||
|
||
const LISTENER_EVENTS = ['create', 'assignment', 'complete', 'all'];
|
||
|
||
// 有专用控件的属性,禁止再从「扩展属性」兜底编辑器写入(去重、防冲突)。
|
||
const RESERVED_EXT_LOCALNAMES = new Set(['candidateUsers', 'formKey', 'collection', 'elementVariable']);
|
||
|
||
// ── 会签完成条件预设 ─────────────────────────────────────────────────────────
|
||
type CompletionType = 'all' | 'majority' | 'count' | 'custom';
|
||
const COMPLETION_ALL = '${nrOfCompletedInstances/nrOfInstances==1}';
|
||
const COMPLETION_MAJORITY = '${nrOfCompletedInstances/nrOfInstances>0.5}';
|
||
|
||
function completionBodyFor(s: CosignState): string {
|
||
if (s.completionType === 'majority') return COMPLETION_MAJORITY;
|
||
if (s.completionType === 'count') return '${nrOfCompletedInstances>=' + s.completionN + '}';
|
||
if (s.completionType === 'custom') return s.completionCustomBody;
|
||
return COMPLETION_ALL;
|
||
}
|
||
// 精确匹配预设串(不用子串,否则 >0.55 会误判为过半);非预设/无 body → 自定义(保留)。
|
||
function parseCompletion(body: string | undefined): {
|
||
type: CompletionType;
|
||
n: number;
|
||
custom: string;
|
||
} {
|
||
if (body === COMPLETION_ALL) return { type: 'all', n: 2, custom: '' };
|
||
if (body === COMPLETION_MAJORITY) return { type: 'majority', n: 2, custom: '' };
|
||
const m = body ? body.match(/^\$\{nrOfCompletedInstances>=(\d+)\}$/) : null;
|
||
if (m) return { type: 'count', n: Number(m[1]), custom: '' };
|
||
return { type: 'custom', n: 2, custom: body ?? '' };
|
||
}
|
||
|
||
// ── moddle 读值(兼容 .get(name) 与直读属性) ──────────────────────────────────
|
||
function mget(obj: unknown, name: string): unknown {
|
||
if (!obj || typeof obj !== 'object') return undefined;
|
||
const o = obj as Record<string, unknown> & { get?: (n: string) => unknown };
|
||
if (typeof o.get === 'function') return o.get(name);
|
||
return o[name];
|
||
}
|
||
|
||
// 非空业务 meta → 只读徽标/确认文案(用于「删光办理人会一并移除 X」)。
|
||
function notableMeta(meta: Record<string, unknown>): string[] {
|
||
const out: string[] = [];
|
||
if (typeof meta.taskSuccessService === 'string' && meta.taskSuccessService)
|
||
out.push('任务后置服务:' + meta.taskSuccessService);
|
||
if (typeof meta.messageTemplate === 'string' && meta.messageTemplate) out.push('消息模板');
|
||
if (meta.isSkipInitiator === true) out.push('跳过发起人');
|
||
if (meta.isNoUserSkip === true) out.push('无办理人自动跳过');
|
||
if (meta.isSkipBeforeTask === true) out.push('跳过前置任务办理人');
|
||
return out;
|
||
}
|
||
|
||
// formKey 下拉展示名:网关无名称时 name 回落为 id(uuid),未命名项显示「<uuid前8>…(未命名)」。
|
||
function wfTemplateLabel(t: WfTemplateOption): string {
|
||
if (t.name && t.name !== t.id) return t.name;
|
||
return (t.id.length > 8 ? t.id.slice(0, 8) : t.id) + '…(未命名)';
|
||
}
|
||
|
||
function newCandidateItem(): UserPropertyItem {
|
||
return {
|
||
type: 'user',
|
||
content: '',
|
||
isCandidate: true,
|
||
isSendMail: false,
|
||
isSendPhone: false,
|
||
isSendMessage: false,
|
||
};
|
||
}
|
||
|
||
interface ListenerRow {
|
||
event: string;
|
||
mode: 'class' | 'delegateExpression';
|
||
value: string;
|
||
/** 原监听器的 field 子元素数组(编辑时原样保留,引用不动)。 */
|
||
fields?: unknown[];
|
||
}
|
||
|
||
interface CosignState {
|
||
enabled: boolean;
|
||
items: UserPropertyItem[];
|
||
/** 保真:串行/并行,读回原值(真实库有 isSequential=true)。 */
|
||
isSequential: boolean;
|
||
/** 保真:循环变量名,读回原值(真实库有 assign/loop_assign/缺省);为空则写回时省略该属性。 */
|
||
elementVariable: string;
|
||
completionType: CompletionType;
|
||
completionN: number;
|
||
/** completionType==='custom' 时保留的原始 body;'' 表示原本无 completionCondition。 */
|
||
completionCustomBody: string;
|
||
}
|
||
|
||
const FRESH_COSIGN: CosignState = {
|
||
enabled: false,
|
||
items: [],
|
||
isSequential: false,
|
||
elementVariable: 'assignee',
|
||
completionType: 'all',
|
||
completionN: 2,
|
||
completionCustomBody: '',
|
||
};
|
||
|
||
// ── items 编辑器(办理人 / 会签人员复用) ──────────────────────────────────────
|
||
function ItemsEditor({
|
||
idPrefix,
|
||
items,
|
||
onChange,
|
||
}: {
|
||
idPrefix: string;
|
||
items: UserPropertyItem[];
|
||
onChange: (next: UserPropertyItem[]) => void;
|
||
}) {
|
||
const patch = (i: number, p: Partial<UserPropertyItem>) =>
|
||
onChange(items.map((it, idx) => (idx === i ? { ...it, ...p } : it)));
|
||
const remove = (i: number) => onChange(items.filter((_, idx) => idx !== i));
|
||
const add = () => onChange([...items, newCandidateItem()]);
|
||
|
||
return (
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
|
||
{items.map((it, i) => (
|
||
<div
|
||
key={i}
|
||
style={{
|
||
border: '1px solid var(--line)',
|
||
borderRadius: 4,
|
||
padding: 6,
|
||
display: 'flex',
|
||
flexDirection: 'column',
|
||
gap: 4,
|
||
}}
|
||
>
|
||
<div style={{ display: 'flex', gap: 4 }}>
|
||
<select
|
||
data-testid={`${idPrefix}-type-${i}`}
|
||
value={it.type}
|
||
onChange={(e) => patch(i, { type: e.target.value })}
|
||
style={{ flex: '0 0 120px' }}
|
||
>
|
||
{CANDIDATE_TYPES.map((t) => (
|
||
<option key={t.value} value={t.value}>
|
||
{t.label}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<input
|
||
data-testid={`${idPrefix}-content-${i}`}
|
||
value={it.content}
|
||
placeholder="人员/部门/岗位 id 或表达式"
|
||
onChange={(e) => patch(i, { content: e.target.value })}
|
||
style={{ flex: 1, minWidth: 0 }}
|
||
/>
|
||
<button
|
||
className="btn btn-sm"
|
||
data-testid={`${idPrefix}-remove-${i}`}
|
||
onClick={() => remove(i)}
|
||
title="删除该行"
|
||
>
|
||
删
|
||
</button>
|
||
</div>
|
||
<div style={{ display: 'flex', gap: 10, flexWrap: 'wrap', fontSize: 12 }}>
|
||
<label>
|
||
<input
|
||
type="checkbox"
|
||
data-testid={`${idPrefix}-iscandidate-${i}`}
|
||
checked={!!it.isCandidate}
|
||
onChange={(e) => patch(i, { isCandidate: e.target.checked })}
|
||
/>{' '}
|
||
候选
|
||
</label>
|
||
<label>
|
||
<input
|
||
type="checkbox"
|
||
data-testid={`${idPrefix}-mail-${i}`}
|
||
checked={!!it.isSendMail}
|
||
onChange={(e) => patch(i, { isSendMail: e.target.checked })}
|
||
/>{' '}
|
||
邮件
|
||
</label>
|
||
<label>
|
||
<input
|
||
type="checkbox"
|
||
data-testid={`${idPrefix}-phone-${i}`}
|
||
checked={!!it.isSendPhone}
|
||
onChange={(e) => patch(i, { isSendPhone: e.target.checked })}
|
||
/>{' '}
|
||
短信
|
||
</label>
|
||
<label>
|
||
<input
|
||
type="checkbox"
|
||
data-testid={`${idPrefix}-msg-${i}`}
|
||
checked={!!it.isSendMessage}
|
||
onChange={(e) => patch(i, { isSendMessage: e.target.checked })}
|
||
/>{' '}
|
||
站内
|
||
</label>
|
||
</div>
|
||
</div>
|
||
))}
|
||
<button className="btn btn-sm" data-testid={`${idPrefix}-add`} onClick={add}>
|
||
+ 添加办理人
|
||
</button>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function Section({ title, children }: { title: string; children: React.ReactNode }) {
|
||
return (
|
||
<div style={{ borderBottom: '1px solid var(--line)', padding: '10px 12px' }}>
|
||
<div style={{ fontWeight: 600, fontSize: 13, marginBottom: 8 }}>{title}</div>
|
||
{children}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export function Eip4Panel({ modeler }: { modeler: PanelModeler | null }) {
|
||
const element = useSelectedElement(modeler);
|
||
const modeling = modeler ? (modeler.get('modeling') as Modeling | undefined) : undefined;
|
||
const moddle = modeler ? (modeler.get('moddle') as Moddle | undefined) : undefined;
|
||
|
||
const isUserTask = element?.type === 'bpmn:UserTask';
|
||
|
||
// ── 本地编辑态(选中变更 / undo-redo 时从 businessObject 重新初始化) ─────────
|
||
const [candidateItems, setCandidateItems] = useState<UserPropertyItem[]>([]);
|
||
const [candidateMeta, setCandidateMeta] = useState<Record<string, unknown>>({});
|
||
const [candidateNonStandard, setCandidateNonStandard] = useState(false);
|
||
const [cosign, setCosign] = useState<CosignState>(FRESH_COSIGN);
|
||
const [cosignMeta, setCosignMeta] = useState<Record<string, unknown>>({});
|
||
const [formKey, setFormKey] = useState('');
|
||
const [extAttrs, setExtAttrs] = useState<Array<{ key: string; value: string }>>([]);
|
||
const [newAttr, setNewAttr] = useState<{ key: string; value: string }>({ key: '', value: '' });
|
||
const [listeners, setListeners] = useState<ListenerRow[]>([]);
|
||
const [templates, setTemplates] = useState<WfTemplateOption[]>([]);
|
||
const [templatesDegraded, setTemplatesDegraded] = useState(false);
|
||
const [formKeyFilter, setFormKeyFilter] = useState('');
|
||
// undo/redo 重灌计数:commandStack.changed(非自身写入)时自增,触发 init 重跑。
|
||
const [syncTick, setSyncTick] = useState(0);
|
||
|
||
// 非 taskListener 的扩展元素(formProperty 等)原样保留(引用不动)。
|
||
const otherExtRef = useRef<unknown[]>([]);
|
||
// 自身写入标记:updateProperties 会触发 commandStack.changed,借此跳过自触发重灌。
|
||
const selfWriteRef = useRef(false);
|
||
|
||
// formKey 数据源加载一次(透传 degraded;1016 项靠过滤输入,不裸滚动)。
|
||
useEffect(() => {
|
||
let alive = true;
|
||
Promise.resolve(fetchWfTemplates())
|
||
.then((r) => {
|
||
if (!alive) return;
|
||
setTemplates(Array.isArray(r?.templates) ? r.templates : []);
|
||
setTemplatesDegraded(!!r?.degraded);
|
||
})
|
||
.catch(() => {
|
||
if (!alive) return;
|
||
setTemplates([]);
|
||
setTemplatesDegraded(true);
|
||
});
|
||
return () => {
|
||
alive = false;
|
||
};
|
||
}, []);
|
||
|
||
// undo/redo 脱同步防线:非自身写入的 commandStack 变更 → 对当前选中重灌本地态。
|
||
useEffect(() => {
|
||
if (!modeler) return;
|
||
const handler = () => {
|
||
if (selfWriteRef.current) {
|
||
selfWriteRef.current = false; // 自身 updateProperties 触发,不重灌(避免规范化闪烁)
|
||
return;
|
||
}
|
||
setSyncTick((t) => t + 1);
|
||
};
|
||
modeler.on('commandStack.changed', handler);
|
||
return () => {
|
||
modeler.off?.('commandStack.changed', handler);
|
||
};
|
||
}, [modeler]);
|
||
|
||
// 选中变更 / 外部命令 → 从 businessObject 重新初始化本地态。
|
||
useEffect(() => {
|
||
if (!element || element.type !== 'bpmn:UserTask') {
|
||
setCandidateItems([]);
|
||
setCandidateMeta({});
|
||
setCandidateNonStandard(false);
|
||
setCosign(FRESH_COSIGN);
|
||
setCosignMeta({});
|
||
setFormKey('');
|
||
setExtAttrs([]);
|
||
setNewAttr({ key: '', value: '' });
|
||
setListeners([]);
|
||
otherExtRef.current = [];
|
||
return;
|
||
}
|
||
const bo = element.businessObject;
|
||
|
||
// 办理人
|
||
const rawCand = bo.get('activiti:candidateUsers') as string | undefined;
|
||
const cand = parseCandidateExpression(rawCand);
|
||
setCandidateItems(cand?.items ?? []);
|
||
setCandidateMeta(cand?.meta ?? {});
|
||
setCandidateNonStandard(!!rawCand && !cand); // 非空但解析失败=非标准表达式
|
||
|
||
// 会签(保真读回 isSequential/elementVariable/completionCondition 原值)
|
||
const loop = bo.get('loopCharacteristics');
|
||
if (loop) {
|
||
const parsed = parseCandidateExpression(mget(loop, 'activiti:collection') as string | undefined);
|
||
const cc = parseCompletion(
|
||
(mget(loop, 'completionCondition') as { body?: string } | undefined)?.body,
|
||
);
|
||
setCosign({
|
||
enabled: true,
|
||
items: parsed?.items ?? [],
|
||
isSequential: Boolean(mget(loop, 'isSequential')),
|
||
elementVariable: (mget(loop, 'activiti:elementVariable') as string) ?? '',
|
||
completionType: cc.type,
|
||
completionN: cc.n,
|
||
completionCustomBody: cc.custom,
|
||
});
|
||
setCosignMeta(parsed?.meta ?? {});
|
||
} else {
|
||
setCosign(FRESH_COSIGN);
|
||
setCosignMeta({});
|
||
}
|
||
|
||
// formKey
|
||
setFormKey((bo.get('activiti:formKey') as string) ?? '');
|
||
|
||
// 扩展属性(长尾 $attrs,只留 activiti:/flowable: 前缀)
|
||
const attrs = bo.$attrs ?? {};
|
||
setExtAttrs(
|
||
Object.entries(attrs)
|
||
.filter(([k]) => k.startsWith('activiti:') || k.startsWith('flowable:'))
|
||
.map(([key, value]) => ({ key, value: String(value) })),
|
||
);
|
||
setNewAttr({ key: '', value: '' });
|
||
|
||
// 任务监听器
|
||
const extEl = bo.get('extensionElements') as { values?: unknown[] } | undefined;
|
||
const values = (extEl?.values ?? []) as unknown[];
|
||
// moddle 类型名遵循 activiti/camunda 惯例=PascalCase(xml.tagAlias:lowerCase 令 XML 元素名仍小写
|
||
// <activiti:taskListener>),故按 $type 'activiti:TaskListener' 识别;小写名会致扩展类型 $pkg 串味、
|
||
// 断部署(见 moddle/serialization.test.ts)。
|
||
otherExtRef.current = values.filter((v) => (v as { $type?: string }).$type !== 'activiti:TaskListener');
|
||
setListeners(
|
||
values
|
||
.filter((v) => (v as { $type?: string }).$type === 'activiti:TaskListener')
|
||
.map((v) => {
|
||
const cls = mget(v, 'class') as string | undefined;
|
||
const del = mget(v, 'delegateExpression') as string | undefined;
|
||
const fields = ((v as { fields?: unknown[] }).fields ?? mget(v, 'fields')) as unknown[] | undefined;
|
||
return {
|
||
event: (mget(v, 'event') as string) ?? 'create',
|
||
mode: cls ? 'class' : del ? 'delegateExpression' : 'class',
|
||
value: cls ?? del ?? '',
|
||
fields: fields && fields.length ? fields : undefined,
|
||
} as ListenerRow;
|
||
}),
|
||
);
|
||
}, [element, syncTick]);
|
||
|
||
function update(props: Record<string, unknown>) {
|
||
if (element && modeling) {
|
||
selfWriteRef.current = true;
|
||
modeling.updateProperties(element, props);
|
||
}
|
||
}
|
||
|
||
// ── 办理人写回 ─────────────────────────────────────────────────────────────
|
||
function writeCandidate(items: UserPropertyItem[]) {
|
||
if (items.length === 0) {
|
||
// 删光 items:若外层含非空业务 meta,confirm 确认才写 undefined(否则连带清除业务字段)。
|
||
const notable = notableMeta(candidateMeta);
|
||
if (
|
||
notable.length > 0 &&
|
||
typeof window !== 'undefined' &&
|
||
!window.confirm('清空办理人将一并移除:' + notable.join('、') + '。确认清空?')
|
||
) {
|
||
return; // 取消:本地态与引擎均不变
|
||
}
|
||
setCandidateItems([]);
|
||
update({ 'activiti:candidateUsers': undefined });
|
||
return;
|
||
}
|
||
setCandidateItems(items);
|
||
update({ 'activiti:candidateUsers': buildCandidateExpression(items, 'candidate', candidateMeta) });
|
||
}
|
||
|
||
// ── 会签写回 ───────────────────────────────────────────────────────────────
|
||
function writeCosign(next: CosignState) {
|
||
setCosign(next);
|
||
if (!next.enabled) {
|
||
update({ loopCharacteristics: undefined });
|
||
return;
|
||
}
|
||
if (!moddle) return;
|
||
const collection = buildCandidateExpression(
|
||
next.items,
|
||
'multiInstance' as CandidateVariant,
|
||
cosignMeta,
|
||
);
|
||
const props: Record<string, unknown> = {
|
||
isSequential: next.isSequential,
|
||
'activiti:collection': collection,
|
||
};
|
||
// elementVariable 为空(真实库缺省态)则省略,不凭空写空属性。
|
||
if (next.elementVariable) props['activiti:elementVariable'] = next.elementVariable;
|
||
const body = completionBodyFor(next);
|
||
// body 为空(原本无 completionCondition)则不发出该子元素,保真。
|
||
if (body) props.completionCondition = moddle.create('bpmn:FormalExpression', { body });
|
||
const loop = moddle.create('bpmn:MultiInstanceLoopCharacteristics', props);
|
||
update({ loopCharacteristics: loop });
|
||
}
|
||
|
||
// ── formKey 写回 ───────────────────────────────────────────────────────────
|
||
function writeFormKey(value: string) {
|
||
setFormKey(value);
|
||
update({ 'activiti:formKey': value === '' ? undefined : value });
|
||
}
|
||
|
||
// ── 扩展属性写回 ───────────────────────────────────────────────────────────
|
||
function addExtAttr() {
|
||
const key = newAttr.key.trim();
|
||
if (!key) return;
|
||
if (!key.startsWith('activiti:') && !key.startsWith('flowable:')) {
|
||
toast('扩展属性键必须以 activiti: 或 flowable: 开头', 'warn');
|
||
return;
|
||
}
|
||
const local = key.slice(key.indexOf(':') + 1);
|
||
if (RESERVED_EXT_LOCALNAMES.has(local)) {
|
||
toast('该属性有专用控件,请用上方对应控件编辑', 'warn');
|
||
return;
|
||
}
|
||
setExtAttrs((prev) => {
|
||
const rest = prev.filter((a) => a.key !== key);
|
||
return [...rest, { key, value: newAttr.value }];
|
||
});
|
||
update({ [key]: newAttr.value });
|
||
setNewAttr({ key: '', value: '' });
|
||
}
|
||
function editExtAttr(key: string, value: string) {
|
||
setExtAttrs((prev) => prev.map((a) => (a.key === key ? { ...a, value } : a)));
|
||
update({ [key]: value });
|
||
}
|
||
function removeExtAttr(key: string) {
|
||
setExtAttrs((prev) => prev.filter((a) => a.key !== key));
|
||
update({ [key]: undefined });
|
||
}
|
||
|
||
// ── 任务监听器写回 ─────────────────────────────────────────────────────────
|
||
function writeListeners(next: ListenerRow[]) {
|
||
setListeners(next);
|
||
if (!moddle) return;
|
||
const listenerEls = next.map((l) => {
|
||
const props: Record<string, unknown> = { event: l.event };
|
||
props[l.mode] = l.value;
|
||
if (l.fields && l.fields.length) props.fields = l.fields;
|
||
return moddle.create('activiti:TaskListener', props);
|
||
});
|
||
const all = [...otherExtRef.current, ...listenerEls];
|
||
const extEl = all.length ? moddle.create('bpmn:ExtensionElements', { values: all }) : undefined;
|
||
update({ extensionElements: extEl });
|
||
}
|
||
function addListener() {
|
||
writeListeners([...listeners, { event: 'create', mode: 'class', value: '' }]);
|
||
}
|
||
function patchListener(i: number, p: Partial<ListenerRow>) {
|
||
writeListeners(listeners.map((l, idx) => (idx === i ? { ...l, ...p } : l)));
|
||
}
|
||
function removeListener(i: number) {
|
||
writeListeners(listeners.filter((_, idx) => idx !== i));
|
||
}
|
||
|
||
// ── 渲染 ───────────────────────────────────────────────────────────────────
|
||
if (!element) {
|
||
return (
|
||
<div data-testid="process-modeler-eip4-panel" style={hintStyle}>
|
||
请选择一个用户任务节点以编辑 EIP4 属性。
|
||
</div>
|
||
);
|
||
}
|
||
if (!isUserTask) {
|
||
return (
|
||
<div data-testid="process-modeler-eip4-panel" style={hintStyle}>
|
||
选中的不是用户任务节点。EIP4 定制控件仅对用户任务(办理人 / 会签 / formKey)可用。
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const candidateBadges = notableMeta(candidateMeta);
|
||
const formKeyQuery = formKeyFilter.trim().toLowerCase();
|
||
const filteredTemplates = formKeyQuery
|
||
? templates.filter(
|
||
(t) => t.name.toLowerCase().includes(formKeyQuery) || t.id.toLowerCase().includes(formKeyQuery),
|
||
)
|
||
: templates;
|
||
|
||
return (
|
||
<div
|
||
data-testid="process-modeler-eip4-panel"
|
||
style={{ overflow: 'auto', maxHeight: '55%', borderBottom: '1px solid var(--line)' }}
|
||
>
|
||
<div style={{ padding: '8px 12px', fontSize: 12, color: 'var(--text-3)', background: 'var(--surface-2, #fafafa)' }}>
|
||
EIP4 引擎以本控件为准,下方标准面板原生字段仅供高级调试。
|
||
</div>
|
||
|
||
<Section title="办理人">
|
||
{candidateBadges.length > 0 ? (
|
||
<div
|
||
data-testid="eip4-candidate-meta-badge"
|
||
style={{ fontSize: 12, color: 'var(--text-2)', marginBottom: 8, lineHeight: 1.6 }}
|
||
>
|
||
含业务配置:{candidateBadges.join('、')}(清空办理人会一并移除)
|
||
</div>
|
||
) : null}
|
||
{candidateNonStandard ? (
|
||
<div
|
||
data-testid="eip4-candidate-nonstandard"
|
||
style={{ fontSize: 12, color: 'var(--warn, #d48806)', marginBottom: 8 }}
|
||
>
|
||
当前为非标准办理人表达式,编辑将覆盖原表达式。
|
||
</div>
|
||
) : null}
|
||
<ItemsEditor idPrefix="eip4-candidate" items={candidateItems} onChange={writeCandidate} />
|
||
</Section>
|
||
|
||
<Section title="会签(多实例)">
|
||
<label style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 13, marginBottom: 8 }}>
|
||
<input
|
||
type="checkbox"
|
||
data-testid="eip4-cosign-toggle"
|
||
checked={cosign.enabled}
|
||
onChange={(e) => writeCosign({ ...cosign, enabled: e.target.checked })}
|
||
/>
|
||
启用会签
|
||
</label>
|
||
{cosign.enabled ? (
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
|
||
<label style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 13 }}>
|
||
<input
|
||
type="checkbox"
|
||
data-testid="eip4-cosign-sequential"
|
||
checked={cosign.isSequential}
|
||
onChange={(e) => writeCosign({ ...cosign, isSequential: e.target.checked })}
|
||
/>
|
||
串行(顺序会签)
|
||
</label>
|
||
<label style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 13 }}>
|
||
<span style={{ flex: '0 0 auto' }}>循环变量</span>
|
||
<input
|
||
data-testid="eip4-cosign-element-variable"
|
||
value={cosign.elementVariable}
|
||
placeholder="assignee"
|
||
onChange={(e) => writeCosign({ ...cosign, elementVariable: e.target.value })}
|
||
style={{ flex: 1, minWidth: 0 }}
|
||
/>
|
||
</label>
|
||
<ItemsEditor
|
||
idPrefix="eip4-cosign"
|
||
items={cosign.items}
|
||
onChange={(items) => writeCosign({ ...cosign, items })}
|
||
/>
|
||
<div style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 13, flexWrap: 'wrap' }}>
|
||
<span>完成条件</span>
|
||
<select
|
||
data-testid="eip4-cosign-completion-type"
|
||
value={cosign.completionType}
|
||
onChange={(e) =>
|
||
writeCosign({ ...cosign, completionType: e.target.value as CompletionType })
|
||
}
|
||
>
|
||
{/* 自定义(保留)仅在当前为自定义态时可选中——presets 不提供切到自定义,防误清 */}
|
||
{cosign.completionType === 'custom' ? (
|
||
<option value="custom">自定义(保留)</option>
|
||
) : null}
|
||
<option value="all">全票通过</option>
|
||
<option value="majority">过半通过</option>
|
||
<option value="count">指定数</option>
|
||
</select>
|
||
{cosign.completionType === 'count' ? (
|
||
<input
|
||
type="number"
|
||
min={1}
|
||
data-testid="eip4-cosign-completion-n"
|
||
value={cosign.completionN}
|
||
onChange={(e) =>
|
||
writeCosign({ ...cosign, completionN: Number(e.target.value) || 1 })
|
||
}
|
||
style={{ width: 64 }}
|
||
/>
|
||
) : null}
|
||
{cosign.completionType === 'custom' ? (
|
||
<span
|
||
data-testid="eip4-cosign-completion-custom"
|
||
style={{ fontSize: 12, color: 'var(--text-3)' }}
|
||
>
|
||
当前条件:<code>{cosign.completionCustomBody || '(无)'}</code>
|
||
</span>
|
||
) : null}
|
||
</div>
|
||
</div>
|
||
) : null}
|
||
</Section>
|
||
|
||
<Section title="表单(formKey)">
|
||
{templatesDegraded ? (
|
||
<div
|
||
data-testid="eip4-formkey-degraded"
|
||
style={{ fontSize: 12, color: 'var(--warn, #d48806)', marginBottom: 6 }}
|
||
>
|
||
模板列表暂不可用(数据源异常);可保留当前值或稍后重试。
|
||
</div>
|
||
) : null}
|
||
<input
|
||
data-testid="eip4-formkey-filter"
|
||
placeholder={`筛选表单(名称或 id,共 ${templates.length} 项)`}
|
||
value={formKeyFilter}
|
||
onChange={(e) => setFormKeyFilter(e.target.value)}
|
||
style={{ width: '100%', marginBottom: 6 }}
|
||
/>
|
||
<select
|
||
data-testid="eip4-formkey-select"
|
||
value={formKey}
|
||
onChange={(e) => writeFormKey(e.target.value)}
|
||
style={{ width: '100%' }}
|
||
>
|
||
<option value="">(无)</option>
|
||
{/* 当前值不在(过滤后的)模板列表时,补一条占位以免下拉丢值 */}
|
||
{formKey && !filteredTemplates.some((t) => t.id === formKey) ? (
|
||
<option value={formKey}>
|
||
{(() => {
|
||
const cur = templates.find((t) => t.id === formKey);
|
||
return (cur ? wfTemplateLabel(cur) : formKey) + '(当前)';
|
||
})()}
|
||
</option>
|
||
) : null}
|
||
{filteredTemplates.map((t) => (
|
||
<option key={t.id} value={t.id}>
|
||
{wfTemplateLabel(t)}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</Section>
|
||
|
||
<Section title="扩展属性(activiti/flowable)">
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
|
||
{extAttrs.map((a, i) => (
|
||
<div key={a.key} style={{ display: 'flex', gap: 4 }}>
|
||
<input
|
||
data-testid={`eip4-extattr-key-${i}`}
|
||
value={a.key}
|
||
readOnly
|
||
style={{ flex: '0 0 130px', minWidth: 0 }}
|
||
/>
|
||
<input
|
||
data-testid={`eip4-extattr-value-${i}`}
|
||
value={a.value}
|
||
onChange={(e) => editExtAttr(a.key, e.target.value)}
|
||
style={{ flex: 1, minWidth: 0 }}
|
||
/>
|
||
<button
|
||
className="btn btn-sm"
|
||
data-testid={`eip4-extattr-remove-${i}`}
|
||
onClick={() => removeExtAttr(a.key)}
|
||
>
|
||
删
|
||
</button>
|
||
</div>
|
||
))}
|
||
<div style={{ display: 'flex', gap: 4 }}>
|
||
<input
|
||
data-testid="eip4-extattr-new-key"
|
||
placeholder="activiti:key"
|
||
value={newAttr.key}
|
||
onChange={(e) => setNewAttr((s) => ({ ...s, key: e.target.value }))}
|
||
style={{ flex: '0 0 130px', minWidth: 0 }}
|
||
/>
|
||
<input
|
||
data-testid="eip4-extattr-new-value"
|
||
placeholder="value"
|
||
value={newAttr.value}
|
||
onChange={(e) => setNewAttr((s) => ({ ...s, value: e.target.value }))}
|
||
style={{ flex: 1, minWidth: 0 }}
|
||
/>
|
||
<button className="btn btn-sm" data-testid="eip4-extattr-add" onClick={addExtAttr}>
|
||
+ 增
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</Section>
|
||
|
||
<Section title="任务监听器(taskListener)">
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
|
||
{listeners.map((l, i) => (
|
||
<div
|
||
key={i}
|
||
style={{ border: '1px solid var(--line)', borderRadius: 4, padding: 6, display: 'flex', flexDirection: 'column', gap: 4 }}
|
||
>
|
||
<div style={{ display: 'flex', gap: 4, alignItems: 'center' }}>
|
||
<select
|
||
data-testid={`eip4-listener-event-${i}`}
|
||
value={l.event}
|
||
onChange={(e) => patchListener(i, { event: e.target.value })}
|
||
style={{ flex: '0 0 110px' }}
|
||
>
|
||
{LISTENER_EVENTS.map((ev) => (
|
||
<option key={ev} value={ev}>
|
||
{ev}
|
||
</option>
|
||
))}
|
||
</select>
|
||
<select
|
||
data-testid={`eip4-listener-mode-${i}`}
|
||
value={l.mode}
|
||
onChange={(e) =>
|
||
patchListener(i, { mode: e.target.value as ListenerRow['mode'] })
|
||
}
|
||
style={{ flex: '0 0 130px' }}
|
||
>
|
||
<option value="class">class</option>
|
||
<option value="delegateExpression">delegateExpression</option>
|
||
</select>
|
||
<button
|
||
className="btn btn-sm"
|
||
data-testid={`eip4-listener-remove-${i}`}
|
||
onClick={() => removeListener(i)}
|
||
>
|
||
删
|
||
</button>
|
||
</div>
|
||
<input
|
||
data-testid={`eip4-listener-value-${i}`}
|
||
value={l.value}
|
||
placeholder={l.mode === 'class' ? '全限定类名' : '${bean}'}
|
||
onChange={(e) => patchListener(i, { value: e.target.value })}
|
||
/>
|
||
{l.fields && l.fields.length ? (
|
||
<span style={{ fontSize: 11, color: 'var(--text-3)' }}>
|
||
含 field 配置({l.fields.length})· 编辑时原样保留
|
||
</span>
|
||
) : null}
|
||
</div>
|
||
))}
|
||
<button className="btn btn-sm" data-testid="eip4-listener-add" onClick={addListener}>
|
||
+ 添加监听器
|
||
</button>
|
||
</div>
|
||
</Section>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const hintStyle: React.CSSProperties = {
|
||
padding: 12,
|
||
color: 'var(--text-3)',
|
||
fontSize: 12,
|
||
borderBottom: '1px solid var(--line)',
|
||
};
|