mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
opt: remove duplicate em highlight
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
|
import 'package:PiliPlus/utils/em.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
|
||||||
|
|
||||||
class SearchSuggestModel {
|
class SearchSuggestModel {
|
||||||
SearchSuggestModel({
|
SearchSuggestModel({
|
||||||
@@ -19,17 +19,10 @@ class SearchSuggestModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SearchSuggestItem {
|
class SearchSuggestItem {
|
||||||
SearchSuggestItem({
|
|
||||||
this.value,
|
|
||||||
this.term,
|
|
||||||
this.spid,
|
|
||||||
this.textRich,
|
|
||||||
});
|
|
||||||
|
|
||||||
String? value;
|
String? value;
|
||||||
String? term;
|
String? term;
|
||||||
int? spid;
|
int? spid;
|
||||||
dynamic textRich;
|
late String textRich;
|
||||||
|
|
||||||
SearchSuggestItem.fromJson(Map<String, dynamic> json, String inputTerm) {
|
SearchSuggestItem.fromJson(Map<String, dynamic> json, String inputTerm) {
|
||||||
value = json['value'];
|
value = json['value'];
|
||||||
@@ -39,57 +32,16 @@ class SearchSuggestItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget highlightText(BuildContext context, String str) {
|
Widget highlightText(BuildContext context, String str) {
|
||||||
// 创建正则表达式,匹配 <em class="suggest_high_light">...</em> 格式的文本
|
return Text.rich(TextSpan(children: [
|
||||||
RegExp regex = RegExp(r'<em class="suggest_high_light">(.*?)<\/em>');
|
for (var i in Em.regTitle(str))
|
||||||
|
TextSpan(
|
||||||
// 用于存储每个匹配项的列表
|
text: i['text'],
|
||||||
List<InlineSpan> children = [];
|
style: i['type'] == 'em'
|
||||||
|
? TextStyle(
|
||||||
// 获取所有匹配项
|
fontWeight: FontWeight.bold,
|
||||||
Iterable<Match> matches = regex.allMatches(str);
|
color: Theme.of(context).colorScheme.primary,
|
||||||
|
)
|
||||||
// 当前索引位置
|
: DefaultTextStyle.of(context).style,
|
||||||
int currentIndex = 0;
|
)
|
||||||
|
]));
|
||||||
// 遍历每个匹配项
|
|
||||||
for (var match in matches) {
|
|
||||||
// 获取当前匹配项之前的普通文本部分
|
|
||||||
String normalText = str.substring(currentIndex, match.start);
|
|
||||||
|
|
||||||
// 获取需要高亮显示的文本部分
|
|
||||||
String highlightedText = match.group(1)!;
|
|
||||||
|
|
||||||
// 如果普通文本部分不为空,则将其添加到 children 列表中
|
|
||||||
if (normalText.isNotEmpty) {
|
|
||||||
children.add(TextSpan(
|
|
||||||
text: normalText,
|
|
||||||
style: DefaultTextStyle.of(Get.context!).style,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将需要高亮显示的文本部分添加到 children 列表中,并设置相应样式
|
|
||||||
children.add(TextSpan(
|
|
||||||
text: highlightedText,
|
|
||||||
style: TextStyle(
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
color: Theme.of(context).colorScheme.primary),
|
|
||||||
));
|
|
||||||
|
|
||||||
// 更新当前索引位置
|
|
||||||
currentIndex = match.end;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果当前索引位置小于文本长度,表示还有剩余的普通文本部分
|
|
||||||
if (currentIndex < str.length) {
|
|
||||||
String remainingText = str.substring(currentIndex);
|
|
||||||
|
|
||||||
// 将剩余的普通文本部分添加到 children 列表中
|
|
||||||
children.add(TextSpan(
|
|
||||||
text: remainingText,
|
|
||||||
style: DefaultTextStyle.of(context).style,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用 Text.rich 创建包含高亮显示的富文本小部件,并返回
|
|
||||||
return Text.rich(TextSpan(children: children));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,15 +14,13 @@ class Em {
|
|||||||
_exp,
|
_exp,
|
||||||
onMatch: (Match match) {
|
onMatch: (Match match) {
|
||||||
String matchStr = match[0]!;
|
String matchStr = match[0]!;
|
||||||
var map = {'type': 'em', 'text': regCate(matchStr)};
|
res.add({'type': 'em', 'text': regCate(matchStr)});
|
||||||
res.add(map);
|
|
||||||
return matchStr;
|
return matchStr;
|
||||||
},
|
},
|
||||||
onNonMatch: (String str) {
|
onNonMatch: (String str) {
|
||||||
if (str != '') {
|
if (str != '') {
|
||||||
str = parse(str).body?.text ?? str;
|
str = decodeHtmlEntities(str);
|
||||||
var map = {'type': 'text', 'text': str};
|
res.add({'type': 'text', 'text': str});
|
||||||
res.add(map);
|
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user