0a3f2c5cc6
- 模块化:EdcModule 契约 + registry + 模块独立目录;发布包增加 modules/registry.json - 更名:PDS流程设计套件 / ProcessDesignSuite;同步构建、发布脚本与文档 - 时序图:依赖偏移以时间显示/编辑;紧贴依赖隐藏箭头并在接缝显示标签;动作条磁吸 - 圆周:依赖标签沿圆弧方向、环间空隙;动作旋转磁吸增强;短弧标签优先动作名 - 清理:release 移至源码同级(本地不入库);tool 合并到 tools;发布说明迁移 docs/release-notes
229 lines
9.4 KiB
Dart
229 lines
9.4 KiB
Dart
/// drawio 导出写入器:把流程图序列化为 diagrams.net(draw.io)可编辑的 `.drawio` 文件。
|
||
///
|
||
/// 架构位置:
|
||
/// - 仅流程图视图支持(条形 / 圆周视图只有 SVG / PNG 两种导出);
|
||
/// - 几何完全复用 [FlowLayout](与屏幕画布、SVG 导出同一套坐标与走线),
|
||
/// 保证三种载体(画布 / drawio / SVG)内容一致;
|
||
/// - 输出标准 `<mxfile>` 文档:节点保留动作颜色与「时长 · 工位」语义,
|
||
/// 连线保留 FS / SS 类型与偏移时间标签,泳道为背景带、相位为竖排文字带,
|
||
/// 导入 draw.io 后可直接继续排布与编辑。
|
||
///
|
||
/// 输出约定:
|
||
/// - 坐标与原画布 1:1(不缩放),原点同样在左上角;
|
||
/// - 页面尺寸按内容范围自动设置,四边各留 [_pad] 留白,整体平移到页内;
|
||
/// - 不写入时间戳等易变字段,保证同一工程多次导出结果稳定(便于测试与归档)。
|
||
library;
|
||
|
||
import 'dart:ui';
|
||
|
||
import 'package:sequence_diagram/modules/sequence/layout/flow_layout.dart';
|
||
import 'package:sequence_diagram/model/models.dart';
|
||
import 'package:sequence_diagram/model/units.dart';
|
||
import 'package:sequence_diagram/theme/tokens.dart';
|
||
import 'export_fit.dart';
|
||
|
||
/// drawio 文档写入器。
|
||
///
|
||
/// 通过内部 [StringBuffer] 逐段拼接 XML,最终由 [build] 返回完整文档。
|
||
class DrawioWriter {
|
||
/// 创建写入器。
|
||
///
|
||
/// 参数:
|
||
/// - [project]:数据源(动作、依赖、相位分区、单位);
|
||
/// - [tokens]:配色令牌(内部转为导出专用纯白主题,与 SVG / PNG 一致)。
|
||
///
|
||
/// 构造时不生成任何内容;调用 [build] 才写出文档。
|
||
DrawioWriter({required this.project, required Tokens tokens})
|
||
: tokens = exportTokens(tokens);
|
||
|
||
/// 数据源工程。
|
||
final Project project;
|
||
/// 导出专用配色令牌。
|
||
final Tokens tokens;
|
||
|
||
/// 页内留白(四周各留的空白,逻辑像素)。
|
||
static const double _pad = 40;
|
||
|
||
/// 输出缓冲区,累积 XML 文本片段。
|
||
final StringBuffer _b = StringBuffer();
|
||
|
||
/// 生成完整 drawio 文档字符串。
|
||
///
|
||
/// 返回:以 `<mxfile>` 开头、`</mxfile>` 结尾的字符串。
|
||
/// 副作用:会持续向内部缓冲区追加内容;约定每次导出新建实例。
|
||
String build() {
|
||
final FlowLayout layout = FlowLayout(project);
|
||
if (layout.nodes.isEmpty) {
|
||
return '<mxfile host="app.diagrams.net"><diagram id="flow" name="流程图">'
|
||
'<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root>'
|
||
'</mxGraphModel></diagram></mxfile>';
|
||
}
|
||
// 平移到页内:原点统一挪到 (pad, pad),页面尺寸随内容自适应。
|
||
final double ox = _pad - layout.bounds.left;
|
||
final double oy = _pad - layout.bounds.top;
|
||
final int pageW = (layout.bounds.width + _pad * 2).ceil();
|
||
final int pageH = (layout.bounds.height + _pad * 2).ceil();
|
||
|
||
_b.write('<mxfile host="app.diagrams.net" agent="Sequence Diagram Studio" '
|
||
'version="24.7.8" type="device">');
|
||
_b.write('<diagram id="flow" name="${_esc(project.name.isEmpty ? '流程图' : project.name)}">');
|
||
_b.write('<mxGraphModel dx="1600" dy="900" grid="1" gridSize="10" guides="1" '
|
||
'tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" '
|
||
'pageWidth="$pageW" pageHeight="$pageH" math="0" shadow="0">');
|
||
_b.write('<root>');
|
||
_b.write('<mxCell id="0"/>');
|
||
_b.write('<mxCell id="1" parent="0"/>');
|
||
|
||
_lanes(layout, ox, oy);
|
||
_phases(layout, ox, oy);
|
||
_edges(layout);
|
||
_nodes(layout, ox, oy);
|
||
|
||
_b.write('</root></mxGraphModel></diagram></mxfile>');
|
||
return _b.toString();
|
||
}
|
||
|
||
/// 写入泳道背景带与轨道标题(原样平移)。
|
||
void _lanes(FlowLayout layout, double ox, double oy) {
|
||
final double lanePitch = FlowLayout.nodeW + FlowLayout.laneGap;
|
||
final double top = layout.bounds.top + 20 + oy;
|
||
final double bottom = layout.bounds.bottom - 20 + oy;
|
||
for (int i = 0; i < layout.lanes.length; i++) {
|
||
final double cx = FlowLayout.marginX + i * lanePitch + FlowLayout.nodeW / 2 + ox;
|
||
_rectCell(
|
||
'lane-${layout.lanes[i]}',
|
||
'轨道 L${i + 1}',
|
||
cx - FlowLayout.nodeW / 2 - 26,
|
||
top,
|
||
FlowLayout.nodeW + 52,
|
||
bottom - top,
|
||
'rounded=0;whiteSpace=wrap;html=1;verticalAlign=top;fontSize=11;fontStyle=1;'
|
||
'fillColor=${_hex(i.isEven ? tokens.ringA : tokens.ringB)};fillOpacity=55;'
|
||
'strokeColor=none;fontColor=${_hex(tokens.sub)};',
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 写入相位竖带:竖排文字(`horizontal=0`)+ 低透明度填充,台阶错位保持一致。
|
||
void _phases(FlowLayout layout, double ox, double oy) {
|
||
final double rowPitch = FlowLayout.nodeH + FlowLayout.rankGap;
|
||
for (int i = 0; i < layout.spans.length; i++) {
|
||
final FlowPhaseSpan s = layout.spans[i];
|
||
final double top = FlowLayout.marginY + s.rankTop * rowPitch - 6 + oy;
|
||
final double bottom =
|
||
FlowLayout.marginY + s.rankBottom * rowPitch + FlowLayout.nodeH + 6 + oy;
|
||
final double left = FlowLayout.railX + s.slot * FlowLayout.railStep + ox;
|
||
_rectCell(
|
||
'phase-$i',
|
||
s.zone.name,
|
||
left,
|
||
top,
|
||
FlowLayout.railW,
|
||
bottom - top,
|
||
'rounded=1;whiteSpace=wrap;html=1;horizontal=0;fontSize=10;fontStyle=1;'
|
||
'fillColor=${_hex(tokens.accent)};fillOpacity=10;'
|
||
'strokeColor=${_hex(tokens.accent)};strokeOpacity=45;fontColor=${_hex(tokens.sub)};',
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 写入动作节点与开始 / 结束胶囊。
|
||
void _nodes(FlowLayout layout, double ox, double oy) {
|
||
for (final FlowNode n in layout.nodes) {
|
||
final Rect r = n.rect;
|
||
if (n.isStart || n.isEnd) {
|
||
_rectCell(
|
||
_cellId(n.id),
|
||
n.title,
|
||
r.left + ox,
|
||
r.top + oy,
|
||
r.width,
|
||
r.height,
|
||
'ellipse;whiteSpace=wrap;html=1;fontSize=13;fontStyle=1;'
|
||
'fillColor=${_hex(tokens.panel2)};strokeColor=${_hex(tokens.accent)};'
|
||
'fontColor=${_hex(tokens.accent)};',
|
||
);
|
||
continue;
|
||
}
|
||
final Action a = n.action!;
|
||
// 节点两行文字:标题(序号. 名称)+ 副标题(时长 · 工位)。
|
||
_rectCell(
|
||
_cellId(n.id),
|
||
'${n.title}\n${n.subtitle}',
|
||
r.left + ox,
|
||
r.top + oy,
|
||
r.width,
|
||
r.height,
|
||
'rounded=1;absoluteArcSize=1;arcSize=10;whiteSpace=wrap;html=1;fontSize=12;fontStyle=1;'
|
||
'fillColor=${_hex(Color(a.color))};fillOpacity=92;'
|
||
'strokeColor=${_hex(tokens.selOutline)};strokeOpacity=25;'
|
||
'fontColor=${_hex(tokens.arcText)};',
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 写入依赖连线:以 source / target 引用节点,drawio 自动正交布线并可再编辑。
|
||
///
|
||
/// 样式与画布一致:FS 绿实线、SS 琥珀虚线(标签标注偏移时间)、关键路径加粗。
|
||
void _edges(FlowLayout layout) {
|
||
int i = 0;
|
||
for (final FlowEdge e in layout.edges) {
|
||
i++;
|
||
final bool fs = e.type == DepType.fs;
|
||
final String col =
|
||
_hex(e.type == null ? tokens.sub : (fs ? tokens.fs : tokens.ss));
|
||
final String label = switch (e.type) {
|
||
null => '',
|
||
DepType.fs => 'FS',
|
||
DepType.ss => e.offset == 0
|
||
? 'SS'
|
||
: 'SS+${deg2msStr(e.offset, project.units, project.cycle.durationMs, dec: 2)}',
|
||
DepType.ff => 'FF',
|
||
DepType.sf => 'SF',
|
||
};
|
||
_b.write('<mxCell id="edge-$i-${_cellId(e.fromId)}-${_cellId(e.toId)}" '
|
||
'value="${_esc(label)}" '
|
||
'style="edgeStyle=orthogonalEdgeStyle;rounded=1;html=1;jettySize=auto;'
|
||
'orthogonalLoop=1;endArrow=block;endFill=1;'
|
||
'strokeWidth=${e.critical ? 2.4 : 1.3};strokeColor=$col;'
|
||
'${e.type == DepType.ss ? 'dashed=1;dashPattern=6 4;' : ''}'
|
||
'fontSize=9;fontColor=$col;labelBackgroundColor=${_hex(tokens.bg)};" '
|
||
'edge="1" parent="1" source="${_cellId(e.fromId)}" target="${_cellId(e.toId)}">'
|
||
'<mxGeometry relative="1" as="geometry"/></mxCell>');
|
||
}
|
||
}
|
||
|
||
/// 写入一个顶点(矩形 / 胶囊 / 竖带)单元格。
|
||
void _rectCell(String id, String value, double x, double y, double w, double h,
|
||
String style) {
|
||
_b.write('<mxCell id="$id" value="${_esc(value)}" '
|
||
'style="$style" vertex="1" parent="1">'
|
||
'<mxGeometry x="${x.toStringAsFixed(1)}" y="${y.toStringAsFixed(1)}" '
|
||
'width="${w.toStringAsFixed(1)}" height="${h.toStringAsFixed(1)}" as="geometry"/>'
|
||
'</mxCell>');
|
||
}
|
||
|
||
/// 节点 id → drawio 单元格 id:开始 / 结束用可读短名,其余原样保留。
|
||
static String _cellId(String nodeId) {
|
||
if (nodeId == FlowNode.startId) return 'flow-start';
|
||
if (nodeId == FlowNode.endId) return 'flow-end';
|
||
return nodeId.replaceAll(RegExp(r'[^A-Za-z0-9_-]'), '_');
|
||
}
|
||
|
||
/// 转义 XML 文本 / 属性中的特殊字符;换行转为 ` `(drawio 多行标签约定)。
|
||
static String _esc(String s) => s
|
||
.replaceAll('&', '&')
|
||
.replaceAll('<', '<')
|
||
.replaceAll('>', '>')
|
||
.replaceAll('"', '"')
|
||
.replaceAll('\n', ' ');
|
||
|
||
/// 把 Flutter [Color] 转为 `#rrggbb` 字符串(忽略 alpha)。
|
||
static String _hex(Color c) {
|
||
final int r = (c.r * 255).round();
|
||
final int g = (c.g * 255).round();
|
||
final int b = (c.b * 255).round();
|
||
return '#${r.toRadixString(16).padLeft(2, '0')}${g.toRadixString(16).padLeft(2, '0')}${b.toRadixString(16).padLeft(2, '0')}';
|
||
}
|
||
}
|