tryMerge method
void
tryMerge()
try to merge this element with adjacents parts if they share the same style.
Implementation
void tryMerge() {
// This is a text node and it can only be merged with other text nodes.
EasyText textPart = this;
final bool isThisLastUsed = (list as EasyTextList?)?.lastUsed == this;
// Merging it with previous textPart if style is the same.
final EasyText? prev = textPart.previous;
if (!textPart.isFirst && prev != null && prev.styles == textPart.styles) {
int? prevLength = prev._length != null ? prev.length : null;
int? nodeLength = textPart._length != null ? textPart.length : null;
prev.text = prev.text + textPart.text;
if (prevLength != null) {
nodeLength ??= textPart.length;
textPart._length = prevLength + nodeLength;
}
// moved the focus to this since at some points, we
// want to move to the wanted fragment without
// searching it first manually
if (isThisLastUsed) {
(list as EasyTextList?)?.lastUsed = prev;
}
textPart.unlink();
textPart = prev;
}
// Merging it with next node if style is the same.
final EasyText? next = textPart.next;
if (!textPart.isLast && next != null && next.styles == textPart.styles) {
int? nextLength = next._length != null ? next.length : null;
int? nodeLength = textPart._length != null ? textPart.length : null;
textPart.text = textPart.text + next.text;
// computes the new length to avoid unnecessary calculations
if (nextLength != null) {
nodeLength ??= textPart.length;
next._length = nextLength + nodeLength;
}
next.unlink();
}
}