@kevlns/xlmerge 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1522 +1,1522 @@
1
- <!DOCTYPE html>
2
- <html lang="zh-CN">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Excel 冲突解决工具</title>
7
- <style>
8
- :root {
9
- --bg-deep: #f4eed8;
10
- --bg-root: #e6dfc8;
11
- --bg-surface: #ffffff;
12
- --border-default: #000000;
13
- --shadow-edge: #625a4d;
14
- --gold: #f2c94c;
15
- --gold-dim: #e0b83e;
16
- --lava: #f29aa3;
17
- --emerald: #a2d2ff;
18
- --sapphire: #9dccff;
19
- --incoming: #cfe9b7;
20
- --incoming-deep: #b6d998;
21
- --tombstone: #77736b;
22
- --tombstone-dark: #58554f;
23
- --text-primary: #111111;
24
- --text-secondary: #444444;
25
- --text-muted: #777777;
26
- --shadow: 4px 4px 0 #000000;
27
- --shadow-hover: 2px 2px 0 #000000;
28
- --border-width: 3px;
29
- }
30
- * { margin: 0; padding: 0; box-sizing: border-box; }
31
- html { width: 100%; height: 100%; overflow: hidden; }
32
- body {
33
- font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
34
- background: var(--bg-deep);
35
- background-image:
36
- linear-gradient(var(--bg-root) 1px, transparent 1px),
37
- linear-gradient(90deg, var(--bg-root) 1px, transparent 1px);
38
- background-size: 20px 20px;
39
- color: var(--text-primary);
40
- width: 100%;
41
- height: 100vh;
42
- min-height: 0;
43
- overflow: hidden;
44
- display: flex;
45
- flex-direction: column;
46
- }
47
- .toolbar {
48
- background: var(--bg-surface);
49
- padding: 12px 20px;
50
- display: flex;
51
- align-items: center;
52
- gap: 16px;
53
- border-bottom: var(--border-width) solid var(--border-default);
54
- flex-wrap: nowrap;
55
- min-width: 0;
56
- }
57
- .toolbar h1 {
58
- flex: 0 0 auto;
59
- font-size: 18px;
60
- font-weight: 700;
61
- white-space: nowrap;
62
- }
63
- .file-tabs, .sheet-tabs {
64
- display: flex;
65
- gap: 6px;
66
- flex-wrap: nowrap;
67
- min-width: 0;
68
- overflow-x: auto;
69
- overflow-y: hidden;
70
- overscroll-behavior-inline: contain;
71
- scrollbar-color: var(--border-default) transparent;
72
- scrollbar-width: thin;
73
- }
74
- .file-tabs { flex: 1 1 auto; padding: 2px 4px 7px 0; }
75
- .sheet-tabs { flex: 1 1 auto; padding: 2px 4px 5px 0; }
76
- .file-tabs::-webkit-scrollbar, .sheet-tabs::-webkit-scrollbar { height: 8px; }
77
- .file-tabs::-webkit-scrollbar-track, .sheet-tabs::-webkit-scrollbar-track { background: transparent; }
78
- .file-tabs::-webkit-scrollbar-thumb, .sheet-tabs::-webkit-scrollbar-thumb {
79
- background: var(--border-default);
80
- border: 2px solid var(--bg-surface);
81
- }
82
- .file-tab, .sheet-tab {
83
- flex: 0 0 auto;
84
- padding: 8px 16px;
85
- background: var(--bg-surface);
86
- color: var(--text-secondary);
87
- cursor: pointer;
88
- font-size: 13px;
89
- font-weight: 600;
90
- border: var(--border-width) solid var(--border-default);
91
- box-shadow: var(--shadow);
92
- transition: transform 150ms ease, box-shadow 150ms ease;
93
- }
94
- .file-tab:hover, .sheet-tab:hover { transform: translate(2px, 2px); box-shadow: var(--shadow-hover); }
95
- .file-tab.active {
96
- background: var(--emerald);
97
- transform: translate(2px, 2px);
98
- box-shadow: var(--shadow-hover);
99
- }
100
- .file-tab.has-conflicts { color: var(--text-primary); }
101
- .sheet-toolbar {
102
- background: #fff8dc;
103
- padding: 9px 20px 11px;
104
- display: flex;
105
- align-items: center;
106
- gap: 12px;
107
- border-bottom: var(--border-width) solid var(--border-default);
108
- flex-wrap: nowrap;
109
- min-width: 0;
110
- }
111
- .sheet-scope {
112
- display: flex;
113
- align-items: center;
114
- gap: 9px;
115
- min-width: 0;
116
- flex: 1 1 auto;
117
- }
118
- .sheet-choice { flex: 0 0 auto; display: flex; gap: 6px; align-items: center; }
119
- .macro-label { font-size: 11px; font-weight: 900; color: #3d3529; white-space: nowrap; }
120
- .macro-toggle { padding: 5px 9px; font-size: 11px; opacity: .55; }
121
- .macro-toggle.is-selected { opacity: 1; outline: 2px solid #4f4638; outline-offset: 1px; }
122
- .macro-toggle:disabled { cursor: not-allowed; filter: grayscale(.45); opacity: .45; }
123
- .macro-note { font-size: 11px; font-weight: 700; color: #6a5142; white-space: nowrap; }
124
- .diff-legend {
125
- display: flex;
126
- align-items: center;
127
- gap: 14px;
128
- padding: 7px 20px;
129
- border-bottom: 2px solid #cfc5ae;
130
- background: #f8f1de;
131
- color: #4b4438;
132
- font-size: 11px;
133
- font-weight: 700;
134
- white-space: nowrap;
135
- overflow-x: auto;
136
- }
137
- .legend-intro { color: #27231d; font-weight: 900; }
138
- .legend-item { display: inline-flex; align-items: center; gap: 5px; }
139
- .legend-swatch { width: 18px; height: 13px; border: 1px solid #6e6657; display: inline-block; }
140
- .legend-local { background: var(--sapphire); box-shadow: inset 0 0 0 2px rgba(35, 84, 130, .42); }
141
- .legend-remote { background: var(--lava); box-shadow: inset 0 0 0 2px rgba(124, 38, 47, .38); }
142
- .legend-incoming { background: var(--incoming); }
143
- .legend-deleted { background: var(--tombstone); }
144
- .sheet-label {
145
- padding: 5px 8px;
146
- background: var(--border-default);
147
- color: var(--bg-surface);
148
- font-size: 11px;
149
- font-weight: 800;
150
- letter-spacing: 1.2px;
151
- }
152
- .sheet-tab { padding: 6px 13px; box-shadow: 3px 3px 0 #000; }
153
- .sheet-tab.active {
154
- background: var(--gold);
155
- transform: translate(2px, 2px);
156
- box-shadow: 1px 1px 0 #000;
157
- }
158
- .tab-count {
159
- margin-left: 7px;
160
- font-size: 11px;
161
- color: var(--text-muted);
162
- }
163
- .actions { flex: 0 0 auto; margin-left: 0; display: flex; gap: 8px; align-items: center; }
164
- .sheet-actions {
165
- flex: 0 0 auto;
166
- display: flex;
167
- align-items: center;
168
- gap: 7px;
169
- padding: 4px 7px;
170
- border: 2px solid #b8a66d;
171
- background: rgba(255, 255, 255, .58);
172
- }
173
- .sheet-actions-label {
174
- color: #6c5518;
175
- font-size: 10px;
176
- font-weight: 900;
177
- white-space: nowrap;
178
- }
179
- .sheet-actions .btn { padding: 7px 11px; font-size: 12px; }
180
- .global-stats {
181
- flex: 0 0 auto;
182
- display: inline-flex;
183
- align-items: center;
184
- gap: 7px;
185
- padding: 5px 8px;
186
- border-left: 2px solid #bfb5a0;
187
- color: #332d24;
188
- white-space: nowrap;
189
- }
190
- .global-stats-label { font-size: 11px; font-weight: 900; }
191
- .global-stats-value { font-size: 16px; font-weight: 900; line-height: 1; }
192
- .global-stats-divider { color: #8c806c; font-size: 16px; font-weight: 700; }
193
- .btn {
194
- padding: 8px 16px;
195
- border: 1px solid #d8cfbd;
196
- border-radius: 3px;
197
- cursor: pointer;
198
- font-size: 13px;
199
- font-weight: 800;
200
- font-family: inherit;
201
- letter-spacing: .2px;
202
- box-shadow: 2px 2px 0 var(--shadow-edge);
203
- transition: transform 120ms ease, filter 120ms ease, box-shadow 120ms ease;
204
- background: linear-gradient(180deg, #f6f0e2, #dcd1bc);
205
- color: #2a261f;
206
- }
207
- .btn:hover { transform: translate(1px, 1px); filter: brightness(1.04); box-shadow: 1px 1px 0 var(--shadow-edge); }
208
- .btn:active { transform: translateY(1px); filter: brightness(.96); }
209
- .btn:focus-visible { outline: 3px solid #a2d2ff; outline-offset: 2px; }
210
- .btn-local { background: linear-gradient(180deg, #d9ecff, #91c4f1); border-color: #b8d8f4; color: #173f63; }
211
- .btn-remote { background: linear-gradient(180deg, #ffd1d5, #e9919a); border-color: #f2b3ba; color: #5a2027; }
212
- .btn-undo { background: linear-gradient(180deg, #f7f2e7, #dcd2bf); border-color: #e4ddcf; color: #2d2923; }
213
- .btn-reset { background: linear-gradient(180deg, #ffe7a5, #efc65c); border-color: #f5dfa1; color: #49360d; }
214
- .btn-cancel { background: linear-gradient(180deg, #f5c9cc, #dfa0a6); border-color: #efc2c6; color: #4a2529; }
215
- .btn-submit {
216
- position: relative;
217
- min-width: 126px;
218
- padding: 11px 28px;
219
- background: linear-gradient(180deg, #ffdc8a, #f4aa3e);
220
- border: 1px solid #f5d18d;
221
- border-bottom-color: #dbab5e;
222
- color: #3d2608;
223
- font-size: 14px;
224
- text-shadow: 0 1px 0 rgba(255, 255, 255, .35);
225
- box-shadow: 0 4px 0 #ad7626, 2px 2px 0 var(--shadow-edge);
226
- }
227
- .btn-submit:hover { transform: translateY(-1px); box-shadow: 0 5px 0 #ad7626, 2px 2px 0 var(--shadow-edge); }
228
- .btn-submit:active { transform: translateY(3px); box-shadow: 0 1px 0 #ad7626; }
229
- .btn:disabled { opacity: .38; cursor: not-allowed; transform: none; filter: none; }
230
- .stats {
231
- font-size: 13px;
232
- color: var(--text-secondary);
233
- font-weight: 600;
234
- white-space: nowrap;
235
- }
236
- .table-stage {
237
- flex: 1 1 auto;
238
- min-width: 0;
239
- min-height: 0;
240
- position: relative;
241
- }
242
- .table-container {
243
- width: 100%;
244
- height: 100%;
245
- overflow: auto;
246
- overscroll-behavior: contain;
247
- scrollbar-gutter: stable;
248
- padding: 16px;
249
- }
250
- .confirmed-overlay {
251
- position: absolute;
252
- inset: 0;
253
- z-index: 10;
254
- display: flex;
255
- align-items: center;
256
- justify-content: center;
257
- background: rgba(70, 70, 70, .58);
258
- cursor: not-allowed;
259
- }
260
- .confirmed-overlay-mark {
261
- width: 112px;
262
- height: 112px;
263
- border-radius: 50%;
264
- display: flex;
265
- align-items: center;
266
- justify-content: center;
267
- background: #2ca45d;
268
- border: 6px solid #d8ffe5;
269
- color: #fff;
270
- font-size: 76px;
271
- line-height: 1;
272
- font-weight: 900;
273
- box-shadow: 0 5px 0 #12552e, 0 0 28px rgba(0, 0, 0, .35);
274
- }
275
- table.diff-table {
276
- border-collapse: collapse;
277
- font-size: 13px;
278
- width: auto;
279
- border: var(--border-width) solid var(--border-default);
280
- box-shadow: var(--shadow);
281
- background: var(--bg-surface);
282
- }
283
- table.diff-table th, table.diff-table td {
284
- border: 2px solid var(--border-default);
285
- padding: 6px 12px;
286
- text-align: left;
287
- min-width: 80px;
288
- max-width: 240px;
289
- overflow: hidden;
290
- text-overflow: ellipsis;
291
- white-space: nowrap;
292
- }
293
- table.diff-table th {
294
- background: var(--gold);
295
- color: #111;
296
- font-weight: 800;
297
- cursor: pointer;
298
- text-shadow: none;
299
- }
300
- table.diff-table th:hover { background: var(--gold-dim); color: #111; }
301
- td.cell-local {
302
- background: var(--sapphire);
303
- color: #111;
304
- cursor: pointer;
305
- }
306
- td.cell-remote {
307
- background: var(--lava);
308
- color: #111;
309
- cursor: pointer;
310
- }
311
- td.cell-no-conflict { background: var(--bg-surface); color: var(--text-secondary); }
312
- .cell-selectable { box-shadow: inset 0 0 0 2px rgba(47, 42, 34, .38); cursor: pointer !important; }
313
- .cell-selectable:hover { filter: brightness(.92) saturate(1.12); box-shadow: inset 0 0 0 3px rgba(47, 42, 34, .58); }
314
- .row-header { cursor: pointer; }
315
- .row-header:hover { background: var(--emerald) !important; }
316
- tr.luban-header th { background: #2d2d2d; color: #f2f2f2; font-weight: 700; cursor: default; font-size: 12px; }
317
- tr.luban-header th:hover { background: #2d2d2d; color: #f2f2f2; }
318
- .axis-added-incoming {
319
- background: var(--incoming) !important;
320
- color: #29401f !important;
321
- }
322
- .axis-added-incoming:hover { background: var(--incoming-deep) !important; }
323
- .axis-tombstone {
324
- background-color: var(--tombstone) !important;
325
- background-image: repeating-linear-gradient(135deg, rgba(255,255,255,.08) 0 8px, rgba(0,0,0,.08) 8px 16px) !important;
326
- color: #f4f1e9 !important;
327
- cursor: not-allowed !important;
328
- text-decoration: line-through;
329
- }
330
- .axis-tombstone:hover { background-color: var(--tombstone-dark) !important; }
331
- .structure-control { min-width: 80px; padding: 4px 5px !important; text-align: center !important; background: #ece5d4 !important; }
332
- .structure-toggle {
333
- border: 1px solid #c6bca8;
334
- border-radius: 3px;
335
- padding: 3px 7px;
336
- background: #fff8dc;
337
- color: #332b20;
338
- font: 700 11px/1.2 inherit;
339
- cursor: pointer;
340
- box-shadow: 1px 1px 0 var(--shadow-edge);
341
- }
342
- .structure-toggle.is-delete { background: var(--tombstone); color: #fff; border-color: var(--tombstone-dark); }
343
- .structure-toggle:disabled { opacity: .42; cursor: not-allowed; box-shadow: none; }
344
- .structure-badge { display: inline-block; padding: 2px 6px; border: 1px solid rgba(0,0,0,.28); font-size: 10px; font-weight: 800; }
345
- .structure-badge.added { background: var(--incoming); color: #29401f; }
346
- .structure-badge.local-existing { background: #eee8dc; color: #4b4438; border-color: #bdb4a4; }
347
- .structure-badge.deleted { background: var(--tombstone); color: #fff; }
348
- .axis-index { display: inline-block; min-width: 22px; font-weight: 900; }
349
- .axis-index.is-removed { color: #f7f1e5; }
350
- .axis-source { display: block; margin-top: 2px; font-size: 9px; font-weight: 700; opacity: .72; }
351
- .sheet-tab.sheet-added { background: var(--incoming); }
352
- .sheet-tab.sheet-deleted { background: var(--tombstone); color: #fff; text-decoration: line-through; }
353
- .sheet-tab.sheet-structural-conflict::before { content: '◆'; margin-right: 5px; color: #a8392f; }
354
- .sheet-confirm-checkbox {
355
- width: 15px;
356
- height: 15px;
357
- margin-right: 7px;
358
- vertical-align: -2px;
359
- accent-color: #278b52;
360
- cursor: pointer;
361
- }
362
- .sheet-confirm-checkbox:focus-visible { outline: 2px solid #278b52; outline-offset: 2px; }
363
- .conflict-nav { flex: 0 0 auto; display: flex; gap: 6px; align-items: center; }
364
- .conflict-nav .btn { padding: 6px 10px; font-size: 11px; }
365
- .conflict-nav-count { min-width: 48px; text-align: center; color: #4b4438; font-size: 11px; font-weight: 800; }
366
- .cell-locked { cursor: not-allowed !important; }
367
- .conflict-cell-flash { animation: conflict-cell-flash .55s ease-in-out 2; position: relative; z-index: 1; }
368
- @keyframes conflict-cell-flash {
369
- 0%, 100% { box-shadow: inset 0 0 0 2px rgba(47, 42, 34, .38); }
370
- 35%, 70% {
371
- background-color: #b86cff !important;
372
- background-image: none !important;
373
- box-shadow: inset 0 0 0 999px rgba(184, 108, 255, .96), 0 0 20px 6px rgba(147, 61, 255, .88);
374
- }
375
- }
376
- .tooltip {
377
- position: fixed;
378
- display: none;
379
- background: var(--bg-surface);
380
- color: var(--text-primary);
381
- border: var(--border-width) solid var(--border-default);
382
- padding: 10px 14px;
383
- font-size: 12px;
384
- z-index: 1000;
385
- pointer-events: none;
386
- max-width: 520px;
387
- box-shadow: var(--shadow);
388
- word-break: break-word;
389
- }
390
- .tooltip .label { color: var(--text-muted); margin-bottom: 6px; font-weight: 600; }
391
- .tooltip .val-base { color: #666; }
392
- .tooltip .val-local { color: #246aa3; }
393
- .tooltip .val-remote { color: #c0392b; }
394
- .tooltip .val-current { color: var(--text-primary); margin-top: 5px; font-weight: 700; }
395
- .tooltip .author { color: #777; margin-left: 18px; font-weight: 400; }
396
- .sheet-message {
397
- max-width: 720px;
398
- margin: 20px;
399
- padding: 24px;
400
- background: var(--bg-surface);
401
- border: var(--border-width) solid var(--border-default);
402
- box-shadow: var(--shadow);
403
- }
404
- .sheet-message h2 { font-size: 17px; margin-bottom: 8px; }
405
- .sheet-message p { color: var(--text-secondary); margin-bottom: 16px; line-height: 1.6; }
406
- .pager { display: flex; gap: 6px; padding: 12px 0; }
407
- .commit-modal { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; }
408
- .commit-modal.is-hidden { display: none; }
409
- .commit-modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); }
410
- .commit-modal-panel {
411
- position: relative;
412
- background: var(--bg-surface);
413
- border: var(--border-width) solid var(--border-default);
414
- box-shadow: 8px 8px 0 #000;
415
- padding: 22px 26px;
416
- max-width: 520px;
417
- width: 88%;
418
- }
419
- .commit-modal-icon { font-size: 28px; margin-bottom: 8px; }
420
- .commit-modal-title { font-size: 17px; font-weight: 700; margin-bottom: 8px; }
421
- .commit-modal-body { font-size: 13px; color: var(--text-secondary); white-space: pre-wrap; word-break: break-word; margin-bottom: 10px; }
422
- .commit-modal-steps { font-size: 12px; font-family: Consolas, monospace; background: rgba(0, 0, 0, .04); border: 1px solid #ddd; padding: 8px 10px; margin-bottom: 12px; white-space: pre-wrap; word-break: break-word; max-height: 45vh; overflow-y: auto; }
423
- .commit-modal-steps:empty { display: none; }
424
- .commit-modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
425
- .commit-modal.is-success .commit-modal-icon::before { content: "\2705"; }
426
- .commit-modal.is-error .commit-modal-icon::before { content: "\274C"; }
427
- .footer {
428
- position: relative;
429
- z-index: 20;
430
- flex: 0 0 auto;
431
- background: var(--bg-surface);
432
- padding: 12px 20px;
433
- display: flex;
434
- align-items: center;
435
- justify-content: space-between;
436
- gap: 20px;
437
- border-top: var(--border-width) solid var(--border-default);
438
- color: var(--text-primary);
439
- font-size: 13px;
440
- }
441
- .footer-actions { flex: 0 0 auto; display: flex; align-items: center; gap: 12px; }
442
- @media (max-width: 900px) {
443
- .toolbar { flex-wrap: wrap; }
444
- .file-tabs { order: 3; flex: 1 0 100%; width: 100%; }
445
- .actions { width: auto; margin-left: auto; }
446
- .sheet-toolbar { align-items: flex-start; flex-wrap: wrap; }
447
- .sheet-scope { flex: 1 0 100%; }
448
- .sheet-actions { margin-left: auto; }
449
- .footer { align-items: flex-end; }
450
- }
451
- </style>
452
- </head>
453
- <body>
454
- <div class="toolbar">
455
- <h1>Excel 冲突解决工具</h1>
456
- <div class="file-tabs" id="fileTabs"></div>
457
- <div class="global-stats" id="globalConfirmationStats" aria-label="全局冲突 Sheet 确认统计">
458
- <span class="global-stats-label">冲突 Sheet 已确认</span>
459
- <span class="global-stats-value" id="confirmedSheetCount">0</span>
460
- <span class="global-stats-divider" aria-hidden="true">/</span>
461
- <span class="global-stats-value" id="totalConflictSheetCount">0</span>
462
- </div>
463
- </div>
464
- <div class="sheet-toolbar">
465
- <div class="sheet-scope" aria-label="当前 Sheet 工区">
466
- <span class="sheet-label">SHEET</span>
467
- <div class="sheet-tabs" id="sheetTabs"></div>
468
- <div class="sheet-actions" id="sheetActions" aria-label="当前 Sheet 操作">
469
- <span class="sheet-actions-label">当前 Sheet 操作</span>
470
- <button type="button" class="btn btn-local" id="allLocalBtn">全部本地</button>
471
- <button type="button" class="btn btn-remote" id="allRemoteBtn">全部远端</button>
472
- <button type="button" class="btn btn-undo" id="undoBtn">撤销</button>
473
- <button type="button" class="btn btn-reset" id="resetBtn">重置</button>
474
- </div>
475
- </div>
476
- <div class="conflict-nav" id="conflictNav" aria-label="冲突定位">
477
- <button type="button" class="btn" id="prevConflictBtn">上一个冲突</button>
478
- <span class="conflict-nav-count" id="conflictNavCount">0 / 0</span>
479
- <button type="button" class="btn" id="nextConflictBtn">下一个冲突</button>
480
- </div>
481
- <div class="sheet-choice" id="sheetChoice"></div>
482
- </div>
483
- <div class="diff-legend" id="diffLegend">
484
- <span class="legend-intro">主行列号=合并后位置</span>
485
- <span class="legend-item"><i class="legend-swatch legend-local"></i>本地值</span>
486
- <span class="legend-item"><i class="legend-swatch legend-remote"></i>远端值</span>
487
- <span class="legend-item"><i class="legend-swatch legend-incoming"></i>远端新增</span>
488
- <span class="legend-item"><i class="legend-swatch legend-deleted"></i>已删除</span>
489
- <span class="legend-item">悬停 Cell 查看 Base / 本地 / 远端</span>
490
- </div>
491
- <div class="table-stage" id="tableStage">
492
- <div class="table-container" id="tableContainer"></div>
493
- </div>
494
- <div class="tooltip" id="tooltip"></div>
495
- <div class="commit-modal is-hidden" id="commitModal" role="dialog" aria-modal="true" aria-labelledby="commitModalTitle">
496
- <div class="commit-modal-backdrop"></div>
497
- <div class="commit-modal-panel">
498
- <div class="commit-modal-icon"></div>
499
- <div class="commit-modal-title" id="commitModalTitle">提交中...</div>
500
- <div class="commit-modal-body" id="commitModalBody"></div>
501
- <div class="commit-modal-steps" id="commitModalSteps"></div>
502
- <div class="commit-modal-actions" id="commitModalActions"></div>
503
- </div>
504
- </div>
505
- <div class="footer">
506
- <span id="footerStatus">加载中...</span>
507
- <div class="footer-actions">
508
- <button type="button" class="btn btn-cancel" id="cancelBtn">取消并退出</button>
509
- <button type="button" class="btn btn-submit" id="submitBtn">提交合并</button>
510
- </div>
511
- </div>
512
- <script>
513
- let model = null;
514
- let currentFile = 0;
515
- let currentSheet = 0;
516
- let currentPage = 0;
517
- let decisions = {files: {}};
518
- let undoStack = [];
519
- const PAGE_SIZE = 500;
520
-
521
- const byId = id => document.getElementById(id);
522
- const fmtVal = value => value === null || value === undefined ? '' : String(value);
523
- function colLabel(n) { let s = ''; while (n > 0) { n--; s = String.fromCharCode(65 + n % 26) + s; n = Math.floor(n / 26); } return s; }
524
- function currentFileData() { return model && model.files[currentFile]; }
525
- function currentSheetData() { const file = currentFileData(); return file && file.diff.sheets[currentSheet]; }
526
- function fileDecision(file) {
527
- if (!decisions.files[file.id]) decisions.files[file.id] = {sheets: {}};
528
- return decisions.files[file.id];
529
- }
530
- function sheetDecision(file, sheet) {
531
- const target = fileDecision(file).sheets;
532
- if (!target[sheet.name]) target[sheet.name] = {cells: {}, rows: {}, columns: {}, confirmed: false};
533
- if (typeof target[sheet.name].confirmed !== 'boolean') target[sheet.name].confirmed = false;
534
- return target[sheet.name];
535
- }
536
- function cellKey(cell) { return cell.id || cell.coordinate; }
537
- function defaultCellChoice(cell) {
538
- if (cell.resolution === 'conflict') return 'theirs';
539
- if (cell.resolution === 'same') return cell.oursPresent === false ? 'theirs' : 'ours';
540
- return cell.resolution;
541
- }
542
- function defaultSheetChoice(sheet) { return sheet.action.startsWith('conflict_') ? 'ours' : null; }
543
- function initializeDecisions() {
544
- for (const file of model.files) {
545
- const fileTarget = fileDecision(file);
546
- if (file.macro) fileTarget.macro = file.macro.default || 'ours';
547
- for (const sheet of file.diff.sheets) {
548
- const target = sheetDecision(file, sheet);
549
- target.confirmed = false;
550
- const sheetChoice = defaultSheetChoice(sheet);
551
- if (sheetChoice) target.sheet = sheetChoice;
552
- if (sheet.renameStatus === 'rename_conflict') target.rename = 'ours';
553
- if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') target.mergedCells = sheet.mergedCells.default || 'ours';
554
- for (const row of sheet.allRows || sheet.rows || []) if (row.state === 'delete_modify') target.rows[row.id] = row.default;
555
- for (const column of sheet.columns || []) if (column.state === 'delete_modify') target.columns[column.id] = column.default;
556
- for (const cell of sheet.cells || []) target.cells[cell.id || cell.coordinate] = defaultCellChoice(cell);
557
- }
558
- }
559
- }
560
- function gridVal(sheet, row, col) {
561
- const values = (sheet.contextRows || {})[String(row)];
562
- return values ? values[col - 1] : '';
563
- }
564
- function enableHorizontalWheel(host) {
565
- host.addEventListener('wheel', event => {
566
- if (host.scrollWidth <= host.clientWidth || Math.abs(event.deltaX) >= Math.abs(event.deltaY)) return;
567
- event.preventDefault();
568
- host.scrollLeft += event.deltaY;
569
- }, {passive: false});
570
- }
571
-
572
- function autoTheirsNote(sep) {
573
- const count = (model && model.summary) ? (model.summary.autoTheirsCount || 0) : 0;
574
- return count ? `${sep}已按远端全量自动处理 ${count} 张表` : '';
575
- }
576
-
577
- async function init() {
578
- const response = await fetch('/api/diff');
579
- model = await response.json();
580
- if (!model.files || model.files.length === 0) {
581
- byId('footerStatus').textContent = '未发现冲突。' + autoTheirsNote('');
582
- return;
583
- }
584
- initializeDecisions();
585
- render();
586
- }
587
-
588
- function render() {
589
- renderFileTabs();
590
- renderSheetTabs();
591
- renderSheetChoice();
592
- renderTable();
593
- renderConflictNavigation();
594
- renderActionAvailability();
595
- updateStats();
596
- }
597
- function renderFileTabs() {
598
- const host = byId('fileTabs');
599
- host.replaceChildren();
600
- model.files.forEach((file, index) => {
601
- const tab = document.createElement('div');
602
- tab.className = 'file-tab' + (index === currentFile ? ' active' : '') + (file.diff.summary.changedCellCount ? ' has-conflicts' : '');
603
- tab.textContent = file.path;
604
- tab.onclick = () => { currentFile = index; currentSheet = 0; currentPage = 0; render(); };
605
- host.append(tab);
606
- });
607
- }
608
- function sheetDecisionCount(file, sheet) {
609
- if (sheet.action.startsWith('conflict_')) return 1;
610
- if (sheet.action !== 'merge') return 1;
611
- let total = sheet.renameStatus === 'rename_conflict' ? 1 : 0;
612
- if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') total++;
613
- total += (sheet.allRows || sheet.rows || []).filter(axis => axis.state === 'delete_modify').length;
614
- total += (sheet.columns || []).filter(axis => axis.state === 'delete_modify').length;
615
- for (const cell of sheet.cells || []) {
616
- if (!sheet.columns || !sheet.allRows || structuralCellSelectable(file, sheet, cell)) total++;
617
- }
618
- return total;
619
- }
620
- function renderSheetTabs() {
621
- const host = byId('sheetTabs');
622
- host.replaceChildren();
623
- const file = currentFileData();
624
- // 只显示需要处理的 Sheet(needsAttention=false 的无冲突/无更改 Sheet 折叠)
625
- const visible = [];
626
- file.diff.sheets.forEach((sheet, index) => {
627
- if (sheet.needsAttention === false) return;
628
- visible.push({ sheet, index });
629
- });
630
- if (!visible.length) {
631
- const empty = document.createElement('div');
632
- empty.className = 'sheet-tab';
633
- empty.textContent = '该文件没有需要处理的 Sheet';
634
- host.append(empty);
635
- return;
636
- }
637
- const current = file.diff.sheets[currentSheet];
638
- if (!current || current.needsAttention === false) {
639
- currentSheet = visible[0].index;
640
- }
641
- visible.forEach(({ sheet, index }) => {
642
- const tab = document.createElement('div');
643
- const added = sheet.action === 'auto_add_theirs';
644
- const deleted = sheet.action === 'auto_delete';
645
- const structuralConflict = sheet.action.startsWith('conflict_') || sheet.renameStatus === 'rename_conflict' || (sheet.structuralConflictCount || 0) > 0;
646
- tab.className = 'sheet-tab' + (index === currentSheet ? ' active' : '') + (added ? ' sheet-added' : '') + (deleted ? ' sheet-deleted' : '') + (structuralConflict ? ' sheet-structural-conflict' : '');
647
- const target = sheetDecision(file, sheet);
648
- const confirmation = document.createElement('input');
649
- confirmation.type = 'checkbox';
650
- confirmation.className = 'sheet-confirm-checkbox';
651
- confirmation.checked = target.confirmed === true;
652
- confirmation.title = '勾选表示你已人工审阅并确认本 Sheet 的所有冲突;取消勾选可继续编辑。';
653
- confirmation.setAttribute('aria-label', `${sheet.name}:已确认(人工审阅完成)`);
654
- confirmation.onclick = event => event.stopPropagation();
655
- confirmation.onchange = event => {
656
- event.stopPropagation();
657
- target.confirmed = confirmation.checked;
658
- render();
659
- };
660
- tab.append(confirmation, document.createTextNode(sheet.name));
661
- const count = document.createElement('span');
662
- count.className = 'tab-count';
663
- count.textContent = sheetDecisionCount(file, sheet);
664
- tab.append(count);
665
- tab.onclick = () => { currentSheet = index; currentPage = 0; render(); };
666
- host.append(tab);
667
- });
668
- }
669
- function renderSheetChoice() {
670
- const host = byId('sheetChoice'); host.replaceChildren();
671
- const file = currentFileData(); const sheet = currentSheetData();
672
- const macro = file && file.macro;
673
- if (macro && macro.status !== 'same') {
674
- const label = document.createElement('span'); label.className = 'macro-label'; label.textContent = '当前文件宏 · VBA/ActiveX';
675
- host.append(label);
676
- if (macro.status === 'conflict') {
677
- const selected = fileDecision(file).macro || macro.default;
678
- const local = document.createElement('button');
679
- local.className = 'btn btn-local macro-toggle' + (selected === 'ours' ? ' is-selected' : '');
680
- local.textContent = '本地宏'; local.onclick = () => chooseMacro(file, 'ours');
681
- const remote = document.createElement('button');
682
- remote.className = 'btn btn-remote macro-toggle' + (selected === 'theirs' ? ' is-selected' : '');
683
- remote.textContent = '远端宏'; remote.onclick = () => chooseMacro(file, 'theirs');
684
- host.append(local, remote);
685
- } else {
686
- const note = document.createElement('span'); note.className = 'macro-note';
687
- note.textContent = macro.status === 'auto_theirs'
688
- ? '仅远端变更,自动采用远端宏'
689
- : macro.status === 'auto_ours'
690
- ? '仅本地变更,自动采用本地宏'
691
- : '两边宏包结构不同,安全保留本地宏';
692
- host.append(note);
693
- }
694
- }
695
- if (!sheet) return;
696
- const target = sheetDecision(file, sheet);
697
- const confirmed = sheetNeedsConfirmation(sheet) && target.confirmed;
698
- if (sheet.renameStatus === 'rename_conflict') {
699
- const local = document.createElement('button'); local.className = 'structure-toggle' + (target.rename === 'ours' ? '' : ' is-delete');
700
- local.textContent = `本地名:${sheet.names.ours}`; local.disabled = confirmed; local.onclick = () => chooseRename(file, sheet, 'ours');
701
- const remote = document.createElement('button'); remote.className = 'structure-toggle' + (target.rename === 'theirs' ? '' : ' is-delete');
702
- remote.textContent = `远端名:${sheet.names.theirs}`; remote.disabled = confirmed; remote.onclick = () => chooseRename(file, sheet, 'theirs');
703
- host.append(local, remote);
704
- }
705
- if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') {
706
- const label = document.createElement('span'); label.className = 'macro-label'; label.textContent = '合并表头';
707
- const local = document.createElement('button');
708
- local.className = 'btn btn-local macro-toggle' + (target.mergedCells === 'ours' ? ' is-selected' : '');
709
- local.textContent = '本地结构'; local.disabled = confirmed; local.onclick = () => chooseMergedCells(file, sheet, 'ours');
710
- const remote = document.createElement('button');
711
- remote.className = 'btn btn-remote macro-toggle' + (target.mergedCells === 'theirs' ? ' is-selected' : '');
712
- remote.textContent = '远端结构'; remote.disabled = confirmed; remote.onclick = () => chooseMergedCells(file, sheet, 'theirs');
713
- host.append(label, local, remote);
714
- }
715
- }
716
-
717
- function appendHeaderCell(row, value, onClick, className) {
718
- const cell = document.createElement('th');
719
- cell.textContent = value;
720
- if (className) cell.className = className;
721
- if (onClick) cell.onclick = onClick;
722
- row.append(cell);
723
- return cell;
724
- }
725
- function bindTip(cell, row, col) {
726
- cell.onmouseenter = event => showTip(event, row, col);
727
- cell.onmousemove = event => positionTip(event);
728
- cell.onmouseleave = hideTip;
729
- }
730
- function renderTable() {
731
- const file = currentFileData();
732
- const sheet = currentSheetData();
733
- const host = byId('tableContainer');
734
- host.replaceChildren();
735
- const oldOverlay = byId('confirmedOverlay');
736
- if (oldOverlay) oldOverlay.remove();
737
- if (!sheet) {
738
- renderMessage(host, '无需处理', '两侧工作簿内容一致,没有需要展示的 Sheet 差异。');
739
- return;
740
- }
741
- if (sheet.action.startsWith('conflict_')) {
742
- renderSheetConflict(host, file, sheet);
743
- renderConfirmedOverlay(host, file, sheet);
744
- return;
745
- }
746
- if (sheet.action !== 'merge') {
747
- const labels = {
748
- auto_add_ours: '该 Sheet 仅存在于本地,将自动保留本地版本。',
749
- auto_add_theirs: '该 Sheet 仅存在于远端,将自动保留远端版本。',
750
- auto_delete: '该 Sheet 的删除只发生在一侧,另一侧未修改,将自动删除。'
751
- };
752
- renderMessage(host, sheet.name, labels[sheet.action] || `将自动执行:${sheet.action}`);
753
- renderConfirmedOverlay(host, file, sheet);
754
- return;
755
- }
756
- renderDiffTable(host, file, sheet);
757
- renderConfirmedOverlay(host, file, sheet);
758
- }
759
- function sheetNeedsConfirmation(sheet) {
760
- return !!sheet && sheet.needsAttention !== false;
761
- }
762
- function renderConfirmedOverlay(host, file, sheet) {
763
- if (!sheetNeedsConfirmation(sheet) || !sheetDecision(file, sheet).confirmed) return;
764
- const overlay = document.createElement('div');
765
- overlay.id = 'confirmedOverlay';
766
- overlay.className = 'confirmed-overlay';
767
- overlay.title = '本 Sheet 已确认;取消 Sheet 标签前的勾选后可继续编辑。';
768
- overlay.setAttribute('aria-label', '本 Sheet 已确认,表格交互已锁定');
769
- const mark = document.createElement('div');
770
- mark.className = 'confirmed-overlay-mark';
771
- mark.textContent = '✓';
772
- overlay.append(mark);
773
- byId('tableStage').append(overlay);
774
- }
775
- function currentSheetIsConfirmed() {
776
- const file = currentFileData(); const sheet = currentSheetData();
777
- return !!file && sheetNeedsConfirmation(sheet) && sheetDecision(file, sheet).confirmed;
778
- }
779
- function renderActionAvailability() {
780
- const confirmed = currentSheetIsConfirmed();
781
- byId('allLocalBtn').disabled = confirmed;
782
- byId('allRemoteBtn').disabled = confirmed;
783
- const file = currentFileData(); const sheet = currentSheetData();
784
- const scope = file && sheet ? `当前 Sheet:${file.path} / ${sheet.name}` : '当前 Sheet';
785
- const message = confirmed ? `${scope} 已确认;取消 Sheet 标签前的勾选后可继续编辑。` : `${scope} 的批量操作`;
786
- byId('allLocalBtn').title = `${message}:将可处理项全部采用本地。`;
787
- byId('allRemoteBtn').title = `${message}:将可处理项全部采用远端。`;
788
- byId('undoBtn').title = `${scope}:撤销最近一次选择,不影响其它 Sheet 或文件级宏。`;
789
- byId('resetBtn').title = `${scope}:恢复本 Sheet 的默认决策,不影响其它 Sheet 或文件级宏。`;
790
- }
791
- function navigableConflictCells(file, sheet) {
792
- if (!sheet || !sheetNeedsConfirmation(sheet)) return [];
793
- if (sheet.columns && sheet.allRows) {
794
- return (sheet.cells || []).filter(cell => cell.resolution === 'conflict' && structuralCellSelectable(file, sheet, cell));
795
- }
796
- return (sheet.cells || []).filter(cell => cell.resolution === 'conflict');
797
- }
798
- function conflictCellKey(cell) { return cell && (cell.id || cell.coordinate); }
799
- function renderConflictNavigation() {
800
- const file = currentFileData(); const sheet = currentSheetData();
801
- const cells = navigableConflictCells(file, sheet);
802
- const count = byId('conflictNavCount');
803
- const previous = byId('prevConflictBtn'); const next = byId('nextConflictBtn');
804
- if (!count || !previous || !next) return;
805
- const currentId = sheet && sheetDecision(file, sheet).lastNavigatedConflict;
806
- const currentIndex = cells.findIndex(cell => conflictCellKey(cell) === currentId);
807
- count.textContent = cells.length ? `${currentIndex < 0 ? 0 : currentIndex + 1} / ${cells.length}` : '0 / 0';
808
- previous.disabled = cells.length === 0;
809
- next.disabled = cells.length === 0;
810
- previous.title = cells.length ? '定位到当前 Sheet 的上一个可处理冲突(循环)' : '当前 Sheet 没有可定位的冲突';
811
- next.title = cells.length ? '定位到当前 Sheet 的下一个可处理冲突(循环)' : '当前 Sheet 没有可定位的冲突';
812
- }
813
- function markConflictCell(cellElement) {
814
- if (!cellElement) return;
815
- cellElement.classList.remove('conflict-cell-flash');
816
- // Force a reflow so repeated navigation to the same cell restarts the flash.
817
- void cellElement.offsetWidth;
818
- cellElement.classList.add('conflict-cell-flash');
819
- window.setTimeout(() => cellElement.classList.remove('conflict-cell-flash'), 1300);
820
- }
821
- function locateConflictCell(direction) {
822
- const file = currentFileData(); const sheet = currentSheetData();
823
- const cells = navigableConflictCells(file, sheet);
824
- if (!cells.length) return;
825
- const state = sheetDecision(file, sheet);
826
- const currentId = state.lastNavigatedConflict;
827
- let index = cells.findIndex(cell => conflictCellKey(cell) === currentId);
828
- index = index < 0 ? (direction > 0 ? 0 : cells.length - 1) : (index + direction + cells.length) % cells.length;
829
- const target = cells[index];
830
- state.lastNavigatedConflict = conflictCellKey(target);
831
- const rowNumber = target.row;
832
- if (!(sheet.columns && sheet.allRows) && rowNumber !== undefined) {
833
- const rows = [...new Set((sheet.cells || []).map(cell => cell.row))].sort((a, b) => a - b);
834
- currentPage = Math.floor(Math.max(0, rows.indexOf(rowNumber)) / PAGE_SIZE);
835
- } else if (sheet.columns && sheet.allRows) {
836
- const rows = sheet.rows || [];
837
- const rowIndex = rows.findIndex(row => row.id === target.rowId);
838
- currentPage = Math.floor(Math.max(0, rowIndex) / PAGE_SIZE);
839
- }
840
- renderTable();
841
- renderConflictNavigation();
842
- requestAnimationFrame(() => {
843
- const element = [...byId('tableContainer').querySelectorAll('[data-conflict-id]')]
844
- .find(item => item.dataset.conflictId === conflictCellKey(target));
845
- if (!element) return;
846
- const container = byId('tableContainer');
847
- container.scrollTop = Math.max(0, element.offsetTop - container.clientHeight / 2);
848
- container.scrollLeft = Math.max(0, element.offsetLeft - container.clientWidth / 2);
849
- element.scrollIntoView({block: 'nearest', inline: 'nearest'});
850
- markConflictCell(element);
851
- });
852
- }
853
- function renderMessage(host, title, message) {
854
- const box = document.createElement('div');
855
- box.className = 'sheet-message';
856
- const heading = document.createElement('h2'); heading.textContent = title;
857
- const body = document.createElement('p'); body.textContent = message;
858
- box.append(heading, body); host.append(box);
859
- }
860
- function renderSheetConflict(host, file, sheet) {
861
- const box = document.createElement('div');
862
- box.className = 'sheet-message';
863
- const heading = document.createElement('h2'); heading.textContent = `${sheet.name}:删除 / 修改冲突`;
864
- const body = document.createElement('p');
865
- body.textContent = '一侧删除了整个 Sheet,另一侧继续修改。选择某一侧后,将按该侧实际状态保留或删除 Sheet。';
866
- const target = sheetDecision(file, sheet);
867
- const names = sheet.names || {};
868
- const local = document.createElement('button');
869
- const localLabel = names.ours ? '保留 Sheet(本地)' : '删除 Sheet(本地)';
870
- local.className = 'btn btn-local'; local.textContent = target.sheet === 'ours' ? `✓ ${localLabel}` : localLabel;
871
- local.onclick = () => chooseSheet(file, sheet, 'ours');
872
- const remote = document.createElement('button');
873
- const remoteLabel = names.theirs ? '保留 Sheet(远端)' : '删除 Sheet(远端)';
874
- remote.className = 'btn btn-remote'; remote.textContent = target.sheet === 'theirs' ? `✓ ${remoteLabel}` : remoteLabel;
875
- remote.onclick = () => chooseSheet(file, sheet, 'theirs');
876
- box.append(heading, body, local, document.createTextNode(' '), remote); host.append(box);
877
- }
878
- function axisChoice(file, sheet, kind, axis) {
879
- if (axis.state !== 'delete_modify') return axis.default;
880
- return sheetDecision(file, sheet)[kind][axis.id] || axis.default;
881
- }
882
- function axisKept(file, sheet, kind, axis) { return axisChoice(file, sheet, kind, axis) !== 'delete'; }
883
- function axisClass(file, sheet, kind, axis) {
884
- if (axis.state === 'deleted' || !axisKept(file, sheet, kind, axis)) return ' axis-tombstone';
885
- if (axis.state === 'added' && axis.incoming) return ' axis-added-incoming';
886
- return '';
887
- }
888
- function axisSource(axis, kind) {
889
- if (kind === 'rows') return `Base ${axis.baseRow ?? '—'} · 本地 ${axis.oursRow ?? '—'} · 远端 ${axis.theirsRow ?? '—'}`;
890
- return `Base ${axis.baseColumn ?? '—'} · 本地 ${axis.oursColumn ?? '—'} · 远端 ${axis.theirsColumn ?? '—'}`;
891
- }
892
- function structuralOutputMaps(file, sheet) {
893
- const headerCount = (sheet.headerRows || []).length;
894
- const rows = new Map(); const columns = new Map(); let rowIndex = headerCount; let columnIndex = 0;
895
- for (const row of sheet.allRows || []) if (axisKept(file, sheet, 'rows', row)) rows.set(row.id, ++rowIndex);
896
- for (const column of sheet.columns || []) if (axisKept(file, sheet, 'columns', column)) columns.set(column.id, colLabel(++columnIndex));
897
- return {rows, columns};
898
- }
899
- function structuralCellSelectable(file, sheet, cell) {
900
- if (!cell || !cell.changed || cell.selectable === false) return false;
901
- const row = (sheet.allRows || []).find(item => item.id === cell.rowId);
902
- const column = (sheet.columns || []).find(item => item.id === cell.colId);
903
- return !!row && !!column && !row.locked && !column.locked
904
- && axisKept(file, sheet, 'rows', row) && axisKept(file, sheet, 'columns', column);
905
- }
906
- function defaultDisplayChoice(cell) {
907
- const choice = defaultCellChoice(cell);
908
- if (choice === 'same') return cell.oursPresent ? 'ours' : 'theirs';
909
- return choice;
910
- }
911
- function structuralCellValue(target, cell) {
912
- const choice = target.cells[cell.id] || defaultDisplayChoice(cell);
913
- if (choice === 'ours' && cell.oursPresent) return cell.ours;
914
- if (choice === 'theirs' && cell.theirsPresent) return cell.theirs;
915
- if (cell.oursPresent) return cell.ours;
916
- if (cell.theirsPresent) return cell.theirs;
917
- return cell.base;
918
- }
919
- function appendStructureControl(row, file, sheet, kind, axis) {
920
- const cell = document.createElement('th'); cell.className = 'structure-control' + axisClass(file, sheet, kind, axis);
921
- cell.title = axisSource(axis, kind);
922
- if (axis.state === 'delete_modify') {
923
- const button = document.createElement('button'); const kept = axisKept(file, sheet, kind, axis);
924
- button.className = 'structure-toggle' + (kept ? '' : ' is-delete');
925
- button.textContent = kept ? '已保留' : '已删除'; button.onclick = event => { event.stopPropagation(); toggleAxis(file, sheet, kind, axis); };
926
- cell.append(button);
927
- } else if (axis.state === 'added') {
928
- const badge = document.createElement('span'); badge.className = `structure-badge ${axis.incoming ? 'added' : 'local-existing'}`;
929
- const source = kind === 'rows' ? (axis.theirsRow ?? axis.oursRow) : (axis.theirsColumn ?? axis.oursColumn);
930
- badge.textContent = `${axis.incoming ? '远端新增' : '本地已有'} · ${source}`; cell.append(badge);
931
- } else if (axis.state === 'deleted') {
932
- const badge = document.createElement('span'); badge.className = 'structure-badge deleted';
933
- badge.textContent = `已删除 · ${kind === 'rows' ? axis.baseRow : axis.baseColumn}`; cell.append(badge);
934
- }
935
- row.append(cell); return cell;
936
- }
937
- function renderDiffTable(host, file, sheet) {
938
- if (sheet.columns && sheet.allRows) return renderStructuralTable(host, file, sheet);
939
- return renderLegacyDiffTable(host, file, sheet);
940
- }
941
- function buildHeaderMergeMap(sheet, selection) {
942
- // 构建 header cell 的合并映射:key = "rowIndex|colIndex" -> {isOrigin, colspan}
943
- const mc = sheet.mergedCells || {};
944
- let regions;
945
- if (mc.status === 'conflict') {
946
- const choice = (selection && selection.mergedCells) || mc.default || 'ours';
947
- regions = mc[choice] || mc.ours || [];
948
- } else {
949
- regions = mc.resolved || [];
950
- }
951
- const columns = sheet.columns || [];
952
- const headerRows = sheet.headerRows || [];
953
- const headerRowNumbers = headerRows.map(r => r.row);
954
- const map = {};
955
- for (const region of regions) {
956
- const rowIdxs = (region.rows || []).map(r => r.startsWith('header:') ? parseInt(r.slice(7)) : null).filter(r => r !== null);
957
- const colIds = region.columns || [];
958
- if (rowIdxs.length < 1 || colIds.length < 2) continue;
959
- const colIdxs = colIds.map(cid => columns.findIndex(c => c.id === cid)).filter(i => i >= 0);
960
- if (colIdxs.length < 2) continue;
961
- const minCol = Math.min(...colIdxs);
962
- const maxCol = Math.max(...colIdxs);
963
- for (const rowNum of rowIdxs) {
964
- const hrIdx = headerRowNumbers.indexOf(rowNum);
965
- if (hrIdx < 0) continue;
966
- map[`${rowNum}|${minCol}`] = {isOrigin: true, colspan: maxCol - minCol + 1};
967
- for (let c = minCol + 1; c <= maxCol; c++) {
968
- map[`${rowNum}|${c}`] = {isOrigin: false};
969
- }
970
- }
971
- }
972
- return map;
973
- }
974
- function renderStructuralTable(host, file, sheet) {
975
- const columns = sheet.columns || []; const rows = sheet.rows || [];
976
- if (!rows.length && !columns.length) { renderMessage(host, sheet.name, '该 Sheet 没有表内数据差异。'); return; }
977
- const target = sheetDecision(file, sheet);
978
- const output = structuralOutputMaps(file, sheet);
979
- const grid = new Map((sheet.gridCells || []).map(cell => [`${cell.rowId}|${cell.colId}`, cell]));
980
- const table = document.createElement('table'); table.className = 'diff-table';
981
- const head = document.createElement('thead');
982
- const controls = document.createElement('tr'); appendHeaderCell(controls, '结构', null, 'structure-control');
983
- for (const column of columns) appendStructureControl(controls, file, sheet, 'columns', column);
984
- head.append(controls);
985
- const letters = document.createElement('tr'); appendHeaderCell(letters, '行');
986
- for (const column of columns) {
987
- const header = appendHeaderCell(letters, output.columns.get(column.id) || '×', () => bulkStructuralColumn(column.id));
988
- header.className += axisClass(file, sheet, 'columns', column);
989
- header.title = `${axisSource(column, 'columns')} · 合并后 ${output.columns.get(column.id) || '删除'}`;
990
- }
991
- head.append(letters);
992
- const headerMergeMap = buildHeaderMergeMap(sheet, sheetDecision(file, sheet));
993
- for (const headerRow of sheet.headerRows || []) {
994
- const row = document.createElement('tr'); row.className = 'luban-header'; appendHeaderCell(row, String(headerRow.row));
995
- headerRow.cells.forEach((value, index) => {
996
- const merge = headerMergeMap[`${headerRow.row}|${index}`];
997
- if (merge && !merge.isOrigin) return; // 被合并的格子跳过
998
- const cell = appendHeaderCell(row, fmtVal(value));
999
- cell.className += axisClass(file, sheet, 'columns', columns[index]);
1000
- if (merge && merge.colspan > 1) cell.colSpan = merge.colspan;
1001
- });
1002
- head.append(row);
1003
- }
1004
- table.append(head);
1005
- const body = document.createElement('tbody');
1006
- const start = currentPage * PAGE_SIZE; const end = Math.min(rows.length, start + PAGE_SIZE);
1007
- for (let index = start; index < end; index++) {
1008
- const axisRow = rows[index]; const row = document.createElement('tr');
1009
- const rowHeader = document.createElement('th'); rowHeader.className = 'row-header' + axisClass(file, sheet, 'rows', axisRow);
1010
- rowHeader.title = `${axisSource(axisRow, 'rows')} · 合并后 ${output.rows.get(axisRow.id) || '删除'}`;
1011
- const outputIndex = document.createElement('span'); outputIndex.className = 'axis-index' + (output.rows.has(axisRow.id) ? '' : ' is-removed');
1012
- outputIndex.textContent = output.rows.has(axisRow.id) ? String(output.rows.get(axisRow.id)) : '×'; rowHeader.append(outputIndex);
1013
- if (axisRow.state === 'delete_modify') {
1014
- const button = document.createElement('button'); const kept = axisKept(file, sheet, 'rows', axisRow);
1015
- button.className = 'structure-toggle' + (kept ? '' : ' is-delete'); button.textContent = kept ? '已保留' : '已删除';
1016
- button.onclick = event => { event.stopPropagation(); toggleAxis(file, sheet, 'rows', axisRow); }; rowHeader.append(button);
1017
- } else {
1018
- if (axisRow.state === 'added' || axisRow.state === 'deleted') {
1019
- const badge = document.createElement('span'); badge.className = `structure-badge ${axisRow.state}`;
1020
- badge.textContent = axisRow.state === 'added' ? (axisRow.incoming ? '远端新增' : '本地已有') : '已删除'; rowHeader.append(document.createTextNode(' '), badge);
1021
- }
1022
- }
1023
- const source = document.createElement('span'); source.className = 'axis-source'; source.textContent = axisSource(axisRow, 'rows'); rowHeader.append(source);
1024
- if (axisKept(file, sheet, 'rows', axisRow) && !axisRow.locked) rowHeader.onclick = () => bulkStructuralRow(axisRow.id);
1025
- row.append(rowHeader);
1026
- for (const column of columns) {
1027
- const cellData = grid.get(`${axisRow.id}|${column.id}`); const cell = document.createElement('td');
1028
- const kept = axisKept(file, sheet, 'rows', axisRow) && axisKept(file, sheet, 'columns', column);
1029
- const structurallyLocked = !kept || axisRow.locked || column.locked;
1030
- if (!kept || axisRow.state === 'deleted' || column.state === 'deleted') cell.className = 'axis-tombstone cell-locked';
1031
- else if ((axisRow.state === 'added' && axisRow.incoming) || (column.state === 'added' && column.incoming)) cell.className = 'axis-added-incoming cell-locked';
1032
- else if (axisRow.state === 'added' || column.state === 'added') cell.className = 'cell-no-conflict cell-locked';
1033
- else if (cellData && cellData.changed) {
1034
- const choice = target.cells[cellData.id] || defaultDisplayChoice(cellData);
1035
- const selectable = structuralCellSelectable(file, sheet, cellData);
1036
- cell.className = (choice === 'ours' ? 'cell-local' : 'cell-remote') + (selectable ? ' cell-selectable' : ' cell-locked');
1037
- } else cell.className = 'cell-no-conflict' + (structurallyLocked ? ' cell-locked' : '');
1038
- if (cellData) {
1039
- cell.textContent = fmtVal(structuralCellValue(target, cellData));
1040
- if (structuralCellSelectable(file, sheet, cellData)) {
1041
- cell.onclick = () => cycleCell(file, sheet, cellData);
1042
- if (cellData.resolution === 'conflict') cell.dataset.conflictId = cellData.id;
1043
- }
1044
- bindStructuralTip(cell, file, sheet, axisRow, column, cellData);
1045
- }
1046
- row.append(cell);
1047
- }
1048
- body.append(row);
1049
- }
1050
- table.append(body); host.append(table); if (rows.length > PAGE_SIZE) renderPager(host, rows.length);
1051
- }
1052
- function renderLegacyDiffTable(host, file, sheet) {
1053
- const changedCells = sheet.cells || [];
1054
- if (!changedCells.length) {
1055
- renderMessage(host, sheet.name, '该 Sheet 没有单元格差异。');
1056
- return;
1057
- }
1058
- const maxCol = sheet.maxCol || 1;
1059
- const changedMap = new Map(changedCells.map(cell => [`${cell.row},${cell.col}`, cell]));
1060
- const changedRows = [...new Set(changedCells.map(cell => cell.row))].sort((a, b) => a - b);
1061
- const a1 = gridVal(sheet, 1, 1);
1062
- const isVertical = typeof a1 === 'string' && a1.startsWith('##column##var');
1063
- const headerRows = [];
1064
- if (!isVertical) {
1065
- for (let row = 1; row <= 20; row++) {
1066
- const value = gridVal(sheet, row, 1);
1067
- if (typeof value === 'string' && value.startsWith('##')) headerRows.push(row);
1068
- else if (headerRows.length) break;
1069
- }
1070
- }
1071
-
1072
- const table = document.createElement('table'); table.className = 'diff-table';
1073
- const head = document.createElement('thead');
1074
- const letters = document.createElement('tr'); appendHeaderCell(letters, '行');
1075
- for (let col = 1; col <= maxCol; col++) appendHeaderCell(letters, colLabel(col), () => bulkCol(col));
1076
- head.append(letters);
1077
- if (isVertical) {
1078
- const meanings = {1: '字段名 ##var', 2: '类型 ##type', 3: '注释'};
1079
- const row = document.createElement('tr'); row.className = 'luban-header'; appendHeaderCell(row, '列义');
1080
- for (let col = 1; col <= maxCol; col++) appendHeaderCell(row, meanings[col] || '');
1081
- head.append(row);
1082
- } else {
1083
- for (const headerRow of headerRows) {
1084
- const row = document.createElement('tr'); row.className = 'luban-header'; appendHeaderCell(row, String(headerRow));
1085
- for (let col = 1; col <= maxCol; col++) {
1086
- const cell = appendHeaderCell(row, fmtVal(gridVal(sheet, headerRow, col)));
1087
- bindTip(cell, headerRow, col);
1088
- }
1089
- head.append(row);
1090
- }
1091
- }
1092
- table.append(head);
1093
-
1094
- const body = document.createElement('tbody');
1095
- const start = currentPage * PAGE_SIZE;
1096
- const end = Math.min(changedRows.length, start + PAGE_SIZE);
1097
- const target = sheetDecision(file, sheet);
1098
- for (let index = start; index < end; index++) {
1099
- const rowNumber = changedRows[index];
1100
- const row = document.createElement('tr');
1101
- appendHeaderCell(row, String(rowNumber), () => bulkRow(rowNumber), 'row-header');
1102
- for (let col = 1; col <= maxCol; col++) {
1103
- const key = `${rowNumber},${col}`;
1104
- const changed = changedMap.get(key);
1105
- const cell = document.createElement('td');
1106
- if (changed) {
1107
- const choice = target.cells[changed.coordinate] || defaultCellChoice(changed);
1108
- cell.className = choice === 'ours' ? 'cell-local' : 'cell-remote';
1109
- cell.textContent = fmtVal(choice === 'ours' ? changed.ours : changed.theirs);
1110
- cell.onclick = () => cycleCell(file, sheet, changed);
1111
- if (changed.resolution === 'conflict') cell.dataset.conflictId = changed.id || changed.coordinate;
1112
- } else {
1113
- cell.className = 'cell-no-conflict';
1114
- cell.textContent = fmtVal(gridVal(sheet, rowNumber, col));
1115
- }
1116
- bindTip(cell, rowNumber, col);
1117
- row.append(cell);
1118
- }
1119
- body.append(row);
1120
- }
1121
- table.append(body); host.append(table);
1122
- if (changedRows.length > PAGE_SIZE) renderPager(host, changedRows.length);
1123
- }
1124
- function renderPager(host, totalRows) {
1125
- const pages = Math.ceil(totalRows / PAGE_SIZE);
1126
- const pager = document.createElement('div'); pager.className = 'pager';
1127
- for (let page = 0; page < pages; page++) {
1128
- const button = document.createElement('button');
1129
- button.className = 'btn' + (page === currentPage ? ' btn-local' : '');
1130
- button.textContent = String(page + 1);
1131
- button.onclick = () => { currentPage = page; renderTable(); };
1132
- pager.append(button);
1133
- }
1134
- host.append(pager);
1135
- }
1136
-
1137
- function changedCellAt(sheet, row, col) { return (sheet.cells || []).find(cell => cell.row === row && cell.col === col); }
1138
- function bindStructuralTip(cell, file, sheet, row, column, data) {
1139
- cell.onmouseenter = event => showStructuralTip(event, file, sheet, row, column, data);
1140
- cell.onmousemove = event => positionTip(event); cell.onmouseleave = hideTip;
1141
- }
1142
- function addTipLine(host, className, label, value, author, date) {
1143
- const line = document.createElement('div'); line.className = className;
1144
- line.append(document.createTextNode(`${label}: ${fmtVal(value)}`));
1145
- const metaParts = [];
1146
- if (author) metaParts.push(`作者: ${author}`);
1147
- if (date) metaParts.push(date);
1148
- if (metaParts.length) { const span = document.createElement('span'); span.className = 'author'; span.textContent = metaParts.join(' '); line.append(span); }
1149
- host.append(line);
1150
- }
1151
- function showTip(event, row, col) {
1152
- const file = currentFileData(); const sheet = currentSheetData(); const changed = changedCellAt(sheet, row, col);
1153
- const tip = byId('tooltip'); tip.replaceChildren();
1154
- const label = document.createElement('div'); label.className = 'label';
1155
- label.textContent = `${file.path} / ${sheet.name} / 第 ${row} 行, ${colLabel(col)} 列`;
1156
- tip.append(label);
1157
- if (changed) {
1158
- const choice = sheetDecision(file, sheet).cells[changed.coordinate] || defaultCellChoice(changed);
1159
- addTipLine(tip, 'val-base', '基线', changed.base, '');
1160
- addTipLine(tip, 'val-local', '本地', changed.ours, file.oursAuthor, file.oursDate);
1161
- addTipLine(tip, 'val-remote', '远端', changed.theirs, file.theirsAuthor, file.theirsDate);
1162
- addTipLine(tip, 'val-current', '当前选择', choice === 'ours' ? '本地' : '远端', '');
1163
- if (changed.resolution !== 'conflict') addTipLine(tip, 'label', '三方建议', changed.resolution === 'ours' ? '自动采用本地' : '自动采用远端', '');
1164
- } else {
1165
- addTipLine(tip, 'val-current', '值', gridVal(sheet, row, col), '');
1166
- }
1167
- tip.style.display = 'block'; positionTip(event);
1168
- }
1169
- function showStructuralTip(event, file, sheet, row, column, cell) {
1170
- const tip = byId('tooltip'); tip.replaceChildren();
1171
- const label = document.createElement('div'); label.className = 'label';
1172
- label.textContent = `${file.path} / ${sheet.name} / ${row.id} / ${column.id}`; tip.append(label);
1173
- addTipLine(tip, 'val-base', '基线', cell.base, '');
1174
- addTipLine(tip, 'val-local', '本地', cell.ours, file.oursAuthor, file.oursDate);
1175
- addTipLine(tip, 'val-remote', '远端', cell.theirs, file.theirsAuthor, file.theirsDate);
1176
- const choice = sheetDecision(file, sheet).cells[cell.id] || defaultDisplayChoice(cell);
1177
- addTipLine(tip, 'val-current', '当前选择', choice === 'ours' ? '本地' : '远端', '');
1178
- addTipLine(tip, 'label', '行来源', axisSource(row, 'rows'), '');
1179
- addTipLine(tip, 'label', '列来源', axisSource(column, 'columns'), '');
1180
- if (row.state !== 'common') addTipLine(tip, 'label', '行状态', row.status, '');
1181
- if (column.state !== 'common') addTipLine(tip, 'label', '列状态', column.status, '');
1182
- addTipLine(tip, 'label', '交互', structuralCellSelectable(file, sheet, cell) ? '可点击切换版本' : '结构已锁定', '');
1183
- tip.style.display = 'block'; positionTip(event);
1184
- }
1185
- function positionTip(event) {
1186
- const tip = byId('tooltip');
1187
- const left = Math.min(event.clientX + 12, window.innerWidth - tip.offsetWidth - 12);
1188
- const top = Math.min(event.clientY + 12, window.innerHeight - tip.offsetHeight - 12);
1189
- tip.style.left = `${Math.max(8, left)}px`; tip.style.top = `${Math.max(8, top)}px`;
1190
- }
1191
- function hideTip() { byId('tooltip').style.display = 'none'; }
1192
-
1193
- function pushCellUndo(file, sheet, cell) {
1194
- const target = sheetDecision(file, sheet); const key = cellKey(cell); const previous = target.cells[key];
1195
- undoStack.push({kind: 'cell', fileId: file.id, sheet: sheet.name, key, previous});
1196
- }
1197
- function cycleCell(file, sheet, cell) {
1198
- const target = sheetDecision(file, sheet); pushCellUndo(file, sheet, cell);
1199
- const key = cellKey(cell);
1200
- target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1201
- renderTable(); renderConflictNavigation(); updateStats();
1202
- }
1203
- function chooseSheet(file, sheet, side) {
1204
- const target = sheetDecision(file, sheet);
1205
- undoStack.push({kind: 'sheet', fileId: file.id, sheet: sheet.name, previous: target.sheet});
1206
- target.sheet = side; renderTable(); updateStats();
1207
- }
1208
- function chooseRename(file, sheet, side) {
1209
- const target = sheetDecision(file, sheet);
1210
- undoStack.push({kind: 'rename', fileId: file.id, sheet: sheet.name, previous: target.rename});
1211
- target.rename = side; renderSheetChoice(); updateStats();
1212
- }
1213
- function chooseMergedCells(file, sheet, side) {
1214
- const target = sheetDecision(file, sheet);
1215
- undoStack.push({kind: 'mergedCells', fileId: file.id, sheet: sheet.name, previous: target.mergedCells});
1216
- target.mergedCells = side; renderSheetChoice(); renderTable(); updateStats();
1217
- }
1218
- function toggleAxis(file, sheet, kind, axis) {
1219
- const target = sheetDecision(file, sheet); const previous = target[kind][axis.id] || axis.default;
1220
- undoStack.push({kind: 'axis', fileId: file.id, sheet: sheet.name, axisKind: kind, axisId: axis.id, previous});
1221
- target[kind][axis.id] = previous === 'keep' ? 'delete' : 'keep';
1222
- renderSheetTabs(); renderTable(); renderConflictNavigation(); updateStats();
1223
- }
1224
- function chooseMacro(file, side) {
1225
- const target = fileDecision(file); const previous = target.macro || file.macro.default;
1226
- if (side === 'theirs' && !file.macro.canChooseTheirs) return;
1227
- undoStack.push({kind: 'macro', fileId: file.id, previous});
1228
- target.macro = side; render();
1229
- }
1230
- function sideAxisChoice(axis, side) {
1231
- const key = side === 'ours' ? ['oursRow', 'oursCol'] : ['theirsRow', 'theirsCol'];
1232
- return key.some(name => axis[name] !== undefined && axis[name] !== null) ? 'keep' : 'delete';
1233
- }
1234
- function bulkChoose(side) {
1235
- const file = currentFileData(); const sheet = currentSheetData(); if (!sheet) return;
1236
- if (currentSheetIsConfirmed()) return;
1237
- if (sheet.action.startsWith('conflict_')) { chooseSheet(file, sheet, side); return; }
1238
- const target = sheetDecision(file, sheet);
1239
- if (sheet.renameStatus === 'rename_conflict') {
1240
- undoStack.push({kind: 'rename', fileId: file.id, sheet: sheet.name, previous: target.rename});
1241
- target.rename = side;
1242
- }
1243
- if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') {
1244
- undoStack.push({kind: 'mergedCells', fileId: file.id, sheet: sheet.name, previous: target.mergedCells});
1245
- target.mergedCells = side;
1246
- }
1247
- for (const [kind, axes] of [['rows', sheet.allRows || sheet.rows || []], ['columns', sheet.columns || []]]) {
1248
- for (const axis of axes) if (axis.state === 'delete_modify') {
1249
- undoStack.push({kind: 'axis', fileId: file.id, sheet: sheet.name, axisKind: kind, axisId: axis.id, previous: target[kind][axis.id]});
1250
- target[kind][axis.id] = sideAxisChoice(axis, side);
1251
- }
1252
- }
1253
- for (const cell of sheet.cells || []) {
1254
- const key = cellKey(cell); const selected = side === 'ours' ? cell.oursPresent !== false : cell.theirsPresent !== false;
1255
- if (!selected) continue;
1256
- pushCellUndo(file, sheet, cell); target.cells[key] = side;
1257
- }
1258
- render();
1259
- }
1260
- function bulkStructuralRow(rowId) {
1261
- const file = currentFileData(); const sheet = currentSheetData(); const target = sheetDecision(file, sheet);
1262
- const row = (sheet.allRows || []).find(item => item.id === rowId);
1263
- if (!row || !axisKept(file, sheet, 'rows', row)) return;
1264
- for (const cell of (sheet.cells || []).filter(item => item.rowId === rowId)) {
1265
- const column = (sheet.columns || []).find(item => item.id === cell.colId);
1266
- if (!column || !axisKept(file, sheet, 'columns', column) || row.locked || column.locked) continue;
1267
- pushCellUndo(file, sheet, cell); const key = cellKey(cell);
1268
- target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1269
- }
1270
- renderTable(); renderConflictNavigation(); updateStats();
1271
- }
1272
- function bulkStructuralColumn(columnId) {
1273
- const file = currentFileData(); const sheet = currentSheetData(); const target = sheetDecision(file, sheet);
1274
- const column = (sheet.columns || []).find(item => item.id === columnId);
1275
- if (!column || !axisKept(file, sheet, 'columns', column)) return;
1276
- for (const cell of (sheet.cells || []).filter(item => item.colId === columnId)) {
1277
- const row = (sheet.allRows || []).find(item => item.id === cell.rowId);
1278
- if (!row || !axisKept(file, sheet, 'rows', row) || row.locked || column.locked) continue;
1279
- pushCellUndo(file, sheet, cell); const key = cellKey(cell);
1280
- target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1281
- }
1282
- renderTable(); renderConflictNavigation(); updateStats();
1283
- }
1284
- function bulkRow(row) {
1285
- const file = currentFileData(); const sheet = currentSheetData();
1286
- for (const cell of (sheet.cells || []).filter(item => item.row === row)) {
1287
- const target = sheetDecision(file, sheet); pushCellUndo(file, sheet, cell);
1288
- const key = cellKey(cell); target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1289
- }
1290
- renderTable(); renderConflictNavigation(); updateStats();
1291
- }
1292
- function bulkCol(col) {
1293
- const file = currentFileData(); const sheet = currentSheetData();
1294
- for (const cell of (sheet.cells || []).filter(item => item.col === col)) {
1295
- const target = sheetDecision(file, sheet); pushCellUndo(file, sheet, cell);
1296
- const key = cellKey(cell); target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1297
- }
1298
- renderTable(); renderConflictNavigation(); updateStats();
1299
- }
1300
- function undo() {
1301
- const file = currentFileData(); const sheet = currentSheetData();
1302
- if (!file || !sheet) return;
1303
- let actionIndex = -1;
1304
- for (let index = undoStack.length - 1; index >= 0; index--) {
1305
- const candidate = undoStack[index];
1306
- if (candidate.fileId === file.id && candidate.sheet === sheet.name) {
1307
- actionIndex = index;
1308
- break;
1309
- }
1310
- }
1311
- if (actionIndex < 0) return;
1312
- const [action] = undoStack.splice(actionIndex, 1);
1313
- const actionFile = model.files.find(item => item.id === action.fileId);
1314
- const actionSheet = actionFile.diff.sheets.find(item => item.name === action.sheet);
1315
- const target = sheetDecision(actionFile, actionSheet);
1316
- target.confirmed = false;
1317
- if (action.kind === 'sheet') target.sheet = action.previous;
1318
- else if (action.kind === 'rename') target.rename = action.previous;
1319
- else if (action.kind === 'mergedCells') target.mergedCells = action.previous;
1320
- else if (action.kind === 'axis') target[action.axisKind][action.axisId] = action.previous;
1321
- else target.cells[action.key] = action.previous;
1322
- render();
1323
- }
1324
- function resetSheet() {
1325
- const file = currentFileData();
1326
- const sheet = currentSheetData();
1327
- if (!file || !sheet) return;
1328
- const target = sheetDecision(file, sheet); const sheetChoice = defaultSheetChoice(sheet);
1329
- target.confirmed = false;
1330
- delete target.lastNavigatedConflict;
1331
- if (sheetChoice) target.sheet = sheetChoice; else delete target.sheet;
1332
- if (sheet.renameStatus === 'rename_conflict') target.rename = 'ours'; else delete target.rename;
1333
- if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') target.mergedCells = sheet.mergedCells.default || 'ours'; else delete target.mergedCells;
1334
- target.rows = {}; target.columns = {}; target.cells = {};
1335
- for (const row of sheet.allRows || sheet.rows || []) if (row.state === 'delete_modify') target.rows[row.id] = row.default;
1336
- for (const column of sheet.columns || []) if (column.state === 'delete_modify') target.columns[column.id] = column.default;
1337
- for (const cell of sheet.cells || []) target.cells[cellKey(cell)] = defaultCellChoice(cell);
1338
- // A reset supersedes prior choices for this Sheet, while preserving other files,
1339
- // other Sheets, and file-level macro choices in the shared undo history.
1340
- undoStack = undoStack.filter(action => action.fileId !== file.id || action.sheet !== sheet.name);
1341
- render();
1342
- }
1343
- function updateGlobalConfirmationStats() {
1344
- let confirmed = 0; let total = 0;
1345
- for (const file of model.files) for (const sheet of file.diff.sheets) {
1346
- if (!sheetNeedsConfirmation(sheet)) continue;
1347
- total++;
1348
- if (sheetDecision(file, sheet).confirmed) confirmed++;
1349
- }
1350
- byId('confirmedSheetCount').textContent = String(confirmed);
1351
- byId('totalConflictSheetCount').textContent = String(total);
1352
- }
1353
- function updateDecisionFooterStats() {
1354
- let total = 0; let local = 0;
1355
- for (const file of model.files) {
1356
- if (file.macro && file.macro.status === 'conflict') {
1357
- total++; if ((fileDecision(file).macro || file.macro.default) === 'ours') local++;
1358
- }
1359
- for (const sheet of file.diff.sheets) {
1360
- const target = sheetDecision(file, sheet);
1361
- if (sheet.action.startsWith('conflict_')) { total++; if (target.sheet === 'ours') local++; }
1362
- if (sheet.renameStatus === 'rename_conflict') { total++; if (target.rename === 'ours') local++; }
1363
- if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') { total++; if (target.mergedCells === 'ours') local++; }
1364
- for (const row of sheet.allRows || sheet.rows || []) if (row.state === 'delete_modify') {
1365
- total++; const oursChoice = row.oursRow !== null && row.oursRow !== undefined ? 'keep' : 'delete';
1366
- if ((target.rows[row.id] || row.default) === oursChoice) local++;
1367
- }
1368
- for (const column of sheet.columns || []) if (column.state === 'delete_modify') {
1369
- total++; const oursChoice = column.oursCol !== null && column.oursCol !== undefined ? 'keep' : 'delete';
1370
- if ((target.columns[column.id] || column.default) === oursChoice) local++;
1371
- }
1372
- for (const cell of sheet.cells || []) {
1373
- if (sheet.columns && sheet.allRows && !structuralCellSelectable(file, sheet, cell)) continue;
1374
- total++; if (target.cells[cellKey(cell)] === 'ours') local++;
1375
- }
1376
- }
1377
- }
1378
- const file = currentFileData(); const sheet = currentSheetData();
1379
- byId('footerStatus').textContent = `共 ${total} 个可决策项,${local} 项采用本地 · 当前:${file.path}${sheet ? ` / ${sheet.name}` : ''}${autoTheirsNote(' · ')}`;
1380
- }
1381
- function updateStats() {
1382
- updateGlobalConfirmationStats();
1383
- updateDecisionFooterStats();
1384
- }
1385
-
1386
- function renderCommitSteps(steps) {
1387
- const host = byId('commitModalSteps'); host.replaceChildren();
1388
- for (const step of steps || []) {
1389
- const line = document.createElement('div');
1390
- line.textContent = `${step.success ? '✓' : '✗'} ${step.step || ''}${step.output ? ` — ${String(step.output).slice(0, 240)}` : ''}`;
1391
- host.append(line);
1392
- }
1393
- }
1394
- function showCommitModal(kind, title, body, steps, buttons) {
1395
- const modal = byId('commitModal'); modal.classList.remove('is-hidden', 'is-success', 'is-error');
1396
- if (kind) modal.classList.add(`is-${kind}`);
1397
- byId('commitModalTitle').textContent = title; byId('commitModalBody').textContent = body || ''; renderCommitSteps(steps);
1398
- const actions = byId('commitModalActions'); actions.replaceChildren();
1399
- for (const spec of buttons || []) { const button = document.createElement('button'); button.className = `btn ${spec.className || ''}`; button.textContent = spec.label; button.onclick = spec.onclick; actions.append(button); }
1400
- }
1401
- function closeCommitModal() { byId('commitModal').classList.add('is-hidden'); }
1402
- async function cancelAndExit() {
1403
- const cancelButton = byId('cancelBtn');
1404
- const submitButton = byId('submitBtn');
1405
- cancelButton.disabled = true;
1406
- submitButton.disabled = true;
1407
- byId('footerStatus').textContent = '正在取消并退出,不会写入任何更改...';
1408
- try {
1409
- const response = await fetch('/api/shutdown', {method: 'POST'});
1410
- const result = await response.json();
1411
- if (!response.ok || !result.success) throw new Error(result.error || '无法关闭解析器');
1412
- byId('footerStatus').textContent = '已取消,未写入任何更改。';
1413
- window.close();
1414
- } catch (error) {
1415
- cancelButton.disabled = false;
1416
- submitButton.disabled = false;
1417
- byId('footerStatus').textContent = `退出失败: ${error.message}`;
1418
- }
1419
- }
1420
- function renderCommitMessage(text) {
1421
- const host = byId('commitModalSteps'); host.replaceChildren();
1422
- const line = document.createElement('div');
1423
- line.textContent = text || '(无冲突 Cell,将使用默认提交信息)';
1424
- host.append(line);
1425
- }
1426
- function unconfirmedSheets() {
1427
- const pending = [];
1428
- for (const [fileIndex, file] of model.files.entries()) {
1429
- for (const [sheetIndex, sheet] of file.diff.sheets.entries()) {
1430
- if (!sheetNeedsConfirmation(sheet) || sheetDecision(file, sheet).confirmed) continue;
1431
- pending.push({file, sheet, fileIndex, sheetIndex, label: `${file.path}-${sheet.name}`});
1432
- }
1433
- }
1434
- return pending;
1435
- }
1436
- function renderUnconfirmedSheetList(pending) {
1437
- const host = byId('commitModalSteps'); host.replaceChildren();
1438
- for (const item of pending) {
1439
- const button = document.createElement('button');
1440
- button.type = 'button';
1441
- button.className = 'btn btn-cancel';
1442
- button.style.display = 'block';
1443
- button.style.width = '100%';
1444
- button.style.marginBottom = '7px';
1445
- button.style.textAlign = 'left';
1446
- button.textContent = item.label;
1447
- button.title = `切换到 ${item.label}`;
1448
- button.onclick = () => {
1449
- currentFile = item.fileIndex;
1450
- currentSheet = item.sheetIndex;
1451
- currentPage = 0;
1452
- closeCommitModal();
1453
- render();
1454
- };
1455
- host.append(button);
1456
- }
1457
- }
1458
- async function submitResolve() {
1459
- const button = byId('submitBtn');
1460
- const pending = unconfirmedSheets();
1461
- if (pending.length) {
1462
- byId('footerStatus').textContent = `仍有 ${pending.length} 个 Sheet 未确认,请先完成审阅。`;
1463
- showCommitModal('', '仍有未确认的 Sheet', '提交前必须人工勾选确认每个需要审阅的 Sheet。点击下方条目可直接切换过去。', [], [
1464
- {label: '返回继续审阅', className: 'btn-submit', onclick: () => { closeCommitModal(); button.disabled = false; }}
1465
- ]);
1466
- renderUnconfirmedSheetList(pending);
1467
- return;
1468
- }
1469
- button.disabled = true;
1470
- byId('footerStatus').textContent = '正在生成 commit 信息预览...';
1471
- try {
1472
- const previewResponse = await fetch('/api/commit-preview', {
1473
- method: 'POST', headers: {'Content-Type': 'application/json'},
1474
- body: JSON.stringify({decisions})
1475
- });
1476
- const preview = await previewResponse.json();
1477
- if (!previewResponse.ok || preview.error) throw new Error(preview.error || '无法生成 commit 信息');
1478
- // 居中二次确认:展示 commit 信息列表(可滑动),确认后才真正执行 add / commit
1479
- showCommitModal('', '确认提交', '请核对以下 commit 信息,确认后将执行 git add / commit:', [], [
1480
- {label: '取消', className: '', onclick: () => { closeCommitModal(); button.disabled = false; byId('footerStatus').textContent = '已取消,未提交任何更改。'; }},
1481
- {label: '确认提交', className: 'btn-submit', onclick: () => { closeCommitModal(); doSubmitResolve(); }}
1482
- ]);
1483
- renderCommitMessage(preview.message || '');
1484
- } catch (error) {
1485
- button.disabled = false;
1486
- byId('footerStatus').textContent = `错误: ${error.message}`;
1487
- }
1488
- }
1489
- async function doSubmitResolve() {
1490
- byId('footerStatus').textContent = '正在合并并提交...';
1491
- showCommitModal('', '正在合并并提交...', '正在写回工作簿并执行 git add / commit,请稍候。', [], []);
1492
- try {
1493
- const response = await fetch('/api/resolve', {
1494
- method: 'POST', headers: {'Content-Type': 'application/json'},
1495
- body: JSON.stringify({decisions, commit: true, push: false})
1496
- });
1497
- const result = await response.json();
1498
- if (!result.success) throw new Error(result.error || (result.steps || []).filter(step => !step.success).map(step => `${step.step}: ${step.output}`).join('\n') || '提交失败');
1499
- byId('footerStatus').textContent = '完成!合并并提交成功。';
1500
- showCommitModal('success', '提交成功', '多 Sheet 合并结果已写入并提交。解析器即将关闭。', result.steps, [
1501
- {label: '关闭窗口', className: 'btn-submit', onclick: () => { try { window.close(); } catch (_) {} }}
1502
- ]);
1503
- } catch (error) {
1504
- const c = byId('submitBtn'); c.disabled = false; byId('footerStatus').textContent = `错误: ${error.message}`;
1505
- showCommitModal('error', '提交失败', error.message, [], [{label: '关闭', onclick: closeCommitModal}]);
1506
- }
1507
- }
1508
-
1509
- byId('allLocalBtn').onclick = () => bulkChoose('ours');
1510
- byId('allRemoteBtn').onclick = () => bulkChoose('theirs');
1511
- byId('undoBtn').onclick = undo;
1512
- byId('resetBtn').onclick = resetSheet;
1513
- byId('cancelBtn').onclick = cancelAndExit;
1514
- byId('submitBtn').onclick = submitResolve;
1515
- byId('prevConflictBtn').onclick = () => locateConflictCell(-1);
1516
- byId('nextConflictBtn').onclick = () => locateConflictCell(1);
1517
- enableHorizontalWheel(byId('fileTabs'));
1518
- enableHorizontalWheel(byId('sheetTabs'));
1519
- init().catch(error => { byId('footerStatus').textContent = `加载失败: ${error.message}`; });
1520
- </script>
1521
- </body>
1522
- </html>
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Excel 冲突解决工具</title>
7
+ <style>
8
+ :root {
9
+ --bg-deep: #f4eed8;
10
+ --bg-root: #e6dfc8;
11
+ --bg-surface: #ffffff;
12
+ --border-default: #000000;
13
+ --shadow-edge: #625a4d;
14
+ --gold: #f2c94c;
15
+ --gold-dim: #e0b83e;
16
+ --lava: #f29aa3;
17
+ --emerald: #a2d2ff;
18
+ --sapphire: #9dccff;
19
+ --incoming: #cfe9b7;
20
+ --incoming-deep: #b6d998;
21
+ --tombstone: #77736b;
22
+ --tombstone-dark: #58554f;
23
+ --text-primary: #111111;
24
+ --text-secondary: #444444;
25
+ --text-muted: #777777;
26
+ --shadow: 4px 4px 0 #000000;
27
+ --shadow-hover: 2px 2px 0 #000000;
28
+ --border-width: 3px;
29
+ }
30
+ * { margin: 0; padding: 0; box-sizing: border-box; }
31
+ html { width: 100%; height: 100%; overflow: hidden; }
32
+ body {
33
+ font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
34
+ background: var(--bg-deep);
35
+ background-image:
36
+ linear-gradient(var(--bg-root) 1px, transparent 1px),
37
+ linear-gradient(90deg, var(--bg-root) 1px, transparent 1px);
38
+ background-size: 20px 20px;
39
+ color: var(--text-primary);
40
+ width: 100%;
41
+ height: 100vh;
42
+ min-height: 0;
43
+ overflow: hidden;
44
+ display: flex;
45
+ flex-direction: column;
46
+ }
47
+ .toolbar {
48
+ background: var(--bg-surface);
49
+ padding: 12px 20px;
50
+ display: flex;
51
+ align-items: center;
52
+ gap: 16px;
53
+ border-bottom: var(--border-width) solid var(--border-default);
54
+ flex-wrap: nowrap;
55
+ min-width: 0;
56
+ }
57
+ .toolbar h1 {
58
+ flex: 0 0 auto;
59
+ font-size: 18px;
60
+ font-weight: 700;
61
+ white-space: nowrap;
62
+ }
63
+ .file-tabs, .sheet-tabs {
64
+ display: flex;
65
+ gap: 6px;
66
+ flex-wrap: nowrap;
67
+ min-width: 0;
68
+ overflow-x: auto;
69
+ overflow-y: hidden;
70
+ overscroll-behavior-inline: contain;
71
+ scrollbar-color: var(--border-default) transparent;
72
+ scrollbar-width: thin;
73
+ }
74
+ .file-tabs { flex: 1 1 auto; padding: 2px 4px 7px 0; }
75
+ .sheet-tabs { flex: 1 1 auto; padding: 2px 4px 5px 0; }
76
+ .file-tabs::-webkit-scrollbar, .sheet-tabs::-webkit-scrollbar { height: 8px; }
77
+ .file-tabs::-webkit-scrollbar-track, .sheet-tabs::-webkit-scrollbar-track { background: transparent; }
78
+ .file-tabs::-webkit-scrollbar-thumb, .sheet-tabs::-webkit-scrollbar-thumb {
79
+ background: var(--border-default);
80
+ border: 2px solid var(--bg-surface);
81
+ }
82
+ .file-tab, .sheet-tab {
83
+ flex: 0 0 auto;
84
+ padding: 8px 16px;
85
+ background: var(--bg-surface);
86
+ color: var(--text-secondary);
87
+ cursor: pointer;
88
+ font-size: 13px;
89
+ font-weight: 600;
90
+ border: var(--border-width) solid var(--border-default);
91
+ box-shadow: var(--shadow);
92
+ transition: transform 150ms ease, box-shadow 150ms ease;
93
+ }
94
+ .file-tab:hover, .sheet-tab:hover { transform: translate(2px, 2px); box-shadow: var(--shadow-hover); }
95
+ .file-tab.active {
96
+ background: var(--emerald);
97
+ transform: translate(2px, 2px);
98
+ box-shadow: var(--shadow-hover);
99
+ }
100
+ .file-tab.has-conflicts { color: var(--text-primary); }
101
+ .sheet-toolbar {
102
+ background: #fff8dc;
103
+ padding: 9px 20px 11px;
104
+ display: flex;
105
+ align-items: center;
106
+ gap: 12px;
107
+ border-bottom: var(--border-width) solid var(--border-default);
108
+ flex-wrap: nowrap;
109
+ min-width: 0;
110
+ }
111
+ .sheet-scope {
112
+ display: flex;
113
+ align-items: center;
114
+ gap: 9px;
115
+ min-width: 0;
116
+ flex: 1 1 auto;
117
+ }
118
+ .sheet-choice { flex: 0 0 auto; display: flex; gap: 6px; align-items: center; }
119
+ .macro-label { font-size: 11px; font-weight: 900; color: #3d3529; white-space: nowrap; }
120
+ .macro-toggle { padding: 5px 9px; font-size: 11px; opacity: .55; }
121
+ .macro-toggle.is-selected { opacity: 1; outline: 2px solid #4f4638; outline-offset: 1px; }
122
+ .macro-toggle:disabled { cursor: not-allowed; filter: grayscale(.45); opacity: .45; }
123
+ .macro-note { font-size: 11px; font-weight: 700; color: #6a5142; white-space: nowrap; }
124
+ .diff-legend {
125
+ display: flex;
126
+ align-items: center;
127
+ gap: 14px;
128
+ padding: 7px 20px;
129
+ border-bottom: 2px solid #cfc5ae;
130
+ background: #f8f1de;
131
+ color: #4b4438;
132
+ font-size: 11px;
133
+ font-weight: 700;
134
+ white-space: nowrap;
135
+ overflow-x: auto;
136
+ }
137
+ .legend-intro { color: #27231d; font-weight: 900; }
138
+ .legend-item { display: inline-flex; align-items: center; gap: 5px; }
139
+ .legend-swatch { width: 18px; height: 13px; border: 1px solid #6e6657; display: inline-block; }
140
+ .legend-local { background: var(--sapphire); box-shadow: inset 0 0 0 2px rgba(35, 84, 130, .42); }
141
+ .legend-remote { background: var(--lava); box-shadow: inset 0 0 0 2px rgba(124, 38, 47, .38); }
142
+ .legend-incoming { background: var(--incoming); }
143
+ .legend-deleted { background: var(--tombstone); }
144
+ .sheet-label {
145
+ padding: 5px 8px;
146
+ background: var(--border-default);
147
+ color: var(--bg-surface);
148
+ font-size: 11px;
149
+ font-weight: 800;
150
+ letter-spacing: 1.2px;
151
+ }
152
+ .sheet-tab { padding: 6px 13px; box-shadow: 3px 3px 0 #000; }
153
+ .sheet-tab.active {
154
+ background: var(--gold);
155
+ transform: translate(2px, 2px);
156
+ box-shadow: 1px 1px 0 #000;
157
+ }
158
+ .tab-count {
159
+ margin-left: 7px;
160
+ font-size: 11px;
161
+ color: var(--text-muted);
162
+ }
163
+ .actions { flex: 0 0 auto; margin-left: 0; display: flex; gap: 8px; align-items: center; }
164
+ .sheet-actions {
165
+ flex: 0 0 auto;
166
+ display: flex;
167
+ align-items: center;
168
+ gap: 7px;
169
+ padding: 4px 7px;
170
+ border: 2px solid #b8a66d;
171
+ background: rgba(255, 255, 255, .58);
172
+ }
173
+ .sheet-actions-label {
174
+ color: #6c5518;
175
+ font-size: 10px;
176
+ font-weight: 900;
177
+ white-space: nowrap;
178
+ }
179
+ .sheet-actions .btn { padding: 7px 11px; font-size: 12px; }
180
+ .global-stats {
181
+ flex: 0 0 auto;
182
+ display: inline-flex;
183
+ align-items: center;
184
+ gap: 7px;
185
+ padding: 5px 8px;
186
+ border-left: 2px solid #bfb5a0;
187
+ color: #332d24;
188
+ white-space: nowrap;
189
+ }
190
+ .global-stats-label { font-size: 11px; font-weight: 900; }
191
+ .global-stats-value { font-size: 16px; font-weight: 900; line-height: 1; }
192
+ .global-stats-divider { color: #8c806c; font-size: 16px; font-weight: 700; }
193
+ .btn {
194
+ padding: 8px 16px;
195
+ border: 1px solid #d8cfbd;
196
+ border-radius: 3px;
197
+ cursor: pointer;
198
+ font-size: 13px;
199
+ font-weight: 800;
200
+ font-family: inherit;
201
+ letter-spacing: .2px;
202
+ box-shadow: 2px 2px 0 var(--shadow-edge);
203
+ transition: transform 120ms ease, filter 120ms ease, box-shadow 120ms ease;
204
+ background: linear-gradient(180deg, #f6f0e2, #dcd1bc);
205
+ color: #2a261f;
206
+ }
207
+ .btn:hover { transform: translate(1px, 1px); filter: brightness(1.04); box-shadow: 1px 1px 0 var(--shadow-edge); }
208
+ .btn:active { transform: translateY(1px); filter: brightness(.96); }
209
+ .btn:focus-visible { outline: 3px solid #a2d2ff; outline-offset: 2px; }
210
+ .btn-local { background: linear-gradient(180deg, #d9ecff, #91c4f1); border-color: #b8d8f4; color: #173f63; }
211
+ .btn-remote { background: linear-gradient(180deg, #ffd1d5, #e9919a); border-color: #f2b3ba; color: #5a2027; }
212
+ .btn-undo { background: linear-gradient(180deg, #f7f2e7, #dcd2bf); border-color: #e4ddcf; color: #2d2923; }
213
+ .btn-reset { background: linear-gradient(180deg, #ffe7a5, #efc65c); border-color: #f5dfa1; color: #49360d; }
214
+ .btn-cancel { background: linear-gradient(180deg, #f5c9cc, #dfa0a6); border-color: #efc2c6; color: #4a2529; }
215
+ .btn-submit {
216
+ position: relative;
217
+ min-width: 126px;
218
+ padding: 11px 28px;
219
+ background: linear-gradient(180deg, #ffdc8a, #f4aa3e);
220
+ border: 1px solid #f5d18d;
221
+ border-bottom-color: #dbab5e;
222
+ color: #3d2608;
223
+ font-size: 14px;
224
+ text-shadow: 0 1px 0 rgba(255, 255, 255, .35);
225
+ box-shadow: 0 4px 0 #ad7626, 2px 2px 0 var(--shadow-edge);
226
+ }
227
+ .btn-submit:hover { transform: translateY(-1px); box-shadow: 0 5px 0 #ad7626, 2px 2px 0 var(--shadow-edge); }
228
+ .btn-submit:active { transform: translateY(3px); box-shadow: 0 1px 0 #ad7626; }
229
+ .btn:disabled { opacity: .38; cursor: not-allowed; transform: none; filter: none; }
230
+ .stats {
231
+ font-size: 13px;
232
+ color: var(--text-secondary);
233
+ font-weight: 600;
234
+ white-space: nowrap;
235
+ }
236
+ .table-stage {
237
+ flex: 1 1 auto;
238
+ min-width: 0;
239
+ min-height: 0;
240
+ position: relative;
241
+ }
242
+ .table-container {
243
+ width: 100%;
244
+ height: 100%;
245
+ overflow: auto;
246
+ overscroll-behavior: contain;
247
+ scrollbar-gutter: stable;
248
+ padding: 16px;
249
+ }
250
+ .confirmed-overlay {
251
+ position: absolute;
252
+ inset: 0;
253
+ z-index: 10;
254
+ display: flex;
255
+ align-items: center;
256
+ justify-content: center;
257
+ background: rgba(70, 70, 70, .58);
258
+ cursor: not-allowed;
259
+ }
260
+ .confirmed-overlay-mark {
261
+ width: 112px;
262
+ height: 112px;
263
+ border-radius: 50%;
264
+ display: flex;
265
+ align-items: center;
266
+ justify-content: center;
267
+ background: #2ca45d;
268
+ border: 6px solid #d8ffe5;
269
+ color: #fff;
270
+ font-size: 76px;
271
+ line-height: 1;
272
+ font-weight: 900;
273
+ box-shadow: 0 5px 0 #12552e, 0 0 28px rgba(0, 0, 0, .35);
274
+ }
275
+ table.diff-table {
276
+ border-collapse: collapse;
277
+ font-size: 13px;
278
+ width: auto;
279
+ border: var(--border-width) solid var(--border-default);
280
+ box-shadow: var(--shadow);
281
+ background: var(--bg-surface);
282
+ }
283
+ table.diff-table th, table.diff-table td {
284
+ border: 2px solid var(--border-default);
285
+ padding: 6px 12px;
286
+ text-align: left;
287
+ min-width: 80px;
288
+ max-width: 240px;
289
+ overflow: hidden;
290
+ text-overflow: ellipsis;
291
+ white-space: nowrap;
292
+ }
293
+ table.diff-table th {
294
+ background: var(--gold);
295
+ color: #111;
296
+ font-weight: 800;
297
+ cursor: pointer;
298
+ text-shadow: none;
299
+ }
300
+ table.diff-table th:hover { background: var(--gold-dim); color: #111; }
301
+ td.cell-local {
302
+ background: var(--sapphire);
303
+ color: #111;
304
+ cursor: pointer;
305
+ }
306
+ td.cell-remote {
307
+ background: var(--lava);
308
+ color: #111;
309
+ cursor: pointer;
310
+ }
311
+ td.cell-no-conflict { background: var(--bg-surface); color: var(--text-secondary); }
312
+ .cell-selectable { box-shadow: inset 0 0 0 2px rgba(47, 42, 34, .38); cursor: pointer !important; }
313
+ .cell-selectable:hover { filter: brightness(.92) saturate(1.12); box-shadow: inset 0 0 0 3px rgba(47, 42, 34, .58); }
314
+ .row-header { cursor: pointer; }
315
+ .row-header:hover { background: var(--emerald) !important; }
316
+ tr.luban-header th { background: #2d2d2d; color: #f2f2f2; font-weight: 700; cursor: default; font-size: 12px; }
317
+ tr.luban-header th:hover { background: #2d2d2d; color: #f2f2f2; }
318
+ .axis-added-incoming {
319
+ background: var(--incoming) !important;
320
+ color: #29401f !important;
321
+ }
322
+ .axis-added-incoming:hover { background: var(--incoming-deep) !important; }
323
+ .axis-tombstone {
324
+ background-color: var(--tombstone) !important;
325
+ background-image: repeating-linear-gradient(135deg, rgba(255,255,255,.08) 0 8px, rgba(0,0,0,.08) 8px 16px) !important;
326
+ color: #f4f1e9 !important;
327
+ cursor: not-allowed !important;
328
+ text-decoration: line-through;
329
+ }
330
+ .axis-tombstone:hover { background-color: var(--tombstone-dark) !important; }
331
+ .structure-control { min-width: 80px; padding: 4px 5px !important; text-align: center !important; background: #ece5d4 !important; }
332
+ .structure-toggle {
333
+ border: 1px solid #c6bca8;
334
+ border-radius: 3px;
335
+ padding: 3px 7px;
336
+ background: #fff8dc;
337
+ color: #332b20;
338
+ font: 700 11px/1.2 inherit;
339
+ cursor: pointer;
340
+ box-shadow: 1px 1px 0 var(--shadow-edge);
341
+ }
342
+ .structure-toggle.is-delete { background: var(--tombstone); color: #fff; border-color: var(--tombstone-dark); }
343
+ .structure-toggle:disabled { opacity: .42; cursor: not-allowed; box-shadow: none; }
344
+ .structure-badge { display: inline-block; padding: 2px 6px; border: 1px solid rgba(0,0,0,.28); font-size: 10px; font-weight: 800; }
345
+ .structure-badge.added { background: var(--incoming); color: #29401f; }
346
+ .structure-badge.local-existing { background: #eee8dc; color: #4b4438; border-color: #bdb4a4; }
347
+ .structure-badge.deleted { background: var(--tombstone); color: #fff; }
348
+ .axis-index { display: inline-block; min-width: 22px; font-weight: 900; }
349
+ .axis-index.is-removed { color: #f7f1e5; }
350
+ .axis-source { display: block; margin-top: 2px; font-size: 9px; font-weight: 700; opacity: .72; }
351
+ .sheet-tab.sheet-added { background: var(--incoming); }
352
+ .sheet-tab.sheet-deleted { background: var(--tombstone); color: #fff; text-decoration: line-through; }
353
+ .sheet-tab.sheet-structural-conflict::before { content: '◆'; margin-right: 5px; color: #a8392f; }
354
+ .sheet-confirm-checkbox {
355
+ width: 15px;
356
+ height: 15px;
357
+ margin-right: 7px;
358
+ vertical-align: -2px;
359
+ accent-color: #278b52;
360
+ cursor: pointer;
361
+ }
362
+ .sheet-confirm-checkbox:focus-visible { outline: 2px solid #278b52; outline-offset: 2px; }
363
+ .conflict-nav { flex: 0 0 auto; display: flex; gap: 6px; align-items: center; }
364
+ .conflict-nav .btn { padding: 6px 10px; font-size: 11px; }
365
+ .conflict-nav-count { min-width: 48px; text-align: center; color: #4b4438; font-size: 11px; font-weight: 800; }
366
+ .cell-locked { cursor: not-allowed !important; }
367
+ .conflict-cell-flash { animation: conflict-cell-flash .55s ease-in-out 2; position: relative; z-index: 1; }
368
+ @keyframes conflict-cell-flash {
369
+ 0%, 100% { box-shadow: inset 0 0 0 2px rgba(47, 42, 34, .38); }
370
+ 35%, 70% {
371
+ background-color: #b86cff !important;
372
+ background-image: none !important;
373
+ box-shadow: inset 0 0 0 999px rgba(184, 108, 255, .96), 0 0 20px 6px rgba(147, 61, 255, .88);
374
+ }
375
+ }
376
+ .tooltip {
377
+ position: fixed;
378
+ display: none;
379
+ background: var(--bg-surface);
380
+ color: var(--text-primary);
381
+ border: var(--border-width) solid var(--border-default);
382
+ padding: 10px 14px;
383
+ font-size: 12px;
384
+ z-index: 1000;
385
+ pointer-events: none;
386
+ max-width: 520px;
387
+ box-shadow: var(--shadow);
388
+ word-break: break-word;
389
+ }
390
+ .tooltip .label { color: var(--text-muted); margin-bottom: 6px; font-weight: 600; }
391
+ .tooltip .val-base { color: #666; }
392
+ .tooltip .val-local { color: #246aa3; }
393
+ .tooltip .val-remote { color: #c0392b; }
394
+ .tooltip .val-current { color: var(--text-primary); margin-top: 5px; font-weight: 700; }
395
+ .tooltip .author { color: #777; margin-left: 18px; font-weight: 400; }
396
+ .sheet-message {
397
+ max-width: 720px;
398
+ margin: 20px;
399
+ padding: 24px;
400
+ background: var(--bg-surface);
401
+ border: var(--border-width) solid var(--border-default);
402
+ box-shadow: var(--shadow);
403
+ }
404
+ .sheet-message h2 { font-size: 17px; margin-bottom: 8px; }
405
+ .sheet-message p { color: var(--text-secondary); margin-bottom: 16px; line-height: 1.6; }
406
+ .pager { display: flex; gap: 6px; padding: 12px 0; }
407
+ .commit-modal { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; }
408
+ .commit-modal.is-hidden { display: none; }
409
+ .commit-modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); }
410
+ .commit-modal-panel {
411
+ position: relative;
412
+ background: var(--bg-surface);
413
+ border: var(--border-width) solid var(--border-default);
414
+ box-shadow: 8px 8px 0 #000;
415
+ padding: 22px 26px;
416
+ max-width: 520px;
417
+ width: 88%;
418
+ }
419
+ .commit-modal-icon { font-size: 28px; margin-bottom: 8px; }
420
+ .commit-modal-title { font-size: 17px; font-weight: 700; margin-bottom: 8px; }
421
+ .commit-modal-body { font-size: 13px; color: var(--text-secondary); white-space: pre-wrap; word-break: break-word; margin-bottom: 10px; }
422
+ .commit-modal-steps { font-size: 12px; font-family: Consolas, monospace; background: rgba(0, 0, 0, .04); border: 1px solid #ddd; padding: 8px 10px; margin-bottom: 12px; white-space: pre-wrap; word-break: break-word; max-height: 45vh; overflow-y: auto; }
423
+ .commit-modal-steps:empty { display: none; }
424
+ .commit-modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
425
+ .commit-modal.is-success .commit-modal-icon::before { content: "\2705"; }
426
+ .commit-modal.is-error .commit-modal-icon::before { content: "\274C"; }
427
+ .footer {
428
+ position: relative;
429
+ z-index: 20;
430
+ flex: 0 0 auto;
431
+ background: var(--bg-surface);
432
+ padding: 12px 20px;
433
+ display: flex;
434
+ align-items: center;
435
+ justify-content: space-between;
436
+ gap: 20px;
437
+ border-top: var(--border-width) solid var(--border-default);
438
+ color: var(--text-primary);
439
+ font-size: 13px;
440
+ }
441
+ .footer-actions { flex: 0 0 auto; display: flex; align-items: center; gap: 12px; }
442
+ @media (max-width: 900px) {
443
+ .toolbar { flex-wrap: wrap; }
444
+ .file-tabs { order: 3; flex: 1 0 100%; width: 100%; }
445
+ .actions { width: auto; margin-left: auto; }
446
+ .sheet-toolbar { align-items: flex-start; flex-wrap: wrap; }
447
+ .sheet-scope { flex: 1 0 100%; }
448
+ .sheet-actions { margin-left: auto; }
449
+ .footer { align-items: flex-end; }
450
+ }
451
+ </style>
452
+ </head>
453
+ <body>
454
+ <div class="toolbar">
455
+ <h1>Excel 冲突解决工具</h1>
456
+ <div class="file-tabs" id="fileTabs"></div>
457
+ <div class="global-stats" id="globalConfirmationStats" aria-label="全局冲突 Sheet 确认统计">
458
+ <span class="global-stats-label">冲突 Sheet 已确认</span>
459
+ <span class="global-stats-value" id="confirmedSheetCount">0</span>
460
+ <span class="global-stats-divider" aria-hidden="true">/</span>
461
+ <span class="global-stats-value" id="totalConflictSheetCount">0</span>
462
+ </div>
463
+ </div>
464
+ <div class="sheet-toolbar">
465
+ <div class="sheet-scope" aria-label="当前 Sheet 工区">
466
+ <span class="sheet-label">SHEET</span>
467
+ <div class="sheet-tabs" id="sheetTabs"></div>
468
+ <div class="sheet-actions" id="sheetActions" aria-label="当前 Sheet 操作">
469
+ <span class="sheet-actions-label">当前 Sheet 操作</span>
470
+ <button type="button" class="btn btn-local" id="allLocalBtn">全部本地</button>
471
+ <button type="button" class="btn btn-remote" id="allRemoteBtn">全部远端</button>
472
+ <button type="button" class="btn btn-undo" id="undoBtn">撤销</button>
473
+ <button type="button" class="btn btn-reset" id="resetBtn">重置</button>
474
+ </div>
475
+ </div>
476
+ <div class="conflict-nav" id="conflictNav" aria-label="冲突定位">
477
+ <button type="button" class="btn" id="prevConflictBtn">上一个冲突</button>
478
+ <span class="conflict-nav-count" id="conflictNavCount">0 / 0</span>
479
+ <button type="button" class="btn" id="nextConflictBtn">下一个冲突</button>
480
+ </div>
481
+ <div class="sheet-choice" id="sheetChoice"></div>
482
+ </div>
483
+ <div class="diff-legend" id="diffLegend">
484
+ <span class="legend-intro">主行列号=合并后位置</span>
485
+ <span class="legend-item"><i class="legend-swatch legend-local"></i>本地值</span>
486
+ <span class="legend-item"><i class="legend-swatch legend-remote"></i>远端值</span>
487
+ <span class="legend-item"><i class="legend-swatch legend-incoming"></i>远端新增</span>
488
+ <span class="legend-item"><i class="legend-swatch legend-deleted"></i>已删除</span>
489
+ <span class="legend-item">悬停 Cell 查看 Base / 本地 / 远端</span>
490
+ </div>
491
+ <div class="table-stage" id="tableStage">
492
+ <div class="table-container" id="tableContainer"></div>
493
+ </div>
494
+ <div class="tooltip" id="tooltip"></div>
495
+ <div class="commit-modal is-hidden" id="commitModal" role="dialog" aria-modal="true" aria-labelledby="commitModalTitle">
496
+ <div class="commit-modal-backdrop"></div>
497
+ <div class="commit-modal-panel">
498
+ <div class="commit-modal-icon"></div>
499
+ <div class="commit-modal-title" id="commitModalTitle">提交中...</div>
500
+ <div class="commit-modal-body" id="commitModalBody"></div>
501
+ <div class="commit-modal-steps" id="commitModalSteps"></div>
502
+ <div class="commit-modal-actions" id="commitModalActions"></div>
503
+ </div>
504
+ </div>
505
+ <div class="footer">
506
+ <span id="footerStatus">加载中...</span>
507
+ <div class="footer-actions">
508
+ <button type="button" class="btn btn-cancel" id="cancelBtn">取消并退出</button>
509
+ <button type="button" class="btn btn-submit" id="submitBtn">提交合并</button>
510
+ </div>
511
+ </div>
512
+ <script>
513
+ let model = null;
514
+ let currentFile = 0;
515
+ let currentSheet = 0;
516
+ let currentPage = 0;
517
+ let decisions = {files: {}};
518
+ let undoStack = [];
519
+ const PAGE_SIZE = 500;
520
+
521
+ const byId = id => document.getElementById(id);
522
+ const fmtVal = value => value === null || value === undefined ? '' : String(value);
523
+ function colLabel(n) { let s = ''; while (n > 0) { n--; s = String.fromCharCode(65 + n % 26) + s; n = Math.floor(n / 26); } return s; }
524
+ function currentFileData() { return model && model.files[currentFile]; }
525
+ function currentSheetData() { const file = currentFileData(); return file && file.diff.sheets[currentSheet]; }
526
+ function fileDecision(file) {
527
+ if (!decisions.files[file.id]) decisions.files[file.id] = {sheets: {}};
528
+ return decisions.files[file.id];
529
+ }
530
+ function sheetDecision(file, sheet) {
531
+ const target = fileDecision(file).sheets;
532
+ if (!target[sheet.name]) target[sheet.name] = {cells: {}, rows: {}, columns: {}, confirmed: false};
533
+ if (typeof target[sheet.name].confirmed !== 'boolean') target[sheet.name].confirmed = false;
534
+ return target[sheet.name];
535
+ }
536
+ function cellKey(cell) { return cell.id || cell.coordinate; }
537
+ function defaultCellChoice(cell) {
538
+ if (cell.resolution === 'conflict') return 'theirs';
539
+ if (cell.resolution === 'same') return cell.oursPresent === false ? 'theirs' : 'ours';
540
+ return cell.resolution;
541
+ }
542
+ function defaultSheetChoice(sheet) { return sheet.action.startsWith('conflict_') ? 'ours' : null; }
543
+ function initializeDecisions() {
544
+ for (const file of model.files) {
545
+ const fileTarget = fileDecision(file);
546
+ if (file.macro) fileTarget.macro = file.macro.default || 'ours';
547
+ for (const sheet of file.diff.sheets) {
548
+ const target = sheetDecision(file, sheet);
549
+ target.confirmed = false;
550
+ const sheetChoice = defaultSheetChoice(sheet);
551
+ if (sheetChoice) target.sheet = sheetChoice;
552
+ if (sheet.renameStatus === 'rename_conflict') target.rename = 'ours';
553
+ if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') target.mergedCells = sheet.mergedCells.default || 'ours';
554
+ for (const row of sheet.allRows || sheet.rows || []) if (row.state === 'delete_modify') target.rows[row.id] = row.default;
555
+ for (const column of sheet.columns || []) if (column.state === 'delete_modify') target.columns[column.id] = column.default;
556
+ for (const cell of sheet.cells || []) target.cells[cell.id || cell.coordinate] = defaultCellChoice(cell);
557
+ }
558
+ }
559
+ }
560
+ function gridVal(sheet, row, col) {
561
+ const values = (sheet.contextRows || {})[String(row)];
562
+ return values ? values[col - 1] : '';
563
+ }
564
+ function enableHorizontalWheel(host) {
565
+ host.addEventListener('wheel', event => {
566
+ if (host.scrollWidth <= host.clientWidth || Math.abs(event.deltaX) >= Math.abs(event.deltaY)) return;
567
+ event.preventDefault();
568
+ host.scrollLeft += event.deltaY;
569
+ }, {passive: false});
570
+ }
571
+
572
+ function autoTheirsNote(sep) {
573
+ const count = (model && model.summary) ? (model.summary.autoTheirsCount || 0) : 0;
574
+ return count ? `${sep}已按远端全量自动处理 ${count} 张表` : '';
575
+ }
576
+
577
+ async function init() {
578
+ const response = await fetch('/api/diff');
579
+ model = await response.json();
580
+ if (!model.files || model.files.length === 0) {
581
+ byId('footerStatus').textContent = '未发现冲突。' + autoTheirsNote('');
582
+ return;
583
+ }
584
+ initializeDecisions();
585
+ render();
586
+ }
587
+
588
+ function render() {
589
+ renderFileTabs();
590
+ renderSheetTabs();
591
+ renderSheetChoice();
592
+ renderTable();
593
+ renderConflictNavigation();
594
+ renderActionAvailability();
595
+ updateStats();
596
+ }
597
+ function renderFileTabs() {
598
+ const host = byId('fileTabs');
599
+ host.replaceChildren();
600
+ model.files.forEach((file, index) => {
601
+ const tab = document.createElement('div');
602
+ tab.className = 'file-tab' + (index === currentFile ? ' active' : '') + (file.diff.summary.changedCellCount ? ' has-conflicts' : '');
603
+ tab.textContent = file.path;
604
+ tab.onclick = () => { currentFile = index; currentSheet = 0; currentPage = 0; render(); };
605
+ host.append(tab);
606
+ });
607
+ }
608
+ function sheetDecisionCount(file, sheet) {
609
+ if (sheet.action.startsWith('conflict_')) return 1;
610
+ if (sheet.action !== 'merge') return 1;
611
+ let total = sheet.renameStatus === 'rename_conflict' ? 1 : 0;
612
+ if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') total++;
613
+ total += (sheet.allRows || sheet.rows || []).filter(axis => axis.state === 'delete_modify').length;
614
+ total += (sheet.columns || []).filter(axis => axis.state === 'delete_modify').length;
615
+ for (const cell of sheet.cells || []) {
616
+ if (!sheet.columns || !sheet.allRows || structuralCellSelectable(file, sheet, cell)) total++;
617
+ }
618
+ return total;
619
+ }
620
+ function renderSheetTabs() {
621
+ const host = byId('sheetTabs');
622
+ host.replaceChildren();
623
+ const file = currentFileData();
624
+ // 只显示需要处理的 Sheet(needsAttention=false 的无冲突/无更改 Sheet 折叠)
625
+ const visible = [];
626
+ file.diff.sheets.forEach((sheet, index) => {
627
+ if (sheet.needsAttention === false) return;
628
+ visible.push({ sheet, index });
629
+ });
630
+ if (!visible.length) {
631
+ const empty = document.createElement('div');
632
+ empty.className = 'sheet-tab';
633
+ empty.textContent = '该文件没有需要处理的 Sheet';
634
+ host.append(empty);
635
+ return;
636
+ }
637
+ const current = file.diff.sheets[currentSheet];
638
+ if (!current || current.needsAttention === false) {
639
+ currentSheet = visible[0].index;
640
+ }
641
+ visible.forEach(({ sheet, index }) => {
642
+ const tab = document.createElement('div');
643
+ const added = sheet.action === 'auto_add_theirs';
644
+ const deleted = sheet.action === 'auto_delete';
645
+ const structuralConflict = sheet.action.startsWith('conflict_') || sheet.renameStatus === 'rename_conflict' || (sheet.structuralConflictCount || 0) > 0;
646
+ tab.className = 'sheet-tab' + (index === currentSheet ? ' active' : '') + (added ? ' sheet-added' : '') + (deleted ? ' sheet-deleted' : '') + (structuralConflict ? ' sheet-structural-conflict' : '');
647
+ const target = sheetDecision(file, sheet);
648
+ const confirmation = document.createElement('input');
649
+ confirmation.type = 'checkbox';
650
+ confirmation.className = 'sheet-confirm-checkbox';
651
+ confirmation.checked = target.confirmed === true;
652
+ confirmation.title = '勾选表示你已人工审阅并确认本 Sheet 的所有冲突;取消勾选可继续编辑。';
653
+ confirmation.setAttribute('aria-label', `${sheet.name}:已确认(人工审阅完成)`);
654
+ confirmation.onclick = event => event.stopPropagation();
655
+ confirmation.onchange = event => {
656
+ event.stopPropagation();
657
+ target.confirmed = confirmation.checked;
658
+ render();
659
+ };
660
+ tab.append(confirmation, document.createTextNode(sheet.name));
661
+ const count = document.createElement('span');
662
+ count.className = 'tab-count';
663
+ count.textContent = sheetDecisionCount(file, sheet);
664
+ tab.append(count);
665
+ tab.onclick = () => { currentSheet = index; currentPage = 0; render(); };
666
+ host.append(tab);
667
+ });
668
+ }
669
+ function renderSheetChoice() {
670
+ const host = byId('sheetChoice'); host.replaceChildren();
671
+ const file = currentFileData(); const sheet = currentSheetData();
672
+ const macro = file && file.macro;
673
+ if (macro && macro.status !== 'same') {
674
+ const label = document.createElement('span'); label.className = 'macro-label'; label.textContent = '当前文件宏 · VBA/ActiveX';
675
+ host.append(label);
676
+ if (macro.status === 'conflict') {
677
+ const selected = fileDecision(file).macro || macro.default;
678
+ const local = document.createElement('button');
679
+ local.className = 'btn btn-local macro-toggle' + (selected === 'ours' ? ' is-selected' : '');
680
+ local.textContent = '本地宏'; local.onclick = () => chooseMacro(file, 'ours');
681
+ const remote = document.createElement('button');
682
+ remote.className = 'btn btn-remote macro-toggle' + (selected === 'theirs' ? ' is-selected' : '');
683
+ remote.textContent = '远端宏'; remote.onclick = () => chooseMacro(file, 'theirs');
684
+ host.append(local, remote);
685
+ } else {
686
+ const note = document.createElement('span'); note.className = 'macro-note';
687
+ note.textContent = macro.status === 'auto_theirs'
688
+ ? '仅远端变更,自动采用远端宏'
689
+ : macro.status === 'auto_ours'
690
+ ? '仅本地变更,自动采用本地宏'
691
+ : '两边宏包结构不同,安全保留本地宏';
692
+ host.append(note);
693
+ }
694
+ }
695
+ if (!sheet) return;
696
+ const target = sheetDecision(file, sheet);
697
+ const confirmed = sheetNeedsConfirmation(sheet) && target.confirmed;
698
+ if (sheet.renameStatus === 'rename_conflict') {
699
+ const local = document.createElement('button'); local.className = 'structure-toggle' + (target.rename === 'ours' ? '' : ' is-delete');
700
+ local.textContent = `本地名:${sheet.names.ours}`; local.disabled = confirmed; local.onclick = () => chooseRename(file, sheet, 'ours');
701
+ const remote = document.createElement('button'); remote.className = 'structure-toggle' + (target.rename === 'theirs' ? '' : ' is-delete');
702
+ remote.textContent = `远端名:${sheet.names.theirs}`; remote.disabled = confirmed; remote.onclick = () => chooseRename(file, sheet, 'theirs');
703
+ host.append(local, remote);
704
+ }
705
+ if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') {
706
+ const label = document.createElement('span'); label.className = 'macro-label'; label.textContent = '合并表头';
707
+ const local = document.createElement('button');
708
+ local.className = 'btn btn-local macro-toggle' + (target.mergedCells === 'ours' ? ' is-selected' : '');
709
+ local.textContent = '本地结构'; local.disabled = confirmed; local.onclick = () => chooseMergedCells(file, sheet, 'ours');
710
+ const remote = document.createElement('button');
711
+ remote.className = 'btn btn-remote macro-toggle' + (target.mergedCells === 'theirs' ? ' is-selected' : '');
712
+ remote.textContent = '远端结构'; remote.disabled = confirmed; remote.onclick = () => chooseMergedCells(file, sheet, 'theirs');
713
+ host.append(label, local, remote);
714
+ }
715
+ }
716
+
717
+ function appendHeaderCell(row, value, onClick, className) {
718
+ const cell = document.createElement('th');
719
+ cell.textContent = value;
720
+ if (className) cell.className = className;
721
+ if (onClick) cell.onclick = onClick;
722
+ row.append(cell);
723
+ return cell;
724
+ }
725
+ function bindTip(cell, row, col) {
726
+ cell.onmouseenter = event => showTip(event, row, col);
727
+ cell.onmousemove = event => positionTip(event);
728
+ cell.onmouseleave = hideTip;
729
+ }
730
+ function renderTable() {
731
+ const file = currentFileData();
732
+ const sheet = currentSheetData();
733
+ const host = byId('tableContainer');
734
+ host.replaceChildren();
735
+ const oldOverlay = byId('confirmedOverlay');
736
+ if (oldOverlay) oldOverlay.remove();
737
+ if (!sheet) {
738
+ renderMessage(host, '无需处理', '两侧工作簿内容一致,没有需要展示的 Sheet 差异。');
739
+ return;
740
+ }
741
+ if (sheet.action.startsWith('conflict_')) {
742
+ renderSheetConflict(host, file, sheet);
743
+ renderConfirmedOverlay(host, file, sheet);
744
+ return;
745
+ }
746
+ if (sheet.action !== 'merge') {
747
+ const labels = {
748
+ auto_add_ours: '该 Sheet 仅存在于本地,将自动保留本地版本。',
749
+ auto_add_theirs: '该 Sheet 仅存在于远端,将自动保留远端版本。',
750
+ auto_delete: '该 Sheet 的删除只发生在一侧,另一侧未修改,将自动删除。'
751
+ };
752
+ renderMessage(host, sheet.name, labels[sheet.action] || `将自动执行:${sheet.action}`);
753
+ renderConfirmedOverlay(host, file, sheet);
754
+ return;
755
+ }
756
+ renderDiffTable(host, file, sheet);
757
+ renderConfirmedOverlay(host, file, sheet);
758
+ }
759
+ function sheetNeedsConfirmation(sheet) {
760
+ return !!sheet && sheet.needsAttention !== false;
761
+ }
762
+ function renderConfirmedOverlay(host, file, sheet) {
763
+ if (!sheetNeedsConfirmation(sheet) || !sheetDecision(file, sheet).confirmed) return;
764
+ const overlay = document.createElement('div');
765
+ overlay.id = 'confirmedOverlay';
766
+ overlay.className = 'confirmed-overlay';
767
+ overlay.title = '本 Sheet 已确认;取消 Sheet 标签前的勾选后可继续编辑。';
768
+ overlay.setAttribute('aria-label', '本 Sheet 已确认,表格交互已锁定');
769
+ const mark = document.createElement('div');
770
+ mark.className = 'confirmed-overlay-mark';
771
+ mark.textContent = '✓';
772
+ overlay.append(mark);
773
+ byId('tableStage').append(overlay);
774
+ }
775
+ function currentSheetIsConfirmed() {
776
+ const file = currentFileData(); const sheet = currentSheetData();
777
+ return !!file && sheetNeedsConfirmation(sheet) && sheetDecision(file, sheet).confirmed;
778
+ }
779
+ function renderActionAvailability() {
780
+ const confirmed = currentSheetIsConfirmed();
781
+ byId('allLocalBtn').disabled = confirmed;
782
+ byId('allRemoteBtn').disabled = confirmed;
783
+ const file = currentFileData(); const sheet = currentSheetData();
784
+ const scope = file && sheet ? `当前 Sheet:${file.path} / ${sheet.name}` : '当前 Sheet';
785
+ const message = confirmed ? `${scope} 已确认;取消 Sheet 标签前的勾选后可继续编辑。` : `${scope} 的批量操作`;
786
+ byId('allLocalBtn').title = `${message}:将可处理项全部采用本地。`;
787
+ byId('allRemoteBtn').title = `${message}:将可处理项全部采用远端。`;
788
+ byId('undoBtn').title = `${scope}:撤销最近一次选择,不影响其它 Sheet 或文件级宏。`;
789
+ byId('resetBtn').title = `${scope}:恢复本 Sheet 的默认决策,不影响其它 Sheet 或文件级宏。`;
790
+ }
791
+ function navigableConflictCells(file, sheet) {
792
+ if (!sheet || !sheetNeedsConfirmation(sheet)) return [];
793
+ if (sheet.columns && sheet.allRows) {
794
+ return (sheet.cells || []).filter(cell => cell.resolution === 'conflict' && structuralCellSelectable(file, sheet, cell));
795
+ }
796
+ return (sheet.cells || []).filter(cell => cell.resolution === 'conflict');
797
+ }
798
+ function conflictCellKey(cell) { return cell && (cell.id || cell.coordinate); }
799
+ function renderConflictNavigation() {
800
+ const file = currentFileData(); const sheet = currentSheetData();
801
+ const cells = navigableConflictCells(file, sheet);
802
+ const count = byId('conflictNavCount');
803
+ const previous = byId('prevConflictBtn'); const next = byId('nextConflictBtn');
804
+ if (!count || !previous || !next) return;
805
+ const currentId = sheet && sheetDecision(file, sheet).lastNavigatedConflict;
806
+ const currentIndex = cells.findIndex(cell => conflictCellKey(cell) === currentId);
807
+ count.textContent = cells.length ? `${currentIndex < 0 ? 0 : currentIndex + 1} / ${cells.length}` : '0 / 0';
808
+ previous.disabled = cells.length === 0;
809
+ next.disabled = cells.length === 0;
810
+ previous.title = cells.length ? '定位到当前 Sheet 的上一个可处理冲突(循环)' : '当前 Sheet 没有可定位的冲突';
811
+ next.title = cells.length ? '定位到当前 Sheet 的下一个可处理冲突(循环)' : '当前 Sheet 没有可定位的冲突';
812
+ }
813
+ function markConflictCell(cellElement) {
814
+ if (!cellElement) return;
815
+ cellElement.classList.remove('conflict-cell-flash');
816
+ // Force a reflow so repeated navigation to the same cell restarts the flash.
817
+ void cellElement.offsetWidth;
818
+ cellElement.classList.add('conflict-cell-flash');
819
+ window.setTimeout(() => cellElement.classList.remove('conflict-cell-flash'), 1300);
820
+ }
821
+ function locateConflictCell(direction) {
822
+ const file = currentFileData(); const sheet = currentSheetData();
823
+ const cells = navigableConflictCells(file, sheet);
824
+ if (!cells.length) return;
825
+ const state = sheetDecision(file, sheet);
826
+ const currentId = state.lastNavigatedConflict;
827
+ let index = cells.findIndex(cell => conflictCellKey(cell) === currentId);
828
+ index = index < 0 ? (direction > 0 ? 0 : cells.length - 1) : (index + direction + cells.length) % cells.length;
829
+ const target = cells[index];
830
+ state.lastNavigatedConflict = conflictCellKey(target);
831
+ const rowNumber = target.row;
832
+ if (!(sheet.columns && sheet.allRows) && rowNumber !== undefined) {
833
+ const rows = [...new Set((sheet.cells || []).map(cell => cell.row))].sort((a, b) => a - b);
834
+ currentPage = Math.floor(Math.max(0, rows.indexOf(rowNumber)) / PAGE_SIZE);
835
+ } else if (sheet.columns && sheet.allRows) {
836
+ const rows = sheet.rows || [];
837
+ const rowIndex = rows.findIndex(row => row.id === target.rowId);
838
+ currentPage = Math.floor(Math.max(0, rowIndex) / PAGE_SIZE);
839
+ }
840
+ renderTable();
841
+ renderConflictNavigation();
842
+ requestAnimationFrame(() => {
843
+ const element = [...byId('tableContainer').querySelectorAll('[data-conflict-id]')]
844
+ .find(item => item.dataset.conflictId === conflictCellKey(target));
845
+ if (!element) return;
846
+ const container = byId('tableContainer');
847
+ container.scrollTop = Math.max(0, element.offsetTop - container.clientHeight / 2);
848
+ container.scrollLeft = Math.max(0, element.offsetLeft - container.clientWidth / 2);
849
+ element.scrollIntoView({block: 'nearest', inline: 'nearest'});
850
+ markConflictCell(element);
851
+ });
852
+ }
853
+ function renderMessage(host, title, message) {
854
+ const box = document.createElement('div');
855
+ box.className = 'sheet-message';
856
+ const heading = document.createElement('h2'); heading.textContent = title;
857
+ const body = document.createElement('p'); body.textContent = message;
858
+ box.append(heading, body); host.append(box);
859
+ }
860
+ function renderSheetConflict(host, file, sheet) {
861
+ const box = document.createElement('div');
862
+ box.className = 'sheet-message';
863
+ const heading = document.createElement('h2'); heading.textContent = `${sheet.name}:删除 / 修改冲突`;
864
+ const body = document.createElement('p');
865
+ body.textContent = '一侧删除了整个 Sheet,另一侧继续修改。选择某一侧后,将按该侧实际状态保留或删除 Sheet。';
866
+ const target = sheetDecision(file, sheet);
867
+ const names = sheet.names || {};
868
+ const local = document.createElement('button');
869
+ const localLabel = names.ours ? '保留 Sheet(本地)' : '删除 Sheet(本地)';
870
+ local.className = 'btn btn-local'; local.textContent = target.sheet === 'ours' ? `✓ ${localLabel}` : localLabel;
871
+ local.onclick = () => chooseSheet(file, sheet, 'ours');
872
+ const remote = document.createElement('button');
873
+ const remoteLabel = names.theirs ? '保留 Sheet(远端)' : '删除 Sheet(远端)';
874
+ remote.className = 'btn btn-remote'; remote.textContent = target.sheet === 'theirs' ? `✓ ${remoteLabel}` : remoteLabel;
875
+ remote.onclick = () => chooseSheet(file, sheet, 'theirs');
876
+ box.append(heading, body, local, document.createTextNode(' '), remote); host.append(box);
877
+ }
878
+ function axisChoice(file, sheet, kind, axis) {
879
+ if (axis.state !== 'delete_modify') return axis.default;
880
+ return sheetDecision(file, sheet)[kind][axis.id] || axis.default;
881
+ }
882
+ function axisKept(file, sheet, kind, axis) { return axisChoice(file, sheet, kind, axis) !== 'delete'; }
883
+ function axisClass(file, sheet, kind, axis) {
884
+ if (axis.state === 'deleted' || !axisKept(file, sheet, kind, axis)) return ' axis-tombstone';
885
+ if (axis.state === 'added' && axis.incoming) return ' axis-added-incoming';
886
+ return '';
887
+ }
888
+ function axisSource(axis, kind) {
889
+ if (kind === 'rows') return `Base ${axis.baseRow ?? '—'} · 本地 ${axis.oursRow ?? '—'} · 远端 ${axis.theirsRow ?? '—'}`;
890
+ return `Base ${axis.baseColumn ?? '—'} · 本地 ${axis.oursColumn ?? '—'} · 远端 ${axis.theirsColumn ?? '—'}`;
891
+ }
892
+ function structuralOutputMaps(file, sheet) {
893
+ const headerCount = (sheet.headerRows || []).length;
894
+ const rows = new Map(); const columns = new Map(); let rowIndex = headerCount; let columnIndex = 0;
895
+ for (const row of sheet.allRows || []) if (axisKept(file, sheet, 'rows', row)) rows.set(row.id, ++rowIndex);
896
+ for (const column of sheet.columns || []) if (axisKept(file, sheet, 'columns', column)) columns.set(column.id, colLabel(++columnIndex));
897
+ return {rows, columns};
898
+ }
899
+ function structuralCellSelectable(file, sheet, cell) {
900
+ if (!cell || !cell.changed || cell.selectable === false) return false;
901
+ const row = (sheet.allRows || []).find(item => item.id === cell.rowId);
902
+ const column = (sheet.columns || []).find(item => item.id === cell.colId);
903
+ return !!row && !!column && !row.locked && !column.locked
904
+ && axisKept(file, sheet, 'rows', row) && axisKept(file, sheet, 'columns', column);
905
+ }
906
+ function defaultDisplayChoice(cell) {
907
+ const choice = defaultCellChoice(cell);
908
+ if (choice === 'same') return cell.oursPresent ? 'ours' : 'theirs';
909
+ return choice;
910
+ }
911
+ function structuralCellValue(target, cell) {
912
+ const choice = target.cells[cell.id] || defaultDisplayChoice(cell);
913
+ if (choice === 'ours' && cell.oursPresent) return cell.ours;
914
+ if (choice === 'theirs' && cell.theirsPresent) return cell.theirs;
915
+ if (cell.oursPresent) return cell.ours;
916
+ if (cell.theirsPresent) return cell.theirs;
917
+ return cell.base;
918
+ }
919
+ function appendStructureControl(row, file, sheet, kind, axis) {
920
+ const cell = document.createElement('th'); cell.className = 'structure-control' + axisClass(file, sheet, kind, axis);
921
+ cell.title = axisSource(axis, kind);
922
+ if (axis.state === 'delete_modify') {
923
+ const button = document.createElement('button'); const kept = axisKept(file, sheet, kind, axis);
924
+ button.className = 'structure-toggle' + (kept ? '' : ' is-delete');
925
+ button.textContent = kept ? '已保留' : '已删除'; button.onclick = event => { event.stopPropagation(); toggleAxis(file, sheet, kind, axis); };
926
+ cell.append(button);
927
+ } else if (axis.state === 'added') {
928
+ const badge = document.createElement('span'); badge.className = `structure-badge ${axis.incoming ? 'added' : 'local-existing'}`;
929
+ const source = kind === 'rows' ? (axis.theirsRow ?? axis.oursRow) : (axis.theirsColumn ?? axis.oursColumn);
930
+ badge.textContent = `${axis.incoming ? '远端新增' : '本地已有'} · ${source}`; cell.append(badge);
931
+ } else if (axis.state === 'deleted') {
932
+ const badge = document.createElement('span'); badge.className = 'structure-badge deleted';
933
+ badge.textContent = `已删除 · ${kind === 'rows' ? axis.baseRow : axis.baseColumn}`; cell.append(badge);
934
+ }
935
+ row.append(cell); return cell;
936
+ }
937
+ function renderDiffTable(host, file, sheet) {
938
+ if (sheet.columns && sheet.allRows) return renderStructuralTable(host, file, sheet);
939
+ return renderLegacyDiffTable(host, file, sheet);
940
+ }
941
+ function buildHeaderMergeMap(sheet, selection) {
942
+ // 构建 header cell 的合并映射:key = "rowIndex|colIndex" -> {isOrigin, colspan}
943
+ const mc = sheet.mergedCells || {};
944
+ let regions;
945
+ if (mc.status === 'conflict') {
946
+ const choice = (selection && selection.mergedCells) || mc.default || 'ours';
947
+ regions = mc[choice] || mc.ours || [];
948
+ } else {
949
+ regions = mc.resolved || [];
950
+ }
951
+ const columns = sheet.columns || [];
952
+ const headerRows = sheet.headerRows || [];
953
+ const headerRowNumbers = headerRows.map(r => r.row);
954
+ const map = {};
955
+ for (const region of regions) {
956
+ const rowIdxs = (region.rows || []).map(r => r.startsWith('header:') ? parseInt(r.slice(7)) : null).filter(r => r !== null);
957
+ const colIds = region.columns || [];
958
+ if (rowIdxs.length < 1 || colIds.length < 2) continue;
959
+ const colIdxs = colIds.map(cid => columns.findIndex(c => c.id === cid)).filter(i => i >= 0);
960
+ if (colIdxs.length < 2) continue;
961
+ const minCol = Math.min(...colIdxs);
962
+ const maxCol = Math.max(...colIdxs);
963
+ for (const rowNum of rowIdxs) {
964
+ const hrIdx = headerRowNumbers.indexOf(rowNum);
965
+ if (hrIdx < 0) continue;
966
+ map[`${rowNum}|${minCol}`] = {isOrigin: true, colspan: maxCol - minCol + 1};
967
+ for (let c = minCol + 1; c <= maxCol; c++) {
968
+ map[`${rowNum}|${c}`] = {isOrigin: false};
969
+ }
970
+ }
971
+ }
972
+ return map;
973
+ }
974
+ function renderStructuralTable(host, file, sheet) {
975
+ const columns = sheet.columns || []; const rows = sheet.rows || [];
976
+ if (!rows.length && !columns.length) { renderMessage(host, sheet.name, '该 Sheet 没有表内数据差异。'); return; }
977
+ const target = sheetDecision(file, sheet);
978
+ const output = structuralOutputMaps(file, sheet);
979
+ const grid = new Map((sheet.gridCells || []).map(cell => [`${cell.rowId}|${cell.colId}`, cell]));
980
+ const table = document.createElement('table'); table.className = 'diff-table';
981
+ const head = document.createElement('thead');
982
+ const controls = document.createElement('tr'); appendHeaderCell(controls, '结构', null, 'structure-control');
983
+ for (const column of columns) appendStructureControl(controls, file, sheet, 'columns', column);
984
+ head.append(controls);
985
+ const letters = document.createElement('tr'); appendHeaderCell(letters, '行');
986
+ for (const column of columns) {
987
+ const header = appendHeaderCell(letters, output.columns.get(column.id) || '×', () => bulkStructuralColumn(column.id));
988
+ header.className += axisClass(file, sheet, 'columns', column);
989
+ header.title = `${axisSource(column, 'columns')} · 合并后 ${output.columns.get(column.id) || '删除'}`;
990
+ }
991
+ head.append(letters);
992
+ const headerMergeMap = buildHeaderMergeMap(sheet, sheetDecision(file, sheet));
993
+ for (const headerRow of sheet.headerRows || []) {
994
+ const row = document.createElement('tr'); row.className = 'luban-header'; appendHeaderCell(row, String(headerRow.row));
995
+ headerRow.cells.forEach((value, index) => {
996
+ const merge = headerMergeMap[`${headerRow.row}|${index}`];
997
+ if (merge && !merge.isOrigin) return; // 被合并的格子跳过
998
+ const cell = appendHeaderCell(row, fmtVal(value));
999
+ cell.className += axisClass(file, sheet, 'columns', columns[index]);
1000
+ if (merge && merge.colspan > 1) cell.colSpan = merge.colspan;
1001
+ });
1002
+ head.append(row);
1003
+ }
1004
+ table.append(head);
1005
+ const body = document.createElement('tbody');
1006
+ const start = currentPage * PAGE_SIZE; const end = Math.min(rows.length, start + PAGE_SIZE);
1007
+ for (let index = start; index < end; index++) {
1008
+ const axisRow = rows[index]; const row = document.createElement('tr');
1009
+ const rowHeader = document.createElement('th'); rowHeader.className = 'row-header' + axisClass(file, sheet, 'rows', axisRow);
1010
+ rowHeader.title = `${axisSource(axisRow, 'rows')} · 合并后 ${output.rows.get(axisRow.id) || '删除'}`;
1011
+ const outputIndex = document.createElement('span'); outputIndex.className = 'axis-index' + (output.rows.has(axisRow.id) ? '' : ' is-removed');
1012
+ outputIndex.textContent = output.rows.has(axisRow.id) ? String(output.rows.get(axisRow.id)) : '×'; rowHeader.append(outputIndex);
1013
+ if (axisRow.state === 'delete_modify') {
1014
+ const button = document.createElement('button'); const kept = axisKept(file, sheet, 'rows', axisRow);
1015
+ button.className = 'structure-toggle' + (kept ? '' : ' is-delete'); button.textContent = kept ? '已保留' : '已删除';
1016
+ button.onclick = event => { event.stopPropagation(); toggleAxis(file, sheet, 'rows', axisRow); }; rowHeader.append(button);
1017
+ } else {
1018
+ if (axisRow.state === 'added' || axisRow.state === 'deleted') {
1019
+ const badge = document.createElement('span'); badge.className = `structure-badge ${axisRow.state}`;
1020
+ badge.textContent = axisRow.state === 'added' ? (axisRow.incoming ? '远端新增' : '本地已有') : '已删除'; rowHeader.append(document.createTextNode(' '), badge);
1021
+ }
1022
+ }
1023
+ const source = document.createElement('span'); source.className = 'axis-source'; source.textContent = axisSource(axisRow, 'rows'); rowHeader.append(source);
1024
+ if (axisKept(file, sheet, 'rows', axisRow) && !axisRow.locked) rowHeader.onclick = () => bulkStructuralRow(axisRow.id);
1025
+ row.append(rowHeader);
1026
+ for (const column of columns) {
1027
+ const cellData = grid.get(`${axisRow.id}|${column.id}`); const cell = document.createElement('td');
1028
+ const kept = axisKept(file, sheet, 'rows', axisRow) && axisKept(file, sheet, 'columns', column);
1029
+ const structurallyLocked = !kept || axisRow.locked || column.locked;
1030
+ if (!kept || axisRow.state === 'deleted' || column.state === 'deleted') cell.className = 'axis-tombstone cell-locked';
1031
+ else if ((axisRow.state === 'added' && axisRow.incoming) || (column.state === 'added' && column.incoming)) cell.className = 'axis-added-incoming cell-locked';
1032
+ else if (axisRow.state === 'added' || column.state === 'added') cell.className = 'cell-no-conflict cell-locked';
1033
+ else if (cellData && cellData.changed) {
1034
+ const choice = target.cells[cellData.id] || defaultDisplayChoice(cellData);
1035
+ const selectable = structuralCellSelectable(file, sheet, cellData);
1036
+ cell.className = (choice === 'ours' ? 'cell-local' : 'cell-remote') + (selectable ? ' cell-selectable' : ' cell-locked');
1037
+ } else cell.className = 'cell-no-conflict' + (structurallyLocked ? ' cell-locked' : '');
1038
+ if (cellData) {
1039
+ cell.textContent = fmtVal(structuralCellValue(target, cellData));
1040
+ if (structuralCellSelectable(file, sheet, cellData)) {
1041
+ cell.onclick = () => cycleCell(file, sheet, cellData);
1042
+ if (cellData.resolution === 'conflict') cell.dataset.conflictId = cellData.id;
1043
+ }
1044
+ bindStructuralTip(cell, file, sheet, axisRow, column, cellData);
1045
+ }
1046
+ row.append(cell);
1047
+ }
1048
+ body.append(row);
1049
+ }
1050
+ table.append(body); host.append(table); if (rows.length > PAGE_SIZE) renderPager(host, rows.length);
1051
+ }
1052
+ function renderLegacyDiffTable(host, file, sheet) {
1053
+ const changedCells = sheet.cells || [];
1054
+ if (!changedCells.length) {
1055
+ renderMessage(host, sheet.name, '该 Sheet 没有单元格差异。');
1056
+ return;
1057
+ }
1058
+ const maxCol = sheet.maxCol || 1;
1059
+ const changedMap = new Map(changedCells.map(cell => [`${cell.row},${cell.col}`, cell]));
1060
+ const changedRows = [...new Set(changedCells.map(cell => cell.row))].sort((a, b) => a - b);
1061
+ const a1 = gridVal(sheet, 1, 1);
1062
+ const isVertical = typeof a1 === 'string' && a1.startsWith('##column##var');
1063
+ const headerRows = [];
1064
+ if (!isVertical) {
1065
+ for (let row = 1; row <= 20; row++) {
1066
+ const value = gridVal(sheet, row, 1);
1067
+ if (typeof value === 'string' && value.startsWith('##')) headerRows.push(row);
1068
+ else if (headerRows.length) break;
1069
+ }
1070
+ }
1071
+
1072
+ const table = document.createElement('table'); table.className = 'diff-table';
1073
+ const head = document.createElement('thead');
1074
+ const letters = document.createElement('tr'); appendHeaderCell(letters, '行');
1075
+ for (let col = 1; col <= maxCol; col++) appendHeaderCell(letters, colLabel(col), () => bulkCol(col));
1076
+ head.append(letters);
1077
+ if (isVertical) {
1078
+ const meanings = {1: '字段名 ##var', 2: '类型 ##type', 3: '注释'};
1079
+ const row = document.createElement('tr'); row.className = 'luban-header'; appendHeaderCell(row, '列义');
1080
+ for (let col = 1; col <= maxCol; col++) appendHeaderCell(row, meanings[col] || '');
1081
+ head.append(row);
1082
+ } else {
1083
+ for (const headerRow of headerRows) {
1084
+ const row = document.createElement('tr'); row.className = 'luban-header'; appendHeaderCell(row, String(headerRow));
1085
+ for (let col = 1; col <= maxCol; col++) {
1086
+ const cell = appendHeaderCell(row, fmtVal(gridVal(sheet, headerRow, col)));
1087
+ bindTip(cell, headerRow, col);
1088
+ }
1089
+ head.append(row);
1090
+ }
1091
+ }
1092
+ table.append(head);
1093
+
1094
+ const body = document.createElement('tbody');
1095
+ const start = currentPage * PAGE_SIZE;
1096
+ const end = Math.min(changedRows.length, start + PAGE_SIZE);
1097
+ const target = sheetDecision(file, sheet);
1098
+ for (let index = start; index < end; index++) {
1099
+ const rowNumber = changedRows[index];
1100
+ const row = document.createElement('tr');
1101
+ appendHeaderCell(row, String(rowNumber), () => bulkRow(rowNumber), 'row-header');
1102
+ for (let col = 1; col <= maxCol; col++) {
1103
+ const key = `${rowNumber},${col}`;
1104
+ const changed = changedMap.get(key);
1105
+ const cell = document.createElement('td');
1106
+ if (changed) {
1107
+ const choice = target.cells[changed.coordinate] || defaultCellChoice(changed);
1108
+ cell.className = choice === 'ours' ? 'cell-local' : 'cell-remote';
1109
+ cell.textContent = fmtVal(choice === 'ours' ? changed.ours : changed.theirs);
1110
+ cell.onclick = () => cycleCell(file, sheet, changed);
1111
+ if (changed.resolution === 'conflict') cell.dataset.conflictId = changed.id || changed.coordinate;
1112
+ } else {
1113
+ cell.className = 'cell-no-conflict';
1114
+ cell.textContent = fmtVal(gridVal(sheet, rowNumber, col));
1115
+ }
1116
+ bindTip(cell, rowNumber, col);
1117
+ row.append(cell);
1118
+ }
1119
+ body.append(row);
1120
+ }
1121
+ table.append(body); host.append(table);
1122
+ if (changedRows.length > PAGE_SIZE) renderPager(host, changedRows.length);
1123
+ }
1124
+ function renderPager(host, totalRows) {
1125
+ const pages = Math.ceil(totalRows / PAGE_SIZE);
1126
+ const pager = document.createElement('div'); pager.className = 'pager';
1127
+ for (let page = 0; page < pages; page++) {
1128
+ const button = document.createElement('button');
1129
+ button.className = 'btn' + (page === currentPage ? ' btn-local' : '');
1130
+ button.textContent = String(page + 1);
1131
+ button.onclick = () => { currentPage = page; renderTable(); };
1132
+ pager.append(button);
1133
+ }
1134
+ host.append(pager);
1135
+ }
1136
+
1137
+ function changedCellAt(sheet, row, col) { return (sheet.cells || []).find(cell => cell.row === row && cell.col === col); }
1138
+ function bindStructuralTip(cell, file, sheet, row, column, data) {
1139
+ cell.onmouseenter = event => showStructuralTip(event, file, sheet, row, column, data);
1140
+ cell.onmousemove = event => positionTip(event); cell.onmouseleave = hideTip;
1141
+ }
1142
+ function addTipLine(host, className, label, value, author, date) {
1143
+ const line = document.createElement('div'); line.className = className;
1144
+ line.append(document.createTextNode(`${label}: ${fmtVal(value)}`));
1145
+ const metaParts = [];
1146
+ if (author) metaParts.push(`作者: ${author}`);
1147
+ if (date) metaParts.push(date);
1148
+ if (metaParts.length) { const span = document.createElement('span'); span.className = 'author'; span.textContent = metaParts.join(' '); line.append(span); }
1149
+ host.append(line);
1150
+ }
1151
+ function showTip(event, row, col) {
1152
+ const file = currentFileData(); const sheet = currentSheetData(); const changed = changedCellAt(sheet, row, col);
1153
+ const tip = byId('tooltip'); tip.replaceChildren();
1154
+ const label = document.createElement('div'); label.className = 'label';
1155
+ label.textContent = `${file.path} / ${sheet.name} / 第 ${row} 行, ${colLabel(col)} 列`;
1156
+ tip.append(label);
1157
+ if (changed) {
1158
+ const choice = sheetDecision(file, sheet).cells[changed.coordinate] || defaultCellChoice(changed);
1159
+ addTipLine(tip, 'val-base', '基线', changed.base, '');
1160
+ addTipLine(tip, 'val-local', '本地', changed.ours, file.oursAuthor, file.oursDate);
1161
+ addTipLine(tip, 'val-remote', '远端', changed.theirs, file.theirsAuthor, file.theirsDate);
1162
+ addTipLine(tip, 'val-current', '当前选择', choice === 'ours' ? '本地' : '远端', '');
1163
+ if (changed.resolution !== 'conflict') addTipLine(tip, 'label', '三方建议', changed.resolution === 'ours' ? '自动采用本地' : '自动采用远端', '');
1164
+ } else {
1165
+ addTipLine(tip, 'val-current', '值', gridVal(sheet, row, col), '');
1166
+ }
1167
+ tip.style.display = 'block'; positionTip(event);
1168
+ }
1169
+ function showStructuralTip(event, file, sheet, row, column, cell) {
1170
+ const tip = byId('tooltip'); tip.replaceChildren();
1171
+ const label = document.createElement('div'); label.className = 'label';
1172
+ label.textContent = `${file.path} / ${sheet.name} / ${row.id} / ${column.id}`; tip.append(label);
1173
+ addTipLine(tip, 'val-base', '基线', cell.base, '');
1174
+ addTipLine(tip, 'val-local', '本地', cell.ours, file.oursAuthor, file.oursDate);
1175
+ addTipLine(tip, 'val-remote', '远端', cell.theirs, file.theirsAuthor, file.theirsDate);
1176
+ const choice = sheetDecision(file, sheet).cells[cell.id] || defaultDisplayChoice(cell);
1177
+ addTipLine(tip, 'val-current', '当前选择', choice === 'ours' ? '本地' : '远端', '');
1178
+ addTipLine(tip, 'label', '行来源', axisSource(row, 'rows'), '');
1179
+ addTipLine(tip, 'label', '列来源', axisSource(column, 'columns'), '');
1180
+ if (row.state !== 'common') addTipLine(tip, 'label', '行状态', row.status, '');
1181
+ if (column.state !== 'common') addTipLine(tip, 'label', '列状态', column.status, '');
1182
+ addTipLine(tip, 'label', '交互', structuralCellSelectable(file, sheet, cell) ? '可点击切换版本' : '结构已锁定', '');
1183
+ tip.style.display = 'block'; positionTip(event);
1184
+ }
1185
+ function positionTip(event) {
1186
+ const tip = byId('tooltip');
1187
+ const left = Math.min(event.clientX + 12, window.innerWidth - tip.offsetWidth - 12);
1188
+ const top = Math.min(event.clientY + 12, window.innerHeight - tip.offsetHeight - 12);
1189
+ tip.style.left = `${Math.max(8, left)}px`; tip.style.top = `${Math.max(8, top)}px`;
1190
+ }
1191
+ function hideTip() { byId('tooltip').style.display = 'none'; }
1192
+
1193
+ function pushCellUndo(file, sheet, cell) {
1194
+ const target = sheetDecision(file, sheet); const key = cellKey(cell); const previous = target.cells[key];
1195
+ undoStack.push({kind: 'cell', fileId: file.id, sheet: sheet.name, key, previous});
1196
+ }
1197
+ function cycleCell(file, sheet, cell) {
1198
+ const target = sheetDecision(file, sheet); pushCellUndo(file, sheet, cell);
1199
+ const key = cellKey(cell);
1200
+ target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1201
+ renderTable(); renderConflictNavigation(); updateStats();
1202
+ }
1203
+ function chooseSheet(file, sheet, side) {
1204
+ const target = sheetDecision(file, sheet);
1205
+ undoStack.push({kind: 'sheet', fileId: file.id, sheet: sheet.name, previous: target.sheet});
1206
+ target.sheet = side; renderTable(); updateStats();
1207
+ }
1208
+ function chooseRename(file, sheet, side) {
1209
+ const target = sheetDecision(file, sheet);
1210
+ undoStack.push({kind: 'rename', fileId: file.id, sheet: sheet.name, previous: target.rename});
1211
+ target.rename = side; renderSheetChoice(); updateStats();
1212
+ }
1213
+ function chooseMergedCells(file, sheet, side) {
1214
+ const target = sheetDecision(file, sheet);
1215
+ undoStack.push({kind: 'mergedCells', fileId: file.id, sheet: sheet.name, previous: target.mergedCells});
1216
+ target.mergedCells = side; renderSheetChoice(); renderTable(); updateStats();
1217
+ }
1218
+ function toggleAxis(file, sheet, kind, axis) {
1219
+ const target = sheetDecision(file, sheet); const previous = target[kind][axis.id] || axis.default;
1220
+ undoStack.push({kind: 'axis', fileId: file.id, sheet: sheet.name, axisKind: kind, axisId: axis.id, previous});
1221
+ target[kind][axis.id] = previous === 'keep' ? 'delete' : 'keep';
1222
+ renderSheetTabs(); renderTable(); renderConflictNavigation(); updateStats();
1223
+ }
1224
+ function chooseMacro(file, side) {
1225
+ const target = fileDecision(file); const previous = target.macro || file.macro.default;
1226
+ if (side === 'theirs' && !file.macro.canChooseTheirs) return;
1227
+ undoStack.push({kind: 'macro', fileId: file.id, previous});
1228
+ target.macro = side; render();
1229
+ }
1230
+ function sideAxisChoice(axis, side) {
1231
+ const key = side === 'ours' ? ['oursRow', 'oursCol'] : ['theirsRow', 'theirsCol'];
1232
+ return key.some(name => axis[name] !== undefined && axis[name] !== null) ? 'keep' : 'delete';
1233
+ }
1234
+ function bulkChoose(side) {
1235
+ const file = currentFileData(); const sheet = currentSheetData(); if (!sheet) return;
1236
+ if (currentSheetIsConfirmed()) return;
1237
+ if (sheet.action.startsWith('conflict_')) { chooseSheet(file, sheet, side); return; }
1238
+ const target = sheetDecision(file, sheet);
1239
+ if (sheet.renameStatus === 'rename_conflict') {
1240
+ undoStack.push({kind: 'rename', fileId: file.id, sheet: sheet.name, previous: target.rename});
1241
+ target.rename = side;
1242
+ }
1243
+ if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') {
1244
+ undoStack.push({kind: 'mergedCells', fileId: file.id, sheet: sheet.name, previous: target.mergedCells});
1245
+ target.mergedCells = side;
1246
+ }
1247
+ for (const [kind, axes] of [['rows', sheet.allRows || sheet.rows || []], ['columns', sheet.columns || []]]) {
1248
+ for (const axis of axes) if (axis.state === 'delete_modify') {
1249
+ undoStack.push({kind: 'axis', fileId: file.id, sheet: sheet.name, axisKind: kind, axisId: axis.id, previous: target[kind][axis.id]});
1250
+ target[kind][axis.id] = sideAxisChoice(axis, side);
1251
+ }
1252
+ }
1253
+ for (const cell of sheet.cells || []) {
1254
+ const key = cellKey(cell); const selected = side === 'ours' ? cell.oursPresent !== false : cell.theirsPresent !== false;
1255
+ if (!selected) continue;
1256
+ pushCellUndo(file, sheet, cell); target.cells[key] = side;
1257
+ }
1258
+ render();
1259
+ }
1260
+ function bulkStructuralRow(rowId) {
1261
+ const file = currentFileData(); const sheet = currentSheetData(); const target = sheetDecision(file, sheet);
1262
+ const row = (sheet.allRows || []).find(item => item.id === rowId);
1263
+ if (!row || !axisKept(file, sheet, 'rows', row)) return;
1264
+ for (const cell of (sheet.cells || []).filter(item => item.rowId === rowId)) {
1265
+ const column = (sheet.columns || []).find(item => item.id === cell.colId);
1266
+ if (!column || !axisKept(file, sheet, 'columns', column) || row.locked || column.locked) continue;
1267
+ pushCellUndo(file, sheet, cell); const key = cellKey(cell);
1268
+ target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1269
+ }
1270
+ renderTable(); renderConflictNavigation(); updateStats();
1271
+ }
1272
+ function bulkStructuralColumn(columnId) {
1273
+ const file = currentFileData(); const sheet = currentSheetData(); const target = sheetDecision(file, sheet);
1274
+ const column = (sheet.columns || []).find(item => item.id === columnId);
1275
+ if (!column || !axisKept(file, sheet, 'columns', column)) return;
1276
+ for (const cell of (sheet.cells || []).filter(item => item.colId === columnId)) {
1277
+ const row = (sheet.allRows || []).find(item => item.id === cell.rowId);
1278
+ if (!row || !axisKept(file, sheet, 'rows', row) || row.locked || column.locked) continue;
1279
+ pushCellUndo(file, sheet, cell); const key = cellKey(cell);
1280
+ target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1281
+ }
1282
+ renderTable(); renderConflictNavigation(); updateStats();
1283
+ }
1284
+ function bulkRow(row) {
1285
+ const file = currentFileData(); const sheet = currentSheetData();
1286
+ for (const cell of (sheet.cells || []).filter(item => item.row === row)) {
1287
+ const target = sheetDecision(file, sheet); pushCellUndo(file, sheet, cell);
1288
+ const key = cellKey(cell); target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1289
+ }
1290
+ renderTable(); renderConflictNavigation(); updateStats();
1291
+ }
1292
+ function bulkCol(col) {
1293
+ const file = currentFileData(); const sheet = currentSheetData();
1294
+ for (const cell of (sheet.cells || []).filter(item => item.col === col)) {
1295
+ const target = sheetDecision(file, sheet); pushCellUndo(file, sheet, cell);
1296
+ const key = cellKey(cell); target.cells[key] = target.cells[key] === 'ours' ? 'theirs' : 'ours';
1297
+ }
1298
+ renderTable(); renderConflictNavigation(); updateStats();
1299
+ }
1300
+ function undo() {
1301
+ const file = currentFileData(); const sheet = currentSheetData();
1302
+ if (!file || !sheet) return;
1303
+ let actionIndex = -1;
1304
+ for (let index = undoStack.length - 1; index >= 0; index--) {
1305
+ const candidate = undoStack[index];
1306
+ if (candidate.fileId === file.id && candidate.sheet === sheet.name) {
1307
+ actionIndex = index;
1308
+ break;
1309
+ }
1310
+ }
1311
+ if (actionIndex < 0) return;
1312
+ const [action] = undoStack.splice(actionIndex, 1);
1313
+ const actionFile = model.files.find(item => item.id === action.fileId);
1314
+ const actionSheet = actionFile.diff.sheets.find(item => item.name === action.sheet);
1315
+ const target = sheetDecision(actionFile, actionSheet);
1316
+ target.confirmed = false;
1317
+ if (action.kind === 'sheet') target.sheet = action.previous;
1318
+ else if (action.kind === 'rename') target.rename = action.previous;
1319
+ else if (action.kind === 'mergedCells') target.mergedCells = action.previous;
1320
+ else if (action.kind === 'axis') target[action.axisKind][action.axisId] = action.previous;
1321
+ else target.cells[action.key] = action.previous;
1322
+ render();
1323
+ }
1324
+ function resetSheet() {
1325
+ const file = currentFileData();
1326
+ const sheet = currentSheetData();
1327
+ if (!file || !sheet) return;
1328
+ const target = sheetDecision(file, sheet); const sheetChoice = defaultSheetChoice(sheet);
1329
+ target.confirmed = false;
1330
+ delete target.lastNavigatedConflict;
1331
+ if (sheetChoice) target.sheet = sheetChoice; else delete target.sheet;
1332
+ if (sheet.renameStatus === 'rename_conflict') target.rename = 'ours'; else delete target.rename;
1333
+ if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') target.mergedCells = sheet.mergedCells.default || 'ours'; else delete target.mergedCells;
1334
+ target.rows = {}; target.columns = {}; target.cells = {};
1335
+ for (const row of sheet.allRows || sheet.rows || []) if (row.state === 'delete_modify') target.rows[row.id] = row.default;
1336
+ for (const column of sheet.columns || []) if (column.state === 'delete_modify') target.columns[column.id] = column.default;
1337
+ for (const cell of sheet.cells || []) target.cells[cellKey(cell)] = defaultCellChoice(cell);
1338
+ // A reset supersedes prior choices for this Sheet, while preserving other files,
1339
+ // other Sheets, and file-level macro choices in the shared undo history.
1340
+ undoStack = undoStack.filter(action => action.fileId !== file.id || action.sheet !== sheet.name);
1341
+ render();
1342
+ }
1343
+ function updateGlobalConfirmationStats() {
1344
+ let confirmed = 0; let total = 0;
1345
+ for (const file of model.files) for (const sheet of file.diff.sheets) {
1346
+ if (!sheetNeedsConfirmation(sheet)) continue;
1347
+ total++;
1348
+ if (sheetDecision(file, sheet).confirmed) confirmed++;
1349
+ }
1350
+ byId('confirmedSheetCount').textContent = String(confirmed);
1351
+ byId('totalConflictSheetCount').textContent = String(total);
1352
+ }
1353
+ function updateDecisionFooterStats() {
1354
+ let total = 0; let local = 0;
1355
+ for (const file of model.files) {
1356
+ if (file.macro && file.macro.status === 'conflict') {
1357
+ total++; if ((fileDecision(file).macro || file.macro.default) === 'ours') local++;
1358
+ }
1359
+ for (const sheet of file.diff.sheets) {
1360
+ const target = sheetDecision(file, sheet);
1361
+ if (sheet.action.startsWith('conflict_')) { total++; if (target.sheet === 'ours') local++; }
1362
+ if (sheet.renameStatus === 'rename_conflict') { total++; if (target.rename === 'ours') local++; }
1363
+ if (sheet.mergedCells && sheet.mergedCells.status === 'conflict') { total++; if (target.mergedCells === 'ours') local++; }
1364
+ for (const row of sheet.allRows || sheet.rows || []) if (row.state === 'delete_modify') {
1365
+ total++; const oursChoice = row.oursRow !== null && row.oursRow !== undefined ? 'keep' : 'delete';
1366
+ if ((target.rows[row.id] || row.default) === oursChoice) local++;
1367
+ }
1368
+ for (const column of sheet.columns || []) if (column.state === 'delete_modify') {
1369
+ total++; const oursChoice = column.oursCol !== null && column.oursCol !== undefined ? 'keep' : 'delete';
1370
+ if ((target.columns[column.id] || column.default) === oursChoice) local++;
1371
+ }
1372
+ for (const cell of sheet.cells || []) {
1373
+ if (sheet.columns && sheet.allRows && !structuralCellSelectable(file, sheet, cell)) continue;
1374
+ total++; if (target.cells[cellKey(cell)] === 'ours') local++;
1375
+ }
1376
+ }
1377
+ }
1378
+ const file = currentFileData(); const sheet = currentSheetData();
1379
+ byId('footerStatus').textContent = `共 ${total} 个可决策项,${local} 项采用本地 · 当前:${file.path}${sheet ? ` / ${sheet.name}` : ''}${autoTheirsNote(' · ')}`;
1380
+ }
1381
+ function updateStats() {
1382
+ updateGlobalConfirmationStats();
1383
+ updateDecisionFooterStats();
1384
+ }
1385
+
1386
+ function renderCommitSteps(steps) {
1387
+ const host = byId('commitModalSteps'); host.replaceChildren();
1388
+ for (const step of steps || []) {
1389
+ const line = document.createElement('div');
1390
+ line.textContent = `${step.success ? '✓' : '✗'} ${step.step || ''}${step.output ? ` — ${String(step.output).slice(0, 240)}` : ''}`;
1391
+ host.append(line);
1392
+ }
1393
+ }
1394
+ function showCommitModal(kind, title, body, steps, buttons) {
1395
+ const modal = byId('commitModal'); modal.classList.remove('is-hidden', 'is-success', 'is-error');
1396
+ if (kind) modal.classList.add(`is-${kind}`);
1397
+ byId('commitModalTitle').textContent = title; byId('commitModalBody').textContent = body || ''; renderCommitSteps(steps);
1398
+ const actions = byId('commitModalActions'); actions.replaceChildren();
1399
+ for (const spec of buttons || []) { const button = document.createElement('button'); button.className = `btn ${spec.className || ''}`; button.textContent = spec.label; button.onclick = spec.onclick; actions.append(button); }
1400
+ }
1401
+ function closeCommitModal() { byId('commitModal').classList.add('is-hidden'); }
1402
+ async function cancelAndExit() {
1403
+ const cancelButton = byId('cancelBtn');
1404
+ const submitButton = byId('submitBtn');
1405
+ cancelButton.disabled = true;
1406
+ submitButton.disabled = true;
1407
+ byId('footerStatus').textContent = '正在取消并退出,不会写入任何更改...';
1408
+ try {
1409
+ const response = await fetch('/api/shutdown', {method: 'POST'});
1410
+ const result = await response.json();
1411
+ if (!response.ok || !result.success) throw new Error(result.error || '无法关闭解析器');
1412
+ byId('footerStatus').textContent = '已取消,未写入任何更改。';
1413
+ window.close();
1414
+ } catch (error) {
1415
+ cancelButton.disabled = false;
1416
+ submitButton.disabled = false;
1417
+ byId('footerStatus').textContent = `退出失败: ${error.message}`;
1418
+ }
1419
+ }
1420
+ function renderCommitMessage(text) {
1421
+ const host = byId('commitModalSteps'); host.replaceChildren();
1422
+ const line = document.createElement('div');
1423
+ line.textContent = text || '(无冲突 Cell,将使用默认提交信息)';
1424
+ host.append(line);
1425
+ }
1426
+ function unconfirmedSheets() {
1427
+ const pending = [];
1428
+ for (const [fileIndex, file] of model.files.entries()) {
1429
+ for (const [sheetIndex, sheet] of file.diff.sheets.entries()) {
1430
+ if (!sheetNeedsConfirmation(sheet) || sheetDecision(file, sheet).confirmed) continue;
1431
+ pending.push({file, sheet, fileIndex, sheetIndex, label: `${file.path}-${sheet.name}`});
1432
+ }
1433
+ }
1434
+ return pending;
1435
+ }
1436
+ function renderUnconfirmedSheetList(pending) {
1437
+ const host = byId('commitModalSteps'); host.replaceChildren();
1438
+ for (const item of pending) {
1439
+ const button = document.createElement('button');
1440
+ button.type = 'button';
1441
+ button.className = 'btn btn-cancel';
1442
+ button.style.display = 'block';
1443
+ button.style.width = '100%';
1444
+ button.style.marginBottom = '7px';
1445
+ button.style.textAlign = 'left';
1446
+ button.textContent = item.label;
1447
+ button.title = `切换到 ${item.label}`;
1448
+ button.onclick = () => {
1449
+ currentFile = item.fileIndex;
1450
+ currentSheet = item.sheetIndex;
1451
+ currentPage = 0;
1452
+ closeCommitModal();
1453
+ render();
1454
+ };
1455
+ host.append(button);
1456
+ }
1457
+ }
1458
+ async function submitResolve() {
1459
+ const button = byId('submitBtn');
1460
+ const pending = unconfirmedSheets();
1461
+ if (pending.length) {
1462
+ byId('footerStatus').textContent = `仍有 ${pending.length} 个 Sheet 未确认,请先完成审阅。`;
1463
+ showCommitModal('', '仍有未确认的 Sheet', '提交前必须人工勾选确认每个需要审阅的 Sheet。点击下方条目可直接切换过去。', [], [
1464
+ {label: '返回继续审阅', className: 'btn-submit', onclick: () => { closeCommitModal(); button.disabled = false; }}
1465
+ ]);
1466
+ renderUnconfirmedSheetList(pending);
1467
+ return;
1468
+ }
1469
+ button.disabled = true;
1470
+ byId('footerStatus').textContent = '正在生成 commit 信息预览...';
1471
+ try {
1472
+ const previewResponse = await fetch('/api/commit-preview', {
1473
+ method: 'POST', headers: {'Content-Type': 'application/json'},
1474
+ body: JSON.stringify({decisions})
1475
+ });
1476
+ const preview = await previewResponse.json();
1477
+ if (!previewResponse.ok || preview.error) throw new Error(preview.error || '无法生成 commit 信息');
1478
+ // 居中二次确认:展示 commit 信息列表(可滑动),确认后才真正执行 add / commit
1479
+ showCommitModal('', '确认提交', '请核对以下 commit 信息,确认后将执行 git add / commit:', [], [
1480
+ {label: '取消', className: '', onclick: () => { closeCommitModal(); button.disabled = false; byId('footerStatus').textContent = '已取消,未提交任何更改。'; }},
1481
+ {label: '确认提交', className: 'btn-submit', onclick: () => { closeCommitModal(); doSubmitResolve(); }}
1482
+ ]);
1483
+ renderCommitMessage(preview.message || '');
1484
+ } catch (error) {
1485
+ button.disabled = false;
1486
+ byId('footerStatus').textContent = `错误: ${error.message}`;
1487
+ }
1488
+ }
1489
+ async function doSubmitResolve() {
1490
+ byId('footerStatus').textContent = '正在合并并提交...';
1491
+ showCommitModal('', '正在合并并提交...', '正在写回工作簿并执行 git add / commit,请稍候。', [], []);
1492
+ try {
1493
+ const response = await fetch('/api/resolve', {
1494
+ method: 'POST', headers: {'Content-Type': 'application/json'},
1495
+ body: JSON.stringify({decisions, commit: true, push: false})
1496
+ });
1497
+ const result = await response.json();
1498
+ if (!result.success) throw new Error(result.error || (result.steps || []).filter(step => !step.success).map(step => `${step.step}: ${step.output}`).join('\n') || '提交失败');
1499
+ byId('footerStatus').textContent = '完成!合并并提交成功。';
1500
+ showCommitModal('success', '提交成功', '多 Sheet 合并结果已写入并提交。解析器即将关闭。', result.steps, [
1501
+ {label: '关闭窗口', className: 'btn-submit', onclick: () => { try { window.close(); } catch (_) {} }}
1502
+ ]);
1503
+ } catch (error) {
1504
+ const c = byId('submitBtn'); c.disabled = false; byId('footerStatus').textContent = `错误: ${error.message}`;
1505
+ showCommitModal('error', '提交失败', error.message, [], [{label: '关闭', onclick: closeCommitModal}]);
1506
+ }
1507
+ }
1508
+
1509
+ byId('allLocalBtn').onclick = () => bulkChoose('ours');
1510
+ byId('allRemoteBtn').onclick = () => bulkChoose('theirs');
1511
+ byId('undoBtn').onclick = undo;
1512
+ byId('resetBtn').onclick = resetSheet;
1513
+ byId('cancelBtn').onclick = cancelAndExit;
1514
+ byId('submitBtn').onclick = submitResolve;
1515
+ byId('prevConflictBtn').onclick = () => locateConflictCell(-1);
1516
+ byId('nextConflictBtn').onclick = () => locateConflictCell(1);
1517
+ enableHorizontalWheel(byId('fileTabs'));
1518
+ enableHorizontalWheel(byId('sheetTabs'));
1519
+ init().catch(error => { byId('footerStatus').textContent = `加载失败: ${error.message}`; });
1520
+ </script>
1521
+ </body>
1522
+ </html>