-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdexdecoder-first-principles-guide.html
More file actions
782 lines (705 loc) · 25.6 KB
/
dexdecoder-first-principles-guide.html
File metadata and controls
782 lines (705 loc) · 25.6 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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DexDecoder 六步法·第一性原理可视化导读</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=ZCOOL+XiaoWei&family=IBM+Plex+Mono:wght@400;500;600&display=swap" rel="stylesheet">
<style>
:root {
--font-title: 'ZCOOL XiaoWei', serif;
--font-mono: 'IBM Plex Mono', 'SF Mono', Consolas, monospace;
--font-sans: 'PingFang SC', 'Noto Sans SC', 'Microsoft YaHei', sans-serif;
--bg: #f8f7f2;
--surface: #fffdf8;
--surface2: #f1ede0;
--border: rgba(38, 41, 46, 0.12);
--border-strong: rgba(38, 41, 46, 0.22);
--text: #1d2630;
--text-dim: #5e6976;
--ink: #1f3a5f;
--ink-dim: rgba(31, 58, 95, 0.12);
--amber: #a35d00;
--amber-dim: rgba(163, 93, 0, 0.12);
--green: #2f6b45;
--green-dim: rgba(47, 107, 69, 0.11);
--red: #a52b2b;
--red-dim: rgba(165, 43, 43, 0.11);
--purple: #5a4699;
--purple-dim: rgba(90, 70, 153, 0.11);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #131a23;
--surface: #1b2430;
--surface2: #232f3c;
--border: rgba(255, 255, 255, 0.1);
--border-strong: rgba(255, 255, 255, 0.2);
--text: #e9eef5;
--text-dim: #a7b6c7;
--ink: #7bb2ff;
--ink-dim: rgba(123, 178, 255, 0.15);
--amber: #ffbd6f;
--amber-dim: rgba(255, 189, 111, 0.15);
--green: #7ed9a0;
--green-dim: rgba(126, 217, 160, 0.14);
--red: #ff9f9f;
--red-dim: rgba(255, 159, 159, 0.14);
--purple: #c3b0ff;
--purple-dim: rgba(195, 176, 255, 0.14);
}
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: var(--font-sans);
background: var(--bg);
color: var(--text);
line-height: 1.65;
padding: 32px;
background-image:
radial-gradient(ellipse at 12% 0%, var(--ink-dim) 0%, transparent 48%),
radial-gradient(ellipse at 85% 100%, var(--amber-dim) 0%, transparent 38%),
repeating-linear-gradient(0deg, transparent, transparent 27px, var(--border) 27px, var(--border) 28px);
}
@keyframes fadeUp {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.anim { animation: fadeUp .45s ease-out both; animation-delay: calc(var(--i, 0) * .06s); }
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
.mermaid-wrap .mermaid { transition: none !important; }
}
.wrap {
max-width: 1380px;
margin: 0 auto;
display: grid;
grid-template-columns: 190px 1fr;
gap: 0 34px;
}
.main { min-width: 0; }
.toc {
position: sticky;
top: 24px;
align-self: start;
max-height: calc(100dvh - 48px);
overflow-y: auto;
padding: 12px 0;
border-right: 1px dashed var(--border);
padding-right: 14px;
}
.toc::-webkit-scrollbar { width: 4px; }
.toc::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }
.toc-title {
font-family: var(--font-mono);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 1.8px;
color: var(--text-dim);
margin-bottom: 10px;
padding-bottom: 8px;
border-bottom: 1px solid var(--border);
}
.toc a {
display: block;
text-decoration: none;
color: var(--text-dim);
font-size: 12px;
padding: 6px 8px;
margin-bottom: 2px;
border-left: 2px solid transparent;
border-radius: 6px;
transition: all .15s ease;
}
.toc a:hover { color: var(--text); background: var(--surface2); }
.toc a.active { color: var(--text); border-left-color: var(--ink); background: var(--surface2); }
h1 {
font-family: var(--font-title);
font-size: 44px;
letter-spacing: .6px;
line-height: 1.2;
margin-bottom: 8px;
text-wrap: balance;
}
.subtitle {
color: var(--text-dim);
font-family: var(--font-mono);
font-size: 12px;
margin-bottom: 26px;
}
.hero {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 16px;
padding: 26px;
box-shadow: 0 8px 26px rgba(0,0,0,.06);
margin-bottom: 22px;
}
.equation {
margin-top: 14px;
font-family: var(--font-mono);
font-size: 15px;
padding: 12px 14px;
border-radius: 10px;
background: var(--ink-dim);
border: 1px dashed var(--border-strong);
overflow-x: auto;
}
.grid-3 {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
margin-top: 14px;
}
.chip {
border-radius: 10px;
padding: 10px 12px;
border: 1px solid var(--border);
background: var(--surface2);
font-size: 13px;
min-width: 0;
}
.sec-head {
margin-top: 26px;
margin-bottom: 10px;
font-family: var(--font-mono);
font-size: 12px;
letter-spacing: 1.2px;
text-transform: uppercase;
color: var(--text-dim);
display: flex;
align-items: center;
gap: 8px;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink); }
.card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 14px;
padding: 18px;
margin-bottom: 16px;
min-width: 0;
}
.card p { color: var(--text-dim); font-size: 14px; }
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 10px;
margin-bottom: 6px;
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-dim);
}
.legend .swatch {
width: 10px;
height: 10px;
border-radius: 3px;
display: inline-block;
margin-right: 6px;
vertical-align: middle;
}
.table-wrap {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 14px;
overflow: hidden;
margin-bottom: 16px;
}
.table-scroll { overflow-x: auto; }
table {
width: 100%;
border-collapse: collapse;
min-width: 860px;
font-size: 13px;
}
thead th {
position: sticky;
top: 0;
z-index: 2;
background: var(--surface2);
border-bottom: 2px solid var(--border-strong);
text-align: left;
padding: 12px 14px;
font-family: var(--font-mono);
font-size: 10px;
letter-spacing: 1.2px;
text-transform: uppercase;
color: var(--text-dim);
}
tbody td {
padding: 12px 14px;
border-bottom: 1px solid var(--border);
vertical-align: top;
}
tbody tr:nth-child(even) { background: color-mix(in srgb, var(--surface2) 65%, transparent); }
tbody tr:hover { background: color-mix(in srgb, var(--ink-dim) 35%, transparent); }
.status {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 4px 10px;
border-radius: 999px;
font-size: 10px;
font-family: var(--font-mono);
letter-spacing: .3px;
white-space: nowrap;
border: 1px solid transparent;
}
.status::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.ok { background: var(--green-dim); color: var(--green); border-color: color-mix(in srgb, var(--green) 35%, transparent); }
.warn { background: var(--amber-dim); color: var(--amber); border-color: color-mix(in srgb, var(--amber) 35%, transparent); }
.risk { background: var(--red-dim); color: var(--red); border-color: color-mix(in srgb, var(--red) 35%, transparent); }
code {
font-family: var(--font-mono);
font-size: 11px;
padding: 2px 5px;
border-radius: 4px;
background: var(--ink-dim);
color: var(--ink);
overflow-wrap: break-word;
}
.mermaid-wrap {
position: relative;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 14px;
padding: 30px 18px 18px;
overflow: auto;
margin-bottom: 16px;
scrollbar-width: thin;
scrollbar-color: var(--border) transparent;
}
.mermaid-wrap::-webkit-scrollbar { width: 6px; height: 6px; }
.mermaid-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.mermaid-wrap .mermaid {
display: flex;
justify-content: center;
transform-origin: top center;
transition: transform .2s ease;
}
.zoom-controls {
position: absolute;
top: 8px;
right: 8px;
display: flex;
gap: 3px;
z-index: 8;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 6px;
padding: 2px;
}
.zoom-controls button {
width: 28px;
height: 28px;
border: none;
border-radius: 4px;
background: transparent;
color: var(--text-dim);
font-family: var(--font-mono);
cursor: pointer;
}
.zoom-controls button:hover { background: var(--surface2); color: var(--text); }
.mermaid-wrap.is-zoomed { cursor: grab; }
.mermaid-wrap.is-panning { cursor: grabbing; user-select: none; }
.mermaid .nodeLabel {
font-family: var(--font-sans) !important;
font-size: 14px !important;
}
.mermaid .edgeLabel {
font-family: var(--font-mono) !important;
font-size: 11px !important;
}
.reading-path {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 12px;
}
.path-item {
background: var(--surface2);
border: 1px solid var(--border);
border-radius: 12px;
padding: 12px;
min-width: 0;
}
.path-item h4 {
font-family: var(--font-mono);
font-size: 11px;
letter-spacing: 1px;
margin-bottom: 6px;
color: var(--ink);
text-transform: uppercase;
}
.path-item p { font-size: 13px; color: var(--text-dim); }
.footer-note {
margin-top: 12px;
color: var(--text-dim);
font-size: 12px;
font-family: var(--font-mono);
}
@media (max-width: 1000px) {
.wrap { grid-template-columns: 1fr; }
.toc {
position: sticky;
top: 0;
z-index: 50;
max-height: none;
border-right: none;
border-bottom: 1px solid var(--border);
background: color-mix(in srgb, var(--bg) 88%, transparent);
display: flex;
gap: 4px;
overflow-x: auto;
padding: 10px 0;
margin: 0 -32px 14px;
padding-left: 32px;
padding-right: 32px;
}
.toc::-webkit-scrollbar { display: none; }
.toc-title { display: none; }
.toc a {
white-space: nowrap;
flex-shrink: 0;
border-left: none;
border-bottom: 2px solid transparent;
border-radius: 6px 6px 0 0;
}
.toc a.active { border-bottom-color: var(--ink); }
.grid-3, .reading-path { grid-template-columns: 1fr; }
h1 { font-size: 30px; }
}
@media (max-width: 768px) {
body { padding: 16px; }
.toc { margin: 0 -16px 12px; padding-left: 16px; padding-right: 16px; }
.hero, .card, .table-wrap, .mermaid-wrap { border-radius: 12px; }
}
</style>
</head>
<body>
<div class="wrap">
<nav class="toc" id="toc">
<div class="toc-title">Contents</div>
<a href="#s1">1. 问题定义</a>
<a href="#s2">2. 六步流程图</a>
<a href="#s3">3. 步骤对照表</a>
<a href="#s4">4. 代码架构映射</a>
<a href="#s5">5. 事件扫描层</a>
<a href="#s6">6. 阅读路径</a>
</nav>
<main class="main">
<section class="hero anim" style="--i:0">
<h1>DexDecoder 六步法:第一性原理可视化导读</h1>
<p class="subtitle">for readers who don't yet understand protocol internals</p>
<p>先不要看实现细节,先看根问题:任何 DEX 报价,本质都在计算同一个函数。</p>
<div class="equation">amountOut = F(protocol_rules, state_before_swap, amountIn, direction)</div>
<div class="grid-3">
<div class="chip"><strong>rules</strong>:协议规则(加权曲线 / 集中流动性 / Hook / Plugin)</div>
<div class="chip"><strong>state</strong>:报价前的真实链上状态快照</div>
<div class="chip"><strong>input</strong>:交易输入量与方向</div>
</div>
</section>
<div id="s1" class="sec-head anim" style="--i:1"><span class="dot"></span>1 — 为什么必须六步</div>
<section class="card anim" style="--i:2">
<p>六步不是流程形式主义,而是「逐步消除不确定性」的验证闭环:先确定池子是谁,再确定状态是什么,再把规则转为可计算模型,最后用链上真值反证本地模拟是否可靠。</p>
</section>
<div id="s2" class="sec-head anim" style="--i:3"><span class="dot"></span>2 — 六步流程图(可缩放)</div>
<section class="mermaid-wrap anim" style="--i:4">
<div class="zoom-controls">
<button onclick="zoomDiagram(this, 1.2)" title="Zoom in">+</button>
<button onclick="zoomDiagram(this, 0.8)" title="Zoom out">−</button>
<button onclick="resetZoom(this)" title="Reset">⟳</button>
</div>
<pre class="mermaid">
flowchart LR
A[Step 1\nPool Discovery] --> B[Step 2\nState Reading]
B --> C[Step 3\nMath Modeling]
C --> D[Step 4\nLocal Simulation]
D --> E[Step 5\nOn-chain Quote]
E --> F[Step 6\nAccuracy Check]
A -.if wrong pool.-> X1[报价对象错误]
B -.if stale/incomplete state.-> X2[模型输入失真]
C -.if formula mismatch.-> X3[数学规则错位]
F --> G{误差在阈值内?}
G -->|Yes| H[模型可用于生产]
G -->|No| I[回溯根因并修正]
classDef step fill:#eaf3ff,stroke:#1f3a5f,color:#1f3a5f,stroke-width:1.5px;
classDef bad fill:#fff1f1,stroke:#a52b2b,color:#a52b2b,stroke-width:1.2px;
classDef gate fill:#fff8e7,stroke:#a35d00,color:#a35d00,stroke-width:1.2px;
classDef ok fill:#ecf9f0,stroke:#2f6b45,color:#2f6b45,stroke-width:1.2px;
class A,B,C,D,E,F step;
class X1,X2,X3 bad;
class G gate;
class H,I ok;
</pre>
</section>
<div class="legend anim" style="--i:5">
<span><i class="swatch" style="background:var(--ink)"></i>流程步骤</span>
<span><i class="swatch" style="background:var(--amber)"></i>验收关口</span>
<span><i class="swatch" style="background:var(--red)"></i>失败模式</span>
<span><i class="swatch" style="background:var(--green)"></i>可交付结果</span>
</div>
<div id="s3" class="sec-head anim" style="--i:6"><span class="dot"></span>3 — 六步输入/输出/失败模式对照</div>
<section class="table-wrap anim" style="--i:7">
<div class="table-scroll">
<table>
<thead>
<tr>
<th>Step</th>
<th>要解决的问题</th>
<th>输入</th>
<th>输出</th>
<th>失败模式</th>
<th>代码主入口</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>1</strong></td>
<td>我在和哪个池子交互?</td>
<td>事件日志</td>
<td><code>PoolMeta</code></td>
<td><span class="status risk">错池 / 漏池</span></td>
<td><code>discover_pools()</code></td>
</tr>
<tr>
<td><strong>2</strong></td>
<td>池子当前状态是什么?</td>
<td><code>PoolMeta</code> + <code>eth_call</code></td>
<td><code>RawState</code></td>
<td><span class="status risk">快照不一致</span></td>
<td><code>fetch_state()</code></td>
</tr>
<tr>
<td><strong>3</strong></td>
<td>规则如何映射数学?</td>
<td>协议规则 + <code>RawState</code></td>
<td><code>AMMConstraint</code></td>
<td><span class="status warn">公式/单位错位</span></td>
<td><code>build_constraint()</code></td>
</tr>
<tr>
<td><strong>4</strong></td>
<td>本地执行会发生什么?</td>
<td><code>AMMConstraint</code> + 参数</td>
<td>local <code>amountOut</code></td>
<td><span class="status warn">tick 迭代错误</span></td>
<td><code>simulate_swap()</code></td>
</tr>
<tr>
<td><strong>5</strong></td>
<td>链上真实输出是什么?</td>
<td>Quoter / Query 合约</td>
<td>on-chain <code>amountOut</code></td>
<td><span class="status warn">Quoter 限制</span></td>
<td><code>quote_onchain()</code></td>
</tr>
<tr>
<td><strong>6</strong></td>
<td>误差是否可接受?</td>
<td>Step4 vs Step5</td>
<td><code>AccuracyReport</code></td>
<td><span class="status ok">可量化验收</span></td>
<td><code>validate_accuracy()</code></td>
</tr>
</tbody>
</table>
</div>
</section>
<div id="s4" class="sec-head anim" style="--i:8"><span class="dot"></span>4 — 代码架构映射(可缩放)</div>
<section class="mermaid-wrap anim" style="--i:9">
<div class="zoom-controls">
<button onclick="zoomDiagram(this, 1.2)" title="Zoom in">+</button>
<button onclick="zoomDiagram(this, 0.8)" title="Zoom out">−</button>
<button onclick="resetZoom(this)" title="Reset">⟳</button>
</div>
<pre class="mermaid">
flowchart TD
M[main.py] --> R[ProtocolRegistry]
R --> BA[BalancerV2Adapter]
R --> V3[UniswapV3Adapter]
R --> V4[UniswapV4Adapter]
R --> AL[AlgebraAdapter]
BA --> CORE[core.py\nProtocolAdapter / AMMConstraint]
V3 --> CORE
V4 --> CORE
AL --> CORE
V3 --> CL[protocols/concentrated\nswap_math / swap_engine]
V4 --> CL
AL --> CL
EV[events/scanner.py] --> AN[events/analyzer.py]
AN --> REP[events/reporter.py]
EV --> OUT[events.ndjson]
REP --> SUM[summary.json]
REP --> MD[analysis.md]
classDef adapter fill:#ece8ff,stroke:#5a4699,color:#5a4699;
classDef core fill:#eaf3ff,stroke:#1f3a5f,color:#1f3a5f;
classDef event fill:#ecf9f0,stroke:#2f6b45,color:#2f6b45;
classDef output fill:#fff8e7,stroke:#a35d00,color:#a35d00;
class BA,V3,V4,AL adapter;
class CORE,CL core;
class EV,AN,REP event;
class OUT,SUM,MD output;
</pre>
</section>
<div id="s5" class="sec-head anim" style="--i:10"><span class="dot"></span>5 — 事件扫描层如何补充六步</div>
<section class="card anim" style="--i:11">
<p><strong>六步法</strong>解决的是“某个池子在某个时刻的报价是否可信”;<strong>事件层</strong>解决的是“最近一周发生了什么变化”。两者互补,不互相替代。</p>
<div class="grid-3">
<div class="chip"><strong>scan</strong><br><code>scan_events_last_n_days()</code></div>
<div class="chip"><strong>analyze</strong><br><code>build_weekly_report()</code></div>
<div class="chip"><strong>deliver</strong><br><code>events.ndjson / summary.json / analysis.md</code></div>
</div>
<p class="footer-note">默认窗口:<code>days=7</code>;CLI:<code>python main.py --event-scan --protocol all --days 7 ...</code></p>
</section>
<div id="s6" class="sec-head anim" style="--i:12"><span class="dot"></span>6 — 建议阅读路径(新读者)</div>
<section class="card anim" style="--i:13">
<div class="reading-path">
<article class="path-item">
<h4>Step A · 抽象层</h4>
<p>先读 <code>src/core.py</code>,建立统一心智模型:<code>PoolMeta</code>、<code>RawState</code>、<code>AMMConstraint</code>、<code>ProtocolAdapter</code>。</p>
</article>
<article class="path-item">
<h4>Step B · 最简单协议</h4>
<p>再读 Balancer:<code>src/balancer_adapter.py</code> + <code>src/vault_client.py</code>,看完整闭环。</p>
</article>
<article class="path-item">
<h4>Step C · 共性数学</h4>
<p>读集中流动性共享库:<code>src/protocols/concentrated/swap_math.py</code> 与 <code>swap_engine.py</code>。</p>
</article>
<article class="path-item">
<h4>Step D · 差异层与观测层</h4>
<p>最后读 V3/V4/Algebra 差异,再看 <code>src/events/*</code> 理解“状态变化分析”。</p>
</article>
</div>
</section>
</main>
</div>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
mermaid.initialize({
startOnLoad: true,
theme: 'base',
look: 'handDrawn',
themeVariables: {
primaryColor: '#eaf3ff',
primaryTextColor: '#1d2630',
primaryBorderColor: '#1f3a5f',
lineColor: '#5e6976',
secondaryColor: '#ecf9f0',
tertiaryColor: '#fff8e7',
fontFamily: 'PingFang SC, Noto Sans SC, sans-serif',
fontSize: '16px',
edgeLabelBackground: '#fffdf8'
},
flowchart: { htmlLabels: true, curve: 'basis' }
});
function updateZoomState(wrap) {
const scale = Number(wrap.dataset.scale || '1');
wrap.classList.toggle('is-zoomed', scale > 1.01);
if (!wrap.classList.contains('is-panning')) {
wrap.style.cursor = scale > 1.01 ? 'grab' : 'default';
}
}
window.zoomDiagram = function(btn, factor) {
const wrap = btn.closest('.mermaid-wrap');
const mer = wrap.querySelector('.mermaid');
const current = Number(wrap.dataset.scale || '1');
const next = Math.min(3, Math.max(0.6, current * factor));
wrap.dataset.scale = String(next);
mer.style.transform = `scale(${next})`;
updateZoomState(wrap);
};
window.resetZoom = function(btn) {
const wrap = btn.closest('.mermaid-wrap');
const mer = wrap.querySelector('.mermaid');
wrap.dataset.scale = '1';
mer.style.transform = 'scale(1)';
wrap.scrollLeft = 0;
wrap.scrollTop = 0;
updateZoomState(wrap);
};
document.querySelectorAll('.mermaid-wrap').forEach(wrap => {
wrap.dataset.scale = '1';
const mer = wrap.querySelector('.mermaid');
wrap.addEventListener('wheel', e => {
if (!(e.ctrlKey || e.metaKey)) return;
e.preventDefault();
const factor = e.deltaY < 0 ? 1.1 : 0.9;
const current = Number(wrap.dataset.scale || '1');
const next = Math.min(3, Math.max(0.6, current * factor));
wrap.dataset.scale = String(next);
mer.style.transform = `scale(${next})`;
updateZoomState(wrap);
}, { passive: false });
let panning = false;
let sx = 0;
let sy = 0;
let sl = 0;
let st = 0;
wrap.addEventListener('pointerdown', e => {
const scale = Number(wrap.dataset.scale || '1');
if (scale <= 1.01) return;
if (e.target.closest('.zoom-controls')) return;
panning = true;
wrap.classList.add('is-panning');
sx = e.clientX;
sy = e.clientY;
sl = wrap.scrollLeft;
st = wrap.scrollTop;
wrap.setPointerCapture(e.pointerId);
});
wrap.addEventListener('pointermove', e => {
if (!panning) return;
wrap.scrollLeft = sl - (e.clientX - sx);
wrap.scrollTop = st - (e.clientY - sy);
});
function endPan(e) {
if (!panning) return;
panning = false;
wrap.classList.remove('is-panning');
try { wrap.releasePointerCapture(e.pointerId); } catch (_) {}
updateZoomState(wrap);
}
wrap.addEventListener('pointerup', endPan);
wrap.addEventListener('pointercancel', endPan);
updateZoomState(wrap);
});
(function setupToc() {
const toc = document.getElementById('toc');
const links = toc.querySelectorAll('a');
const sections = [];
links.forEach(link => {
const id = link.getAttribute('href').slice(1);
const el = document.getElementById(id);
if (el) sections.push({ id, el, link });
});
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (!entry.isIntersecting) return;
links.forEach(l => l.classList.remove('active'));
const match = sections.find(s => s.el === entry.target);
if (!match) return;
match.link.classList.add('active');
if (window.innerWidth <= 1000) {
match.link.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' });
}
});
}, { rootMargin: '-8% 0px -78% 0px' });
sections.forEach(s => observer.observe(s.el));
links.forEach(link => {
link.addEventListener('click', e => {
e.preventDefault();
const id = link.getAttribute('href').slice(1);
const el = document.getElementById(id);
if (!el) return;
el.scrollIntoView({ behavior: 'smooth', block: 'start' });
history.replaceState(null, '', `#${id}`);
});
});
})();
</script>
</body>
</html>