feat(modeler): /process-modeler 页骨架(Modeler+标准属性面板+存量载入+设计中心入口)
阶段1 T1.5:admin 门 + 三栏布局(左画布/右上 EIP4 侧板占位/右下标准属性面板) + bpmn-js lib/Modeler 挂载(只载 activiti moddle、只引标准 BpmnPropertiesProvider, 严禁 Camunda provider)+ ?procDefId= 时 fetchProcDefSource→importXML(idMap 存 ref) + 无参时 process-select 存量流程选择器 + 设计中心「流程设计」入口。 api: processModeler.ts(fetchProcDefSource / fetchProcessOptions,裸 JSON 防御性默认)。 - procDefId 用 encodeURIComponent 进 path(真实库 id 形如「总包合同交底:3:<uuid>」中文+冒号)。 - found:false 先判后返,不解构 bpmnXml/idMap(后端此分支省略这些键)。 tsconfig.app.json 开 resolveJsonModule(首个 app 代码 import json)。 bpmn-js-properties-panel 补 ambient d.ts(包未发布类型)。 测试:ProcessModelerPage.test.tsx 5 例 + processModeler.test.ts 6 例(含中文+冒号 procDefId 断言 URL 已编码 / found:false 只返 found 键 / amis 信封解包); DesignCenterPage.entrances 加 dc-process-modeler admin-only 断言。 全量 vitest 22 文件 112 例绿;tsc -b 干净;npm run build 成功(bpmn-js 进主 bundle)。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -30,10 +30,11 @@ describe('DesignCenterPage 入口按钮', () => {
|
||||
profileMock.mockReset();
|
||||
});
|
||||
|
||||
it('admin 看到 [+ 新建功能] 与 [⚡ AI 建功能]', async () => {
|
||||
it('admin 看到 [流程设计] [+ 新建功能] 与 [⚡ AI 建功能]', async () => {
|
||||
profileMock.mockReturnValue({ userId: 'ADMIN', account: 'admin', name: 'x', roles: [] });
|
||||
renderPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('dc-process-modeler')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('dc-new-feature')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('dc-ai-feature')).toBeInTheDocument();
|
||||
});
|
||||
@@ -42,6 +43,7 @@ describe('DesignCenterPage 入口按钮', () => {
|
||||
it('非 admin 不渲染入口(整页降级)', () => {
|
||||
profileMock.mockReturnValue({ userId: 'U1', account: 'zhangsan', name: 'z', roles: [] });
|
||||
renderPage();
|
||||
expect(screen.queryByTestId('dc-process-modeler')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('dc-new-feature')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('dc-ai-feature')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
@@ -73,6 +73,14 @@ export function DesignCenterPage() {
|
||||
</div>
|
||||
</div>
|
||||
<div style={{ flex: 1 }} />
|
||||
<button
|
||||
className="btn btn-sm"
|
||||
data-testid="dc-process-modeler"
|
||||
onClick={() => navigate('/process-modeler')}
|
||||
title="在线流程设计器:可视化编辑存量 BPMN 流程 / 新建流程"
|
||||
>
|
||||
流程设计
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-sm"
|
||||
data-testid="dc-new-feature"
|
||||
|
||||
97
src/pages/ProcessModelerPage.test.tsx
Normal file
97
src/pages/ProcessModelerPage.test.tsx
Normal file
@@ -0,0 +1,97 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { SYNTHETIC_BPMN } from '../modeler/moddle/fixtures/synthetic-full-surface.bpmn';
|
||||
|
||||
// bpmn-js Modeler:jsdom 跑不动真渲染,stub 成类(可断言 importXML/destroy 调用)。
|
||||
const { importXMLMock, destroyMock, getMock, onMock, ModelerCtor } = vi.hoisted(() => {
|
||||
const importXMLMock = vi.fn();
|
||||
const destroyMock = vi.fn();
|
||||
const getMock = vi.fn(() => ({}));
|
||||
const onMock = vi.fn();
|
||||
const ModelerCtor = vi.fn().mockImplementation(() => ({
|
||||
importXML: importXMLMock,
|
||||
destroy: destroyMock,
|
||||
get: getMock,
|
||||
on: onMock,
|
||||
}));
|
||||
return { importXMLMock, destroyMock, getMock, onMock, ModelerCtor };
|
||||
});
|
||||
vi.mock('bpmn-js/lib/Modeler', () => ({ default: ModelerCtor }));
|
||||
// 标准属性面板模块只作为 additionalModules 配置项传入被 mock 的 Modeler,永不真正执行。
|
||||
vi.mock('bpmn-js-properties-panel', () => ({
|
||||
BpmnPropertiesPanelModule: {},
|
||||
BpmnPropertiesProviderModule: {},
|
||||
}));
|
||||
vi.mock('../api/processModeler', () => ({
|
||||
fetchProcDefSource: vi.fn(),
|
||||
fetchProcessOptions: vi.fn(),
|
||||
}));
|
||||
vi.mock('../auth/token', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../auth/token')>()),
|
||||
getProfile: vi.fn(),
|
||||
}));
|
||||
|
||||
import { ProcessModelerPage } from './ProcessModelerPage';
|
||||
import { fetchProcDefSource, fetchProcessOptions } from '../api/processModeler';
|
||||
import { getProfile } from '../auth/token';
|
||||
|
||||
const mSource = vi.mocked(fetchProcDefSource);
|
||||
const mOptions = vi.mocked(fetchProcessOptions);
|
||||
const mProfile = vi.mocked(getProfile);
|
||||
|
||||
function renderPage(initialPath = '/process-modeler') {
|
||||
return render(
|
||||
<MemoryRouter initialEntries={[initialPath]}>
|
||||
<ProcessModelerPage />
|
||||
</MemoryRouter>,
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
importXMLMock.mockReset().mockResolvedValue(undefined);
|
||||
destroyMock.mockReset();
|
||||
getMock.mockReset().mockReturnValue({});
|
||||
onMock.mockReset();
|
||||
ModelerCtor.mockClear();
|
||||
mSource.mockReset().mockResolvedValue({ found: true, bpmnXml: SYNTHETIC_BPMN, idMap: { '总包合同交底': 'Proc_zbht' } });
|
||||
mOptions.mockReset().mockResolvedValue([]);
|
||||
mProfile.mockReset().mockReturnValue({ userId: 'ADMIN', account: 'admin', name: '管理员', roles: [] });
|
||||
});
|
||||
|
||||
describe('ProcessModelerPage', () => {
|
||||
it('admin 渲染画布容器与标准属性面板容器', () => {
|
||||
renderPage();
|
||||
expect(screen.getByTestId('process-modeler-canvas')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('process-modeler-props')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('非 admin 降级为「仅管理员可用」文案,不渲染画布', () => {
|
||||
mProfile.mockReturnValue({ userId: 'U9', account: 'zhang', name: '张三', roles: [] });
|
||||
renderPage();
|
||||
expect(screen.getByText(/仅管理员可用/)).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('process-modeler-canvas')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('?procDefId= 时载入存量流程并把返回的 bpmnXml 交给 Modeler.importXML', async () => {
|
||||
renderPage('/process-modeler?procDefId=PD-1');
|
||||
await waitFor(() => expect(mSource).toHaveBeenCalledWith('PD-1'));
|
||||
await waitFor(() => expect(importXMLMock).toHaveBeenCalledWith(SYNTHETIC_BPMN));
|
||||
expect(ModelerCtor).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('接口 found:false 时显示未找到文案且不 importXML', async () => {
|
||||
mSource.mockResolvedValue({ found: false });
|
||||
renderPage('/process-modeler?procDefId=PD-x');
|
||||
await waitFor(() => expect(screen.getByText(/未找到|无 BPMN|加载失败/)).toBeInTheDocument());
|
||||
expect(importXMLMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('无 procDefId 时展示「打开存量流程」选择器(数据源 process-select)', async () => {
|
||||
mOptions.mockResolvedValue([{ label: '总包合同交底', value: '总包合同交底', id: 'PD-9', key: 'zbht' }]);
|
||||
renderPage();
|
||||
await waitFor(() => expect(mOptions).toHaveBeenCalled());
|
||||
expect(screen.getByTestId('process-modeler-open-select')).toBeInTheDocument();
|
||||
expect(await screen.findByText('总包合同交底')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
253
src/pages/ProcessModelerPage.tsx
Normal file
253
src/pages/ProcessModelerPage.tsx
Normal file
@@ -0,0 +1,253 @@
|
||||
// 在线流程 Modeler(B3)——可编辑 BPMN 设计器页骨架。
|
||||
// 本阶段(T1.5)只做:admin 门 + 三栏布局 + bpmn-js Modeler 挂载 + 存量流程只读载入。
|
||||
// EIP4 定制侧板(办理人/会签/formKey…)是 T2.4,本页右上只放占位容器;
|
||||
// 「保存草稿」「发布」是 T2.5/T3.3,本页顶栏先放 disabled 占位按钮。
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useNavigate, useSearchParams } from 'react-router-dom';
|
||||
import Modeler from 'bpmn-js/lib/Modeler';
|
||||
import { BpmnPropertiesPanelModule, BpmnPropertiesProviderModule } from 'bpmn-js-properties-panel';
|
||||
import 'bpmn-js/dist/assets/diagram-js.css';
|
||||
import 'bpmn-js/dist/assets/bpmn-js.css';
|
||||
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css';
|
||||
import '@bpmn-io/properties-panel/dist/assets/properties-panel.css';
|
||||
import activitiModdle from '../modeler/moddle/activiti.json';
|
||||
import { getProfile, isAdminProfile } from '../auth/token';
|
||||
import { fetchProcDefSource, fetchProcessOptions, type ProcessOption } from '../api/processModeler';
|
||||
|
||||
// bpmn-js/lib/Modeler 自带类型,但选项对象里的标准面板模块(本项目无类型)与 moddle JSON
|
||||
// 会与官方选项类型产生摩擦;照 ProcessDiagram 惯例把构造器收窄为本页用到的最小契约再实例化。
|
||||
interface ModelerLike {
|
||||
importXML(xml: string): Promise<unknown>;
|
||||
destroy(): void;
|
||||
get(service: string): unknown;
|
||||
on(event: string, cb: (e: unknown) => void): void;
|
||||
}
|
||||
const ModelerCtor = Modeler as unknown as new (options: Record<string, unknown>) => ModelerLike;
|
||||
|
||||
type LoadState = 'idle' | 'loading' | 'ready' | 'error';
|
||||
|
||||
export function ProcessModelerPage() {
|
||||
const profile = getProfile();
|
||||
const isAdmin = isAdminProfile(profile);
|
||||
const navigate = useNavigate();
|
||||
const [params, setParams] = useSearchParams();
|
||||
const procDefId = params.get('procDefId') ?? '';
|
||||
|
||||
const canvasRef = useRef<HTMLDivElement>(null);
|
||||
const panelRef = useRef<HTMLDivElement>(null);
|
||||
const modelerRef = useRef<ModelerLike | null>(null);
|
||||
// 净化映射 原id→净化id:本阶段仅暂存,发布还原(T3.x)时用。
|
||||
const idMapRef = useRef<Record<string, string>>({});
|
||||
|
||||
const [loadState, setLoadState] = useState<LoadState>('idle');
|
||||
const [loadError, setLoadError] = useState('');
|
||||
const [options, setOptions] = useState<ProcessOption[]>([]);
|
||||
const [optionsError, setOptionsError] = useState('');
|
||||
|
||||
// 无参时拉存量流程选项(数据源复用旧 ProcessSelectDialog 端点)。
|
||||
useEffect(() => {
|
||||
if (!isAdmin || procDefId) return;
|
||||
let alive = true;
|
||||
fetchProcessOptions()
|
||||
.then((opts) => alive && setOptions(opts))
|
||||
.catch(() => alive && setOptionsError('存量流程列表加载失败'));
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [isAdmin, procDefId]);
|
||||
|
||||
// 有 procDefId 时挂载 Modeler 并载入净化后的存量 BPMN。
|
||||
useEffect(() => {
|
||||
if (!isAdmin || !procDefId) return;
|
||||
let alive = true;
|
||||
let modeler: ModelerLike | null = null;
|
||||
setLoadState('loading');
|
||||
setLoadError('');
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const src = await fetchProcDefSource(procDefId);
|
||||
if (!alive) return;
|
||||
if (!src.found || !src.bpmnXml) {
|
||||
setLoadError('未找到该流程的 BPMN 源(可能未部署或无布局信息)');
|
||||
setLoadState('error');
|
||||
return;
|
||||
}
|
||||
if (!canvasRef.current) return;
|
||||
modeler = new ModelerCtor({
|
||||
container: canvasRef.current,
|
||||
propertiesPanel: { parent: panelRef.current },
|
||||
// ⚠️ 只载标准 BpmnPropertiesProviderModule,严禁引 Camunda provider——它会把
|
||||
// camunda 描述符注进 moddle,与 activiti 描述符在 bpmn:UserTask 上同名扩展冲突
|
||||
// (not allowed without redefines) 静默丢弃 userTask(T1.1 评审 M-3 实证)。
|
||||
additionalModules: [BpmnPropertiesPanelModule, BpmnPropertiesProviderModule],
|
||||
// ⚠️ moddleExtensions 只声明 activiti:语料 1509/3 两命名空间不混用,双载会因同名
|
||||
// 扩展属性冲突静默丢弃 userTask;单载 activiti 时 flowable 文档经 moddle lax 往返零丢失。
|
||||
moddleExtensions: { activiti: activitiModdle },
|
||||
});
|
||||
idMapRef.current = src.idMap ?? {};
|
||||
await modeler.importXML(src.bpmnXml);
|
||||
if (!alive) return;
|
||||
modelerRef.current = modeler;
|
||||
try {
|
||||
const canvas = modeler.get('canvas') as { zoom(mode: string, point?: string): void };
|
||||
canvas.zoom('fit-viewport', 'auto');
|
||||
} catch {
|
||||
/* stub / 无布局时忽略 */
|
||||
}
|
||||
setLoadState('ready');
|
||||
} catch {
|
||||
if (alive) {
|
||||
setLoadError('流程加载失败');
|
||||
setLoadState('error');
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
alive = false;
|
||||
if (modeler) {
|
||||
try {
|
||||
modeler.destroy();
|
||||
} catch {
|
||||
/* 视图已卸载 */
|
||||
}
|
||||
}
|
||||
modelerRef.current = null;
|
||||
};
|
||||
}, [isAdmin, procDefId]);
|
||||
|
||||
if (!isAdmin) {
|
||||
return <div style={{ padding: 24, color: 'var(--text-3)' }}>流程设计器仅管理员可用。</div>;
|
||||
}
|
||||
|
||||
function openProcess(id: string) {
|
||||
if (id) setParams({ procDefId: id });
|
||||
}
|
||||
|
||||
const loadableOptions = options.filter((o) => !!o.id);
|
||||
|
||||
return (
|
||||
<div
|
||||
data-testid="process-modeler-root"
|
||||
style={{ display: 'flex', flexDirection: 'column', height: '100%', overflow: 'hidden' }}
|
||||
>
|
||||
{/* 顶栏 */}
|
||||
<div
|
||||
style={{
|
||||
height: 48,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: 12,
|
||||
padding: '0 16px',
|
||||
borderBottom: '1px solid var(--line)',
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
<span style={{ fontWeight: 600, fontSize: 15 }}>流程设计器</span>
|
||||
<span className="muted" style={{ fontSize: 12, flex: 1 }}>
|
||||
{procDefId ? `已打开:${procDefId}` : '在线编辑 BPMN 流程定义(可编辑存量 / 新建)'}
|
||||
</span>
|
||||
<button className="btn btn-sm" data-testid="process-modeler-save-draft" disabled title="保存草稿(下阶段)">
|
||||
保存草稿
|
||||
</button>
|
||||
<button className="btn btn-sm btn-primary" data-testid="process-modeler-publish" disabled title="发布(下阶段)">
|
||||
发布
|
||||
</button>
|
||||
<button className="btn btn-sm" data-testid="process-modeler-back" onClick={() => navigate('/design-center')}>
|
||||
返回
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 主体:左画布 + 右栏(上=EIP4 侧板占位 / 下=标准属性面板) */}
|
||||
<div style={{ flex: 1, display: 'flex', minHeight: 0 }}>
|
||||
<div style={{ flex: 1, position: 'relative', minWidth: 0, background: '#fff' }}>
|
||||
<div ref={canvasRef} data-testid="process-modeler-canvas" style={{ width: '100%', height: '100%' }} />
|
||||
|
||||
{/* 未打开任何流程:叠加「打开存量流程」选择器 + 新建占位 */}
|
||||
{!procDefId ? (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
gap: 12,
|
||||
background: 'var(--surface, #fff)',
|
||||
}}
|
||||
>
|
||||
<div style={{ color: 'var(--text-3)', fontSize: 13 }}>选择一个存量流程开始编辑</div>
|
||||
<select
|
||||
data-testid="process-modeler-open-select"
|
||||
defaultValue=""
|
||||
onChange={(e) => openProcess(e.target.value)}
|
||||
style={{ minWidth: 280, padding: '6px 8px' }}
|
||||
>
|
||||
<option value="">打开存量流程…</option>
|
||||
{loadableOptions.map((o) => (
|
||||
<option key={o.id} value={o.id}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{optionsError ? (
|
||||
<div style={{ color: 'var(--err)', fontSize: 12 }}>{optionsError}</div>
|
||||
) : null}
|
||||
<button className="btn btn-sm" data-testid="process-modeler-new" disabled title="新建流程(下阶段)">
|
||||
+ 新建流程
|
||||
</button>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{/* 载入中 / 载入失败 */}
|
||||
{procDefId && loadState === 'loading' ? (
|
||||
<div style={overlayMsgStyle('var(--text-3)')} data-testid="process-modeler-loading">
|
||||
加载流程中…
|
||||
</div>
|
||||
) : null}
|
||||
{procDefId && loadState === 'error' ? (
|
||||
<div style={overlayMsgStyle('var(--warn, #d48806)')} data-testid="process-modeler-error">
|
||||
{loadError}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div
|
||||
style={{
|
||||
width: 360,
|
||||
flexShrink: 0,
|
||||
borderLeft: '1px solid var(--line)',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
minHeight: 0,
|
||||
}}
|
||||
>
|
||||
{/* EIP4 定制侧板占位(办理人/会签/formKey…由 T2.4 接入) */}
|
||||
<div
|
||||
data-testid="process-modeler-eip4-panel"
|
||||
style={{ padding: 12, borderBottom: '1px solid var(--line)', color: 'var(--text-3)', fontSize: 12 }}
|
||||
>
|
||||
EIP4 定制控件(办理人 / 会签 / formKey)将在此叠加,下阶段接入。
|
||||
</div>
|
||||
{/* 标准属性面板宿主(bpmn-js-properties-panel 渲染进此容器) */}
|
||||
<div data-testid="process-modeler-props" style={{ flex: 1, overflow: 'auto', minHeight: 0 }} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function overlayMsgStyle(color: string): React.CSSProperties {
|
||||
return {
|
||||
position: 'absolute',
|
||||
top: 16,
|
||||
left: 16,
|
||||
padding: '8px 12px',
|
||||
background: 'rgba(255,255,255,0.92)',
|
||||
borderRadius: 4,
|
||||
color,
|
||||
fontSize: 13,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user