-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathflow-graph.html
More file actions
611 lines (572 loc) · 23.7 KB
/
Copy pathflow-graph.html
File metadata and controls
611 lines (572 loc) · 23.7 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<!doctype html>
<html lang="zh-CN">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
<title>jmGraph 工作流流程图(DAG)示例</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
display: flex;
background: #f8fafc;
color: #1e293b;
font-family: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
font-size: 13px;
}
/* ---------- 左侧:节点面板 ---------- */
.palette {
width: 200px;
min-width: 200px;
height: 100vh;
background: #fff;
border-right: 1px solid #e2e8f0;
padding: 14px 12px;
overflow-y: auto;
}
.palette h3 { font-size: 13px; color: #0f172a; margin-bottom: 4px; }
.palette .tip { font-size: 11px; color: #94a3b8; line-height: 1.6; margin-bottom: 10px; }
.p-item {
display: flex; align-items: center; gap: 8px;
border: 1px solid #e2e8f0; border-radius: 8px;
padding: 8px 10px; margin-bottom: 8px;
cursor: grab; user-select: none;
transition: box-shadow .15s, transform .15s;
background: #fff;
}
.p-item:hover { box-shadow: 0 2px 10px rgba(16,24,40,.1); transform: translateY(-1px); }
.p-item:active { cursor: grabbing; }
.p-icon {
width: 24px; height: 24px; border-radius: 6px; color: #fff;
display: flex; align-items: center; justify-content: center;
font-size: 13px; font-weight: 700; flex-shrink: 0;
}
.p-name { font-size: 12px; font-weight: 600; }
.p-desc { font-size: 10px; color: #94a3b8; }
/* ---------- 中间:画布 ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100vh; position: relative; }
.toolbar {
display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
padding: 8px 12px; background: #fff; border-bottom: 1px solid #e2e8f0;
}
.toolbar .group { display: flex; align-items: center; gap: 4px; }
.toolbar .sep { width: 1px; height: 18px; background: #e2e8f0; margin: 0 4px; }
.toolbar .title { font-weight: 700; color: #0f172a; margin-right: 6px; }
.toolbar .zoom { font-size: 12px; color: #64748b; min-width: 44px; text-align: center; }
button {
border: 1px solid #e2e8f0; background: #fff; color: #334155;
padding: 4px 10px; border-radius: 6px; font-size: 12px; cursor: pointer;
font-family: inherit; transition: background .15s, border-color .15s;
}
button:hover { background: #f1f5f9; border-color: #cbd5e1; }
button.primary { background: #2563eb; border-color: #2563eb; color: #fff; }
button.primary:hover { background: #1d4ed8; }
button.on { background: #eff6ff; border-color: #93c5fd; color: #1d4ed8; font-weight: 600; }
.canvas-wrap { flex: 1; position: relative; min-height: 0; overflow: hidden; }
#canvas { width: 100%; height: 100%; }
.empty {
position: absolute; inset: 0; display: none;
flex-direction: column; align-items: center; justify-content: center;
color: #cbd5e1; pointer-events: none; font-size: 13px; gap: 6px;
}
.empty .ico { font-size: 40px; }
.minimap {
position: absolute; right: 12px; bottom: 12px;
border: 1px solid #e2e8f0; border-radius: 8px; overflow: hidden;
box-shadow: 0 2px 10px rgba(16,24,40,.12); background: #f1f5f9;
}
.minimap canvas { display: block; cursor: pointer; }
/* ---------- 右侧:详情 ---------- */
.panel {
width: 260px; min-width: 260px; height: 100vh; overflow-y: auto;
background: #fff; border-left: 1px solid #e2e8f0; padding: 14px 12px;
}
.panel h4 { font-size: 12px; color: #94a3b8; text-transform: uppercase; letter-spacing: 1px; margin: 14px 0 8px; }
.panel h4:first-child { margin-top: 0; }
.kv { display: flex; font-size: 12px; padding: 3px 0; }
.kv b { color: #64748b; font-weight: 500; width: 62px; flex-shrink: 0; }
.kv span { color: #0f172a; word-break: break-all; }
.none { font-size: 12px; color: #cbd5e1; }
.rename { display: flex; gap: 6px; margin-top: 8px; }
.rename input {
flex: 1; border: 1px solid #e2e8f0; border-radius: 6px; padding: 4px 8px;
font-size: 12px; font-family: inherit; min-width: 0;
}
.rename input:focus { outline: none; border-color: #93c5fd; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; }
.legend i {
width: 10px; height: 10px; border-radius: 50%; display: inline-block;
margin-right: 5px; vertical-align: -1px;
}
.legend span { font-size: 11px; color: #475569; }
.log {
background: #0f172a; border-radius: 8px; padding: 8px;
height: 200px; overflow-y: auto; font-size: 11px; line-height: 1.7;
font-family: Consolas, Menlo, monospace;
}
.log div { color: #94a3b8; }
.log .ok { color: #4ade80; }
.log .error { color: #f87171; }
.log .warn { color: #fbbf24; }
.log .info { color: #7dd3fc; }
/* ---------- 右键菜单 ---------- */
.menu {
position: absolute; display: none; z-index: 99;
background: #fff; border: 1px solid #e2e8f0; border-radius: 8px;
box-shadow: 0 8px 24px rgba(16,24,40,.16); padding: 4px; min-width: 132px;
}
.menu div { padding: 6px 10px; border-radius: 6px; font-size: 12px; cursor: pointer; }
.menu div:hover { background: #f1f5f9; }
.menu div.danger { color: #dc2626; }
.menu div.danger:hover { background: #fef2f2; }
/* ---------- 移动端适配 ---------- */
.canvas-wrap, #canvas {
touch-action: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
user-select: none;
}
.drawer-toggle { display: none; }
.backdrop {
position: fixed; inset: 0; background: rgba(15,23,42,.42);
z-index: 55; opacity: 0; visibility: hidden; transition: opacity .22s;
}
.backdrop.show { opacity: 1; visibility: visible; }
.hint {
position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%);
z-index: 58; background: #0f172a; color: #fff; padding: 8px 14px;
border-radius: 999px; font-size: 12px; box-shadow: 0 4px 16px rgba(16,24,40,.3);
display: none; max-width: 90%; text-align: center;
}
.hint.show { display: block; }
@media (max-width: 820px) {
body { font-size: 12px; }
.palette, .panel {
position: fixed; top: 0; bottom: 0; z-index: 60;
width: 78%; max-width: 320px; min-width: 0; height: 100%;
box-shadow: 0 0 28px rgba(16,24,40,.2);
transition: transform .25s ease; will-change: transform;
}
.palette { left: 0; transform: translateX(-106%); }
.panel { right: 0; transform: translateX(106%); }
.palette.open, .panel.open { transform: translateX(0); }
.drawer-toggle { display: inline-flex; }
.main { height: 100%; }
.toolbar { padding: 6px 8px; gap: 6px; }
.toolbar .title { display: none; }
.toolbar .sep { display: none; }
.toolbar .group { gap: 4px; }
button { padding: 5px 9px; font-size: 12px; }
.zoom { min-width: 38px; }
.minimap { display: none; }
.p-item.active { box-shadow: 0 0 0 2px #2563eb inset; }
html, body { height: 100%; }
}
@media (max-width: 820px) and (display-mode: browser) {
/* 处理移动端地址栏导致的 100vh 抖动 */
.palette, .panel, .main { height: 100dvh; }
}
</style>
</head>
<body>
<aside class="palette">
<h3>节点面板</h3>
<div class="tip">编辑模式下拖拽到画布添加节点;拖动节点边上的连接点可建立依赖。</div>
<div id="palette"></div>
</aside>
<section class="main">
<div class="toolbar">
<button id="btnPalette" class="drawer-toggle" title="节点面板">☰ 节点</button>
<button id="btnPanel" class="drawer-toggle" title="详情">ℹ 详情</button>
<span class="title">jmGraph 流程图</span>
<div class="group">
<button id="btnMode">✏️ 编辑模式</button>
</div>
<div class="sep"></div>
<div class="group">
<button id="btnRun" class="primary">▶ 执行流程</button>
<button id="btnResetRun">重置状态</button>
</div>
<div class="sep"></div>
<div class="group">
<button id="btnLayout">自动布局</button>
<button id="btnFit">适应视图</button>
</div>
<div class="sep"></div>
<div class="group">
<button id="btnZoomOut">-</button>
<span class="zoom" id="zoom">100%</span>
<button id="btnZoomIn">+</button>
<button id="btnReset">1:1</button>
</div>
<div class="sep"></div>
<div class="group">
<button id="btnExport">导出 PNG</button>
</div>
</div>
<div class="canvas-wrap" id="wrap">
<div id="canvas"></div>
<div class="empty" id="empty"><div class="ico">▦</div><div>暂无节点,切换到编辑模式拖入节点</div></div>
<div class="minimap"><canvas id="minimap" width="220" height="150"></canvas></div>
<div class="menu" id="menu"></div>
</div>
</section>
<aside class="panel">
<h4>节点详情</h4>
<div id="detail"><div class="none">点击节点查看详情</div></div>
<div class="rename" id="renameBox" style="display:none">
<input id="renameInput" placeholder="节点 ID" />
<button id="btnRename">保存</button>
</div>
<h4>运行状态图例</h4>
<div class="legend" id="legend"></div>
<h4>运行日志</h4>
<div class="log" id="log"></div>
</aside>
<div class="backdrop" id="backdrop"></div>
<div class="hint" id="hint"></div>
<script type="module">
// 引入官方流程图组件(createFlowGraph 返回 FlowGraphComponent 实例)
import { createFlowGraph, NODE_TYPES, STATUS_META, typeColor } from './js/flow-graph.js';
// ---------- 示例数据 ----------
const STAGES = [
{ id: 'fetch_user', type: 'fetch', config: {}, inputs: [], outputs: ['user'], dependsOn: [] },
{ id: 'fetch_order', type: 'fetch', config: {}, inputs: [], outputs: ['orders'], dependsOn: [] },
{
id: 'normalize', type: 'transform', config: {}, outputs: ['rows'],
inputs: [{ from: 'fetch_user.user' }, { from: 'fetch_order.orders' }],
dependsOn: ['fetch_user', 'fetch_order']
},
{
id: 'score', type: 'script', config: {}, outputs: ['scores'],
inputs: [{ from: 'normalize.rows' }],
dependsOn: ['normalize'], retry: { maxRetries: 2, intervalMs: 1000 }
},
{
id: 'summary', type: 'agent', config: {}, outputs: ['report'],
inputs: [{ from: 'score.scores' }],
dependsOn: ['score']
},
{
id: 'export', type: 'custom', config: {}, inputs: [{ from: 'summary.report' }],
outputs: [], dependsOn: ['summary']
},
{
id: 'notify', type: 'custom', config: {}, inputs: [{ from: 'summary.report' }],
outputs: [], dependsOn: ['summary']
}
];
// ---------- 节点面板 ----------
const palette = document.getElementById('palette');
NODE_TYPES.forEach(t => {
const el = document.createElement('div');
el.className = 'p-item';
el.draggable = true;
el.innerHTML = '<div class="p-icon" style="background:' + typeColor[t.type] + '">' + t.icon +
'</div><div><div class="p-name">' + t.label + '</div><div class="p-desc">' + t.desc + '</div></div>';
el.addEventListener('dragstart', e => {
e.dataTransfer.setData('text/flow-type', t.type);
e.dataTransfer.effectAllowed = 'move';
});
// 移动端:点选面板项 -> 选中待添加类型,再点画布空白处放置
el.addEventListener('click', () => {
if (!isMobile()) return;
if (fg.getMode() !== 'edit') { log('请先切换到编辑模式再添加节点', 'warn'); return; }
pendingAddType = (pendingAddType === t.type) ? null : t.type;
document.querySelectorAll('.p-item').forEach(p => p.classList.remove('active'));
if (pendingAddType) {
el.classList.add('active');
showHint('已选择「' + t.label + '」,点击画布空白处放置节点');
closeDrawers();
} else {
hideHint();
}
});
palette.appendChild(el);
});
// ---------- 图例 ----------
const legend = document.getElementById('legend');
Object.keys(STATUS_META).forEach(k => {
const m = STATUS_META[k];
const s = document.createElement('span');
s.innerHTML = '<i style="background:' + m.color + '"></i>' + m.label;
legend.appendChild(s);
});
// ---------- 日志 ----------
const logBox = document.getElementById('log');
function log(msg, level) {
const d = document.createElement('div');
if (level) d.className = level;
d.textContent = msg;
logBox.appendChild(d);
logBox.scrollTop = logBox.scrollHeight;
while (logBox.childNodes.length > 200) logBox.removeChild(logBox.firstChild);
}
// ---------- 创建流程图 ----------
const wrap = document.getElementById('wrap');
const canvasEl = document.getElementById('canvas');
const menu = document.getElementById('menu');
const detail = document.getElementById('detail');
const renameBox = document.getElementById('renameBox');
const renameInput = document.getElementById('renameInput');
let mode = 'view';
let selected = null;
let uiReady = false; // fg 初始化期间回调会触发渲染,此时 DOM/fg 尚未就绪
let pendingAddType = null; // 移动端:待放置的节点类型(点选面板后点画布添加)
function handleSelect(id) {
selected = id;
renderDetail();
if (isMobile() && id) openDrawer(panelEl);
}
const fg = createFlowGraph(canvasEl, {
stages: STAGES,
mode: 'view',
onSelect: handleSelect,
onEdgeClick: edge => log('点击连线:' + edge.source + ' → ' + edge.target),
onNodeEdit: () => { renameInput.focus(); renameInput.select(); },
onLog: log,
onContextMenu: openContextMenu
});
window.__fg = fg; // temp for debugging
fg.attachMinimap(document.getElementById('minimap'));
uiReady = true;
renderDetail();
// ---------- 工具栏 ----------
const btnMode = document.getElementById('btnMode');
function refreshModeBtn() {
btnMode.textContent = mode === 'edit' ? '👁 查看(编辑中)' : '✏️ 编辑模式';
btnMode.classList.toggle('on', mode === 'edit');
}
btnMode.onclick = () => {
mode = fg.setMode(mode === 'edit' ? 'view' : 'edit');
if (mode !== 'edit') {
pendingAddType = null;
document.querySelectorAll('.p-item').forEach(p => p.classList.remove('active'));
hideHint();
} else if (isMobile()) {
log('移动端提示:点选上方「节点」面板中的类型,再点画布空白处即可添加节点', 'info');
}
refreshModeBtn();
renderDetail();
log(mode === 'edit' ? '已进入编辑模式:可拖动节点、拖拽连接点建边、长按节点操作' : '已切换到查看模式');
};
refreshModeBtn();
document.getElementById('btnRun').onclick = () => {
if (fg.isRunning()) return;
document.getElementById('btnRun').textContent = '⏳ 执行中…';
fg.run().then(() => {
document.getElementById('btnRun').textContent = '▶ 执行流程';
});
};
document.getElementById('btnResetRun').onclick = () => fg.resetRun();
document.getElementById('btnLayout').onclick = () => fg.autoLayout();
document.getElementById('btnFit').onclick = () => fg.fit();
document.getElementById('btnZoomIn').onclick = () => fg.zoomIn();
document.getElementById('btnZoomOut').onclick = () => fg.zoomOut();
document.getElementById('btnReset').onclick = () => fg.resetView();
document.getElementById('btnExport').onclick = () => fg.exportPNG('jmgraph-flow');
// 缩放比例显示
setInterval(() => {
document.getElementById('zoom').textContent = Math.round(fg.getZoom() * 100) + '%';
}, 200);
// ---------- 详情面板 ----------
function renderDetail() {
if (!uiReady) return;
const stage = fg.getStages().find(s => s.id === selected);
if (!stage) {
detail.innerHTML = '<div class="none">点击节点查看详情</div>';
renameBox.style.display = 'none';
return;
}
const meta = STATUS_META[fg.getRunStatus()[stage.id] || 'idle'] || STATUS_META.idle;
const rows = [
['ID', stage.id],
['类型', stage.type],
['状态', meta.label],
['输入', (stage.inputs || []).map(i => i.from || '(value)').join(', ') || '—'],
['输出', (stage.outputs || []).join(', ') || '—'],
['依赖', (stage.dependsOn || []).join(', ') || '—'],
['重试', stage.retry ? stage.retry.maxRetries + ' 次 / ' + stage.retry.intervalMs + 'ms' : '—']
];
detail.innerHTML = rows.map(r =>
'<div class="kv"><b>' + r[0] + '</b><span>' + r[1] + '</span></div>').join('');
renameBox.style.display = mode === 'edit' ? 'flex' : 'none';
renameInput.value = stage.id;
}
document.getElementById('btnRename').onclick = () => {
if (fg.renameStage(selected, renameInput.value)) renderDetail();
};
renameInput.addEventListener('keydown', e => {
if (e.key === 'Enter') document.getElementById('btnRename').click();
});
// ---------- 拖拽新增节点 ----------
wrap.addEventListener('dragover', e => { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; });
wrap.addEventListener('drop', e => {
e.preventDefault();
if (fg.getMode() !== 'edit') { log('请先切换到编辑模式再添加节点', 'warn'); return; }
const type = e.dataTransfer.getData('text/flow-type');
if (!type) return;
const pos = fg.graph.getPosition();
const world = fg.graph.screenToWorld({ x: e.pageX - pos.left, y: e.pageY - pos.top });
fg.addStage(type, world);
});
// ---------- 右键菜单 ----------
function showMenu(x, y, items) {
menu.innerHTML = '';
items.forEach(it => {
const d = document.createElement('div');
d.textContent = it.label;
if (it.danger) d.className = 'danger';
d.onclick = () => { hideMenu(); if (it.action) it.action(); };
menu.appendChild(d);
});
menu.style.display = 'block';
const rect = wrap.getBoundingClientRect();
const mw = menu.offsetWidth, mh = menu.offsetHeight;
menu.style.left = Math.min(x - rect.left, rect.width - mw - 6) + 'px';
menu.style.top = Math.min(y - rect.top, rect.height - mh - 6) + 'px';
}
function hideMenu() { menu.style.display = 'none'; }
document.addEventListener('mousedown', e => { if (!menu.contains(e.target)) hideMenu(); });
// ---------- 右键 / 长按 菜单(基于组件 onContextMenu 事件) ----------
// 桌面端由库的 onContextMenu 事件驱动(已含命中结果与世界坐标);
// 移动端长按不是原生 contextmenu 事件,需自行换算坐标后复用同一处理函数。
function contextMenuAtClient(clientX, clientY) {
const rect = wrap.getBoundingClientRect();
const world = fg.graph.screenToWorld({ x: clientX - rect.left, y: clientY - rect.top });
const node = fg.nodeAt(world);
if (node) return { type: 'node', id: node.stage.id, stage: node.stage, clientX, clientY, x: world.x, y: world.y };
return { type: 'pane', id: null, stage: null, clientX, clientY, x: world.x, y: world.y };
}
function openContextMenu(info) {
const { clientX, clientY, type } = info;
if (type === 'node') {
fg.select(info.id);
showMenu(clientX, clientY, [
{ label: '✏️ 重命名节点', action: () => { renameInput.focus(); renameInput.select(); } },
{ label: '▶ 从该节点执行', action: () => log('(示例)从 ' + info.id + ' 开始执行') },
{ label: '🗑 删除节点', danger: true, action: () => fg.removeStage(info.id) }
]);
return;
}
// edge / pane:空白区与连线上的操作(info.x/info.y 为世界坐标)
const world = { x: info.x, y: info.y };
const items = [];
if (type === 'edge' && info.edge) {
items.push({ label: '🔗 ' + info.edge.source + ' → ' + info.edge.target });
}
items.push(
{ label: '🔍 适应视图', action: () => fg.fit() },
{ label: '📐 自动布局', action: () => fg.autoLayout() },
{ label: '➕ 添加节点(编辑模式)', action: () => {
if (fg.getMode() !== 'edit') { log('请先切换到编辑模式', 'warn'); return; }
fg.addStage('custom', world);
} }
);
showMenu(clientX, clientY, items);
}
// ---------- 移动端通用工具 ----------
function isMobile() { return window.matchMedia('(max-width: 820px)').matches; }
function showHint(msg) { const h = document.getElementById('hint'); h.textContent = msg; h.classList.add('show'); }
function hideHint() { document.getElementById('hint').classList.remove('show'); }
// ---------- 移动端抽屉(节点面板 / 详情) ----------
const paletteEl = document.querySelector('.palette');
const panelEl = document.querySelector('.panel');
const backdrop = document.getElementById('backdrop');
function openDrawer(el) { closeDrawers(); el.classList.add('open'); backdrop.classList.add('show'); }
function closeDrawers() {
paletteEl.classList.remove('open');
panelEl.classList.remove('open');
backdrop.classList.remove('show');
}
document.getElementById('btnPalette').onclick = () =>
paletteEl.classList.contains('open') ? closeDrawers() : openDrawer(paletteEl);
document.getElementById('btnPanel').onclick = () =>
panelEl.classList.contains('open') ? closeDrawers() : openDrawer(panelEl);
backdrop.onclick = closeDrawers;
// ---------- 移动端:点选面板项后点画布空白处添加节点 ----------
wrap.addEventListener('click', e => {
if (!pendingAddType) return;
const rect = wrap.getBoundingClientRect();
const world = fg.graph.screenToWorld({ x: e.clientX - rect.left, y: e.clientY - rect.top });
if (fg.nodeAt(world)) return; // 点中已有节点则不放置
fg.addStage(pendingAddType, world);
pendingAddType = null;
document.querySelectorAll('.p-item').forEach(p => p.classList.remove('active'));
hideHint();
});
// ---------- 移动端:长按菜单 + 双指缩放 ----------
let lpTimer = null, lpPt = null, lpMoved = false, lpFired = false, pinch = null;
function clearLp() { if (lpTimer) { clearTimeout(lpTimer); lpTimer = null; } lpPt = null; lpMoved = false; lpFired = false; }
wrap.addEventListener('touchstart', e => {
if (e.touches.length === 2) {
clearLp();
const [a, b] = [e.touches[0], e.touches[1]];
pinch = { d: Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY) };
return;
}
if (e.touches.length !== 1) { clearLp(); return; }
lpPt = { x: e.touches[0].clientX, y: e.touches[0].clientY };
lpMoved = false;
lpTimer = setTimeout(() => {
lpTimer = null;
if (!lpMoved) { lpFired = true; openContextMenu(contextMenuAtClient(lpPt.x, lpPt.y)); }
}, 500);
}, { passive: true });
wrap.addEventListener('touchmove', e => {
if (e.touches.length === 2 && pinch) {
e.preventDefault();
const [a, b] = [e.touches[0], e.touches[1]];
const d = Math.hypot(a.clientX - b.clientX, a.clientY - b.clientY);
if (d > 0 && pinch.d > 0) {
const rect = wrap.getBoundingClientRect();
const cx = (a.clientX + b.clientX) / 2, cy = (a.clientY + b.clientY) / 2;
fg.graph.setZoom(fg.graph.scaleFactor * (d / pinch.d), cx - rect.left, cy - rect.top);
}
pinch.d = d;
return;
}
if (lpPt && e.touches[0]) {
const dx = e.touches[0].clientX - lpPt.x, dy = e.touches[0].clientY - lpPt.y;
if (Math.hypot(dx, dy) > 10) { lpMoved = true; clearLp(); }
}
}, { passive: false });
wrap.addEventListener('touchend', e => {
if (e.touches.length < 2) pinch = null;
if (!lpMoved && !lpFired && lpPt && e.changedTouches.length === 1) {
const t = e.changedTouches[0];
const rect = wrap.getBoundingClientRect();
const world = fg.graph.screenToWorld({ x: t.clientX - rect.left, y: t.clientY - rect.top });
const node = fg.nodeAt(world);
if (pendingAddType) {
if (!node) {
fg.addStage(pendingAddType, world);
pendingAddType = null;
document.querySelectorAll('.p-item').forEach(p => p.classList.remove('active'));
hideHint();
}
} else if (node) {
fg.select(node.stage.id);
} else {
fg.select(null);
}
}
clearLp();
}, { passive: true });
// 触控点击空白处关闭菜单
document.addEventListener('touchstart', e => { if (!menu.contains(e.target)) hideMenu(); }, true);
// ---------- 删除键 ----------
document.addEventListener('keydown', e => {
if (e.key !== 'Delete' && e.key !== 'Backspace') return;
if (e.target && /INPUT|TEXTAREA/.test(e.target.tagName)) return;
if (fg.getMode() !== 'edit' || !selected) return;
fg.removeStage(selected);
});
// ---------- 尺寸自适应 ----------
window.addEventListener('resize', () => fg.resize());
setTimeout(() => fg.resize(), 0);
log('jmGraph 流程图示例已就绪');
log('提示:滚轮缩放 / 空白处拖拽平移 / 点击节点查看详情', 'info');
</script>
</body>
</html>