logisheets_controller 1.16.1

Core spreadsheet engine for LogiSheets: formula evaluation, dependency tracking, and undo/redo.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use std::collections::{HashMap, HashSet};

use logisheets_base::{BlockId, CellId, ColId, RowId, SheetId, errors::BasicError};

use crate::{
    Error,
    controller::status::Status,
    edit_action::{EditPayload, PayloadsAction},
};

use super::ctx::{VersionExecCtx, VersionExecCtxImpl};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Diff {
    CellValue(CellId),
    CellStyle(CellId),
    RowInfo(RowId),
    ColInfo(ColId),
    BlockUpdate { sheet_id: SheetId, id: BlockId },
    SheetProperty, // like tab color and hidden
    Unavailable,
}

#[derive(Debug, Clone, Default)]
pub struct SheetDiff {
    pub data: HashSet<Diff>,
}

impl SheetDiff {
    pub fn insert_diff(&mut self, diff: Diff) {
        if diff == Diff::Unavailable {
            self.data = HashSet::from([diff]);
        } else if !self.data.contains(&Diff::Unavailable) {
            self.data.insert(diff);
        }
    }

    pub fn diff_unavailable(&self) -> bool {
        self.data.contains(&Diff::Unavailable)
    }
}

// Turning `Process` into `SheetDiff` is for recording the `id` rather than `idx`.
pub fn convert_payloads_to_sheet_diff(
    status: &mut Status,
    process: PayloadsAction,
    updated_cells: HashSet<(SheetId, CellId)>,
) -> HashMap<SheetId, SheetDiff> {
    let mut result: HashMap<SheetId, SheetDiff> = HashMap::new();
    let ctx = VersionExecCtxImpl::new(status);

    process.payloads.into_iter().for_each(|sp| {
        if let Ok(Some((diff, sheet_id))) = convert_diff(sp, &ctx) {
            let sheet_diff = result.entry(sheet_id).or_insert(SheetDiff::default());
            sheet_diff.insert_diff(diff);
        }
    });

    updated_cells.into_iter().for_each(|(sheet_id, cell_id)| {
        let diff = Diff::CellValue(cell_id);
        let sheet_diff = result.entry(sheet_id).or_insert(SheetDiff::default());
        sheet_diff.insert_diff(diff);
    });

    result
}

