Skip to content

Commit 58837e3

Browse files
committed
fix: improve erasing logic to handle no content erased scenario
1 parent cae9c8f commit 58837e3

File tree

1 file changed

+68
-24
lines changed

1 file changed

+68
-24
lines changed

lib/src/view/notifier/scribble_notifier.dart

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,19 @@ class ScribbleNotifier extends ScribbleNotifierBase
337337
pointerPosition: _getPointFromEvent(event),
338338
);
339339
} else if (value is Erasing) {
340-
temporaryValue = _erasePoint(event).copyWith(
341-
pointerPosition: _getPointFromEvent(event),
342-
);
340+
final erasedState = _erasePoint(event);
341+
// Check if content was actually erased
342+
if (erasedState != null) {
343+
// Content was actually erased, add to undo stack
344+
value = erasedState.copyWith(
345+
pointerPosition: _getPointFromEvent(event),
346+
);
347+
} else {
348+
// No content erased, only update pointer position
349+
temporaryValue = value.copyWith(
350+
pointerPosition: _getPointFromEvent(event),
351+
);
352+
}
343353
}
344354
}
345355

@@ -356,11 +366,24 @@ class ScribbleNotifier extends ScribbleNotifierBase
356366
value.activePointerIds.where((id) => id != event.pointer).toList(),
357367
);
358368
} else if (value is Erasing) {
359-
value = _erasePoint(event).copyWith(
360-
pointerPosition: pos,
361-
activePointerIds:
362-
value.activePointerIds.where((id) => id != event.pointer).toList(),
363-
);
369+
final erasedState = _erasePoint(event);
370+
// Only update value when content was actually erased (affects undo stack)
371+
if (erasedState != null) {
372+
value = erasedState.copyWith(
373+
pointerPosition: pos,
374+
activePointerIds: value.activePointerIds
375+
.where((id) => id != event.pointer)
376+
.toList(),
377+
);
378+
} else {
379+
// No content erased, only update pointer position
380+
temporaryValue = value.copyWith(
381+
pointerPosition: pos,
382+
activePointerIds: value.activePointerIds
383+
.where((id) => id != event.pointer)
384+
.toList(),
385+
);
386+
}
364387
}
365388
}
366389

@@ -375,11 +398,24 @@ class ScribbleNotifier extends ScribbleNotifierBase
375398
value.activePointerIds.where((id) => id != event.pointer).toList(),
376399
);
377400
} else if (value is Erasing) {
378-
value = _erasePoint(event).copyWith(
379-
pointerPosition: null,
380-
activePointerIds:
381-
value.activePointerIds.where((id) => id != event.pointer).toList(),
382-
);
401+
final erasedState = _erasePoint(event);
402+
// Only update value when content was actually erased (affects undo stack)
403+
if (erasedState != null) {
404+
value = erasedState.copyWith(
405+
pointerPosition: null,
406+
activePointerIds: value.activePointerIds
407+
.where((id) => id != event.pointer)
408+
.toList(),
409+
);
410+
} else {
411+
// No content erased, only update pointer position
412+
temporaryValue = value.copyWith(
413+
pointerPosition: null,
414+
activePointerIds: value.activePointerIds
415+
.where((id) => id != event.pointer)
416+
.toList(),
417+
);
418+
}
383419
}
384420
}
385421

@@ -411,17 +447,25 @@ class ScribbleNotifier extends ScribbleNotifierBase
411447
);
412448
}
413449

414-
ScribbleState _erasePoint(PointerEvent event) {
415-
return value.copyWith.sketch(
416-
lines: value.sketch.lines
417-
.where(
418-
(l) => l.points.every(
419-
(p) =>
420-
(event.localPosition - p.asOffset).distance >
421-
l.width + value.selectedWidth,
422-
),
423-
)
424-
.toList(),
450+
ScribbleState? _erasePoint(PointerEvent event) {
451+
final filteredLines = value.sketch.lines
452+
.where(
453+
(l) => l.points.every(
454+
(p) =>
455+
(event.localPosition - p.asOffset).distance >
456+
l.width + value.selectedWidth,
457+
),
458+
)
459+
.toList();
460+
// If no lines were erased, return null to avoid unnecessary state updates
461+
if (filteredLines.length == value.sketch.lines.length) {
462+
return null;
463+
}
464+
465+
return value.copyWith(
466+
sketch: value.sketch.copyWith(
467+
lines: filteredLines,
468+
),
425469
);
426470
}
427471

0 commit comments

Comments
 (0)