-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/selectable_highlight.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:highlight/highlight.dart'; | ||
|
||
/// Highlight Flutter Widget | ||
class SelectableHighlightView extends StatelessWidget { | ||
SelectableHighlightView( | ||
String input, { | ||
super.key, | ||
this.language, | ||
this.theme = const {}, | ||
this.padding, | ||
this.textStyle, | ||
int tabSize = 8, | ||
}) : source = input.replaceAll('\t', ' ' * tabSize); | ||
|
||
/// The original code to be highlighted | ||
final String source; | ||
|
||
/// Highlight language | ||
/// | ||
/// It is recommended to give it a value for performance | ||
/// | ||
/// [All available languages](https://github.com/pd4d10/highlight/tree/master/highlight/lib/languages) | ||
final String? language; | ||
|
||
/// Highlight theme | ||
/// | ||
/// [All available themes](https://github.com/pd4d10/highlight/blob/master/flutter_highlight/lib/themes) | ||
final Map<String, TextStyle> theme; | ||
|
||
/// Padding | ||
final EdgeInsetsGeometry? padding; | ||
|
||
/// Text styles | ||
/// | ||
/// Specify text styles such as font family and font size | ||
final TextStyle? textStyle; | ||
|
||
List<TextSpan> _convert(List<Node> nodes) { | ||
final List<TextSpan> spans = []; | ||
var currentSpans = spans; | ||
final List<List<TextSpan>> stack = []; | ||
|
||
// ignore: always_declare_return_types | ||
traverse(Node node) { | ||
if (node.value != null) { | ||
currentSpans.add( | ||
node.className == null | ||
? TextSpan(text: node.value) | ||
: TextSpan(text: node.value, style: theme[node.className!]), | ||
); | ||
} else if (node.children != null) { | ||
final List<TextSpan> tmp = []; | ||
currentSpans | ||
.add(TextSpan(children: tmp, style: theme[node.className!])); | ||
stack.add(currentSpans); | ||
currentSpans = tmp; | ||
|
||
for (final n in node.children!) { | ||
traverse(n); | ||
if (n == node.children!.last) { | ||
currentSpans = stack.isEmpty ? spans : stack.removeLast(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (final node in nodes) { | ||
traverse(node); | ||
} | ||
|
||
return spans; | ||
} | ||
|
||
static const _rootKey = 'root'; | ||
static const _defaultBackgroundColor = Color(0xffffffff); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
color: theme[_rootKey]?.backgroundColor ?? _defaultBackgroundColor, | ||
padding: padding, | ||
child: SelectableText.rich( | ||
TextSpan( | ||
style: textStyle, | ||
children: | ||
_convert(highlight.parse(source, language: language).nodes!), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters