chore: adjust replace, insert below, discard selection behavior

This commit is contained in:
Richard Shiue 2025-04-10 15:24:20 +08:00
parent 4997ac99cf
commit a1c49d6c4e
3 changed files with 20 additions and 4 deletions

View file

@ -57,8 +57,13 @@ class AiWriterCubit extends Cubit<AiWriterState> {
bool withDiscard = true,
bool withUnformat = true,
}) async {
if (aiWriterNode == null) {
return;
}
if (withDiscard) {
await _textRobot.discard();
await _textRobot.discard(
afterSelection: aiWriterNode!.aiWriterSelection,
);
}
_textRobot.clear();
_textRobot.reset();
@ -232,6 +237,12 @@ class AiWriterCubit extends Cubit<AiWriterState> {
'trigger accept action, markdown text: $trimmedMarkdownText',
);
await formatSelection(
editorState,
selection,
ApplySuggestionFormatType.clear,
);
await _textRobot.deleteAINodes();
await _textRobot.replace(

View file

@ -40,6 +40,7 @@ class _AiWriterScrollWrapperState extends State<AiWriterScrollWrapper> {
onRemoveNode: () {
aiWriterRegistered = false;
widget.editorState.service.keyboardService?.enableShortcuts();
widget.editorState.service.keyboardService?.enable();
},
onAppendToDocument: onAppendToDocument,
);

View file

@ -118,7 +118,7 @@ class MarkdownTextRobot {
}
await _lock.synchronized(() async {
await _refresh(inMemoryUpdate: false);
await _refresh(inMemoryUpdate: false, updateSelection: true);
});
if (_enableDebug) {
@ -156,7 +156,9 @@ class MarkdownTextRobot {
}
/// Discard the inserted content
Future<void> discard() async {
Future<void> discard({
Selection? afterSelection,
}) async {
final start = _insertPosition;
if (start == null) {
return;
@ -165,6 +167,8 @@ class MarkdownTextRobot {
return;
}
afterSelection ??= Selection.collapsed(start);
// fallback to the calculated position if the selection is null.
final end = Position(
path: start.path.nextNPath(_insertedNodes.length - 1),
@ -174,7 +178,7 @@ class MarkdownTextRobot {
);
final transaction = editorState.transaction
..deleteNodes(deletedNodes)
..afterSelection = Selection.collapsed(start);
..afterSelection = afterSelection;
await editorState.apply(
transaction,