Skip to content

Commit

Permalink
new: copy btn of code block (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit authored Feb 4, 2025
1 parent 97619a1 commit 6b4b9bd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
54 changes: 32 additions & 22 deletions lib/view/page/home/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,7 @@ class _ChatPageState extends State<_ChatPage>
return FloatingActionButton(
key: ValueKey(up),
mini: true,
onPressed: () async {
if (!_chatScrollCtrl.hasClients) return;
if (up) {
await _chatScrollCtrl.animateTo(0,
duration: _durationMedium, curve: Curves.easeInOut);
} else {
_scrollBottom();
}
_chatFabRN.notify();
},
onPressed: () => _onTapFAB(up),
child: Icon(icon),
);
}
Expand Down Expand Up @@ -169,18 +160,7 @@ class _ChatPageState extends State<_ChatPage>
borderRadius: BorderRadius.circular(13),
onLongPress: isHovered
? null
: () {
final funcs = _buildChatItemFuncs(chatItems, chatItem);
context.showRoundDialog(
contentPadding: const EdgeInsets.all(11),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: funcs,
),
),
);
},
: () => _onLongPressChatItem(context, chatItems, chatItem),
child: child,
);

Expand Down Expand Up @@ -331,3 +311,33 @@ class _ChatPageState extends State<_ChatPage>
@override
bool get wantKeepAlive => true;
}

extension on _ChatPageState {
void _onTapFAB(bool up) async {
if (!_chatScrollCtrl.hasClients) return;
if (up) {
await _chatScrollCtrl.animateTo(0,
duration: _durationMedium, curve: Curves.easeInOut);
} else {
_scrollBottom();
}
_chatFabRN.notify();
}

void _onLongPressChatItem(
BuildContext context,
List<ChatHistoryItem> chatItems,
ChatHistoryItem chatItem,
) {
final funcs = _buildChatItemFuncs(chatItems, chatItem);
context.showRoundDialog(
contentPadding: const EdgeInsets.all(11),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: funcs,
),
),
);
}
}
43 changes: 40 additions & 3 deletions lib/view/widget/code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:google_fonts/google_fonts.dart';
import 'package:gpt_box/data/res/rnode.dart';
import 'package:gpt_box/data/store/all.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:icons_plus/icons_plus.dart';

final _textStyle = GoogleFonts.robotoMono();

Expand Down Expand Up @@ -98,16 +99,52 @@ class CodeElementBuilder extends MarkdownElementBuilder {
selectable: false,
padding: const EdgeInsets.symmetric(vertical: 7, horizontal: 11),
);
return ValBuilder(
listenable: Stores.setting.softWrap.listenable(),
builder: (val) {

var lineFeedCount = 0;
// If line feed count is greater than 5, a copy btn will be shown.
const maxLineFeedCount = 5;
for (var i = 0; i < textContent.length; i++) {
if (textContent[i] == '\n') {
lineFeedCount++;
if (lineFeedCount > maxLineFeedCount) {
break;
}
}
}

final autoWrapped = Stores.setting.softWrap.listenable().listenVal(
(val) {
if (val) return child;
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: child,
);
},
);

if (lineFeedCount <= maxLineFeedCount) {
return autoWrapped;
}

return Stack(
children: [
autoWrapped,
Positioned(
right: 0,
top: 0,
child: Btn.icon(
icon: const Icon(
MingCute.copy_2_fill,
size: 15,
color: Color.fromARGB(117, 129, 129, 129),
),
onTap: () {
onCopy?.call(element.textContent.trim());
},
),
),
],
);
}

Map<String, TextStyle> get _theme {
Expand Down

0 comments on commit 6b4b9bd

Please sign in to comment.