Skip to content

Commit c344237

Browse files
committed
Fixed a bug when the text storage is being emptied at once
1 parent 6b88b67 commit c344237

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Sources/ParagraphTextKit/Structs/ParagraphRangeChange.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ internal extension ParagraphTextStorage {
5151
// then this is actually an exception from the rule that 'edited index will always be the first one'
5252
// and in this case the LAST index should be notified as 'edited index'
5353
if insertionLocation == baseParagraphRange.location && removedRange == baseParagraphRange && baseParagraphRange.max > 0 {
54-
if insertionRange.location == baseParagraphRange.location && difference.insertions.count > 1 ||
55-
removedRange.location == baseParagraphRange.location && difference.removals.count > 1 {
56-
lastIndexEdited = true
57-
break
54+
if insertionRange.location == baseParagraphRange.location && difference.insertions.count > 1 ||
55+
removedRange.location == baseParagraphRange.location && difference.removals.count > 1 {
56+
// except the case when the whole text storage content has been deleted...
57+
// ... like when the user selects all the text and hits 'Delete'
58+
if removedRange.max > 0 && insertionRange.max == 0 { } else {
59+
lastIndexEdited = true
60+
break
61+
}
5862
}
5963
}
6064

@@ -94,9 +98,10 @@ internal extension ParagraphTextStorage {
9498
if lastIndexEdited && difference.removals.first == change && difference.insertions.count > difference.removals.count ||
9599
lastIndexEdited && difference.removals.last == change && difference.removals.count > difference.insertions.count {
96100
if difference.removals.count > difference.insertions.count,
97-
let firstInsertion = difference.insertions.first, let lastTouched = difference.removals.last,
98-
case CollectionDifference<NSRange>.Change.insert(offset: _, element: let range, associatedWith: _) = firstInsertion,
99-
case CollectionDifference<NSRange>.Change.remove(offset: _, element: let touchedRange, associatedWith: _) = lastTouched {
101+
let firstInsertion = difference.insertions.first,
102+
let lastTouched = difference.removals.last,
103+
case CollectionDifference<NSRange>.Change.insert(offset: _, element: let range, associatedWith: _) = firstInsertion,
104+
case CollectionDifference<NSRange>.Change.remove(offset: _, element: let touchedRange, associatedWith: _) = lastTouched {
100105
if touchedRange.length != range.length {
101106
changes.append(.editedParagraph(index: paragraphIndex, range: range))
102107
}

0 commit comments

Comments
 (0)