Skip to content

Commit 87581e8

Browse files
authored
fix: when merging punctuation into tokens to prevent line breaks, account for punctuation preceding tokens (#1812)
1 parent 9d13d2f commit 87581e8

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

lib/pangea/events/utils/message_text_util.dart

+17-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MessageTextUtil {
2828
final tokens = pangeaMessageEvent.messageDisplayRepresentation!.tokens!;
2929
int pointer = 0;
3030
while (pointer < tokens.length) {
31-
final token = tokens[pointer];
31+
PangeaToken token = tokens[pointer];
3232
final start = token.start;
3333
final end = token.end;
3434

@@ -56,13 +56,23 @@ class MessageTextUtil {
5656
);
5757
}
5858

59-
// group tokens with punctuation next to it so punctuation doesn't cause newline
60-
final List<PangeaToken> followingPunctTokens = [];
59+
// group tokens with punctuation before and after so punctuation doesn't cause newline
6160
int nextTokenPointer = pointer + 1;
6261
while (nextTokenPointer < tokens.length) {
6362
final nextToken = tokens[nextTokenPointer];
64-
if (nextToken.pos == 'PUNCT') {
65-
followingPunctTokens.add(nextToken);
63+
if (token.pos == 'PUNCT' && token.end == nextToken.start) {
64+
token = nextToken;
65+
nextTokenPointer++;
66+
endIndex = messageCharacters.take(nextToken.end).length;
67+
continue;
68+
}
69+
break;
70+
}
71+
72+
while (nextTokenPointer < tokens.length) {
73+
final nextToken = tokens[nextTokenPointer];
74+
75+
if (nextToken.pos == 'PUNCT' && token.end == nextToken.start) {
6676
nextTokenPointer++;
6777
endIndex = messageCharacters.take(nextToken.end).length;
6878
continue;
@@ -74,8 +84,8 @@ class MessageTextUtil {
7484
TokenPosition(
7585
start: startIndex,
7686
end: endIndex,
77-
tokenStart: startIndex,
78-
tokenEnd: messageCharacters.take(end).length,
87+
tokenStart: messageCharacters.take(token.start).length,
88+
tokenEnd: messageCharacters.take(token.end).length,
7989
token: token,
8090
hideContent: hideContent,
8191
selected: (isSelected?.call(token) ?? false) &&

lib/pangea/toolbar/widgets/message_token_text.dart

+30-8
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,24 @@ class MessageTextWidget extends StatelessWidget {
238238
);
239239
}
240240

241-
// if the tokenPosition is a combination of the token and following punctuation
241+
// if the tokenPosition is a combination of the token and preceding / following punctuation
242242
// split them so that only the token itself is highlighted when clicked
243-
String firstSubstring = substring;
244-
String secondSubstring = '';
243+
String start = '';
244+
String middle = substring;
245+
String end = '';
246+
247+
if (tokenPosition.tokenStart != tokenPosition.start) {
248+
final splitIndex =
249+
(tokenPosition.tokenStart - tokenPosition.start);
250+
start = substring.substring(0, splitIndex);
251+
middle = substring.substring(splitIndex);
252+
}
245253

246254
if (tokenPosition.end != tokenPosition.tokenEnd) {
247255
final splitIndex = (tokenPosition.end - tokenPosition.start) -
248256
(tokenPosition.end - tokenPosition.tokenEnd);
249-
firstSubstring = substring.substring(0, splitIndex);
250-
secondSubstring = substring.substring(splitIndex);
257+
middle = middle.substring(0, splitIndex);
258+
end = substring.substring(splitIndex);
251259
}
252260

253261
return WidgetSpan(
@@ -260,8 +268,22 @@ class MessageTextWidget extends StatelessWidget {
260268
child: RichText(
261269
text: TextSpan(
262270
children: [
271+
if (start.isNotEmpty)
272+
LinkifySpan(
273+
text: start,
274+
style: style,
275+
linkStyle: TextStyle(
276+
decoration: TextDecoration.underline,
277+
color: Theme.of(context).brightness ==
278+
Brightness.light
279+
? Theme.of(context).colorScheme.primary
280+
: Theme.of(context).colorScheme.onPrimary,
281+
),
282+
onOpen: (url) =>
283+
UrlLauncher(context, url.url).launchUrl(),
284+
),
263285
LinkifySpan(
264-
text: firstSubstring,
286+
text: middle,
265287
style: style.merge(
266288
TextStyle(
267289
backgroundColor: backgroundColor,
@@ -277,9 +299,9 @@ class MessageTextWidget extends StatelessWidget {
277299
onOpen: (url) =>
278300
UrlLauncher(context, url.url).launchUrl(),
279301
),
280-
if (secondSubstring.isNotEmpty)
302+
if (end.isNotEmpty)
281303
LinkifySpan(
282-
text: secondSubstring,
304+
text: end,
283305
style: style,
284306
linkStyle: TextStyle(
285307
decoration: TextDecoration.underline,

0 commit comments

Comments
 (0)