#[inline]
fn convert_diff<C: VersionExecCtx>(
    payload: EditPayload,
    ctx: &C,
) -> Result<Option<(Diff, SheetId)>, Error> {
    match payload {
        EditPayload::UpsertFieldRenderInfo(_) => Ok(None),
        // An enum set is workbook-level metadata, not cell content. What
        // changing one CAN affect is the membership rule of every field that
        // declares it — those are per-record shadows, dirtied through the
        // schema, not through this cell-level diff.
        EditPayload::UpsertEnumSet(_) => Ok(None),
        EditPayload::RemoveEnumSet(_) => Ok(None),
        // A defined name is workbook-level too. The cells whose values change
        // because of it are recalculated, and reach the diff that way.
        EditPayload::DefineName(_) | EditPayload::RenameName(_) | EditPayload::RemoveName(_) => {
            Ok(None)
        }
        // A conditional-formatting edit changes how the whole covered range
        // renders, and the range is not known here without resolving anchors, so
        // the sheet is marked wholly stale rather than diffed cell by cell.
        EditPayload::CreateConditionalFormattingRule(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::UpdateConditionalFormattingRule(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::MoveConditionalFormattingRule(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::DeleteConditionalFormattingRule(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::BlockInput(bi) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(bi.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let cell_id = ctx.fetch_block_cell_id(&sheet_id, &bi.block_id, bi.row, bi.col)?;
            Ok(Some((
                Diff::CellValue(CellId::BlockCell(cell_id)),
                sheet_id,
            )))
        }
        EditPayload::MoveBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::RemoveBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::CreateBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        // Metadata only: no cell changes value or moves, so the host just
        // needs to know the block itself is different.
        EditPayload::SetBlockDescription(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        // Re-pointing a block at a different source changes what every one of
        // its cells computes, so the whole block is stale — same as a
        // permissions or description change, which also alter the block
        // wholesale rather than any one cell.
        EditPayload::SetBlockAnalyzes(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::SetBlockPermissions(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::ConvertBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::CreateLink(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::CellStyleUpdate(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let id = ctx.fetch_cell_id(&sheet_id, p.row, p.col)?;
            Ok(Some((Diff::CellStyle(id), sheet_id)))
        }
        EditPayload::BlockStyleUpdate(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let id = ctx.fetch_block_cell_id(&sheet_id, &p.block_id, p.row, p.col)?;
            Ok(Some((Diff::CellStyle(CellId::BlockCell(id)), sheet_id)))
        }
        EditPayload::CellInput(ci) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(ci.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let cell_id = ctx.fetch_cell_id(&sheet_id, ci.row, ci.col)?;
            Ok(Some((Diff::CellValue(cell_id), sheet_id)))
        }
        EditPayload::SetCellImage(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let cell_id = ctx.fetch_cell_id(&sheet_id, p.row, p.col)?;
            Ok(Some((Diff::CellValue(cell_id), sheet_id)))
        }
        EditPayload::DeleteCellImage(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let cell_id = ctx.fetch_cell_id(&sheet_id, p.row, p.col)?;
            Ok(Some((Diff::CellValue(cell_id), sheet_id)))
        }
        EditPayload::MoveChart(p) => {
            // No chart-specific Diff variant; a move re-anchors a floating
            // object, so mark the sheet as needing a full redraw.
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::DeleteChart(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::CreateChart(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::UpdateChart(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::SetColWidth(col) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(col.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let col_id = ctx.fetch_col_id(&sheet_id, col.col)?;
            Ok(Some((Diff::ColInfo(col_id), sheet_id)))
        }
        EditPayload::SetRowHeight(row) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(row.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let row_id = ctx.fetch_row_id(&sheet_id, row.row)?;
            Ok(Some((Diff::RowInfo(row_id), sheet_id)))
        }
        EditPayload::SetVisible(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::SheetProperty, sheet_id)))
        }
        EditPayload::SheetRename(_) => Ok(Some((Diff::SheetProperty, 0))),
        EditPayload::CreateSheet(_) => Ok(Some((Diff::SheetProperty, 0))),
        EditPayload::DeleteSheet(_) => Ok(Some((Diff::SheetProperty, 0))),
        EditPayload::InsertCols(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::DeleteCols(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::InsertRows(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::DeleteRows(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::InsertColsInBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::DeleteColsInBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::InsertRowsInBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::DeleteRowsInBlock(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::CellClear(cr) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(cr.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let cell_id = ctx.fetch_cell_id(&sheet_id, cr.row, cr.col)?;
            Ok(Some((Diff::CellValue(cell_id), sheet_id)))
        }
        EditPayload::LineStyleUpdate(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::MergeCells(p) => {
            // TODO
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::SplitMergedCells(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::CellFormatBrush(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.dst_sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::LineFormatBrush(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.dst_sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::EphemeralCellStyleUpdate(_) => Ok(None),
        EditPayload::EphemeralCellInput(_) => Ok(None),
        EditPayload::EphemeralCellRemove(_) => Ok(None),
        EditPayload::CreateDiyCell(create_diy_cell) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(create_diy_cell.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let row = create_diy_cell.row;
            let col = create_diy_cell.col;
            let cell_id = ctx
                .fetch_cell_id(&sheet_id, row, col)
                .map_err(|_| BasicError::CellIdNotFound(row, col))?;
            Ok(Some((Diff::CellValue(cell_id), sheet_id)))
        }
        EditPayload::CreateDiyCellById(p) => Ok(Some((
            Diff::BlockUpdate {
                sheet_id: p.sheet_id,
                id: p.block_id,
            },
            p.sheet_id,
        ))),
        EditPayload::RemoveDiyCell(remove_diy_cell) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(remove_diy_cell.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            let row = remove_diy_cell.row;
            let col = remove_diy_cell.col;
            let cell_id = ctx
                .fetch_cell_id(&sheet_id, row, col)
                .map_err(|_| BasicError::CellIdNotFound(row, col))?;
            Ok(Some((Diff::CellValue(cell_id), sheet_id)))
        }
        EditPayload::RemoveDiyCellById(p) => Ok(Some((
            Diff::BlockUpdate {
                sheet_id: p.sheet_id,
                id: p.block_id,
            },
            p.sheet_id,
        ))),
        EditPayload::CreateAppendix(p) => {
            let sheet_id = if let Some(id) = p.sheet_id {
                id
            } else {
                let idx = p
                    .sheet_idx
                    .ok_or(BasicError::IncompletePayload("a sheetId or a sheetIdx"))?;
                ctx.fetch_sheet_id_by_index(idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?
            };
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::RemoveAppendix(p) => {
            let sheet_id = if let Some(id) = p.sheet_id {
                id
            } else {
                let idx = p
                    .sheet_idx
                    .ok_or(BasicError::IncompletePayload("a sheetId or a sheetIdx"))?;
                ctx.fetch_sheet_id_by_index(idx)
                    .map_err(|l| BasicError::SheetIdxExceed(l))?
            };
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::ResizeBlock(resize_block) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(resize_block.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: resize_block.id,
                },
                sheet_id,
            )))
        }
        EditPayload::ReproduceCells(rc) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(rc.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::SetSheetColor(_) => Ok(Some((Diff::SheetProperty, 0))),
        EditPayload::SetSheetVisible(_) => Ok(Some((Diff::SheetProperty, 0))),
        EditPayload::BlockLineStyleUpdate(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::BlockLineNameFieldUpdate(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::BindFormSchema(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::UpsertFieldFormulas(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::BindRandomSchema(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::MoveBlockLine(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        EditPayload::ReorderBlockLines(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((
                Diff::BlockUpdate {
                    sheet_id,
                    id: p.block_id,
                },
                sheet_id,
            )))
        }
        // RestoreCheckpoint wholesale-replaces Status. No per-sheet
        // diff is meaningful — the host's redraw path is signalled
        // via cell_updated + sheet_updated flags on the Executor
        // (set in executor.rs's special-case branch). Surface here
        // as "no diff to record", which is the conservative answer.
        EditPayload::RestoreCheckpoint(_) => Ok(None),
        // Comment mutations don't change cell values, but they alter the
        // comment overlay for the sheet. Record a conservative sheet-wide
        // diff so undo/redo re-renders comment indicators. `UpsertPerson`
        // is workbook-scoped with no per-sheet visual, so it records nothing.
        EditPayload::AddComment(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::EditComment(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::DeleteComment(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::ResolveComment(p) => {
            let sheet_id = ctx
                .fetch_sheet_id_by_index(p.sheet_idx)
                .map_err(|l| BasicError::SheetIdxExceed(l))?;
            Ok(Some((Diff::Unavailable, sheet_id)))
        }
        EditPayload::UpsertPerson(_) => Ok(None),
    }
}