This commit is contained in:
parent
7bc9b5d709
commit
e0d18e2e62
|
|
@ -1122,13 +1122,14 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
///
|
///
|
||||||
/// 采用自定义解析器,逐字符扫描并使用递归解析子内容,从而支持任意嵌套。
|
/// 采用自定义解析器,逐字符扫描并使用递归解析子内容,从而支持任意嵌套。
|
||||||
List<TextSpan> _parseRichText(String content, ColorScheme colorScheme) {
|
List<TextSpan> _parseRichText(String content, ColorScheme colorScheme) {
|
||||||
final result = _parseRichTextInternal(content, colorScheme, 0, null);
|
final styles = _RichTextStyles(colorScheme);
|
||||||
|
final result = _parseRichTextInternal(content, styles, 0, null);
|
||||||
return result.spans;
|
return result.spans;
|
||||||
}
|
}
|
||||||
|
|
||||||
_RichTextParseResult _parseRichTextInternal(
|
_RichTextParseResult _parseRichTextInternal(
|
||||||
String text,
|
String text,
|
||||||
ColorScheme colorScheme,
|
_RichTextStyles styles,
|
||||||
int startIndex,
|
int startIndex,
|
||||||
String? endToken,
|
String? endToken,
|
||||||
) {
|
) {
|
||||||
|
|
@ -1140,7 +1141,7 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
if (buffer.isEmpty) return;
|
if (buffer.isEmpty) return;
|
||||||
spans.add(TextSpan(
|
spans.add(TextSpan(
|
||||||
text: buffer.toString(),
|
text: buffer.toString(),
|
||||||
style: _defaultRichTextStyle(colorScheme),
|
style: styles.defaultStyle,
|
||||||
));
|
));
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
}
|
}
|
||||||
|
|
@ -1157,13 +1158,13 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
|
|
||||||
// [高亮]
|
// [高亮]
|
||||||
if (currentChar == '[') {
|
if (currentChar == '[') {
|
||||||
final innerResult = _parseRichTextInternal(text, colorScheme, index + 1, ']');
|
final innerResult = _parseRichTextInternal(text, styles, index + 1, ']');
|
||||||
if (innerResult.closed) {
|
if (innerResult.closed) {
|
||||||
flushBuffer();
|
flushBuffer();
|
||||||
final innerText = text.substring(index + 1, innerResult.nextIndex - 1);
|
final innerText = text.substring(index + 1, innerResult.nextIndex - 1);
|
||||||
spans.addAll(_applyStyleToSpans(
|
spans.addAll(_applyStyleToSpans(
|
||||||
innerResult.spans,
|
innerResult.spans,
|
||||||
_highlightTextStyle(colorScheme),
|
styles.highlightStyle,
|
||||||
innerText,
|
innerText,
|
||||||
));
|
));
|
||||||
index = innerResult.nextIndex;
|
index = innerResult.nextIndex;
|
||||||
|
|
@ -1183,7 +1184,7 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
final codeText = text.substring(index + 1, closingIndex);
|
final codeText = text.substring(index + 1, closingIndex);
|
||||||
spans.add(TextSpan(
|
spans.add(TextSpan(
|
||||||
text: codeText,
|
text: codeText,
|
||||||
style: _codeTextStyle(colorScheme),
|
style: styles.codeStyle,
|
||||||
));
|
));
|
||||||
index = closingIndex + 1;
|
index = closingIndex + 1;
|
||||||
handled = true;
|
handled = true;
|
||||||
|
|
@ -1196,13 +1197,13 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
}
|
}
|
||||||
// **粗体**
|
// **粗体**
|
||||||
else if (text.startsWith('**', index)) {
|
else if (text.startsWith('**', index)) {
|
||||||
final innerResult = _parseRichTextInternal(text, colorScheme, index + 2, '**');
|
final innerResult = _parseRichTextInternal(text, styles, index + 2, '**');
|
||||||
if (innerResult.closed) {
|
if (innerResult.closed) {
|
||||||
flushBuffer();
|
flushBuffer();
|
||||||
final innerText = text.substring(index + 2, innerResult.nextIndex - 2);
|
final innerText = text.substring(index + 2, innerResult.nextIndex - 2);
|
||||||
spans.addAll(_applyStyleToSpans(
|
spans.addAll(_applyStyleToSpans(
|
||||||
innerResult.spans,
|
innerResult.spans,
|
||||||
_boldTextStyle(colorScheme),
|
styles.boldStyle,
|
||||||
innerText,
|
innerText,
|
||||||
));
|
));
|
||||||
index = innerResult.nextIndex;
|
index = innerResult.nextIndex;
|
||||||
|
|
@ -1216,13 +1217,13 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
}
|
}
|
||||||
// __斜体__
|
// __斜体__
|
||||||
else if (text.startsWith('__', index)) {
|
else if (text.startsWith('__', index)) {
|
||||||
final innerResult = _parseRichTextInternal(text, colorScheme, index + 2, '__');
|
final innerResult = _parseRichTextInternal(text, styles, index + 2, '__');
|
||||||
if (innerResult.closed) {
|
if (innerResult.closed) {
|
||||||
flushBuffer();
|
flushBuffer();
|
||||||
final innerText = text.substring(index + 2, innerResult.nextIndex - 2);
|
final innerText = text.substring(index + 2, innerResult.nextIndex - 2);
|
||||||
spans.addAll(_applyStyleToSpans(
|
spans.addAll(_applyStyleToSpans(
|
||||||
innerResult.spans,
|
innerResult.spans,
|
||||||
_italicTextStyle(colorScheme),
|
styles.italicStyle,
|
||||||
innerText,
|
innerText,
|
||||||
));
|
));
|
||||||
index = innerResult.nextIndex;
|
index = innerResult.nextIndex;
|
||||||
|
|
@ -1244,51 +1245,6 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
return _RichTextParseResult(spans, index, false);
|
return _RichTextParseResult(spans, index, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
TextStyle _defaultRichTextStyle(ColorScheme colorScheme) {
|
|
||||||
return TextStyle(
|
|
||||||
fontSize: 13,
|
|
||||||
color: colorScheme.onSurface.withOpacity(0.8),
|
|
||||||
height: 1.4,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
TextStyle _boldTextStyle(ColorScheme colorScheme) {
|
|
||||||
return TextStyle(
|
|
||||||
fontSize: 13,
|
|
||||||
color: colorScheme.onSurface.withOpacity(0.9),
|
|
||||||
height: 1.4,
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
TextStyle _italicTextStyle(ColorScheme colorScheme) {
|
|
||||||
return TextStyle(
|
|
||||||
fontSize: 13,
|
|
||||||
color: colorScheme.onSurface.withOpacity(0.9),
|
|
||||||
height: 1.4,
|
|
||||||
fontStyle: FontStyle.italic,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
TextStyle _codeTextStyle(ColorScheme colorScheme) {
|
|
||||||
return TextStyle(
|
|
||||||
fontSize: 12,
|
|
||||||
color: colorScheme.primary,
|
|
||||||
height: 1.4,
|
|
||||||
fontFamily: 'monospace',
|
|
||||||
backgroundColor: colorScheme.primaryContainer.withOpacity(0.2),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
TextStyle _highlightTextStyle(ColorScheme colorScheme) {
|
|
||||||
return TextStyle(
|
|
||||||
fontSize: 13,
|
|
||||||
color: colorScheme.primary,
|
|
||||||
height: 1.4,
|
|
||||||
fontWeight: FontWeight.w600,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<TextSpan> _applyStyleToSpans(List<TextSpan> spans, TextStyle style, String fallbackText) {
|
List<TextSpan> _applyStyleToSpans(List<TextSpan> spans, TextStyle style, String fallbackText) {
|
||||||
if (spans.isEmpty) {
|
if (spans.isEmpty) {
|
||||||
return [
|
return [
|
||||||
|
|
@ -2218,6 +2174,46 @@ class _ToastWidget extends StatelessWidget {
|
||||||
|
|
||||||
typedef BoolCallback = void Function(bool success);
|
typedef BoolCallback = void Function(bool success);
|
||||||
|
|
||||||
|
class _RichTextStyles {
|
||||||
|
_RichTextStyles(ColorScheme colorScheme)
|
||||||
|
: defaultStyle = TextStyle(
|
||||||
|
fontSize: 13,
|
||||||
|
color: colorScheme.onSurface.withOpacity(0.8),
|
||||||
|
height: 1.4,
|
||||||
|
),
|
||||||
|
boldStyle = TextStyle(
|
||||||
|
fontSize: 13,
|
||||||
|
color: colorScheme.onSurface.withOpacity(0.9),
|
||||||
|
height: 1.4,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
italicStyle = TextStyle(
|
||||||
|
fontSize: 13,
|
||||||
|
color: colorScheme.onSurface.withOpacity(0.9),
|
||||||
|
height: 1.4,
|
||||||
|
fontStyle: FontStyle.italic,
|
||||||
|
),
|
||||||
|
codeStyle = TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
color: colorScheme.primary,
|
||||||
|
height: 1.4,
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
backgroundColor: colorScheme.primaryContainer.withOpacity(0.2),
|
||||||
|
),
|
||||||
|
highlightStyle = TextStyle(
|
||||||
|
fontSize: 13,
|
||||||
|
color: colorScheme.primary,
|
||||||
|
height: 1.4,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
);
|
||||||
|
|
||||||
|
final TextStyle defaultStyle;
|
||||||
|
final TextStyle boldStyle;
|
||||||
|
final TextStyle italicStyle;
|
||||||
|
final TextStyle codeStyle;
|
||||||
|
final TextStyle highlightStyle;
|
||||||
|
}
|
||||||
|
|
||||||
class _RichTextParseResult {
|
class _RichTextParseResult {
|
||||||
final List<TextSpan> spans;
|
final List<TextSpan> spans;
|
||||||
final int nextIndex;
|
final int nextIndex;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue