Skip to content

Commit

Permalink
Better Shift-Enter (carriage return) and Ctrl-Enter(newline) handling
Browse files Browse the repository at this point in the history
  • Loading branch information
1runeberg committed Aug 27, 2024
1 parent 86f5f54 commit 5d6acf5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
18 changes: 4 additions & 14 deletions confichat/lib/ui_canvass.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1357,20 +1357,10 @@ class ShiftEnterTextFormFieldState extends State<ShiftEnterTextFormField> {
onKeyEvent: (KeyEvent event) {
if (event is KeyDownEvent) {
if (HardwareKeyboard.instance.isShiftPressed && event.logicalKey == LogicalKeyboardKey.enter) {

// Insert a newline at the current cursor position
final currentText = widget.promptController.text;
final cursorPosition = widget.promptController.selection.baseOffset;
final newText = '${currentText.substring(0, cursorPosition)}\n${currentText.substring(cursorPosition)}';

setState(() {
widget.promptController.text = newText;
widget.promptController.selection = TextSelection.fromPosition(
TextPosition(offset: cursorPosition + 1),
);

});

// Capture shift-enter
} else if (HardwareKeyboard.instance.isControlPressed && event.logicalKey == LogicalKeyboardKey.enter) {
// Capture ctrl-enter
widget.promptController.text += '\n';
} else if (event.logicalKey == LogicalKeyboardKey.enter) {
sendPromptKeyEvent();
}
Expand Down
2 changes: 1 addition & 1 deletion confichat/lib/ui_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ class ChatBubble extends StatelessWidget {
child: SelectionArea(
child: Container(
margin: const EdgeInsets.all(5),
child: Markdown(
child: isUser? Text(sanitizedText) : Markdown(
data: sanitizedText,
builders: {
'code': CodePreviewBuilder(context),
Expand Down

0 comments on commit 5d6acf5

Please sign in to comment.