Skip to content

Commit 21199c0

Browse files
authored
Fix/1936 (#1939)
* fix: [Bug] Code block jump skip blank line * fix: auto completion interaction * Revert "fix: [Bug] Code block jump skip blank line" This reverts commit 5a252bc. * fix: [Bug] Code block jump skip blank line * fix: number list parse error
1 parent d3823eb commit 21199c0

File tree

7 files changed

+75
-26
lines changed

7 files changed

+75
-26
lines changed

frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/openai/service/openai_client.dart

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'dart:convert';
22

33
import 'package:appflowy/plugins/document/presentation/plugins/openai/service/text_edit.dart';
44
import 'package:appflowy_editor/appflowy_editor.dart';
5-
import 'package:flutter/material.dart';
65

76
import 'text_completion.dart';
87
import 'package:dartz/dartz.dart';
@@ -39,18 +38,18 @@ abstract class OpenAIRepository {
3938
Future<Either<OpenAIError, TextCompletionResponse>> getCompletions({
4039
required String prompt,
4140
String? suffix,
42-
int maxTokens = 500,
41+
int maxTokens = 2048,
4342
double temperature = .3,
4443
});
4544

4645
Future<void> getStreamedCompletions({
4746
required String prompt,
4847
required Future<void> Function() onStart,
4948
required Future<void> Function(TextCompletionResponse response) onProcess,
50-
required VoidCallback onEnd,
49+
required Future<void> Function() onEnd,
5150
required void Function(OpenAIError error) onError,
5251
String? suffix,
53-
int maxTokens = 500,
52+
int maxTokens = 2048,
5453
double temperature = 0.3,
5554
});
5655

@@ -85,7 +84,7 @@ class HttpOpenAIRepository implements OpenAIRepository {
8584
Future<Either<OpenAIError, TextCompletionResponse>> getCompletions({
8685
required String prompt,
8786
String? suffix,
88-
int maxTokens = 500,
87+
int maxTokens = 2048,
8988
double temperature = 0.3,
9089
}) async {
9190
final parameters = {
@@ -121,10 +120,10 @@ class HttpOpenAIRepository implements OpenAIRepository {
121120
required String prompt,
122121
required Future<void> Function() onStart,
123122
required Future<void> Function(TextCompletionResponse response) onProcess,
124-
required VoidCallback onEnd,
123+
required Future<void> Function() onEnd,
125124
required void Function(OpenAIError error) onError,
126125
String? suffix,
127-
int maxTokens = 500,
126+
int maxTokens = 2048,
128127
double temperature = 0.3,
129128
}) async {
130129
final parameters = {
@@ -159,21 +158,23 @@ class HttpOpenAIRepository implements OpenAIRepository {
159158
continue;
160159
}
161160
final data = chunk.trim().split('data: ');
162-
if (data.length > 1 && data[1] != '[DONE]') {
163-
final response = TextCompletionResponse.fromJson(
164-
json.decode(data[1]),
165-
);
166-
if (response.choices.isNotEmpty) {
167-
final text = response.choices.first.text;
168-
if (text == previousSyntax && text == '\n') {
169-
continue;
161+
Log.editor.info(data.toString());
162+
if (data.length > 1) {
163+
if (data[1] != '[DONE]') {
164+
final response = TextCompletionResponse.fromJson(
165+
json.decode(data[1]),
166+
);
167+
if (response.choices.isNotEmpty) {
168+
final text = response.choices.first.text;
169+
if (text == previousSyntax && text == '\n') {
170+
continue;
171+
}
172+
await onProcess(response);
173+
previousSyntax = response.choices.first.text;
170174
}
171-
await onProcess(response);
172-
previousSyntax = response.choices.first.text;
173-
Log.editor.info(response.choices.first.text);
175+
} else {
176+
onEnd();
174177
}
175-
} else {
176-
onEnd();
177178
}
178179
}
179180
} else {

frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/openai/widgets/auto_completion_node_widget.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
6969
void dispose() {
7070
controller.dispose();
7171
textFieldFocusNode.removeListener(_onFocusChanged);
72+
widget.editorState.service.selectionService.currentSelection
73+
.removeListener(_onCancelWhenSelectionChanged);
7274

7375
super.dispose();
7476
}
@@ -239,6 +241,7 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
239241
final result = await UserBackendService.getCurrentUserProfile();
240242

241243
result.fold((userProfile) async {
244+
BarrierDialog? barrierDialog;
242245
final openAIRepository = HttpOpenAIRepository(
243246
client: http.Client(),
244247
apiKey: userProfile.openaiKey,
@@ -247,6 +250,8 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
247250
prompt: controller.text,
248251
onStart: () async {
249252
loading.stop();
253+
barrierDialog = BarrierDialog(context);
254+
barrierDialog?.show();
250255
await _makeSurePreviousNodeIsEmptyTextNode();
251256
},
252257
onProcess: (response) async {
@@ -255,10 +260,15 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
255260
await widget.editorState.autoInsertText(
256261
text,
257262
inputType: TextRobotInputType.word,
263+
delay: Duration.zero,
258264
);
259265
}
260266
},
261-
onEnd: () {},
267+
onEnd: () async {
268+
await barrierDialog?.dismiss();
269+
widget.editorState.service.selectionService.currentSelection
270+
.addListener(_onCancelWhenSelectionChanged);
271+
},
262272
onError: (error) async {
263273
loading.stop();
264274
await _showError(error.message);
@@ -353,4 +363,6 @@ class _AutoCompletionInputState extends State<_AutoCompletionInput> {
353363
widget.editorState.service.keyboardService?.enable();
354364
}
355365
}
366+
367+
void _onCancelWhenSelectionChanged() {}
356368
}

frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/openai/widgets/loading.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ class Loading {
3232
return Navigator.of(loadingContext).pop();
3333
}
3434
}
35+
36+
class BarrierDialog {
37+
BarrierDialog(
38+
this.context,
39+
);
40+
41+
late BuildContext loadingContext;
42+
final BuildContext context;
43+
44+
Future<void> show() async {
45+
return showDialog<void>(
46+
context: context,
47+
barrierDismissible: false,
48+
barrierColor: Colors.transparent,
49+
builder: (BuildContext context) {
50+
loadingContext = context;
51+
return Container();
52+
},
53+
);
54+
}
55+
56+
Future<void> dismiss() async {
57+
return Navigator.of(loadingContext).pop();
58+
}
59+
}

frontend/appflowy_flutter/packages/appflowy_editor/example/macos/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ EXTERNAL SOURCES:
3838
SPEC CHECKSUMS:
3939
flowy_infra_ui: c34d49d615ed9fe552cd47f90d7850815a74e9e9
4040
FlutterMacOS: ae6af50a8ea7d6103d888583d46bd8328a7e9811
41-
path_provider_foundation: 37748e03f12783f9de2cb2c4eadfaa25fe6d4852
41+
path_provider_foundation: c68054786f1b4f3343858c1e1d0caaded73f0be9
4242
rich_clipboard_macos: 43364b66b9dc69d203eb8dd6d758e2d12e02723c
43-
shared_preferences_foundation: 297b3ebca31b34ec92be11acd7fb0ba932c822ca
44-
url_launcher_macos: c04e4fa86382d4f94f6b38f14625708be3ae52e2
43+
shared_preferences_foundation: 986fc17f3d3251412d18b0265f9c64113a8c2472
44+
url_launcher_macos: 5335912b679c073563f29d89d33d10d459f95451
4545

4646
PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c
4747

frontend/appflowy_flutter/packages/appflowy_editor/lib/src/render/rich_text/flowy_rich_text.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,18 @@ class _FlowyRichTextState extends State<FlowyRichText> with SelectableMixin {
9595
textPosition, Rect.zero) ??
9696
Offset.zero;
9797
}
98+
if (widget.cursorHeight != null && cursorHeight != null) {
99+
cursorOffset = Offset(
100+
cursorOffset.dx,
101+
cursorOffset.dy + (cursorHeight - widget.cursorHeight!) / 2,
102+
);
103+
cursorHeight = widget.cursorHeight;
104+
}
98105
final rect = Rect.fromLTWH(
99106
cursorOffset.dx - (widget.cursorWidth / 2.0),
100107
cursorOffset.dy,
101108
widget.cursorWidth,
102-
widget.cursorHeight ?? cursorHeight ?? 16.0,
109+
cursorHeight ?? 16.0,
103110
);
104111
return rect;
105112
}

frontend/appflowy_flutter/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/whitespace_handler.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ ShortcutEventHandler whiteSpaceHandler = (editorState, event) {
5656
} else if (numberMatch != null) {
5757
final matchText = numberMatch.group(0);
5858
final numText = numberMatch.group(1);
59-
if (matchText != null && numText != null) {
59+
if (matchText != null &&
60+
numText != null &&
61+
matchText.length == selection.startIndex) {
6062
return _toNumberList(editorState, textNode, matchText, numText);
6163
}
6264
}

frontend/appflowy_flutter/packages/appflowy_editor_plugins/lib/src/code_block/code_block_node_widget.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class __CodeBlockNodeWidgeState extends State<_CodeBlockNodeWidge>
102102
key: _richTextKey,
103103
textNode: widget.textNode,
104104
editorState: widget.editorState,
105+
lineHeight: 1.0,
106+
cursorHeight: 15.0,
105107
textSpanDecorator: (textSpan) => TextSpan(
106108
style: widget.editorState.editorStyle.textStyle,
107109
children: codeTextSpan,

0 commit comments

Comments
 (0)